项目版本
Spring Boot版本:2.2.9.RELEASE
Spring Cloud版本:Hoxton.SR7
Spring Cloud Alibaba版本:2.2.1.RELEASE
官方doc
介绍
本文分为以下几部分:
- 普通服务集成
- 控制台整合Nacos 自己Github重新封装地址
- 网关集成(spring cloud gateway)
- 控制台启动、参数和指定Nacos地址+namespace等
Nacos持久化最终效果图如下:

普通服务

注:Sentinel兼容Hystrix的FallbackFactory
从 Hystrix 迁移到 Sentinel方案(官方)
- https://github.com/alibaba/Sentinel/wiki/Guideline:-%E4%BB%8E-Hystrix-%E8%BF%81%E7%A7%BB%E5%88%B0-Sentinel
Sentinel 与 Hystrix 的对比(官方)
- https://github.com/alibaba/Sentinel/wiki/Sentinel-%E4%B8%8E-Hystrix-%E7%9A%84%E5%AF%B9%E6%AF%94
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class NacosProviderClientFallback implements FallbackFactory<NacosProviderClient> {
@Override
public NacosProviderClient create(Throwable throwable) {
return new NacosProviderClient() {
@Override
public String abc(String name, int age) {
log.error("用 nacos-provider abc 方法失败, 异常信息={}", throwable.getMessage());
//throw new RuntimeException("abc:" + throwable.getMessage());
//这里直接返回BusinessException(),因为
throw new RuntimeException("aaaaaaadssssssssssss");
// return throwable.getClass().getName() + " : " + throwable.getMessage();
}
@Override
public String getEcho(String string) {
log.error("用 nacos-provider getEcho 方法失败", throwable);
throw new RuntimeException("getEcho:" + throwable.getMessage());
}
};
}
}
基础包
基础包大概截图:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4jyKDPvL-1603193351351)(https://upload-images.jianshu.io/upload_images/23703439-9c0ba26f2dac32e3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)]
引入以下两个包,一个是sentinel核心组件,一个是整合nacos实现持久化功能组件(无关顺序)
<!-- alibaba Sentinel组件 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!-- alibaba Sentinel整合nacos组件 -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
添加自定义异常处理(不加会返回默认的错误信息Blocked by Sentinel: XXXX)
注: 网上方案实现 UrlBlockHandler 接口的版本已经比较老了,新版本已经没有UrlBlockHandler 接口
/**
* 没有配资源,默认Block异常处理(只能是Block异常)
*/
@Slf4j
@Component
public class CustomBlockExceptionHandler implements BlockExceptionHandler {
@Autowired
private ObjectMapper objectMapper;
@Override
public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, BlockException e) throws Exception {
httpServletResponse.setStatus(HttpStatus.SERVICE_UNAVAILABLE.value());
if (e instanceof FlowException) {
log.error("FlowException 普通服务限流,资源信息:" + JSON.toJSONString(e.getRule()));
httpServletResponse.setStatus(org.springframework.http.HttpStatus.TOO_MANY_REQUESTS.value());
ResponseUtil.responseFailed(objectMapper, httpServletResponse, 1000, "API interface limited flow.");
} else if (e instanceof DegradeException) {
log.error("DegradeException 普通服务降级,资源信息:" + JSON.toJSONString(e.getRule()));
ResponseUtil.responseFailed(objectMapper, httpServletResponse, 1001, "API interface has been degraded.");
} else if (e instanceof ParamFlowException) {
ParamFlowException ex = (ParamFlowException) e;
log.error("ParamFlowException 参数热点限流,资源名={},参数={},资源信息={}", ex.getResourceName(), ex.getLimitParam(), JSON.toJSONString(ex.getRule()));
ResponseUtil.responseFailed(objectMapper, httpServletResponse, 1002, "API interface limited flow by params.");
} else if (e instanceof AuthorityException) {
log.error("AuthorityException 授权规则,资源信息:" + JSON.toJSONString(e.getRule()));
ResponseUtil.responseFailed(objectMapper, httpServletResponse, 1003, "API interface limited by autho

最低0.47元/天 解锁文章
3322

被折叠的 条评论
为什么被折叠?



