springboot整合sentinel的方法教程
用到的依賴(lài)及配置
<!-- Sentinel核心服務(wù) -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-core</artifactId>
<version>1.8.6</version>
</dependency>
<!-- Sentinel核心服務(wù) -->
<!-- Sentinel本地應(yīng)用接入控制臺(tái) -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-transport-simple-http</artifactId>
<version>1.8.6</version>
</dependency>
<!-- Sentinel本地應(yīng)用接入控制臺(tái) -->
<!-- Sentinel提供注解無(wú)侵入定義資源 -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-annotation-aspectj</artifactId>
<version>1.8.6</version>
</dependency>
<!-- Sentinel提供注解無(wú)侵入定義資源 -->
配置
spring:
application:
name: sentinel
1、搭建項(xiàng)目
1.1、 Maven 依賴(lài)
<!-- 版本與控制臺(tái)保持一致即可 -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-core</artifactId>
<version>1.8.6</version>
</dependency>
<!-- 版本與控制臺(tái)保持一致即可 -->
1.2、Controller 層
@RestController
@RequestMapping("/user")
public class UserController {
@GetMapping("/add")
public String create(){
try {
// 設(shè)置一個(gè)資源名稱(chēng)為 Hello
Entry ignored = SphU.entry("AddUser");
System.out.println("新建一個(gè)用戶(hù)");
return "新建一個(gè)用戶(hù)";
} catch (BlockException e) {
System.out.println("系統(tǒng)繁忙,請(qǐng)稍后");
e.printStackTrace();
return "系統(tǒng)繁忙,請(qǐng)稍后";
}
}
/**
* 使用代碼編寫(xiě)流控規(guī)則,項(xiàng)目中不推薦使用,這是硬編碼方式
*
* 注解 @PostConstruct 的含義是:本類(lèi)構(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 會(huì)對(duì)哪個(gè)資源生效 */
rule.setResource("AddUser");
/* 定義限流的類(lèi)型(此處使用 QPS 作為限流類(lèi)型) */
rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
/* 定義 QPS 每秒通過(guò)的請(qǐng)求數(shù) */
rule.setCount(2);
/* 3.將限流規(guī)則存放到集合中 */
rules.add(rule);
/* 4.加載限流規(guī)則 */
FlowRuleManager.loadRules(rules);
}
@GetMapping("/edit")
public String edit(){
return "編輯一個(gè)用戶(hù)";
}
}
2、搭建 Sentinel 控制臺(tái)
下載 Sentinel 控制臺(tái) jar 包:https://github.com/alibaba/Sentinel/releases
啟動(dòng) Sentinel 控制臺(tái),如下圖所示
java -Dserver.port=9000 -jar sentinel-dashboard-1.8.6.jar

訪問(wèn) Sentinel 控制臺(tái):http://127.0.0.1:9000/

賬號(hào)/密碼:sentinel/sentinel
3、SpringBoot 整合 Sentinel
3.1、Maven 依賴(lài)
<!-- Sentinel本地應(yīng)用接入控制臺(tái),版本與控制臺(tái)保持一致即可 -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-transport-simple-http</artifactId>
<version>1.8.6</version>
</dependency>
<!-- Sentinel本地應(yīng)用接入控制臺(tái),版本與控制臺(tái)保持一致即可 -->
3.2、在 idea 中設(shè)置本地應(yīng)用的 JVM 啟動(dòng)參數(shù)
-Dcsp.sentinel.dashboard.server=127.0.0.1:9000 Sentinel控制臺(tái)的地址和端口號(hào) -Dproject.name=sentinel 本地應(yīng)用在控制臺(tái)中的名稱(chēng)

3.3. 運(yùn)行測(cè)試
第一次查看控制臺(tái),需要先訪問(wèn)一次被限流控制的接口,否則控制臺(tái)中沒(méi)有東西

快速在頁(yè)面刷新,就會(huì)出現(xiàn)限流后的返回提示語(yǔ)

3.4. 采用控制臺(tái)設(shè)置流控規(guī)則
3.4.1. 修改上述 UserController 類(lèi)
刪除使用代碼編寫(xiě)的流控規(guī)則,項(xiàng)目中不推薦使用,這是硬編碼方式
@RestController
@RequestMapping("/user")
public class UserController {
@GetMapping("/add")
public String create(){
try {
// 設(shè)置一個(gè)資源名稱(chēng)為 Hello
Entry ignored = SphU.entry("AddUser");
System.out.println("新建一個(gè)用戶(hù)");
return "新建一個(gè)用戶(hù)";
} catch (BlockException e) {
System.out.println("系統(tǒng)繁忙,請(qǐng)稍后");
e.printStackTrace();
return "系統(tǒng)繁忙,請(qǐng)稍后";
}
}
@GetMapping("/edit")
public String edit(){
return "編輯一個(gè)用戶(hù)";
}
}
3.4.2. 啟動(dòng)上述項(xiàng)目,如下操作

3.4.3、測(cè)試
- 正常請(qǐng)求

- 快速刷新頁(yè)面

4、注解方式無(wú)侵入定義資源(推薦使用)
Sentinel 支持通過(guò) @SentinelResource 注解來(lái)定義資源,并配置 blockHandler 函數(shù)來(lái)進(jìn)行限流之后的處理
4.1、Maven 依賴(lài)
<dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-annotation-aspectj</artifactId> <version>1.8.0</version> </dependency>
4.2、AspectJ 的配置類(lèi)
@Configuration
public class SentinelAspectConfiguration {
@Bean
public SentinelResourceAspect sentinelResourceAspect(){
return new SentinelResourceAspect();
}
}
4.3、Controller 層
@RestController
@RequestMapping("/user")
public class UserController {
// value:資源名稱(chēng) blockHandler:設(shè)置限流或降級(jí)處理的類(lèi)
@SentinelResource(value = "AddUser", blockHandler = "exceptionHandler")
@GetMapping("/add")
public String create(){
return "新增一個(gè)用戶(hù)";
}
// 限流處理類(lèi)
public String exceptionHandler(@NotNull BlockException e) {
e.printStackTrace();
return "系統(tǒng)繁忙,請(qǐng)稍后再試";
}
}
5、SpringBoot 整合 Sentinel 實(shí)現(xiàn)限流熔斷的其他方式
實(shí)現(xiàn)方式有以下幾種
- 拋出異常的方式
- 返回布爾值的方式
- 異步調(diào)用支持
- 注解方式(推薦,見(jiàn)目錄4)
5.1、拋出異常的方式定義資源示例
使用這種方式當(dāng)資源發(fā)生限流后會(huì)拋出 BlockException 異常。這個(gè)時(shí)候可以捕獲異常,進(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)繁忙,請(qǐng)稍后");
e.printStackTrace();
return "系統(tǒng)繁忙,請(qǐng)稍后";
}
}
5.2、返回布爾值的方式定義資源示例
使用的 API 為 SphO,限流后返回的值為 boolean 類(lèi)型。注意:SphO.entry 必須和 SphO.exit 成對(duì)出現(xiàn) 否則會(huì)報(bào)錯(cuò)
@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)繁忙,請(qǐng)稍后再試");
return false;
}
}
5.3、異步調(diào)用支持示例
在啟動(dòng)類(lèi)中添加 @EnableAsync,表示 SpringBoot 項(xiàng)目開(kāi)啟異步調(diào)用支持
@SpringBootApplication
@EnableAsync
public class SentinelQuickStartApplication {
public static void main(String[] args) {
SpringApplication.run(SentinelQuickStartApplication.class, args);
}
}
5.3.1、創(chuàng)建 AsyncService 編寫(xiě)異步調(diào)用的方法
@Service
public class AsyncService {
// Async表示方法為異步調(diào)用
@Async
public void hello(){
System.out.println("異步調(diào)用開(kāi)始======");
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("異步測(cè)試");
} catch (BlockException e) {
e.printStackTrace();
System.out.println("系統(tǒng)繁忙請(qǐng)稍后再試");
} finally {
if (asyncEntry != null) {
asyncEntry.exit(); // 限流出口
}
}
}總結(jié)
到此這篇關(guān)于springboot整合sentinel的文章就介紹到這了,更多相關(guān)springboot整合sentinel內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Sentinel原理與SpringBoot整合實(shí)戰(zhàn)案例講解
- Springboot?中使用Sentinel的詳細(xì)步驟
- springboot整合sentinel接口熔斷的實(shí)現(xiàn)示例
- 在SpringBoot項(xiàng)目中使用Spring Cloud Sentinel實(shí)現(xiàn)流量控制
- springboot?整合sentinel的示例代碼
- 詳解Springboot集成sentinel實(shí)現(xiàn)接口限流入門(mén)
- SpringBoot2.0+阿里巴巴Sentinel動(dòng)態(tài)限流實(shí)戰(zhàn)(附源碼)
- SpringBoot基于Sentinel在服務(wù)上實(shí)現(xiàn)接口限流
- 詳解SpringBoot Redis自適應(yīng)配置(Cluster Standalone Sentinel)
- SpringBoot整合Sentinel啟動(dòng)失敗及運(yùn)行時(shí)常見(jiàn)錯(cuò)誤總結(jié)
相關(guān)文章
SpringBoot項(xiàng)目修改訪問(wèn)端口和訪問(wèn)路徑的方法
這篇文章主要介紹了SpringBoot項(xiàng)目修改訪問(wèn)端口和訪問(wèn)路徑的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12
java中TCP實(shí)現(xiàn)回顯服務(wù)器及客戶(hù)端
本文主要介紹了java中TCP實(shí)現(xiàn)回顯服務(wù)器及客戶(hù)端,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
mybatis-plus批處理IService的實(shí)現(xiàn)示例
這篇文章主要介紹了mybatis-plus批處理IService的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
Java如何實(shí)現(xiàn)將類(lèi)文件打包為jar包
這篇文章主要介紹了Java如何實(shí)現(xiàn)將類(lèi)文件打包為jar包,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06

