眾所周知,Sentinel 對監(jiān)控數(shù)據(jù)的做法是定時落盤在客戶端,然后 Sentinel 提供接口去拉取日志文件。所以 Sentinel 在監(jiān)控數(shù)據(jù)上理論上是最少存儲 1 天以上的數(shù)據(jù);然而作為控制臺展示,則僅在內(nèi)存中聚合 5 分鐘以內(nèi)的統(tǒng)計數(shù)據(jù),不進(jìn)行持久化。
官方鼓勵大家對 Dashboard 進(jìn)行改造實現(xiàn)指標(biāo)信息的持久化,并從其它的存儲中(如 RDBMS、時序數(shù)據(jù)庫等)拉取的監(jiān)控信息,包括實時的和歷史的數(shù)據(jù)。
基于此,結(jié)合公司內(nèi)部的需求,我們自行改造并實現(xiàn)了監(jiān)控指標(biāo)的持久化。本文把一些實現(xiàn)過程分享給大家!
Sentinel 是阿里巴巴開源的流量治理平臺,提供了 流量控制、熔斷降級、系統(tǒng)負(fù)載保護(hù)、黑白名單訪問控制 等功能。在實際的生產(chǎn)需求中,筆者進(jìn)行了部分?jǐn)U展:
流控規(guī)則持久化:適配 Apollo、Nacos、Zookeeper
監(jiān)控數(shù)據(jù)持久化:適配 InfluxDB、Kafka、Elasticsearch
監(jiān)控面板優(yōu)化:新增時間控件,允許在任意時刻內(nèi)查詢監(jiān)控數(shù)據(jù)。
演示圖例
改造前
改造后
快捷時間選擇。
自定義時間選擇。
如何構(gòu)建
本項目默認(rèn)使用 Maven 來構(gòu)建,最快的使用方式是把我給你的代碼導(dǎo)入的開發(fā)工具中?;蛟陧椖康母夸泩?zhí)行 mvn install -T 4C 完成本項目的構(gòu)建。
如何啟動
IDEA 啟動
本項目默認(rèn)不依賴外部組件,可以直接啟動運行。
在項目目錄下運行 mvn install(如果不想運行測試,可以加上 -DskipTests 參數(shù))。
進(jìn)入 sentinel-dashboard 目錄,執(zhí)行 mvn spring-boot:run 或者啟動 SentinelApplication 類。運行成功的話,可以看到 Spring Boot 啟動成功的界面。
在實際的生產(chǎn)需求,Sentinel 保存的規(guī)則和監(jiān)控是需要持久化落盤的,因此,您可以在 sentinel-dashboard/src/main/resources/application.properties 接入外部組件。
規(guī)則存儲類型:memory(默認(rèn))、nacos(推薦)、apollo、zookeeper
#規(guī)則存儲類型,可選項:memory(默認(rèn))、nacos(推薦)、apollo、zookeeper sentinel.rule.type=nacos #Nacos存儲規(guī)則,如果您設(shè)置了sentinel.metrics.type=nacos,需要調(diào)整相關(guān)配置 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存儲規(guī)則,如果您設(shè)置了sentinel.metrics.type=apollo,需要調(diào)整相關(guān)配置 sentinel.rule.apollo.portal-url=http://localhost:10034 sentinel.rule.apollo.token= sentinel.rule.apollo.env= #Zookeeper存儲規(guī)則,如果您設(shè)置了sentinel.metrics.type=zookeeper,需要調(diào)整相關(guān)配置 sentinel.rule.zookeeper.connect-string=localhost:2181 sentinel.rule.zookeeper.root-path=/sentinel_rule
監(jiān)控存儲類型:memory(默認(rèn))、influxdb(推薦)、elasticsearch
#監(jiān)控存儲類型,可選項:memory(默認(rèn))、influxdb(推薦)、elasticsearch sentinel.metrics.type=memory #InfluxDB存儲監(jiān)控數(shù)據(jù),如果您設(shè)置了sentinel.metrics.type=influxdb,需要調(diào)整相關(guān)配置 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存儲監(jiān)控數(shù)據(jù),如果您設(shè)置了sentinel.metrics.type=elasticsearch,需要調(diào)整相關(guān)配置 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= #監(jiān)控數(shù)據(jù)存儲緩沖設(shè)置,降低底層存儲組件寫入壓力??蛇x項:none(默認(rèn)不啟用)、kafka(推薦) sentinel.metrics.sender.type=none #Kafka存儲監(jiān)控數(shù)據(jù),如果您設(shè)置了sentinel.metrics.sender.type=kafka,需要調(diào)整相關(guān)配置 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
鏡像啟動
本項目已發(fā)布到 Docker Hubhttps://hub.docker.com/repository/docker/shiyindaxiaojie/sentinel-dashboard,請執(zhí)行參考命令運行。
dockerrun-p8090:8090--name=sentinel-dashboard-dshiyindaxiaojie/sentinel-dashboard
如何部署
FatJar 部署
執(zhí)行 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 -jartarget/sentinel-dashboard.jar
Docker 部署
本項目使用了 Spring Boot 的鏡像分層特性優(yōu)化了鏡像的構(gòu)建效率,請確保正確安裝了 Docker 工具,然后執(zhí)行以下命令。
dockerbuild-fdocker/Dockerfile-tsentinel-dashboard:{tag}.
Helm 部署
以應(yīng)用為中心,建議使用 Helm 統(tǒng)一管理所需部署的 K8s 資源描述文件,請參考以下命令完成應(yīng)用的安裝和卸載。
helminstallsentinel-dashboard./helm#部署資源 helmuninstallsentinel-dashboard#卸載資源
如何接入
為了減少客戶端集成的工作,您可以使用 eden-architect 框架,只需要兩步就可以完成 Sentinel 的集成。
引入 Sentinel 依賴
io.github.shiyindaxiaojie eden-sentinel-spring-cloud-starter
開啟 Sentinel 配置
spring: cloud: sentinel:#流量治理組件 enabled:false#默認(rèn)關(guān)閉,請按需開啟 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
審核編輯:劉清
-
RDBMS
+關(guān)注
關(guān)注
0文章
9瀏覽量
5864 -
nacos
+關(guān)注
關(guān)注
0文章
10瀏覽量
221
原文標(biāo)題:基于 Sentinel 實現(xiàn)歷史監(jiān)控數(shù)據(jù)回看
文章出處:【微信號:芋道源碼,微信公眾號:芋道源碼】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
相關(guān)推薦
評論