概述:本文基于apollo熟练使用的小伙伴参考,因为本文没有安装启动apollo的步骤,只提供apollo的对应的appid和相应的配置,具体apollo部署及客户端配置使用本文未提供,apollo相关依赖本文未提供;
本文使用:
spring-boot-2.6.8
版本
spring-cloud-starter-alibaba-2021.0.1.0
版本
sentinel-dashboard-1.8.3
版本
一、调整sentinel-dashboard源码,调整步骤如下:
- 将test包中的apollo包移到rule包下面
- 将最外层pom文件中的
sentinel-datasource-apollo
依赖的范围test注释掉 - 修改apollo包下面的
ApolloConfig
类,修改apollo的地址和token (获取apollotoken的方式在第"四"步) - 修改apollo包下面的
FlowRuleApolloProvider
类,修改apollo的appid和命名空间,都根据实际情况修改即可 - 修改apollo包下面的
FlowRuleApolloPublisher
类,修改apollo的appid和命名空间,都根据实际情况修改即可 - 修改controller-v2包下面的
FlowControllerV2
类,修改注入的规则了为apollo的
通过以上调整,sentinel-dashboard控制台的包调整完成,重新打包部署就可以了,下面展示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: # 流控规则
apollo:
namespaceName: sino-sentinel
flowRulesKey: flowRules
rule-type: flow #flow,degrade,authority,system, param-flow
degrade: # 熔断降级规则
apollo:
namespaceName: sino-sentinel
flowRulesKey: degrades
rule-type: degrade
authority: # 授权规则 未验证,官方不推荐
apollo:
namespaceName: sino-sentinel
flowRulesKey: authoritys
rule-type: authority
system: # 系统规则
apollo:
namespaceName: sino-sentinel
flowRulesKey: systems
rule-type: system
param-flow: # 热点规则
apollo:
namespaceName: sino-sentinel
flowRulesKey: paramflows
rule-type: param-flow
image.png
四、获取apollo第三方接入的token
image.png
image.png
image.png
五、查看结果
将sentinel控制台启动,apollo服务启动及配置完成,客户端服务启动。然后访问客户端服务的接口,即可在sentinel控制台看到对应的接口访问数据,同时也可以看到对应的流控规则等其他在apollo配置的规则,也在sentinel控制台上展示出来了
image.png
image.png
网友评论