SpringCloud?集成Sentinel的實(shí)戰(zhàn)教程
一、Seninel簡介
二、Sentinel和Hystrix的區(qū)別
三、sentinel可視化界面安裝
下載對(duì)應(yīng)版本的sentinel的jar包,通過終端命令:
java -jar jar包名
啟動(dòng)
訪問對(duì)應(yīng)路徑:控制臺(tái)如下:
四、在springcloudalibaba中整合sentinel
(1)添加依賴
<!--sentinel啟動(dòng)器--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency>
(2)配置yml
server: port: 8002 spring: application: name: WXL-DEV-SERVICE-2 cloud: sentinel: transport: dashboard: 127.0.0.1:8080
(3)啟動(dòng)服務(wù),再訪問服務(wù)后,觀察控制臺(tái):因?yàn)樵L問接口以后才會(huì)注冊到sentinel當(dāng)中。
五、流控規(guī)則
(1)實(shí)時(shí)監(jiān)控,可用于查看接口訪問情況
(2)簇點(diǎn)鏈路,可以對(duì)對(duì)應(yīng)的資源流控降級(jí)
可以設(shè)置閥值來流控:
(3)QPS流控
可以看到當(dāng)每秒超過2次時(shí)被流控:
流控文字可自定義:
@GetMapping("/world") @SentinelResource(value = "helloWorld",blockHandlerClass = TestController.class,blockHandler = "helloBlock") public String helloWorld() { return "Hello world"; } public static String helloBlock(BlockException e){ return "你已被流控"; }
value將該方法定義為sentinel的資源,blockHandlerClass指明流控處理的類,blockHandler是流控時(shí)調(diào)用的方法。
這里需要注意處理異常的方法必須是靜態(tài)方法添加static, 并需要添加sentinel的異常參數(shù)BlockException。
統(tǒng)一異??刂铺幚?/p>
上面通過注解實(shí)現(xiàn)流控靈活性更高,對(duì)于需要統(tǒng)一管理的流控處理,我們可以通過統(tǒng)一異常處理來實(shí)現(xiàn)??梢宰远x處理不同類型的限流。
只需實(shí)現(xiàn)對(duì)應(yīng)接口即可,案例代碼如下:
package com.dragonwu.exception; import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler; import com.alibaba.csp.sentinel.slots.block.BlockException; import com.alibaba.csp.sentinel.slots.block.authority.AuthorityException; import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException; import com.alibaba.csp.sentinel.slots.block.flow.FlowException; import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException; import com.alibaba.csp.sentinel.slots.system.SystemBlockException; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.http.MediaType; import org.springframework.stereotype.Component; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.HashMap; import java.util.Map; @Component public class MyBlockExceptionHandler implements BlockExceptionHandler { @Override public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, BlockException e) throws Exception { System.out.println("BlockExceptioonHandler ++++++++++++++++++++++++++"+e.getRule()); Map<Integer,String> hashMap=new HashMap<>(); if(e instanceof FlowException){ hashMap.put(100,"接口限流了"); }else if(e instanceof DegradeException){ hashMap.put(101,"服務(wù)降級(jí)了"); }else if(e instanceof ParamFlowException){ hashMap.put(102,"熱點(diǎn)參數(shù)限流了"); }else if(e instanceof SystemBlockException){ hashMap.put(103,"觸發(fā)系統(tǒng)保護(hù)規(guī)則了"); }else if(e instanceof AuthorityException){ hashMap.put(104,"授權(quán)規(guī)則不通過"); } //返回json數(shù)據(jù) httpServletResponse.setStatus(500); httpServletResponse.setCharacterEncoding("utf-8"); httpServletResponse.setContentType(MediaType.APPLICATION_JSON_VALUE); new ObjectMapper().writeValue(httpServletResponse.getWriter(),hashMap); } }
(4)線程流控
(5)關(guān)聯(lián)限流
這里的意思是如果/hello/add接口一秒鐘之內(nèi)訪問超過2次,則/hello/query會(huì)被限流。
(6)熔斷降級(jí)
也要設(shè)置熔斷時(shí)長,熔斷時(shí)長過完之后會(huì)進(jìn)入半開狀態(tài),即若下一次請(qǐng)求為慢請(qǐng)求則再次熔斷,直到第一次請(qǐng)求不是慢請(qǐng)求才會(huì)恢復(fù)正常狀態(tài)。
六、OpenFeign整合Sentinel
(1)導(dǎo)入依賴:
<!--OpenFeign--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
(2)調(diào)用者開發(fā)整合配置:
feign: sentinel: enabled: true #開啟openFeign對(duì)sentinel的整合
(3)添加openFeign調(diào)用接口
package com.wxl.feign; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; @FeignClient(name = "WXL-DEV-SERVICE-2", path = "/hello",fallback = ServiceFailFeign.class) public interface Service1HelloInterface { @GetMapping("/world") String helloWorld(); }
并且這里添加參數(shù)fallback值為失敗時(shí)回調(diào)的實(shí)現(xiàn)類。
實(shí)現(xiàn)類如下:
package com.wxl.feign; import org.springframework.stereotype.Component; @Component public class ServiceFailFeign implements Service1HelloInterface{ public String helloWorld() { return "降級(jí)了?。。?; } }
當(dāng)接口請(qǐng)求失敗時(shí)便會(huì)調(diào)用失敗類里的該方法。
這里我們?yōu)榱耸褂眯Ч?,在服?wù)生產(chǎn)者的接口里故意寫入報(bào)錯(cuò)代碼:
@GetMapping("/world") public String helloWorld() { int i=1/0; return "Hello world"; }
請(qǐng)求該消費(fèi)者服務(wù)接口:
調(diào)用了失敗回調(diào)方法!
七、規(guī)則持久化
(1)引入依賴
<!--sentinel持久化存儲(chǔ)--> <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-datasource-nacos</artifactId> </dependency>
(2)為nacos添加配置
更多見:
到此這篇關(guān)于SpringCloud 集成Sentinel的文章就介紹到這了,更多相關(guān)SpringCloud 集成Sentinel內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 在SpringBoot項(xiàng)目中使用Spring Cloud Sentinel實(shí)現(xiàn)流量控制
- Spring?Cloud?中使用?Sentinel?實(shí)現(xiàn)服務(wù)限流的兩種方式
- Spring?Cloud中Sentinel的兩種限流模式介紹
- springcloud3 Sentinel的搭建及案例操作方法
- Spring?Cloud微服務(wù)架構(gòu)Sentinel數(shù)據(jù)雙向同步
- Spring?Cloud?Alibaba微服務(wù)組件Sentinel實(shí)現(xiàn)熔斷限流
- Spring?Cloud?Gateway整合sentinel?實(shí)現(xiàn)流控熔斷的問題
- Java之SpringCloudAlibaba Sentinel組件案例講解
- Sentinel 斷路器在Spring Cloud使用詳解
相關(guān)文章
如何基于mybatis框架查詢數(shù)據(jù)庫表數(shù)據(jù)并打印
這篇文章主要介紹了如何基于mybatis框架查詢數(shù)據(jù)庫表數(shù)據(jù)并打印,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11springboot啟動(dòng)掃描不到dao層接口的解決方案
這篇文章主要介紹了springboot啟動(dòng)掃描不到dao層接口的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07SpringBoot入門實(shí)現(xiàn)第一個(gè)SpringBoot項(xiàng)目
今天我們一起來完成一個(gè)簡單的SpringBoot(Hello World)。就把他作為你的第一個(gè)SpringBoot項(xiàng)目。具有一定的參考價(jià)值,感興趣的可以了解一下2021-09-09Java多線程實(shí)現(xiàn)簡易微信發(fā)紅包的方法實(shí)例
這篇文章主要給大家介紹了關(guān)于Java多線程實(shí)現(xiàn)簡易微信發(fā)紅包的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01Java微服務(wù)分布式調(diào)度Elastic-job環(huán)境搭建及配置
Elastic-Job在配置中提供了JobEventConfiguration,支持?jǐn)?shù)據(jù)庫方式配置,會(huì)在數(shù)據(jù)庫中自動(dòng)創(chuàng)建JOB_EXECUTION_LOG和JOB_STATUS_TRACE_LOG兩張表以及若干索引,來記錄作業(yè)的相關(guān)信息2023-02-02一篇文章教你如何用Java自定義一個(gè)參數(shù)校驗(yàn)器
這篇文章主要介紹了使用java自定義一個(gè)參數(shù)校驗(yàn)器,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)2021-09-09解析SpringCloud簡介與微服務(wù)架構(gòu)
這篇文章主要介紹了SpringCloud簡介與微服務(wù)架構(gòu),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01解決springboot無法注入JpaRepository的問題
這篇文章主要介紹了解決springboot無法注入JpaRepository的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-01-01