當(dāng)前位置:首頁 > IT技術(shù) > 其他 > 正文

Sentinel 基于Nacos規(guī)則持久化-推模式
2022-09-06 22:51:05


文章目錄

一、推模式架構(gòu)圖

Sentinel 基于Nacos規(guī)則持久化-推模式_推送

圖片引用自 https://github.com/alibaba/Sentinel/wiki/在生產(chǎn)環(huán)境中使用-Sentinel

二、原理簡述
2.1. 組件版本關(guān)系

Spring Cloud Alibaba Version

Sentinel Version

Nacos Version

RocketMQ Version

Dubbo Version

Seata Version

2.2.6.RELEASE

1.8.1

1.4.2

4.4.0

2.7.8

1.3.0

2021.1 or 2.2.5.RELEASE or 2.1.4.RELEASE or 2.0.4.RELEASE

1.8.0

1.4.1

4.4.0

2.7.8

1.3.0

Spring Cloud Version

Spring Cloud Alibaba Version

Spring Boot Version

Nacos Version

jdk

Spring Cloud Hoxton.SR9

2.2.6.RELEASE

2.3.2.RELEASE

1.4.2

1.8.202

2.2. 控制臺推送規(guī)則

將規(guī)則推送到Nacos或其他遠(yuǎn)程配置中心
Sentinel客戶端鏈接Nacos,獲取規(guī)則配置;并監(jiān)聽Nacos配置變化,如發(fā)生變化,就更新本地緩存(從而讓本地緩存總是和Nacos一致)
控制臺監(jiān)聽Nacos配置變化,如發(fā)生變化就更新本地緩存(從而讓控制臺本地緩存總是和Nacos一致)

三、Sentinel控制臺改造

控制臺改造主要是為規(guī)則實現(xiàn)
DynamicRuleProvider:從Nacos上讀取配置
DynamicRulePublisher:將規(guī)則推送到Nacos上

3.1. 下載源碼

這里使用1.8版本演示

??https://github.com/alibaba/Sentinel/tags??

Sentinel 基于Nacos規(guī)則持久化-推模式_sentinel_02

3.2. 修改pom

Sentinel 基于Nacos規(guī)則持久化-推模式_sentinel_03



<!-- for Nacos rule publisher sample -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
<scope>test</scope>
</dependency>

改為

<!-- for Nacos rule publisher sample -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
3.3. 重要文件復(fù)制

由于sentinel持久化默認(rèn)不支持持久化,但是官方關(guān)于sentinel提供了nacos、zookeeper、apollo這3種方式,但是需要自己集成。

找到 ??sentinel-dashboard/src/test/java/com/alibaba/csp/sentinel/dashboard/rule/nacos???目錄,將整個目錄拷貝到 sentinel-??dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/nacos??,如圖:

Sentinel 基于Nacos規(guī)則持久化-推模式_sentinel_04

3.4. 注冊地址修改

Sentinel 基于Nacos規(guī)則持久化-推模式_ide_05

3.5. 請求實例需改

修改sentinel頁面端保存規(guī)則請求的url控制層

Sentinel 基于Nacos規(guī)則持久化-推模式_sentinel_06


修改后的配置

Sentinel 基于Nacos規(guī)則持久化-推模式_sentinel_07

("flowRuleNacosProvider")
private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider;
@Autowired
@Qualifier("flowRuleNacosPublisher")
private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher;
3.6. 菜單新增

修改 sentinel-??dashboard/src/main/webapp/resources/app/scripts/directives/sidebar/sidebar.html??,找到:

Sentinel 基于Nacos規(guī)則持久化-推模式_spring_08


修改后

Sentinel 基于Nacos規(guī)則持久化-推模式_推送_09


齊活兒啦!終于把流控規(guī)則改造成推模式持久化啦!

四、編譯 & 啟動
4.1. 先啟動nacos

??https://nacos.io/zh-cn/docs/quick-start.html?? 這里一windows環(huán)境演示

startup.cmd -m standalone

Sentinel 基于Nacos規(guī)則持久化-推模式_spring_10


Sentinel 基于Nacos規(guī)則持久化-推模式_sentinel_11


里面的配置先忽略

4.2. 編譯打包

執(zhí)行 ??mvn clean package -DskipTests??? 在項目的 target 目錄找到??sentinel-dashboard.jar?? ,執(zhí)行 ??java -jar sentinel-dashboard.jar?? 啟動控制臺。

Sentinel 基于Nacos規(guī)則持久化-推模式_spring_12

登錄

Sentinel 基于Nacos規(guī)則持久化-推模式_ide_13

Sentinel 基于Nacos規(guī)則持久化-推模式_ide_14

4.3. 創(chuàng)建微服務(wù) && 啟動

創(chuàng)建微服務(wù)模塊
添加依賴

<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependencyManagement>
<dependencies>
<!--spring-cloud-alibaba 版本控制-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.6.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

配置文件

server:
port: 9000
spring:
cloud:
nacos:
discovery:
service: product-serv
server-addr: localhost:8848
sentinel:
transport:
dashboard: localhost:8080
filter:
enabled: false
management:
endpoints:
web:
exposure:
include: '*'

創(chuàng)建測試方法

package com.gblfy.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ProductController {

//http://localhost:9000/product/" + productId
@GetMapping("/product/{productId}")
public String getProductName(@PathVariable Integer productId) {
return "IPhone 12";
}
}

啟動應(yīng)用

請求url
???http://localhost:9000/product/1??

Sentinel 基于Nacos規(guī)則持久化-推模式_推送_15

4.4. 刷新sentinel

Sentinel 基于Nacos規(guī)則持久化-推模式_spring_16

4.5. 登錄nacos

Sentinel 基于Nacos規(guī)則持久化-推模式_sentinel_17

Sentinel 基于Nacos規(guī)則持久化-推模式_spring_18


Sentinel 基于Nacos規(guī)則持久化-推模式_spring_19

4.6. sentinel 規(guī)則刪除

Sentinel 基于Nacos規(guī)則持久化-推模式_sentinel_20

4.7. nacos規(guī)則查看

Sentinel 基于Nacos規(guī)則持久化-推模式_ide_21


規(guī)則也隨之刪除了

4.8. (企業(yè)案例)規(guī)則擴(kuò)展

以上只是將流控規(guī)則擴(kuò)展好了,其他需要其他模塊,還需要根據(jù)官網(wǎng)文檔自行擴(kuò)展

附上:
???(企業(yè)案例)使用Nacos持久化規(guī)則,改造sentinel-dashboard??


本文摘自 :https://blog.51cto.com/g

開通會員,享受整站包年服務(wù)立即開通 >