springboot整合sentinel和对feign熔断降级

一、准备

  • docker安装好sentinel-dashboard(sentinel控制台),参考docker安装好各个组件的命令
  • 启动sentinel-dashboard,我的虚拟机ip为192.168.200.131,sentinel-dashboard的端口为8858

二、整合sentinel的主要工作

  • 在需要监控的服务中引入spring-cloud-starter-alibaba-sentinel依赖
  • bootstrap.yml中设置好sentinel-dashboardip:port

在我的项目中,给order-serviceproduct-service都引入了sentinel

# pom.xml
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>

# bootstrap.yml
spring:
  cloud:
    sentinel:
      transport:
        dashboard: 192.168.200.131:8858

三、查看sentinel控制台

  • 启动两个服务
  • docker启动sentinel控制台
    • 浏览器输入:http://192.168.200.131:8858/
    • 输入默认账号密码都是sentinel

刚开始控制台左边只能看到sentinel-dashboard,只有当我们第一次访问过后,相应的服务和请求才会在左边(和簇点链路)中出现

在这里插入图片描述

四、控制台基础功能

4.1 流量控制

4.2 熔断降级

4.2.1 当未配置熔断降级功能时

product-service/product/getByObj限流为QPS=10,超过10则迅速失败

在这里插入图片描述使用jmeter进行压测,QPS为20,访问接口为http://127.0.0.1:8081/order/get2,发现20个请求里有10个请求访问失败,浏览器报500错误

在这里插入图片描述
此外,idea控制台报异常

feign.FeignException$TooManyRequests: [429] during [POST] to [http://product-service/product/getByObj] [IProductClient#getByObj(ProductDTO)]: [Blocked by Sentinel (flow limiting)]

sentinel控制台显示有10个请求被拒绝

在这里插入图片描述

4.2.1 给IProductClient配置熔断降级功能

IProductClient这个feign接口为例,当这个接口对应的http请求被限流阻断时,启用一个新的IProductClient对服务进行降级(实际项目中比如:"当前请求繁忙,请稍后访问"或者推荐别的页面)。
步骤如下:

  • sd-api模块的com.hdl.api.client.product包下建一个fallback包,用来存放feignclient的请求调用被熔断时降级处理的类
  • 在该包下建ProductClientFallback
  • IProductClient@FeignClient注解上配置fallbackFactory = ProductClientFallback.class 【1】
  • 需要将ProductClientFallback类放入ioc容器【2】,因此在com.hdl.api.config包下建一个FallbackConfig,用来生产ProductClientFallbackbean,同时将FallbackConfig放到·spring.factories·文件里
  • order-serviceproduct-service模块的bootstrap.yml中配置feign.sentinel.enabled=true【3】

结构如下
在这里插入图片描述
sd-api模块不需要引入sentinel依赖,ProductClientFallback类代码如下:

@Slf4j
public class ProductClientFallback implements FallbackFactory<IProductClient> {
    @Override
    public IProductClient create(Throwable cause) {
        log.error("查询product服务异常",cause);
        return new IProductClient() {
            @Override
            public ProductDTO getById(Integer id, Integer price) {
                ProductDTO productDTO = new ProductDTO();
                productDTO.setName("getById的默认product");
                return productDTO;
            }

            @Override
            public ProductDTO getByObj(ProductDTO productDTO) {
                ProductDTO productDTO1 = new ProductDTO();
                productDTO.setName("getByObj的默认product");
                return productDTO1;
            }

            @Override
            public ProductDTO getByParams(ProductDTO productDTO, Integer price) {
                ProductDTO productDTO2 = new ProductDTO();
                productDTO.setName("getByParams的默认product");
                return productDTO2;
            }
        };
    }
}

其它两个模块结构
在这里插入图片描述

测试
sentinel限流qps为10
在这里插入图片描述

使用jmeter进行压测,QPS为20,访问接口为http://127.0.0.1:8081/order/get2,发现20个请求里虽然超过了限流但,返回全都成功了,并且熔断的feign走的fallback返回的是默认的null
在这里插入图片描述

idea控制台打印出了fallback类的日志,并且有异常

在这里插入图片描述

feign.FeignException$TooManyRequests: [429] during [POST] to [http://product-service/product/getByObj] [IProductClient#getByObj(ProductDTO)]: [Blocked by Sentinel (flow limiting)]

sentinel控制台通过了12个(跟限流的10差不多),拒绝了8个
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值