springboot整合sentinel接口熔斷的實現(xiàn)示例
背景
請求第三方接口或者慢接口需要增加熔斷處理,避免因為慢接口qps過大導致應用大量工作線程陷入阻塞以至于其他正常接口都不可用,最近項目測試環(huán)境就因為一個查詢的慢接口調(diào)用次數(shù)過多,導致前端整個首頁都無法加載。
依賴下載
springboot
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.3</version> </parent> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
sentinel dashboard
下載地址:
https://github.com/alibaba/Sentinel/releases
版本:
sentinel-dashboard-1.8.3.jar
啟動命令:
java -Dserver.port=9000 -Dcsp.sentinel.dashboard.server=localhost:9000 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.8.3.jar
sentinel springboot 依賴
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> <version>2021.0.1.0</version> </dependency>
熔斷嘗試
使用SentinelResource注解
編寫慢接口
@RestController
@RequestMapping(path = "/user")
@RequiredArgsConstructor
@Slf4j
public class UserCtrl {
private final IUserService userService;
@GetMapping("/{id}")
@SneakyThrows
@SentinelResource(value = "findById", fallback = "findByIdExt")
public User findById(@PathVariable("id") Long id) {
TimeUnit.SECONDS.sleep(3);
return userService.findById(id);
}
public User findByIdExt(Long id) {
log.error("觸發(fā)熔斷");
throw new IllegalStateException(String.format("id[{}]觸發(fā)熔斷", id));
}
}
應用注冊到sentinel dashboard
添加jvm啟動參數(shù):-Dcsp.sentinel.dashboard.server=${sentinel-dashboard域名}:9000
指定客戶端監(jiān)控 API 的端口(默認是 8719)-Dcsp.sentinel.api.port=8720

啟動應用,進行一次接口調(diào)用
Sentinel 會在客戶端首次調(diào)用的時候進行初始化,開始向控制臺發(fā)送心跳包。

配置熔斷規(guī)則


效果
快速調(diào)用3次慢接口,可以看到觸發(fā)熔斷


10秒熔斷失效后可再次成功訪問
不使用SentinelResource注解
慢接口代碼
@RestController
@RequestMapping(path = "/user")
@RequiredArgsConstructor
@Slf4j
public class UserCtrl {
private final IUserService userService;
@GetMapping("/{id}")
@SneakyThrows
public User findById(@PathVariable("id") Long id) {
TimeUnit.SECONDS.sleep(3);
return userService.findById(id);
}
}
配置熔斷規(guī)則


效果
快速訪問多次慢接口

對熔斷統(tǒng)一添加異常處理
import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.fastjson.JSON;
import com.test.test.model.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @description sentinel 降級處理
* @date 2024/6/14
*/
@Slf4j
public class WebBlockExceptionHandler implements BlockExceptionHandler {
@Override
public void handle(HttpServletRequest httpServletRequest, HttpServletResponse response, BlockException e) throws Exception {
log.error(String.format("sentinel 降級 資源名稱%s", e.getRule().getResource()), e);
response.setContentType("application/json");
response.setStatus(HttpStatus.TOO_MANY_REQUESTS.value());
response.getWriter().print(JSON.toJSON(R.err(e.getMessage())));
}
}
import com.alibaba.cloud.sentinel.feign.SentinelFeignAutoConfiguration;
import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
import com.test.test.hanlder.WebBlockExceptionHandler;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @description
* @date 2024/6/14
*/
@Configuration(proxyBeanMethods = false)
@AutoConfigureBefore(SentinelFeignAutoConfiguration.class)
public class SentinelAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public BlockExceptionHandler blockExceptionHandler() {
return new WebBlockExceptionHandler();
}
}
統(tǒng)一降級異常處理效果


到此這篇關于springboot整合sentinel接口熔斷的實現(xiàn)示例的文章就介紹到這了,更多相關springboot sentinel接口熔斷內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- Sentinel原理與SpringBoot整合實戰(zhàn)案例講解
- Springboot?中使用Sentinel的詳細步驟
- 在SpringBoot項目中使用Spring Cloud Sentinel實現(xiàn)流量控制
- springboot?整合sentinel的示例代碼
- 詳解Springboot集成sentinel實現(xiàn)接口限流入門
- SpringBoot2.0+阿里巴巴Sentinel動態(tài)限流實戰(zhàn)(附源碼)
- springboot整合sentinel的方法教程
- SpringBoot基于Sentinel在服務上實現(xiàn)接口限流
- 詳解SpringBoot Redis自適應配置(Cluster Standalone Sentinel)
- SpringBoot整合Sentinel啟動失敗及運行時常見錯誤總結
相關文章
springboot+vue實現(xiàn)oss文件存儲的示例代碼
對象存儲服務是一種海量、安全、低成本、高可靠的云存儲服務,本文主要介紹了springboot+vue實現(xiàn)oss文件存儲的示例代碼,具有一定的參考價值,感興趣的可以了解一下2024-02-02
java EasyExcel面向Excel文檔讀寫邏輯示例詳解
這篇文章主要為大家介紹了java EasyExcel面向Excel文檔讀寫邏輯示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-07-07
Java基于解釋器模式實現(xiàn)定義一種簡單的語言功能示例
這篇文章主要介紹了Java基于解釋器模式實現(xiàn)定義一種簡單的語言功能,簡單描述了解釋器模式的概念、功能及Java使用解釋器模式定義一種簡單語言的相關實現(xiàn)與使用技巧,需要的朋友可以參考下2018-05-05
SpringBoot使用jasypt加解密密碼的實現(xiàn)方法(二)
這篇文章主要介紹了SpringBoot使用jasypt加解密密碼的實現(xiàn)方法(二),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10
SpringMVC結合ajaxfileupload.js實現(xiàn)文件無刷新上傳
這篇文章主要介紹了SpringMVC結合ajaxfileupload.js實現(xiàn)文件無刷新上傳,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10

