sentinel持久化接入Apollo(控制台改造)

一、下载官网源码:https://github.com/alibaba/Sentinel/

 

二、改造推送模式:

1、修改pom文件

将<scope>test</scope>注释掉,修改后如下:

 

2、java代码迁移

 

 

 3、修改流控模式,指向Apollo模式

 

 

 

 4、去掉注释

 

 

 经过以上步骤就已经把流控规则改造成推模式持久化

三、Apollo配置

1、Apollo控制台创建第三方应用

首先正常创建项目,然后管理员权限下创建

上图(1)

 

上图(2)

 

 

 

上图(3)

重点:必须创建公共命名空间,多个应用可同时访问才可用于同一控制台,否则不可用;

创建应用后,未来Sentinel控制台在启动是需要指定Apollo应用ID才能接入Apollo,而接入Apollo之后Sentinel的规则需要写入该应用下的namespace空间,因此还需要创建针对该应用的namespace空间

 

 

创建一个具体存储Sentinel各种限流、熔断降级等规则的Apollo存储空间,这里需要注意的是所创建的空间类型一定要是"public"公共空间

因为最终这些规则是需要具体的微服务应用去获取的,而在Apollo中应用下只有公共Namecspace才能被其他应用继承

 

四、sentinel控制配置

1、添加配置项

 

2、修改Apollo连接信息

 

 

 3、修改Apollo配置信息

 

 

 改造完成后,并可以启动sentinel-dashboard

有不懂的可以下载我的源码:预留gitee下载链接

 五、客户端接入

 引入jar包

 

 

 编写数据源,支持从Apollo读取

#启用OpenFeign整合Sentinel的自动配置
feign.sentinel.enabled= true

#是否开启熔断
spring.cloud.sentinel.enabled=true
#控制台地址
spring.cloud.sentinel.transport.dashboard=192.168.1.182:8090
#客户端跟控制台交互的端口号
spring.cloud.sentinel.transport.port=8089


#指定该数据源为限流规则 spring.cloud.sentinel.datasource.flow.apollo.rule
-type = flow spring.cloud.sentinel.datasource.flow.apollo.namespaceName = zt-sentinel-config spring.cloud.sentinel.datasource.flow.apollo.flowRulesKey = ${spring.application.name}-${spring.cloud.sentinel.datasource.flow.apollo.rule-type} #熔断降级规则 spring.cloud.sentinel.datasource.degrade.apollo.rule-type = degrade spring.cloud.sentinel.datasource.degrade.apollo.namespaceName = zt-sentinel-config spring.cloud.sentinel.datasource.degrade.apollo.flowRulesKey = ${spring.application.name}-${spring.cloud.sentinel.datasource.degrade.apollo.rule-type} #热点规则 spring.cloud.sentinel.datasource.paramFlow.apollo.rule-type = param_flow spring.cloud.sentinel.datasource.paramFlow.apollo.namespaceName = sentinel-config spring.cloud.sentinel.datasource.paramFlow.apollo.flowRulesKey = ${spring.application.name}-${spring.cloud.sentinel.datasource.paramFlow.apollo.rule-type}
#系统规则 spring.cloud.sentinel.datasource.system.apollo.rule-type = system spring.cloud.sentinel.datasource.system.apollo.namespaceName = sentinel-config spring.cloud.sentinel.datasource.system.apollo.flowRulesKey = ${spring.application.name}-${spring.cloud.sentinel.datasource.system.apollo.rule-type}
#降级规则 spring.cloud.sentinel.datasource.authority.apollo.rule-type = authority spring.cloud.sentinel.datasource.authority.apollo.namespaceName = sentinel-config spring.cloud.sentinel.datasource.authority.apollo.flowRulesKey = ${spring.application.name}-${spring.cloud.sentinel.datasource.authority.apollo.rule-type}

具体sentinel的客户端使用后续在写;

 

 

 

改造完成后,并可以启动sentinel-dashboard

posted @ 2021-08-13 09:07  夜空中的萤火虫  阅读(1329)  评论(1编辑  收藏  举报