【Sentinel整合Apollo进行规则持久化】对Sentinel的代码进行了改造,【Apollo配置中心管理后台的详解】对Apollo管理后台进行了讲解。今天就最终实现:Sentinel整合Apollo进行规则持久化
0x01:先把Apollo配置中心启动
主要启动如下三个服务:
apollo-configservice:提供配置获取接口,提供配置更新推送接口,接口服务对象为Apollo客户端
apollo-adminservice:提供配置管理接口,提供配置修改、发布等接口,接口服务对象为Portal,以及Eureka
apollo-portal:提供Web界面供用户管理配置
0x02:sentinel-dashboard代码改动
【Sentinel整合Apollo进行规则持久化】整体只是把相关代码移动了一些位置,现在还需做些许改动。
配置类设置参数
根据实际情况修改Apollo管理后台地址和开发平台授权token
Apollo管理后台地址
token
FlowRuleApolloProvider修改
FlowRuleApolloPublisher修改:
备注:这里要注意如果使用ApolloConfigUtil.getFlowDataId()方法获取flowDataId,则在Apollo配置中心需要按照约定建立配置项的Key。
改造完成后,并可以启动sentinel-dashboard
0x03:Apollo配置中心添加配置
添加配置并发布
源码
flowRules = [{"app":"pay-service","clusterMode":false,"controlBehavior":0,"count":300,"gmtModified":1596855658214,"grade":1,"id":1,"limitApp":"default","resource":"/getUser","strategy":0}]
degrades = [{"resource": "/getUser","count": 50,"timeWindow": 5,"grade": 0},{"resource": "/getUser","count": 5,"timeWindow": 8,"grade": 2},{"resource": "/erro","count": 0.5,"timeWindow": 5,"grade": 1}]
authoritys = [{"resource": "/getUser","limitApp": "192.168.12.215","strategy": 1}]
paramflows = [{"resource": "/getUser","grade": 1,"paramIdx": 1,"count": 10,"paramFlowItemList": []}]
systems = [{"qps": 20}]
0x04:微服务改造
-
新建项目olive-apollo-sentinel-datasource,对应的pom.xml文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sentinel</groupId>
<artifactId>olive-apollo-sentinel-datasource</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<name>olive-nacos-sentinel-datasource</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-apollo</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.1.0.RELEASE </version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
-
修改配置文件
server:
port: 8866
spring:
application:
name: pay-service #olive-apollo-sentinel-datasource
cloud:
sentinel:
transport:
port: 8719 # 向sentinel-dashboard传输数据的端口 默认:8719
dashboard: 127.0.0.1:8081 # sentinel-dashboard
log:
dir: ./logs # 默认值${home}/logs/csp/
switch-pid: true # 日志带上线程id
datasource:
flow: # 流控规则
apollo:
namespaceName: application
flowRulesKey: flowRules
rule-type: flow #flow,degrade,authority,system, param-flow
degrade: # 熔断降级规则
apollo:
namespaceName: application
flowRulesKey: degrades
rule-type: degrade
authority: # 授权规则 未验证,官方不推荐
apollo:
namespaceName: application
flowRulesKey: authoritys
rule-type: authority
system: # 系统规则
apollo:
namespaceName: application
flowRulesKey: systems
rule-type: system
param-flow: # 热点规则
apollo:
namespaceName: application
flowRulesKey: paramflows
rule-type: param-flow
app:
id: payservice # 指定规则项目在 apollo 的app.id,要与 sentinel 控制台启动参数一致
apollo:
bootstrap:
enabled: true # 开启 apollo
namespaces: application
meta: http://192.168.56.1:8080
cacheDir: ./apolloconfig # 缓存文件位置
-
备注:注意这里的flowRuleKey的值,需要跟Apollo配置中心配置的Key一致,同时跟Sentinel中FlowRuleApolloProvider和FlowRuleApolloProvider类定义的flowDataId一致。另外,apollo.mata配置项的值为配置中心Eureka的地址
-
新建控制器
package com.olive.controller;
import java.util.HashMap;
import java.util.Map;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@GetMapping("/getUser")
public Map<String, Object> getUser() {
Map<String, Object> result = new HashMap<>();
result.put("code", "000000");
result.put("message", "ok");
return result;
}
}
-
新建SpringBoot启动类
package com.olive;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
-
启动微服务
0x05:测试验证
-
访问接口
http://localhost:8866/getUser
-
访问Sentinel
备注:可以通过修改相关配置来验证,Apollo和Sentinel是否相互同步,即在Apollo修改后,是否Sentinel能获取最新配置;或者在Sentinel修改后,是否Apollo能获取最新配置。
最后,这里只改造了流控规则,其他规则的改造与以上改造一致