springboot整合sentinel的方法教程
用到的依賴及配置
<!-- Sentinel核心服務(wù) --> <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-core</artifactId> <version>1.8.6</version> </dependency> <!-- Sentinel核心服務(wù) --> <!-- Sentinel本地應(yīng)用接入控制臺 --> <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-transport-simple-http</artifactId> <version>1.8.6</version> </dependency> <!-- Sentinel本地應(yīng)用接入控制臺 --> <!-- Sentinel提供注解無侵入定義資源 --> <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-annotation-aspectj</artifactId> <version>1.8.6</version> </dependency> <!-- Sentinel提供注解無侵入定義資源 -->
配置
spring: application: name: sentinel
1、搭建項(xiàng)目
1.1、 Maven 依賴
<!-- 版本與控制臺保持一致即可 --> <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-core</artifactId> <version>1.8.6</version> </dependency> <!-- 版本與控制臺保持一致即可 -->
1.2、Controller 層
@RestController @RequestMapping("/user") public class UserController { @GetMapping("/add") public String create(){ try { // 設(shè)置一個資源名稱為 Hello Entry ignored = SphU.entry("AddUser"); System.out.println("新建一個用戶"); return "新建一個用戶"; } catch (BlockException e) { System.out.println("系統(tǒng)繁忙,請稍后"); e.printStackTrace(); return "系統(tǒng)繁忙,請稍后"; } } /** * 使用代碼編寫流控規(guī)則,項(xiàng)目中不推薦使用,這是硬編碼方式 * * 注解 @PostConstruct 的含義是:本類構(gòu)造方法執(zhí)行結(jié)束后執(zhí)行 */ @PostConstruct public void initFlowRule() { /* 1.創(chuàng)建存放限流規(guī)則的集合 */ List<FlowRule> rules = new ArrayList<>(); /* 2.創(chuàng)建限流規(guī)則 */ FlowRule rule = new FlowRule(); /* 定義資源,表示 Sentinel 會對哪個資源生效 */ rule.setResource("AddUser"); /* 定義限流的類型(此處使用 QPS 作為限流類型) */ rule.setGrade(RuleConstant.FLOW_GRADE_QPS); /* 定義 QPS 每秒通過的請求數(shù) */ rule.setCount(2); /* 3.將限流規(guī)則存放到集合中 */ rules.add(rule); /* 4.加載限流規(guī)則 */ FlowRuleManager.loadRules(rules); } @GetMapping("/edit") public String edit(){ return "編輯一個用戶"; } }
2、搭建 Sentinel 控制臺
下載 Sentinel 控制臺 jar 包:https://github.com/alibaba/Sentinel/releases
啟動 Sentinel 控制臺,如下圖所示
java -Dserver.port=9000 -jar sentinel-dashboard-1.8.6.jar
訪問 Sentinel 控制臺:http://127.0.0.1:9000/
賬號/密碼:sentinel/sentinel
3、SpringBoot 整合 Sentinel
3.1、Maven 依賴
<!-- Sentinel本地應(yīng)用接入控制臺,版本與控制臺保持一致即可 --> <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-transport-simple-http</artifactId> <version>1.8.6</version> </dependency> <!-- Sentinel本地應(yīng)用接入控制臺,版本與控制臺保持一致即可 -->
3.2、在 idea 中設(shè)置本地應(yīng)用的 JVM 啟動參數(shù)
-Dcsp.sentinel.dashboard.server=127.0.0.1:9000 Sentinel控制臺的地址和端口號 -Dproject.name=sentinel 本地應(yīng)用在控制臺中的名稱
3.3. 運(yùn)行測試
第一次查看控制臺,需要先訪問一次被限流控制的接口,否則控制臺中沒有東西
快速在頁面刷新,就會出現(xiàn)限流后的返回提示語
3.4. 采用控制臺設(shè)置流控規(guī)則
3.4.1. 修改上述 UserController 類
刪除使用代碼編寫的流控規(guī)則,項(xiàng)目中不推薦使用
,這是硬編碼方式
@RestController @RequestMapping("/user") public class UserController { @GetMapping("/add") public String create(){ try { // 設(shè)置一個資源名稱為 Hello Entry ignored = SphU.entry("AddUser"); System.out.println("新建一個用戶"); return "新建一個用戶"; } catch (BlockException e) { System.out.println("系統(tǒng)繁忙,請稍后"); e.printStackTrace(); return "系統(tǒng)繁忙,請稍后"; } } @GetMapping("/edit") public String edit(){ return "編輯一個用戶"; } }
3.4.2. 啟動上述項(xiàng)目,如下操作
3.4.3、測試
- 正常請求
- 快速刷新頁面
4、注解方式無侵入定義資源(推薦使用)
Sentinel
支持通過 @SentinelResource
注解來定義資源,并配置 blockHandler
函數(shù)來進(jìn)行限流之后的處理
4.1、Maven 依賴
<dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-annotation-aspectj</artifactId> <version>1.8.0</version> </dependency>
4.2、AspectJ 的配置類
@Configuration public class SentinelAspectConfiguration { @Bean public SentinelResourceAspect sentinelResourceAspect(){ return new SentinelResourceAspect(); } }
4.3、Controller 層
@RestController @RequestMapping("/user") public class UserController { // value:資源名稱 blockHandler:設(shè)置限流或降級處理的類 @SentinelResource(value = "AddUser", blockHandler = "exceptionHandler") @GetMapping("/add") public String create(){ return "新增一個用戶"; } // 限流處理類 public String exceptionHandler(@NotNull BlockException e) { e.printStackTrace(); return "系統(tǒng)繁忙,請稍后再試"; } }
5、SpringBoot 整合 Sentinel 實(shí)現(xiàn)限流熔斷的其他方式
實(shí)現(xiàn)方式有以下幾種
- 拋出異常的方式
- 返回布爾值的方式
- 異步調(diào)用支持
- 注解方式(推薦,見目錄4)
5.1、拋出異常的方式定義資源示例
使用這種方式當(dāng)資源發(fā)生限流后會拋出 BlockException
異常。這個時候可以捕獲異常,進(jìn)行限流之后的邏輯處理,關(guān)鍵代碼如下
@RequestMapping(path = {"/hello"}, method = RequestMethod.GET) @ResponseBody public String hello() { try { Entry ignored = SphU.entry("Hello"); System.out.println("Hello Sentinel"); return "Hello Sentinel"; } catch (BlockException e) { System.out.println("系統(tǒng)繁忙,請稍后"); e.printStackTrace(); return "系統(tǒng)繁忙,請稍后"; } }
5.2、返回布爾值的方式定義資源示例
使用的 API
為 SphO
,限流后返回的值為 boolean
類型。注意:SphO.entry
必須和 SphO.exit
成對出現(xiàn) 否則會報錯
@GetMapping("/boolean") public boolean returnBoolean() { // 使用限流規(guī)則 if (SphO.entry("Sentinel-boolean")){ try { System.out.println("Hello Sentinel"); return true; }finally { // 限流的出口 SphO.exit(); } } else { // 限流后進(jìn)行的操作 System.out.println("系統(tǒng)繁忙,請稍后再試"); return false; } }
5.3、異步調(diào)用支持示例
在啟動類中添加 @EnableAsync
,表示 SpringBoot
項(xiàng)目開啟異步調(diào)用支持
@SpringBootApplication @EnableAsync public class SentinelQuickStartApplication { public static void main(String[] args) { SpringApplication.run(SentinelQuickStartApplication.class, args); } }
5.3.1、創(chuàng)建 AsyncService 編寫異步調(diào)用的方法
@Service public class AsyncService { // Async表示方法為異步調(diào)用 @Async public void hello(){ System.out.println("異步調(diào)用開始======"); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("異步調(diào)用結(jié)束====="); } }
5.3.2、Controller層
@Autowired private AsyncService asyncService; @GetMapping("/async") public void async() { // 1.進(jìn)行限流控制 AsyncEntry asyncEntry = null; try { asyncEntry = SphU.asyncEntry("Sentinel_Async"); // 限流入口 asyncService.hello(); // 異步調(diào)用方法 System.out.println("異步測試"); } catch (BlockException e) { e.printStackTrace(); System.out.println("系統(tǒng)繁忙請稍后再試"); } finally { if (asyncEntry != null) { asyncEntry.exit(); // 限流出口 } } }
總結(jié)
到此這篇關(guān)于springboot整合sentinel的文章就介紹到這了,更多相關(guān)springboot整合sentinel內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- springboot整合sentinel接口熔斷的實(shí)現(xiàn)示例
- 在SpringBoot項(xiàng)目中使用Spring Cloud Sentinel實(shí)現(xiàn)流量控制
- springboot?整合sentinel的示例代碼
- 詳解Springboot集成sentinel實(shí)現(xiàn)接口限流入門
- SpringBoot2.0+阿里巴巴Sentinel動態(tài)限流實(shí)戰(zhàn)(附源碼)
- SpringBoot基于Sentinel在服務(wù)上實(shí)現(xiàn)接口限流
- 詳解SpringBoot Redis自適應(yīng)配置(Cluster Standalone Sentinel)
- Springboot 中使用Sentinel的詳細(xì)步驟
相關(guān)文章
Java判斷是否為簡體中文字符的實(shí)現(xiàn)方法
在應(yīng)用開發(fā)中,判斷簡體中文字符是一個重要但常被忽視的任務(wù),簡體中文和繁體中文在字符上有顯著的區(qū)別,因此在某些場景下我們需要判斷輸入的文本是否為簡體中文,本文將介紹如何使用Java進(jìn)行此判斷,并提供相應(yīng)的代碼示例,幫助開發(fā)者更好地理解這一過程2024-09-09詳解基于java的Socket聊天程序——服務(wù)端(附demo)
這篇文章主要介紹了詳解基于java的Socket聊天程序——服務(wù)端(附demo),具有一定的參考價值,感興趣的小伙伴們可以參考一下。2016-12-12Java Apache Shiro安全框架快速開發(fā)詳解流程
Apache Shiro是一個強(qiáng)大且易用的Java安全框架,執(zhí)行身份驗(yàn)證、授權(quán)、密碼和會話管理。使用Shiro的易于理解的API,您可以快速、輕松地獲得任何應(yīng)用程序,從最小的移動應(yīng)用程序到最大的網(wǎng)絡(luò)和企業(yè)應(yīng)用程序2021-10-10javax.management.InvalidApplicationException的問題解決
javax.management.InvalidApplicationException是與Java Management Extensions (JMX) API相關(guān)的一個常見異常,本文主要介紹了javax.management.InvalidApplicationException的問題解決,感興趣的可以了解一下2024-08-08SpringBoot數(shù)據(jù)訪問的實(shí)現(xiàn)
本文主要介紹了SpringBoot數(shù)據(jù)訪問的實(shí)現(xiàn),引入各種xxxTemplate,xxxRepository來簡化我們對數(shù)據(jù)訪問層的操作,感興趣的可以了解一下2023-11-11解決myBatis中openSession()自動提交的問題
在學(xué)習(xí)MySQL過程中,發(fā)現(xiàn)插入操作自動提交,問題原因可能是myBatis中的openSession()方法設(shè)置了自動提交,或者是MySQL的默認(rèn)引擎設(shè)置為不支持事務(wù)的MyISAM,解決辦法包括更改myBatis的提交設(shè)置或?qū)ySQL表的引擎改為InnoDB2024-09-09