欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot整合sentinel接口熔斷的實現(xiàn)示例

 更新時間:2024年09月12日 09:45:25   作者:morganEngineer  
為了防止慢接口導(dǎo)致的服務(wù)阻塞,可以通過添加熔斷處理來避免應(yīng)用的大量工作線程陷入阻塞,保證其他接口的正常運行,本文介紹了如何使用Spring Boot與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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論