众所周知,sentinel 对监控数据的做法是定时落盘在客户端,然后 sentinel 提供接口去拉取日志文件。所以 sentinel 在监控数据上理论上是最少存储 1 天以上的数据;然而作为控制台展示,则仅在内存中聚合 5 分钟以内的统计数据,不进行持久化。
官方鼓励大家对 dashboard 进行改造实现指标信息的持久化,并从其它的存储中(如 rdbms、时序数据库等)拉取的监控信息,包括实时的和历史的数据。
基于此,结合公司内部的需求,我们自行改造并实现了监控指标的持久化。本文把一些实现过程分享给大家!
sentinel 是阿里巴巴开源的流量治理平台,提供了 流量控制、熔断降级、系统负载保护、黑白名单访问控制 等功能。在实际的生产需求中,笔者进行了部分扩展:
流控规则持久化:适配 apollo、nacos、zookeeper
监控数据持久化:适配 influxdb、kafka、elasticsearch
监控面板优化:新增时间控件,允许在任意时刻内查询监控数据。
演示图例
改造前
改造后
快捷时间选择。
自定义时间选择。
如何构建
本项目默认使用 maven 来构建,最快的使用方式是把我给你的代码导入的开发工具中。或在项目的根目录执行 mvn install -t 4c 完成本项目的构建。
如何启动
idea 启动
本项目默认不依赖外部组件,可以直接启动运行。
在项目目录下运行 mvn install(如果不想运行测试,可以加上 -dskiptests 参数)。
进入 sentinel-dashboard 目录,执行 mvn spring-boot:run 或者启动 sentinelapplication 类。运行成功的话,可以看到 spring boot 启动成功的界面。
在实际的生产需求,sentinel 保存的规则和监控是需要持久化落盘的,因此,您可以在 sentinel-dashboard/src/main/resources/application.properties 接入外部组件。
规则存储类型:memory(默认)、nacos(推荐)、apollo、zookeeper
# 规则存储类型,可选项:memory(默认)、nacos(推荐)、apollo、zookeepersentinel.rule.type=nacos# nacos 存储规则,如果您设置了 sentinel.metrics.type=nacos,需要调整相关配置sentinel.rule.nacos.server-addr=localhost:8848sentinel.rule.nacos.namespace=demosentinel.rule.nacos.group-id=sentinelsentinel.rule.nacos.username=nacossentinel.rule.nacos.password=nacos# apollo 存储规则,如果您设置了 sentinel.metrics.type=apollo,需要调整相关配置sentinel.rule.apollo.portal-url=http://localhost:10034sentinel.rule.apollo.token=sentinel.rule.apollo.env=# zookeeper 存储规则,如果您设置了 sentinel.metrics.type=zookeeper,需要调整相关配置sentinel.rule.zookeeper.connect-string=localhost:2181sentinel.rule.zookeeper.root-path=/sentinel_rule
监控存储类型:memory(默认)、influxdb(推荐)、elasticsearch
# 监控存储类型,可选项:memory(默认)、influxdb(推荐)、elasticsearchsentinel.metrics.type=memory# influxdb 存储监控数据,如果您设置了 sentinel.metrics.type=influxdb,需要调整相关配置influx.url=http://localhost:8086/influx.token=ufgaw37a93pkncmjum25g7m2qkbg6xqqjgthh-o-uiviync_-q7rfwlttepmqhglcuasx64k3isc2un33ygelw==influx.org=sentinelinflux.bucket=sentinelinflux.log-level=noneinflux.read-timeout=10sinflux.write-timeout=10sinflux.connect-timeout=10s# elasticsearch 存储监控数据,如果您设置了 sentinel.metrics.type=elasticsearch,需要调整相关配置sentinel.metrics.elasticsearch.index-name=sentinel_metricspring.elasticsearch.rest.uris=http://localhost:9200spring.elasticsearch.rest.connection-timeout=3000spring.elasticsearch.rest.read-timeout=5000spring.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_metricspring.kafka.producer.bootstrap-servers=localhost:9092spring.kafka.producer.batch-size=4096spring.kafka.producer.buffer-memory=40960spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.stringserializerspring.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 的集成。
引入 sentinel 依赖
io.github.shiyindaxiaojie eden-sentinel-spring-cloud-starter
开启 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
微软新专利--拥有多功能的多点触控笔“ePen”
电路板喷三防漆步骤
笔记本电脑怎么选?11代酷睿处理器让你摆脱选择困难症
西门子SMARTP通过MODBUS RTU实现一主多从的步骤
把稳舵 发展前景自然来
基于Sentinel实现历史监控数据回看
!销售/回收HP8672A信号源HP 8672A小兵/孟S1
车用图像传感器参数小议——信噪比知多少?
特斯拉汽车租赁业务比例下降,与二手车转售价值疲软有关
存放电池电量计DS2780参数-Storing Batter
电源差异化的两大要素:高能效与高可靠性
精谱测控薄膜瑕疵检测系统的检测原理是怎样的
新款221系列导线连接器上市:适于Ex防爆应用,更简单、快速、可靠
工业领域自动驾驶的春天到来 安全生产问题必须要十分重要
简易光控电路图实物图
通用航空相关政策赋能行业发展,黑龙江省是中国通用航空业之王
河道流量在线监测系统实时监测河道流量
电瓶修复技术—电池破壳为什么总是修补不了
隔离开关的额定电流怎么计算出来
小米6和小米6plus:外形或超越友商2年!价格超过1999元