springboot整合sentinel接口熔断

背景

请求第三方接口或者慢接口需要增加熔断处理,避免因为慢接口qps过大导致应用大量工作线程陷入阻塞以至于其他正常接口都不可用,最近项目测试环境就因为一个查询的慢接口调用次数过多,导致前端整个首页都无法加载。

依赖下载

  • springboot
xml 复制代码
<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 依赖
xml 复制代码
<dependency>
	<groupId>com.alibaba.cloud</groupId>
	<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
	<version>2021.0.1.0</version>
</dependency>

熔断尝试

使用SentinelResource注解

  • 编写慢接口
java 复制代码
@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("触发熔断");
        throw new IllegalStateException(String.format("id[{}]触发熔断", id));
    }
}
  • 应用注册到sentinel dashboard

    添加jvm启动参数:-Dcsp.sentinel.dashboard.server=${sentinel-dashboard域名}:9000

    指定客户端监控 API 的端口(默认是 8719)-Dcsp.sentinel.api.port=8720

  • 启动应用,进行一次接口调用

    Sentinel 会在客户端首次调用的时候进行初始化,开始向控制台发送心跳包。

  • 配置熔断规则

  • 效果

    快速调用3次慢接口,可以看到触发熔断
    10秒熔断失效后可再次成功访问

不使用SentinelResource注解

  • 慢接口代码
java 复制代码
@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);
    }

}
  • 配置熔断规则

  • 效果
    快速访问多次慢接口
  • 对熔断统一添加异常处理
java 复制代码
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();
    }
}
  • 统一降级异常处理效果
相关推荐
37手游后端团队21 分钟前
8分钟带你看懂什么是MCP
人工智能·后端·面试
小华同学ai1 小时前
千万别错过!这个国产开源项目彻底改变了你的域名资产管理方式,收藏它相当于多一个安全专家!
前端·后端·github
Vowwwwwww1 小时前
GIT历史存在大文件的解决办法
前端·git·后端
捡田螺的小男孩1 小时前
京东一面:接口性能优化,有哪些经验和手段
java·后端·面试
艾露z2 小时前
深度解析Mysql中MVCC的工作机制
java·数据库·后端·mysql
神仙别闹2 小时前
基于Java(SpringBoot、Mybatis、SpringMvc)+MySQL实现(Web)小二结账系统
java·spring boot·mybatis
前端付豪2 小时前
揭秘网易统一日志采集与故障定位平台揭秘:如何在亿级请求中1分钟定位线上异常
前端·后端·架构
陈随易2 小时前
Lodash 杀手来了!es-toolkit v1.39.0 已完全兼容4年未更新的 Lodash
前端·后端·程序员
全职计算机毕业设计2 小时前
SpringBoot+Mysql实现的停车场收费小程序系统+文档
spring boot·mysql·小程序
未来影子2 小时前
SpringAI(GA):Nacos3下的分布式MCP
后端·架构·ai编程