Sentinel 整合 Spring Cloud + Gateway + 控制台Nacos持久化

项目版本

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持久化最终效果图如下:

image.png

普通服务

普通服务效果图

注: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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值