前面講過了Sentinel的簡單應(yīng)用,但是有一個問題:如果直接使用阿里提供的Jar包龄句,當Sentinel重啟后回论,之前制定的規(guī)則就會丟失。那么我們就需要將數(shù)據(jù)持久化分歇。
Sentinel是支持持久化的傀蓉,不然阿里的商業(yè)版怎么用的。我們可以下載Sentinel源碼职抡,自己稍作改動葬燎,使規(guī)則能持久化到數(shù)據(jù)庫或硬盤。因為微服務(wù)使用的是Spring-cloud-alibaba缚甩,Sentinel支持和nacos整合谱净,就持久化到nacos數(shù)據(jù)庫中,同時sentinel還能讀取nacos中做的流控規(guī)則擅威。
先列出Spring-cloud-alibaba的版本:
這是阿里給出的,可以自己查:spring-cloud-alibaba版本對應(yīng)關(guān)系
Sentinel文檔郊丛,里面有下載的鏈接李请。
1. 后臺源碼修改
源碼下載完,我是用eclipse打開的厉熟,有一個依賴版本需要2處需要改一下:
sentinel中有和nacos中對接的源碼导盅,只不過沒有使用。我們修改:1是改成 默認后臺使用sentinel對接nacos庆猫,而不是存到內(nèi)存,1是前臺頁面接口調(diào)用nacos對應(yīng)的接口
1.1 sentinel-dashboard中添加nacos
nacos部分代碼已經(jīng)有了绅络,在test中月培,需要將代碼移到src/main/java
中,就可以使用恩急。
- pom.xml中將
sentinel-datasource-nacos
包的scope注釋掉
scope注釋
- pom.xml中將
-
代碼移動
代碼移動
-
nacos包中的4個類:
- FlowRuleNacosProvider: 動態(tài)獲取Nacos配置中心流控規(guī)則杉畜,讀取流控規(guī)則
- FlowRuleNacosPublisher: publish上傳流控規(guī)則到Nacos配置中心,寫入流控規(guī)則
- NacosConfig: Nacos配置
- NacosConfigUtils: 流控規(guī)則在nacos中配置文件的一些細節(jié):后綴衷恭、組別等
這里需要注意一點:在獲取nacos中的流控規(guī)則的時候此叠,nacos中流控配置文件,加了后綴随珠,組也給了特定的灭袁,我們不用改源碼猬错,后面我們在nacos中添加配置文件的時候按他給的名字來就行,如下圖:
1.2 配置連接自己的nacos
自帶的nacos配置只寫了個localhost
茸歧,我們按著他給的倦炒,連接我們自己的。修改NacosConfig.java
參數(shù)也可以寫到配置文件中软瞎,在這里接收逢唤。
@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);
}
若寫到配置文件中,這樣寫:
1.3 修改代碼Controller層涤浇,調(diào)用nacos提供的服務(wù)層
修改文件:com.alibaba.csp.sentinel.dashboard.controller.v2.FlowControllerV2.java
這個時候應(yīng)該可以啟動sentinel-dashboard了鳖藕,啟動后,登陸頁面左側(cè)是空白的只锭。
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;
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> 流控規(guī)則</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> 流控規(guī)則</a>
</li>
2.3 注釋掉一個按鈕
修改src/main/webapp/resources/app/views/flow_v2.html
注釋掉回到單機頁面
按鈕著恩。
3. 微服務(wù)(非網(wǎng)關(guān))
3.1 微服務(wù)引入jar包
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持久化數(shù)據(jù)源 -->
<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 微服務(wù)配置文件
sentinel有哪些規(guī)則,在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:
# 服務(wù)名稱必須帶上,不然nacos服務(wù)列表中沒有铺呵,也不會有注冊成功的信息
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
# 暫時只弄一個流控規(guī)則的吧裹驰,其他的自己添加
# 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中添加配置文件
添加的時候,格式選json片挂,內(nèi)容就先填個
{}
就行幻林。
3.4 測試
這個坑我遇到了,不知道什么原因音念。注意:打包放到linux系統(tǒng)中去測試沪饺,要不簇點鏈路
中無數(shù)據(jù)。
-
在linux中分別啟動服務(wù)闷愤,調(diào)幾個微服務(wù)的接口測試一下:
接口測試
-
-
等幾秒鐘整葡,在sentinel中查看,微服務(wù)已經(jīng)注冊上來了
微服務(wù)
-
-
簇點鏈路中能看到接口信息
簇點鏈路
-
-
選2個接口讥脐,添加流控規(guī)則
規(guī)則1步
規(guī)則2步
添加完成
-
-
查看nacos
有內(nèi)容了
格式化遭居,可以大致看到規(guī)則
-
以上就是 微服務(wù)整合Sentinel持久化到nacos,需要說明一點旬渠,這只是微服務(wù)俱萍,如果使用網(wǎng)關(guān)整合 sentinel和這個還是不一樣的,下次再寫網(wǎng)關(guān)gateway整合sentinel的告丢。
參考文章:https://blog.csdn.net/qq_44870331/article/details/129886930