Java中基于Nacos實(shí)現(xiàn)Sentinel規(guī)則持久化詳解
前言
Sentinel Dashboard中添加的規(guī)則數(shù)據(jù)存儲(chǔ)在內(nèi)存,微服務(wù)停掉規(guī)則數(shù)據(jù)就消失,在?產(chǎn)環(huán)境下不合適。我們可以將Sentinel規(guī)則數(shù)據(jù)持久化到Nacos配置中?,讓微服務(wù)從Nacos獲取規(guī)則數(shù)據(jù)。

構(gòu)建
依賴
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--sentinel 核心環(huán)境 依賴-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!-- Sentinel?持采? Nacos 作為規(guī)則配置數(shù)據(jù)源,引?該適配依賴 -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>application.yml中配置Nacos數(shù)據(jù)源
server:
port: 8100
spring:
profiles:
active: dev
application:
name: nacos-sentinel-8100
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
# namespace + group 隔離環(huán)境
namespace: 86614fa3-f528-44e7-95e5-265cc51feb41
group: DEFAULT_GROUP
sentinel:
transport:
dashboard: 127.0.0.1:8080 # sentinel dashboard/console地址
port: 8100 # sentinel會(huì)在該端?啟動(dòng)http server,那么這樣 的話,控制臺(tái)定義的?些限流等規(guī)則才能發(fā)送傳遞過(guò)來(lái),
#如果 8100 端?被占?,那么會(huì)依次+1
datasource:
# 此處的flow為?定義數(shù)據(jù)源名
flow: # 流控規(guī)則
nacos:
server-addr: ${spring.cloud.nacos.discovery.server-addr}
# namespace + data-id + group 定位資源配置文件
namespace: 86614fa3-f528-44e7-95e5-265cc51feb41
data-id: ${spring.application.name}-flow-rules
groupId: DEFAULT_GROUP
data-type: json
rule-type: flow # 類型來(lái)?RuleType類
degrade:
nacos:
server-addr: ${spring.cloud.nacos.discovery.server-addr}
namespace: 86614fa3-f528-44e7-95e5-265cc51feb41
data-id: ${spring.application.name}-degrade-rules
groupId: DEFAULT_GROUP
data-type: json
rule-type: degrade # 類型來(lái)?RuleType類
management:
endpoints:
web:
exposure:
include: "*"
# 暴露健康接口的細(xì)節(jié)
endpoint:
health:
show-details: always接口
@RestController
@RequestMapping("/api")
public class Test1Controller {
@GetMapping("/test1")
public String test1(String str) {
return str +"test1 : GetMapping " + LocalDateTime.now();
}
@GetMapping("/test2")
public String test2(String str) {
int i = 1/0;
return str +"test2 : GetMapping " + LocalDateTime.now();
}
@RequestMapping("/test3")
public String test3(String str) {
int i = 1/0;
return str +"test3 : GetMapping " + LocalDateTime.now();
}
}nacos中添加配置限流和熔斷規(guī)則



屬性配置說(shuō)明
流量配置

熔斷配置

在之前自定義流量限流和熔斷限流中可以知道,捕獲異常規(guī)則時(shí)候類是下面這些類。 FlowRule流量控制,DegradeRule熔斷規(guī)則。

我們重啟服務(wù)后可以看到/api/test1限流規(guī)則

/api/test2熔斷規(guī)則

注意
1)?個(gè)資源可以同時(shí)有多個(gè)限流規(guī)則和降級(jí)規(guī)則,所以配置集中是?個(gè)json數(shù)組。
2)Sentinel控制臺(tái)中修改規(guī)則,僅是內(nèi)存中?效,不會(huì)修改Nacos中的配置值,重啟后恢復(fù)原來(lái)的值; Nacos控制臺(tái)中修改規(guī)則,不僅內(nèi)存中?效,Nacos中持久化規(guī)則也?效,重啟后規(guī)則依然保持。
到此這篇關(guān)于Java中基于Nacos實(shí)現(xiàn)Sentinel規(guī)則持久化詳解的文章就介紹到這了,更多相關(guān)Nacos實(shí)現(xiàn)Sentinel規(guī)則持久化內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java之標(biāo)準(zhǔn)序列化機(jī)制詳解
本文介紹了Java中的序列化機(jī)制,包括Serializable接口的實(shí)現(xiàn)、ObjectOutputStream和ObjectInputStream的用法、transient關(guān)鍵字和writeObject/readObject方法的的自定義序列化2026-05-05
詳解如何使用SpringBoot實(shí)現(xiàn)下載JSON文件
在?Spring?Boot?中實(shí)現(xiàn)文件下載功能,可以通過(guò)將?JSON?字符串作為文件內(nèi)容返回給客戶端從而實(shí)現(xiàn)JSON文件下載效果,下面我們就來(lái)看看具體操作吧2025-02-02
Spring集成Swagger常見(jiàn)錯(cuò)誤及解決辦法
這篇文章主要介紹了Spring集成Swagger常見(jiàn)錯(cuò)誤及解決辦法,幫助大家更好的理解和學(xué)習(xí)使用Spring,感興趣的朋友可以了解下2021-05-05
Java Socket循環(huán)接收數(shù)據(jù)readLine()阻塞的解決方案
這篇文章主要介紹了Java Socket循環(huán)接收數(shù)據(jù)readLine()阻塞的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08

