概述:本文基于apollo熟練使用的小伙伴參考,因為本文沒有安裝啟動apollo的步驟,只提供apollo的對應的appid和相應的配置,具體apollo部署及客戶端配置使用本文未提供,apollo相關(guān)依賴本文未提供;
本文使用:
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依賴的范圍test注釋掉 - 修改apollo包下面的
ApolloConfig類,修改apollo的地址和token (獲取apollotoken的方式在第"四"步) - 修改apollo包下面的
FlowRuleApolloProvider類,修改apollo的appid和命名空間,都根據(jù)實際情況修改即可 - 修改apollo包下面的
FlowRuleApolloPublisher類,修改apollo的appid和命名空間,都根據(jù)實際情況修改即可 - 修改controller-v2包下面的
FlowControllerV2類,修改注入的規(guī)則了為apollo的
通過以上調(diào)整,sentinel-dashboard控制臺的包調(diào)整完成,重新打包部署就可以了,下面展示apollo中的配置
移動apollo包
注釋掉依賴的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
三、客戶端服務配置
客戶端服務需添加的依賴:
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
###應用編碼配置
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: # 熔斷降級規(guī)則
apollo:
namespaceName: sino-sentinel
flowRulesKey: degrades
rule-type: degrade
authority: # 授權(quán)規(guī)則 未驗證,官方不推薦
apollo:
namespaceName: sino-sentinel
flowRulesKey: authoritys
rule-type: authority
system: # 系統(tǒng)規(guī)則
apollo:
namespaceName: sino-sentinel
flowRulesKey: systems
rule-type: system
param-flow: # 熱點規(guī)則
apollo:
namespaceName: sino-sentinel
flowRulesKey: paramflows
rule-type: param-flow
image.png
四、獲取apollo第三方接入的token
image.png
image.png
image.png
五、查看結(jié)果
將sentinel控制臺啟動,apollo服務啟動及配置完成,客戶端服務啟動。然后訪問客戶端服務的接口,即可在sentinel控制臺看到對應的接口訪問數(shù)據(jù),同時也可以看到對應的流控規(guī)則等其他在apollo配置的規(guī)則,也在sentinel控制臺上展示出來了
image.png
image.png













