概述:本文基于apollo熟練使用的小伙伴參考撕攒,因?yàn)楸疚臎](méi)有安裝啟動(dòng)apollo的步驟蒸苇,只提供apollo的對(duì)應(yīng)的appid和相應(yīng)的配置磷蛹,具體apollo部署及客戶端配置使用本文未提供,apollo相關(guān)依賴(lài)本文未提供溪烤;
本文使用:
spring-boot-2.6.8
版本
spring-cloud-starter-alibaba-2021.0.1.0
版本
sentinel-dashboard-1.8.3
版本
一味咳、調(diào)整sentinel-dashboard源碼,調(diào)整步驟如下:
- 將test包中的apollo包移到rule包下面
- 將最外層pom文件中的
sentinel-datasource-apollo
依賴(lài)的范圍test注釋掉 - 修改apollo包下面的
ApolloConfig
類(lèi)檬嘀,修改apollo的地址和token (獲取apollotoken的方式在第"四"步) - 修改apollo包下面的
FlowRuleApolloProvider
類(lèi)槽驶,修改apollo的appid和命名空間,都根據(jù)實(shí)際情況修改即可 - 修改apollo包下面的
FlowRuleApolloPublisher
類(lèi)枪眉,修改apollo的appid和命名空間,都根據(jù)實(shí)際情況修改即可 - 修改controller-v2包下面的
FlowControllerV2
類(lèi)再层,修改注入的規(guī)則了為apollo的
通過(guò)以上調(diào)整贸铜,sentinel-dashboard控制臺(tái)的包調(diào)整完成,重新打包部署就可以了聂受,下面展示apollo中的配置
移動(dòng)apollo包
注釋掉依賴(lài)的test
修改apollo地址和token
image.png
image.png
image.png
二蒿秦、apollo配置
spring.application.name = SentinelService
spring.cloud.sentinel.transport.dashboard = localhost:39999
spring.cloud.sentinel.log.dir = /logs
flowRules = [{"app":"SentinelService","clusterMode":false,"controlBehavior":0,"count":1,"gmtModified":1596855658214,"grade":1,"id":1,"limitApp":"default","resource":"/getMethod","strategy":0}]
degrades = [{"resource": "/getMethod","count": 50,"timeWindow": 5,"grade": 0},{"resource": "/getMethod","count": 5,"timeWindow": 8,"grade": 2},{"resource": "/erro","count": 0.5,"timeWindow": 5,"grade": 1}]
authoritys = [{"resource": "/getMethod","limitApp": "192.168.12.215","strategy": 1}]
paramflows = [{"resource": "/getMethod","grade": 1,"paramIdx": 1,"count": 10,"paramFlowItemList": []}]
systems = [{"qps": 20}]
image.png
image.png
三、客戶端服務(wù)配置
客戶端服務(wù)需添加的依賴(lài):
pom文件配置
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>2021.0.1.0</version>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-apollo</artifactId>
<version>1.8.3</version>
<exclusions>
<exclusion>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-extension</artifactId>
</exclusion>
<exclusion>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-core</artifactId>
</exclusion>
</exclusions>
</dependency>
yml文件配置
#Apollo配置
app:
id: hse-backend2.0.0
apollo:
meta: http://127.0.0.1:8080/
bootstrap:
enabled: true
namespaces: sino-sentinel
env: dev
server:
port: 39000
###應(yīng)用編碼配置
appCode: SentinelService
spring:
application.name: SentinelService
cloud:
sentinel:
log:
dir: /logs
switch-pid: true
transport:
dashboard: 127.0.0.1:39999
datasource:
flow: # 流控規(guī)則
apollo:
namespaceName: sino-sentinel
flowRulesKey: flowRules
rule-type: flow #flow,degrade,authority,system, param-flow
degrade: # 熔斷降級(jí)規(guī)則
apollo:
namespaceName: sino-sentinel
flowRulesKey: degrades
rule-type: degrade
authority: # 授權(quán)規(guī)則 未驗(yàn)證,官方不推薦
apollo:
namespaceName: sino-sentinel
flowRulesKey: authoritys
rule-type: authority
system: # 系統(tǒng)規(guī)則
apollo:
namespaceName: sino-sentinel
flowRulesKey: systems
rule-type: system
param-flow: # 熱點(diǎn)規(guī)則
apollo:
namespaceName: sino-sentinel
flowRulesKey: paramflows
rule-type: param-flow
image.png
四蛋济、獲取apollo第三方接入的token
image.png
image.png
image.png
五棍鳖、查看結(jié)果
將sentinel控制臺(tái)啟動(dòng),apollo服務(wù)啟動(dòng)及配置完成碗旅,客戶端服務(wù)啟動(dòng)渡处。然后訪問(wèn)客戶端服務(wù)的接口,即可在sentinel控制臺(tái)看到對(duì)應(yīng)的接口訪問(wèn)數(shù)據(jù)祟辟,同時(shí)也可以看到對(duì)應(yīng)的流控規(guī)則等其他在apollo配置的規(guī)則医瘫,也在sentinel控制臺(tái)上展示出來(lái)了
image.png
image.png