springboot整合sentinel接口熔斷的實現(xiàn)示例
背景
請求第三方接口或者慢接口需要增加熔斷處理,避免因為慢接口qps過大導(dǎo)致應(yīng)用大量工作線程陷入阻塞以至于其他正常接口都不可用,最近項目測試環(huán)境就因為一個查詢的慢接口調(diào)用次數(shù)過多,導(dǎo)致前端整個首頁都無法加載。
依賴下載
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)); } }
應(yīng)用注冊到sentinel dashboard
添加jvm啟動參數(shù):-Dcsp.sentinel.dashboard.server=${sentinel-dashboard域名}:9000
指定客戶端監(jiān)控 API 的端口(默認是 8719)-Dcsp.sentinel.api.port=8720
啟動應(yīng)用,進行一次接口調(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)一降級異常處理效果
到此這篇關(guān)于springboot整合sentinel接口熔斷的實現(xiàn)示例的文章就介紹到這了,更多相關(guān)springboot sentinel接口熔斷內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 在SpringBoot項目中使用Spring Cloud Sentinel實現(xiàn)流量控制
- springboot?整合sentinel的示例代碼
- 詳解Springboot集成sentinel實現(xiàn)接口限流入門
- SpringBoot2.0+阿里巴巴Sentinel動態(tài)限流實戰(zhàn)(附源碼)
- springboot整合sentinel的方法教程
- SpringBoot基于Sentinel在服務(wù)上實現(xiàn)接口限流
- 詳解SpringBoot Redis自適應(yīng)配置(Cluster Standalone Sentinel)
- Springboot?中使用Sentinel的詳細步驟
相關(guān)文章
BeanDefinitionRegistryPostProcessor如何動態(tài)注冊Bean到Spring
這篇文章主要介紹了BeanDefinitionRegistryPostProcessor如何動態(tài)注冊Bean到Spring,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03java web FTPClient實現(xiàn)上傳文件到指定服務(wù)器
這篇文章主要為大家詳細介紹了java web FTPClient實現(xiàn)上傳文件到指定服務(wù)器,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-06-06詳解JDK中ExecutorService與Callable和Future對線程的支持
這篇文章主要介紹了詳解JDK中ExecutorService與Callable和Future對線程的支持的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-09-09java byte數(shù)組與int,long,short,byte的轉(zhuǎn)換實現(xiàn)方法
下面小編就為大家?guī)硪黄猨ava byte數(shù)組與int,long,short,byte的轉(zhuǎn)換實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-10-10Java二叉搜索樹遍歷操作詳解【前序、中序、后序、層次、廣度優(yōu)先遍歷】
這篇文章主要介紹了Java二叉搜索樹遍歷操作,結(jié)合實例形式詳細分析了Java二叉搜索樹前序、中序、后序、層次、廣度優(yōu)先遍歷等相關(guān)原理與操作技巧,需要的朋友可以參考下2020-03-03基于java springboot + mybatis實現(xiàn)電影售票管理系統(tǒng)
這篇文章主要介紹了基于java springboot + mybatis實現(xiàn)的完整電影售票管理系統(tǒng)基于java springboot + mybatis,本文給大家介紹的非常詳細,對大家的學(xué)習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-08-08