SpringCloud
十九、SpringCloud Alibaba Sentinel實作熔斷與限流
簡介
官網:https://github.com/alibaba/sentinel
中文版:https://github.com/alibaba/Sentinel/wiki/%E4%BB%8B%E7%BB%8D
文檔:https://spring-cloud-alibaba-group.github.io/github-pages/greenwich/spring-cloud-alibaba.html#_spring_cloud_alibaba_sentinel
下載下傳
下載下傳:https://github.com/alibaba/Sentinel/releases
普通運作
.jar
檔案:直接
java -jar XXX
指令運作即可
docker
#拉取sentinel鏡像
docker pull bladex/sentinel-dashboard
#運作sentinel(docker裡的sentinel是8858端口)
docker run --name sentinel -d -p 8858:8858 bladex/sentinel-dashboard
#把nacos和mysql也啟動起來
在浏覽器輸入:
http://ip:8858/#/login
(因為linux出了點問題,重裝了,是以ip也變了)
賬号和密碼都是
sentinel
控制台使用說明:https://github.com/alibaba/Sentinel/wiki/%E6%8E%A7%E5%88%B6%E5%8F%B0
初始化示範工程
- 建立子產品cloudalibaba-sentinel-service8401
- pom
<dependencies> <!-- SpringCloud ailibaba nacos--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!-- SpringCloud ailibaba sentinel-datasource-nacos 持久化需要用到--> <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-datasource-nacos</artifactId> </dependency> <!-- SpringCloud ailibaba sentinel--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--監控--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!--熱部署--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </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> </dependencies>
- yml
server: port: 8401 spring: application: name: cloudalibaba-sentinal-service cloud: nacos: discovery: #Nacos服務注冊中心位址(改成自己的伺服器ip位址,本地用localhost) server-addr: localhost:8848 sentinel: transport: #配置Sentin dashboard位址(改成自己的伺服器ip位址,本地用localhost) dashboard: localhost:8080 # 預設8719端口,假如被占用了會自動從8719端口+1進行掃描,直到找到未被占用的 端口 # 8719端口是跟sentinel互動的HTTP client port: 8719 management: endpoints: web: exposure: include: '*'
- 主啟動類
@EnableDiscoveryClient @SpringBootApplication public class MainApp8401 { public static void main(String[] args) { SpringApplication.run(MainApp8401.class, args); } }
- controller
@RestController public class FlowLimitController { @GetMapping("/testA") public String testA() { return "----testA"; } @GetMapping("/testB") public String testB() { return "----testB"; } }
-
測試,啟動8401,然後重新整理sentinel背景頁面(因為sentinel采用懶加載政策,是以需要調用服務後才在背景顯示)
在浏覽器分别輸入,然後重新整理sentinel背景頁面:
http://localhost:8401/testA
http://localhost:8401/testB
流控規則
流量控制
可在流控規則處建立:
也可簇點鍊路處指定添加:
表示1秒鐘内請求一次就OK,若超過次數1,就直接-快速失敗,報預設錯誤
每秒請求數超過1個就會限流。
門檻值類型
QPS與線程數的差別
QPS是直接擋在外面,而線程數是有多少個線程在處理,放進來後,有線程是空閑狀态就對請求進行處理,都沒空閑,就限流(關門打狗)。
流控模式
直接
關聯
A去調用B,B如果資源不足了,就限流A。
此時不管調用多少次A都不會限流,而此時超過1秒調用1次B,則會限流A。
使用Postman進行多次通路。
用postman20個線程每0.3秒給B發送請求,然後在去通路A,發現如下圖,A被阻塞
鍊路
在網上搜關于鍊路的是用下面這個例子,然而顯示不了限流的效果。
建立一個TestService
@Service
public class TestService {
@SentinelResource("getTest")
public void getTest(){
System.out.println("getTest...");
}
}
然後在FlowLimitController添加修改:
@Resource
TestService testService;
@GetMapping("/testA")
public String testA() {
testService.getTest();
return "----testA";
}
@GetMapping("/testB")
public String testB() {
testService.getTest();
return "----testB";
}
然後重新啟動8401,輸入
http://localhost:8401/testA
和
http://localhost:8401/testB
給getTest添加流控鍊路規則。
流控效果
流控效果隻有QPS有,線程數沒有。
快速失敗
預熱(Warm Up)
對testA新增流控規則
一開始的門檻值為10/3(預設為3,冷加載因子)=3門檻值;用5秒漸漸增加到10的門檻值
然後輸入
http://localhost:8401/testA
,不停重新整理,前5秒會限流,5秒後隻要每秒不超過10個請求,就不會限流。
排隊等待
讓請求以均勻的速度通過,單機門檻值為每秒通過數量,其餘的排隊等待; 它還會讓設定一個逾時時間,當請求超過逾時間時間還未處理,則會被丢棄。
在testB方法中添加
然後重新開機8401
postman發起的每秒10個請求進行排隊,testB每秒處理一個請求。
降級規則
時間視窗:降級多少時間(秒)
降級政策實戰
RT
請求數1秒内大于5個,且一個請求處理時間大于設定的值,進入熔斷後降級
在FlowLimitController中添加
@GetMapping("/testC")
public String testC(){
//暫停1秒鐘線程
try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }
log.info(Thread.currentThread().getName()+"\t"+"。。。testC,測試 R T");
return "----testC";
}
啟動8401,在浏覽器輸入
http://localhost:8401/testC
,然後在sentinel設定testC降級規則。
請求處理完成的時間為200毫秒(門檻值),超過這個時間熔斷降級進入時間視窗期不處理請求,1秒後退出時間視窗期,繼續處理請求。(前提是一秒請求數超過5個,如果請求數沒超過5個,就算請求處理的時間超過門檻值也不會熔斷降級)
說白了就是,請求處理是否能在200毫秒完成,不能就拉閘一秒
然後用Jmeter進行壓力測試。·
說明:
上面我們方法需要1秒鐘處理時間;
8401設定/testC,需要在200毫秒内容完成,不然就會1秒鐘時間視窗熔斷;
上面用Jmeter設定向
http://localhost:8401/testC
循環發送請求1秒鐘10個請求
(官方設定1秒内請求大于5個,且對應時刻的平均響應時間均超過門檻值)
意思就是:1s>200ms,1秒請求10>官方設定的5,是以↓↓↓
必然完成不了上面對8401RT的設定,所有就跳閘報錯了
異常比例
異常比例達到設定的百分比,進入熔斷後降級
修改testC
@GetMapping("/testC")
public String testC(){
log.info("testC 異常比例");
int age = 10/0;
return "----testC";
}
啟動8401,浏覽器輸入
http://localhost:8401/testC
,然後在背景設定testD的降級規則。
處理請求出錯超過
0.2
(20%,門檻值),熔斷降級,進入時間視窗期不處理請求,
3秒
後退出時間視窗期繼續處理請求。(前提也是每秒請求數超過5個,防止不會熔斷降級)
用Jmeter進行測試
異常數
異常大于等于設定的值,進入熔斷後降級
因為是按照一分鐘内的異常數對門檻值進行比較,如果時間視窗小于60秒,可能會再次進入熔斷狀态。是以時間視窗一定要大于等于60秒。
在FlowLimitController裡添加:
@GetMapping("/testE")
public String testE() {
log.info("testE 測試異常數");
int age = 10 / 0;
return "----testE 測試異常數";
}
啟動8401,浏覽器輸入
http://localhost:8401/testE
,然後在背景設定testE的降級規則。
一分鐘内,如果通路處理出現異常的次數超過5次,熔斷降級,進入時間視窗期不處理請求,61秒後退出時間視窗期繼續處理請求。(時間視窗必須大于等于60秒,防止再次熔斷降級)
重新整理5次,
http://localhost:8401/testE
熱點key限流
豪豬歌中的@HystrixCommand和現在的@SentinelResource相似度百分數90%
在FlowLimitController中添加:
@GetMapping("/testHotKey")
@SentinelResource(value ="testHotKey",blockHandler = "deal_testHotKey")
//value值:随意設定,隻要名稱唯一,id唯一名字
//blockHandler:指定哪個方法作為兜底方法
public String testHotKey(@RequestParam(value = "p1",required = false)String p1,
@RequestParam(value = "p2",required = false)String p2){
return "-------testHotKey";
}
//兜底方法
public String deal_testHotKey(String p1, String p2, BlockException blockException){
return "-------deal_testHotKey";
// sentinel的預設提示都是: Blocked by Sentinel (flow limiting)
}
重新開機8401,浏覽器輸入
http://localhost:8401/testHotKey
,然後在背景對testHotKey進行熱點規則配置。
如果每秒的通路請求帶有
索引為0
的參數的數量超過1,進入統計視窗期,然後調用兜底方法,1秒後退出統計視窗期,繼續處理請求。
【指定是方法中參數體内的第一個參數】如果第一個(參數索引開始為0)參數的QPS每秒請求次數大于1,就降級1秒鐘,并調用兜底方法。
http://localhost:8401/testHotKey?p1=1
http://localhost:8401/testHotKey?p1=1&p2=2
如果設定熱點規則,而@SentinelResource注解裡
沒有指明blockHandler兜底方法
,就會把直接把錯誤頁面資訊列印到前台。
參數例外項
特定配置,設定參數等于某個值,并為其設定門檻值,如果超過就限流
進階選項隻有在熱點規則裡有,簇點鍊路無法配置進階選項。
索引0的參數的值為vip,則其限流門檻值為200
http://localhost:8401/testHotKey?p1=1
http://localhost:8401/testHotKey?p1=vip
給testHotKey方法添加
int i = 1 / 0;
異常。
然後重新測試,發現兜底方法不适用于異常,有異常會直接列印到頁面。
系統規則
Sentinel 系統自适應限流從
整體次元
對應用入口流量進行控制,結合應用的 Load、CPU 使用率、總體平均 RT、入口 QPS 和并發線程數等幾個次元的監控名額,通過自适應的流控政策,讓系統的入口流量和系統的負載達到一個平衡,讓系統盡可能跑在最大吞吐量的同時保證系統整體的穩定性。
針對整個系統的,每秒通路量超過1(門檻值),限流。
http://localhost:8401/testA
http://localhost:8401/testB
@SentinelResource
按資源名稱限流+後續處理
資源名稱
在pom添加
<!--換成你們直接的包名-->
<!-- 引用自己定義的api通用包,可以使用Payment支付Entity -->
<dependency>
<groupId>com.angenin.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
建立RateLimitController:【前面的controller裡面方法太多了,重寫個,不然暈了】
@RestController
public class RateLimitController {
@GetMapping("/byResource")
@SentinelResource(value = "byResource",blockHandler = "handleException")
public CommonResult byResource() {
return new CommonResult(200,"按照資源名稱限流測試",new Payment(2020L,"serial001"));
}
//兜底方法
public CommonResult handleException(BlockException exception) {
return new CommonResult(444,exception.getClass().getCanonicalName() + "\t 服務不可用");
}
}
啟動8401
http://localhost:8401/byResource
用資源名稱進行配置兜底方法才生效。
多次重新整理頁面
按照URL位址限流+後續處理
URL位址:
在RateLimitController中添加:
@GetMapping("/rateLimit/byUrl")
@SentinelResource(value = "byUrl") //沒有兜底方法,系統就用預設的
public CommonResult byUrl() {
return new CommonResult(200,"按照byUrl限流測試",new Payment(2020L,"serial002"));
}
啟動8401
http://localhost:8401/rateLimit/byUrl
多次重新整理:【不指定兜底方法,就用系統預設的】
上面兜底方案面臨的問題
客戶自定義限流處理邏輯
建立myhandler.CustomerBlockHandler自定義限流處理類:
public class CustomerBlockHandler {
public static CommonResult handlerException(BlockException exception) {
return new CommonResult(444,"按照客戶自定義限流測試,Glogal handlerException ---- 1");
}
public static CommonResult handlerException2(BlockException exception) {
return new CommonResult(444,"按照客戶自定義限流測試,Glogal handlerException ---- 2");
}
}
在RateLimitController中添加:
//CustomerBlockHandler
@GetMapping("/rateLimit/customerBlockHandler")
@SentinelResource(value = "customerBlockHandler",
blockHandlerClass = CustomerBlockHandler.class,
blockHandler = "handlerException2")
//blockHandlerClass:指定用哪個類做阻塞類
//blockHandler:指定上面指定類中的哪個方法為阻塞方法
public CommonResult customerBlockHandler() {
return new CommonResult(200,"按照客戶自定義限流測試",new Payment(2020L,"serial003"));
}
啟動8401
http://localhost:8401/rateLimit/customerBlockHandler
【url配置不會走兜底方法】
用資源名稱進行配置兜底方法才生效。
多次重新整理:
更多注解屬性說明
服務熔斷功能
feign 和 ribbon 是 Spring Cloud 的 Netflix 中提供的兩個實作軟負載均衡的元件,Ribbon 和 Feign 都是用于調用其他服務的,方式不同。Feign 則是在 Ribbon 的基礎上進行了一次改進,采用接口的方式,将需要調用的其他服務的方法定義成抽象方法即可,不需要自己建構 http 請求。不過要注意的是抽象方法的注解、方法簽名要和提供服務的方法完全一緻。
服務降級是出現異常切換到兜底方案不會自動恢複過來,但是熔斷則是服務降級,但是過一段時間會慢慢恢複
Ribbon系列
建立提供者
- 建立子產品cloudalibaba-provider-payment9003
- pom
<dependencies> <!-- SpringCloud ailibaba nacos--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!-- SpringCloud ailibaba sentinel--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency> <!-- 引用自己定義的api通用包,可以使用Payment支付Entity --> <dependency> <groupId>com.achang.springcloud</groupId> <artifactId>cloud-api-common</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--監控--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!--熱部署--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </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> </dependencies>
- yml
server: port: 9003 spring: application: name: nacos-payment-provider cloud: nacos: discovery: server-addr: localhost:8848 #nacos management: endpoints: web: exposure: include: '*'
- 主啟動類
@SpringBootApplication @EnableDiscoveryClient //nacos服務注冊發現 public class PaymentMain9003 { public static void main(String[] args) { SpringApplication.run(PaymentMain9003.class,args); } }
-
controller
直接map模拟dao層
@RestController public class PaymentController { @Value("${server.port}") //spring的注解 private String serverPort; public static HashMap<Long, Payment> map = new HashMap<>(); static { map.put(1L,new Payment(1L,"1111")); map.put(2L,new Payment(2L,"2222")); map.put(3L,new Payment(3L,"3333")); } @GetMapping(value = "/paymentSQL/{id}") public CommonResult<Payment> paymentSQL(@PathVariable("id") Long id) { Payment payment = map.get(id); CommonResult<Payment> result = new CommonResult<>(200,"from mysql,serverPort: " + serverPort,payment); return result; } }
- 按照9003建立9004
建立消費者
- 建立子產品cloudalibaba-consumer-nacos-order84
- pom
<dependencies> <!-- SpringCloud ailibaba nacos--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!-- SpringCloud ailibaba sentinel--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency> <!-- 引用自己定義的api通用包,可以使用Payment支付Entity --> <dependency> <groupId>com.achang.springcloud</groupId> <artifactId>cloud-api-common</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--監控--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!--熱部署--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </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> </dependencies>
- yml
server: port: 84 spring: application: name: nacos-order-consumer cloud: nacos: discovery: server-addr: localhost:8848 #nacos sentinel: transport: dashboard: localhost:8080 #sentinel port: 8719 #消費者将去通路的微服務名稱 server-url: nacos-user-service: http://nacos-payment-provider #激活Sentinel對Feign的支援 feign: sentinel: enabled: true
- 主啟動類
@EnableDiscoveryClient @SpringBootApplication @EnableFeignClients //開啟Feign功能 public class OrderMain84 { public static void main(String[] args) { SpringApplication.run(OrderMain84.class,args); } }
- config
@Configuration public class ApplicationContextConfig { @Bean @LoadBalanced //Ribbon負載均衡 public RestTemplate getRestTemplate() { return new RestTemplate(); } }
- controller
@RestController @Slf4j public class CircleBreakerController { public static final String SERVICE_URL = "http://nacos-payment-provider"; @Resource private RestTemplate restTemplate; @RequestMapping("/consumer/fallback/{id}") @SentinelResource(value = "fallback") //沒有配置 public CommonResult<Payment> fallback(@PathVariable Long id) { CommonResult<Payment> result = restTemplate.getForObject( SERVICE_URL + "/paymentSQL/" + id,CommonResult.class,id); if(id == 4){ throw new IllegalArgumentException("IllegalArgument,非法參數異常..."); }else if(result.getData() == null) { throw new NullPointerException("NullPointerException,該ID沒有對應記錄,空指針異常"); } return result; } }
啟動9003,9004,84
http://localhost:84/consumer/fallback/1
測試是否可實作ribbon
輪循調用
提供者的服務
http://localhost:84/consumer/fallback/4
http://localhost:84/consumer/fallback/5
隻配置fallback
隻負責程式業務異常
修改84的CircleBreakerController類的
fallback
方法中的@SentinelResource注解,并在類中添加
@SentinelResource(value = "fallback",fallback ="handlerFallback") //隻配置fallback(隻負責程式業務異常)
//fallback兜底
public CommonResult handlerFallback(@PathVariable Long id,Throwable e) {
Payment payment = new Payment(id,"null");
return new CommonResult(444,"異常handlerFallback,exception内容: " + e.getMessage(), payment);
}
重新運作
http://localhost:84/consumer/fallback/4
http://localhost:84/consumer/fallback/5
隻配置blockHandler
隻負責sentinel控制台配置違規
修改@SentinelResource注解,并在類中添加
@SentinelResource(value = "fallback", blockHandler = "blockHandler") //隻配置blockHandler(隻負責sentinel控制台配置違規)
//blockHandler兜底
public CommonResult blockHandler(@PathVariable Long id,BlockException e) {
Payment payment = new Payment(id,"null");
return new CommonResult(444,"blockHandler-sentinel 限流,BlockException: " + e.getMessage(), payment);
}
重新開機項目
通路
http://localhost:84/consumer/fallback/1
,然後在sentinel背景進行配置。
http://localhost:84/consumer/fallback/5
前兩次會出現如下
第三次就走我們規定的blockHandler的兜底方法
因為沒配置指定fallback兜底方法,是以會直接顯示錯誤頁面,配置了blockHandler兜底方法,是以當sentinel配置違規會執行blockHandler兜底方法。
配置fallback和blockHandler
修改@SentinelResource注解
重新開機項目,輸入
http://localhost:84/consumer/fallback/1
,然後到背景配置。
http://localhost:84/consumer/fallback/1
多次重新整理執行blockHandler兜底方法。
http://localhost:84/consumer/fallback/5
執行fallback兜底方法。
http://localhost:84/consumer/fallback/5
多次重新整理執行blockHandler兜底方法。
當@SentinelResource注解fallback和blockHandler都指定後,然後同時符合,
優先執行blockHandler兜底方法
。
忽略屬性
修改@SentinelResource注解:
@SentinelResource(value = "fallback",
fallback ="handlerFallback",
blockHandler = "blockHandler",
exceptionsToIgnore = {IllegalArgumentException.class})
//如果出現exceptionsToIgnore中的異常,不運作fallback兜底方法。
http://localhost:84/consumer/fallback/4
id為4時,報的是IllegalArgumentException,非法參數異常
原本應該由fallback指定的兜底方法處理,但是因為配置了exceptionsToIgnore
是以就fallback就不處理了
http://localhost:84/consumer/fallback/5
Feign系列
通過一個接口,标注@FeignClient注解用fallback指定兜底類,value指定要在注冊中心找哪個提供者的服務;接口的方法要與提供者提供的服務參數一緻
修改84
- pom
<!--前面已添加了--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
- yml
#前面也已經添加了 #激活Sentinel對Feign的支援 feign: sentinel: enabled: true
- 主啟動類上添加
(也已經添加了)@EnableFeignClients
@EnableDiscoveryClient @SpringBootApplication @EnableFeignClients //開啟Feign功能 public class OrderMain84 { public static void main(String[] args) { SpringApplication.run(OrderMain84.class,args); } }
-
service
PaymentService接口
@FeignClient(value = "nacos-payment-provider",fallback = PaymentFallbackService.class) public interface PaymentService { @GetMapping(value = "/paymentSQL/{id}") public CommonResult<Payment> paymentSQL(@PathVariable("id") Long id); }
PaymentFallbackService實作類
上面fallback規定的兜底類
@Component //元件加入ioc容器,不要忘記了 public class PaymentFallbackService implements PaymentService{ @Override public CommonResult<Payment> paymentSQL(Long id) { return new CommonResult<>(444,"服務降級傳回,---PaymentFallbackService",new Payment(id,"ErrorSerial")); } }
-
controller
CircleBreakerController中添加:
//======= OpenFeign @Resource private PaymentService paymentService; @GetMapping(value = "/consumer/paymentSQL/{id}") public CommonResult< Payment > paymentSQL(@PathVariable("id") Long id){ return paymentService.paymentSQL(id); }
啟動項目,
http://localhost:84/consumer/paymentSQL/1
關閉9003和9004,執行@FeignClient的fallback兜底方法PaymentFallbackService。
熔斷限流架構對比
規則持久化
半成品持久化,需要手動配置到nacos
修改8401
- pom
<!--之前添加了--> <!-- SpringCloud ailibaba sentinel-datasource-nacos 持久化需要用到--> <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-datasource-nacos</artifactId> </dependency>
- yml
spring: application:.. cloud:.. nacos:.. sentinel:.. transport:.. datasource: ds1: nacos: server-addr: localhost:8848 #nacos dataId: ${spring.application.name} groupId: DEFAULT_GROUP data-type: json rule-type: flow feign: sentinel: enabled: true #激活Sentinel 對Feign的支援
-
在nacos背景添加配置:
與sentinel上的流控配置相對應
看在資料庫是否有↑↑此配置資訊, 是有的↓↓ 啟動8401[ { "resource": "/rateLimit/byUrl", "limitApp": "default", "grade": 1, "count": 1, "strategy": 0, "controlBehavior": 0, "clusterMode": false } ]
多次重新整理http://localhost:8401/rateLimit/byUrl
停止8401
再檢視sentinel背景,發下之前配置的流控規則消失
再次啟動8401,因為sentinel是懶加載模式,是以需要先通路
,然後檢視sentinel背景,發現出現了。 多次重新整理http://localhost:8401/rateLimit/byUrl
實作sentinel配置的持久化。http://localhost:8401/rateLimit/byUrl