前面讲过了Sentinel的简单应用,但是有一个问题:如果直接使用阿里提供的Jar包,当Sentinel重启后,之前制定的规则就会丢失。那么我们就需要将数据持久化。
Sentinel是支持持久化的,不然阿里的商业版怎么用的。我们可以下载Sentinel源码,自己稍作改动,使规则能持久化到数据库或硬盘。因为微服务使用的是Spring-cloud-alibaba,Sentinel支持和nacos整合,就持久化到nacos数据库中,同时sentinel还能读取nacos中做的流控规则。
先列出Spring-cloud-alibaba的版本:
![](https://img.haomeiwen.com/i22972070/ee7a6cd7c603415f.png)
![](https://img.haomeiwen.com/i22972070/b92d1fe9d92b2630.png)
这是阿里给出的,可以自己查:spring-cloud-alibaba版本对应关系
Sentinel文档,里面有下载的链接。
1. 后台源码修改
源码下载完,我是用eclipse打开的,有一个依赖版本需要2处需要改一下:
![](https://img.haomeiwen.com/i22972070/0ff17c39bc424799.png)
![](https://img.haomeiwen.com/i22972070/f10e277f08c99d45.png)
sentinel中有和nacos中对接的源码,只不过没有使用。我们修改:1是改成 默认后台使用sentinel对接nacos,而不是存到内存,1是前台页面接口调用nacos对应的接口
1.1 sentinel-dashboard中添加nacos
nacos部分代码已经有了,在test中,需要将代码移到src/main/java
中,就可以使用。
- pom.xml中将
sentinel-datasource-nacos
包的scope注释掉
scope注释
- pom.xml中将
-
代码移动
代码移动
-
nacos包中的4个类:
- FlowRuleNacosProvider: 动态获取Nacos配置中心流控规则,读取流控规则
- FlowRuleNacosPublisher: publish上传流控规则到Nacos配置中心,写入流控规则
- NacosConfig: Nacos配置
- NacosConfigUtils: 流控规则在nacos中配置文件的一些细节:后缀、组别等
这里需要注意一点:在获取nacos中的流控规则的时候,nacos中流控配置文件,加了后缀,组也给了特定的,我们不用改源码,后面我们在nacos中添加配置文件的时候按他给的名字来就行,如下图:
![](https://img.haomeiwen.com/i22972070/38acd2ee2fca6371.png)
![](https://img.haomeiwen.com/i22972070/570ec6a078c5ff5b.png)
1.2 配置连接自己的nacos
自带的nacos配置只写了个localhost
,我们按着他给的,连接我们自己的。修改NacosConfig.java
![](https://img.haomeiwen.com/i22972070/c52d0d6522ebcf91.png)
![](https://img.haomeiwen.com/i22972070/205975f37ab9093c.png)
参数也可以写到配置文件中,在这里接收。
@Value("${sentinel.nacos.address}")
private String nacosAddr;
@Value("${sentinel.nacos.username}")
private String userName;
@Value("${sentinel.nacos.password}")
private String password;
@Value("${sentinel.nacos.namespace}")
private String namespace;
@Bean
public ConfigService nacosConfigService() throws Exception {
Properties properties = new Properties();
properties.put(PropertyKeyConst.SERVER_ADDR, nacosAddr);
// properties.put(PropertyKeyConst.USERNAME, userName);
// properties.put(PropertyKeyConst.PASSWORD, password);
properties.put(PropertyKeyConst.NAMESPACE, namespace);
return ConfigFactory.createConfigService(properties);
}
若写到配置文件中,这样写:
![](https://img.haomeiwen.com/i22972070/905474105858a53f.png)
1.3 修改代码Controller层,调用nacos提供的服务层
修改文件:com.alibaba.csp.sentinel.dashboard.controller.v2.FlowControllerV2.java
![](https://img.haomeiwen.com/i22972070/9b40c16487f583f0.png)
这个时候应该可以启动sentinel-dashboard了,启动后,登陆页面左侧是空白的。
![](https://img.haomeiwen.com/i22972070/eefa80d9da843ba2.png)
2. 前端页面源码修改
2.1 配置中添加nacos接口并修改地址
修改src/main/webapp/resources/app/scripts/controllers/identity.js
-
第4行将V1改为V2
nacos地址
-
- 直接搜
/dashboard/flow/
定位第101行
- 直接搜
// let url = '/dashboard/flow/' + $scope.app;
let url = '/dashboard/v2/flow/' + $scope.app;
![](https://img.haomeiwen.com/i22972070/48fde3928caf0f98.png)
2.2 修改页面中的路由地址
修改src/main/webapp/resources/app/scripts/directives/sidebar/sidebar.html
直接搜dashboard.flowV1
定位57行去掉V1
<!-- <li ui-sref-active="active" ng-if="!entry.isGateway">
<a ui-sref="dashboard.flowV1({app: entry.app})">
<i class="glyphicon glyphicon-filter"></i> 流控规则</a>
</li> -->
<li ui-sref-active="active" ng-if="!entry.isGateway">
<a ui-sref="dashboard.flow({app: entry.app})">
<i class="glyphicon glyphicon-filter"></i> 流控规则</a>
</li>
![](https://img.haomeiwen.com/i22972070/7262eacfe91a4687.png)
2.3 注释掉一个按钮
修改src/main/webapp/resources/app/views/flow_v2.html
注释掉回到单机页面
按钮。
![](https://img.haomeiwen.com/i22972070/3f4926928cfac7e1.png)
3. 微服务(非网关)
3.1 微服务引入jar包
![](https://img.haomeiwen.com/i22972070/69cd9611dd08c255.png)
pom.xml
如下:
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.zrb.test</groupId>
<artifactId>sentinel-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>user-demo</artifactId>
<name>user-demo</name>
<description>sentinel测试</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-openapi2-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
<!--nacos客户端 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!-- sentinel -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!-- 使用nacos作为sentinel持久化数据源 -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
</dependencies>
<build>
<finalName>user-demo</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
3.2 微服务配置文件
![](https://img.haomeiwen.com/i22972070/3b789f79d133c98d.png)
sentinel有哪些规则,在org.springframework.cloud.alibaba.sentinel.datasource.RuleType
中,名字和他一致比较好。
application.yml
文件:
# 1. server
server:
port: 8001
logging:
level:
'[com.zrb.test.user.dao]': debug
# 3.spring
spring:
# 服务名称必须带上,不然nacos服务列表中没有,也不会有注册成功的信息
application:
name: user-demo
mvc:
pathmatch:
matching-strategy: ant-path-matcher
config:
import:
- nacos:${spring.application.name}.yaml
cloud:
nacos:
config:
server-addr: 192.168.50.89:8848
file-extension: yaml
discovery:
register-enabled: true
server-addr: 192.168.50.89:8848
sentinel:
transport:
dashboard: 192.168.50.89:8080
port: 8721
datasource:
flow:
nacos:
server-addr: 192.168.50.89:8848
namespace:
dataId: ${spring.application.name}-flow-rules
groupId: SENTINEL_GROUP
dataType: json
rule-type: flow
# 暂时只弄一个流控规则的吧,其他的自己添加
# degrade:
# nacos:
# server-addr: 192.168.50.89:8848
# namespace:
# dataId: ${spring.application.name}-degrade-rules
# groupId: SENTINEL_GROUP
# data-type: json
# rule-type: degrade
# system:
# nacos:
# server-addr: 192.168.50.89:8848
# namespace:
# dataId: ${spring.application.name}-system-rules
# groupId: SENTINEL_GROUP
# data-type: json
# rule-type: system
# authority:
# nacos:
# server-addr: 192.168.50.89:8848
# namespace:
# dataId: ${spring.application.name}-authority-rules
# groupId: SENTINEL_GROUP
# data-type: json
# rule-type: authority
# param-flow:
# nacos:
# server-addr: 192.168.50.89:8848
# namespace:
# dataId: ${spring.application.name}-param-flow-rules
# groupId: SENTINEL_GROUP
# data-type: json
# rule-type: param-flow
3.3 nacos中添加配置文件
![](https://img.haomeiwen.com/i22972070/041818a0e38d23df.png)
添加的时候,格式选json,内容就先填个
{}
就行。
3.4 测试
这个坑我遇到了,不知道什么原因。注意:打包放到linux系统中去测试,要不簇点链路
中无数据。
-
在linux中分别启动服务,调几个微服务的接口测试一下:
接口测试
-
-
等几秒钟,在sentinel中查看,微服务已经注册上来了
微服务
-
-
簇点链路中能看到接口信息
簇点链路
-
-
选2个接口,添加流控规则
规则1步
规则2步
添加完成
-
-
查看nacos
有内容了
格式化,可以大致看到规则
-
以上就是 微服务整合Sentinel持久化到nacos,需要说明一点,这只是微服务,如果使用网关整合 sentinel和这个还是不一样的,下次再写网关gateway整合sentinel的。
参考文章:https://blog.csdn.net/qq_44870331/article/details/129886930
网友评论