请求第三方接口或者慢接口需要增加熔断处理,避免因为慢接口qps过大导致应用大量工作线程陷入阻塞以至于其他正常接口都不可用,最近项目测试环境就因为一个查询的慢接口调用次数过多,导致前端整个首页都无法加载。
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.6.3version>
parent>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-sentinelartifactId>
<version>2021.0.1.0version>
dependency>
@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秒熔断失效后可再次成功访问
@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);
}
}
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();
}
}