基于 Sentinel 实现历史监控数据回看

发布时间 2023-11-15 15:55:31作者: 老去的书童

Sentinel 对监控数据的做法是定时落盘在客户端,然后 Sentinel 提供接口去拉取日志文件。所以 Sentinel 在监控数据上理论上是最少存储 1 天以上的数据;然而作为控制台展示,则仅在内存中聚合 5 分钟以内的统计数据,不进行持久化。官方鼓励大家对 Dashboard 进行改造实现指标信息的持久化,并从其它的存储中(如 RDBMS、时序数据库等)拉取的监控信息,包括实时的和历史的数据。基于此,自行改造并实现了监控指标的持久化。本文把一些实现过程分享给大家!

Sentinel 是阿里巴巴开源的流量治理平台,提供了 流量控制熔断降级系统负载保护黑白名单访问控制 等功能。在实际的生产需求中,我在现有功能基础上进行了部分扩展:

  1. 流控规则持久化:适配 ApolloNacosZookeeper
  2. 监控数据持久化:适配 InfluxDBKafkaElasticsearch
  3. 监控面板优化:新增时间控件,允许在任意时刻内查询监控数据。

演示图例

改造前

 

改造后

 

快捷时间选择

 

自定义时间选择。

 

 

如何构建

默认使用 Maven 来构建

如何启动

IDEA 启动

本项目默认不依赖外部组件,可以直接启动运行。
  1. 在项目目录下运行 mvn install(如果不想运行测试,可以加上 -DskipTests 参数)。
  2. 进入 sentinel-dashboard 目录,执行 mvn spring-boot:run 或者启动 SentinelApplication 类。运行成功的话,可以看到 Spring Boot 启动成功的界面。
在实际的生产需求,Sentinel 保存的规则和监控是需要持久化落盘的,因此,可以在 sentinel-dashboard/src/main/resources/application.properties 接入外部组件。
  • 规则存储类型:memory(默认)、nacos(推荐)、apollo、zookeeper
# 规则存储类型,可选项:memory(默认)、nacos(推荐)、apollo、zookeeper
sentinel.rule.type=nacos
# Nacos 存储规则,如果设置了 sentinel.metrics.type=nacos,需要调整相关配置
sentinel.rule.nacos.server-addr=localhost:8848
sentinel.rule.nacos.namespace=demo
sentinel.rule.nacos.group-id=sentinel
sentinel.rule.nacos.username=nacos
sentinel.rule.nacos.password=nacos
# Apollo 存储规则,如果设置了 sentinel.metrics.type=apollo,需要调整相关配置
sentinel.rule.apollo.portal-url=http://localhost:10034
sentinel.rule.apollo.token=
sentinel.rule.apollo.env=
# Zookeeper 存储规则,如果设置了 sentinel.metrics.type=zookeeper,需要调整相关配置
sentinel.rule.zookeeper.connect-string=localhost:2181
sentinel.rule.zookeeper.root-path=/sentinel_rule
  • 监控存储类型:memory(默认)、influxdb(推荐)、elasticsearch
# 监控存储类型,可选项:memory(默认)、influxdb(推荐)、elasticsearch
sentinel.metrics.type=memory
# InfluxDB 存储监控数据,如果设置了 sentinel.metrics.type=influxdb,需要调整相关配置
influx.url=http://localhost:8086/
influx.token=UfgaW37A93PkncmJum25G7M2QkBg6xqqjGthh-o-UIVIynC_-Q7RFWlTtEpMqhGLCuAsX64k3Isc2uN33YgElw==
influx.org=sentinel
influx.bucket=sentinel
influx.log-level=NONE
influx.read-timeout=10s
influx.write-timeout=10s
influx.connect-timeout=10s
# Elasticsearch 存储监控数据,如果设置了 sentinel.metrics.type=elasticsearch,需要调整相关配置
sentinel.metrics.elasticsearch.index-name=sentinel_metric
spring.elasticsearch.rest.uris=http://localhost:9200
spring.elasticsearch.rest.connection-timeout=3000
spring.elasticsearch.rest.read-timeout=5000
spring.elasticsearch.rest.username=
spring.elasticsearch.rest.password=
# 监控数据存储缓冲设置,降低底层存储组件写入压力。可选项:none(默认不启用)、kafka(推荐)
sentinel.metrics.sender.type=none
# Kafka 存储监控数据,如果设置了 sentinel.metrics.sender.type=kafka,需要调整相关配置
sentinel.metrics.sender.kafka.topic=sentinel_metric
spring.kafka.producer.bootstrap-servers=localhost:9092
spring.kafka.producer.batch-size=4096
spring.kafka.producer.buffer-memory=40960
spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer
spring.kafka.producer.value-serializer=org.apache.kafka.common.serialization.StringSerializer

镜像启动

本项目已发布到 Docker Hubhttps://hub.docker.com/repository/docker/shiyindaxiaojie/sentinel-dashboard,请执行参考命令运行。
docker run -p 8090:8090 --name=sentinel-dashboard -d shiyindaxiaojie/sentinel-dashboard

如何部署

FatJar 部署

执行 mvn clean package 打包成一个 fat jar,参考如下命令启动编译后的控制台。
java -Dserver.port=8080 \
-Dsentinel.rule.nacos.server-addr=localhost:8848 \
-Dsentinel.rule.nacos.namespace=demo \
-Dsentinel.rule.nacos.group-id=sentinel \
-Dsentinel.metrics.type=influxdb \
-Dinflux.url=http://localhost:8086 \
-Dinflux.token=XXXXXX \
-Dinflux.org=sentinel \
-Dinflux.bucket=sentinel \
-jar target/sentinel-dashboard.jar

Docker 部署

本项目使用了 Spring Boot 的镜像分层特性优化了镜像的构建效率,请确保正确安装了 Docker 工具,然后执行以下命令。
docker build -f docker/Dockerfile -t sentinel-dashboard:{tag} .

Helm 部署

以应用为中心,建议使用 Helm 统一管理所需部署的 K8s 资源描述文件,请参考以下命令完成应用的安装和卸载。
helm install sentinel-dashboard ./helm # 部署资源
helm uninstall sentinel-dashboard # 卸载资源

如何接入

为了减少客户端集成的工作,可以使用 eden-architect 框架,只需要两步就可以完成 Sentinel 的集成。
  1. 引入 Sentinel 依赖
<dependency>
    <groupId>io.github.shiyindaxiaojie</groupId>
    <artifactId>eden-sentinel-spring-cloud-starter</artifactId>
</dependency>
  1. 开启 Sentinel 配置
spring:
  cloud:
    sentinel: # 流量治理组件
      enabled: false # 默认关闭,请按需开启
      http-method-specify: true # 兼容 RESTful
      eager: true # 立刻刷新到 Dashboard
      transport:
        dashboard: localhost:8090
      datasource:
        flow:
          nacos:
            server-addr: ${spring.cloud.nacos.config.server-addr}
            namespace: ${spring.cloud.nacos.config.namespace}
            groupId: sentinel
            dataId: ${spring.application.name}-flow-rule
            rule-type: flow
            data-type: json
最近任务多,时间关系,先写这么多吧~