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

構建
依賴
<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會在該端?啟動http server,那么這樣 的話,控制臺定義的?些限流等規(guī)則才能發(fā)送傳遞過來,
#如果 8100 端?被占?,那么會依次+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 # 類型來?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 # 類型來?RuleType類
management:
endpoints:
web:
exposure:
include: "*"
# 暴露健康接口的細節(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ī)則



屬性配置說明
流量配置

熔斷配置

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

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

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

注意
1)?個資源可以同時有多個限流規(guī)則和降級規(guī)則,所以配置集中是?個json數(shù)組。
2)Sentinel控制臺中修改規(guī)則,僅是內(nèi)存中?效,不會修改Nacos中的配置值,重啟后恢復原來的值; Nacos控制臺中修改規(guī)則,不僅內(nèi)存中?效,Nacos中持久化規(guī)則也?效,重啟后規(guī)則依然保持。
到此這篇關于Java中基于Nacos實現(xiàn)Sentinel規(guī)則持久化詳解的文章就介紹到這了,更多相關Nacos實現(xiàn)Sentinel規(guī)則持久化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
詳解如何使用SpringBoot實現(xiàn)下載JSON文件
在?Spring?Boot?中實現(xiàn)文件下載功能,可以通過將?JSON?字符串作為文件內(nèi)容返回給客戶端從而實現(xiàn)JSON文件下載效果,下面我們就來看看具體操作吧2025-02-02
Java Socket循環(huán)接收數(shù)據(jù)readLine()阻塞的解決方案
這篇文章主要介紹了Java Socket循環(huán)接收數(shù)據(jù)readLine()阻塞的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08

