SpringCloud学习-part62 sentinel服务熔断与OpenFeign

目标

使用OpenFeign结合与精简restTemplate与负载均衡步骤,降低代码的耦合度。

文件结构

在这里插入图片描述

pom

注意引入openfeign的启动依赖

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

完整pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>cloud2020</artifactId>
        <groupId>com.ezerbel.cloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloudalibaba-consumer-nacos-order84</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

        <dependency>
            <groupId>com.ezerbel.cloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--监控-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!--热部署-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>


</project>

yml

开启OpenFeign对sentinel的支持

feign:
  sentinel:
    enabled: true

完整yml

server:
  port: 84

spring:
  application:
    name: nacos-order-consumer
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
    sentinel:
      transport:
        #配置Sentinel dashboard地址
        dashboard: localhost:8080
        #默认8719端口,假如被占用会自动从8719开始依次+1扫描,直至找到未被占用的端口
        port: 8719
      filter:
        enabled: false
#消费者将要去访问的微服务名称(成功注册入nacos的微服务提供者),用于resttemplate调用前的属性注入
service-url:
  nacos-user-service: http://nacos-payment-provider
management:
  endpoints:
    web:
      exposure:
        include: '*'
# 激活Sentinel对Feign的支持
feign:
  sentinel:
    enabled: true

PaymentService接口

使用服务注册中心中的nacos-payment-provider服务,并能够调用其paymentSQL方法。

@FeignClient(value = "nacos-payment-provider",fallback = PaymentFallbackService.class)
public interface PaymentService
{
    @GetMapping(value = "/paymentSQL/{id}")
    public CommonResult<Payment> paymentSQL(@PathVariable("id") Long id);
}

PaymentService

@FeignClient(value = "nacos-payment-provider",fallback = PaymentFallbackService.class)
public interface PaymentService
{
    @GetMapping(value = "/paymentSQL/{id}")
    public CommonResult<Payment> paymentSQL(@PathVariable("id") Long id);
}

限流异常处理方法

public class BlockHandlers {

    public static CommonResult<Payment> paymentSQLBlockHandler(@PathVariable Long id, BlockException exception){
        Payment payment = new Payment(id,null);
        String msg = exception.getClass().getCanonicalName() + " ,请求被block,服务暂时不可用! request id = " + id;
        return  new CommonResult<>(432,msg,payment) ;
    }
}

内部运行时异常处理

public class FallBacks {
    public static CommonResult<Payment> paymentSQLFallBack(@PathVariable Long id,Throwable e){
        Payment payment = new Payment(id,null);
        String msg = e.getClass().getCanonicalName() + " ,请求出现异常,现在回滚! request id = " + id;
        return  new CommonResult<>(434,msg,payment) ;
    }
}

熔断处理方法

当目标服务不可用或出现异常时,调用此类中的同名方法

@Component
public class PaymentFallbackService implements PaymentService
{
    @Override
    public CommonResult<Payment> paymentSQL(Long id)
    {
        return new CommonResult<>(44444,"服务降级返回,---PaymentFallbackService",new Payment(id,"errorSerial"));
    }
}

Controller中添加请求处理

    //==================OpenFeign
    @Resource
    private PaymentService paymentService;

    @GetMapping(value = "/consumer/paymentSQL/{id}")
    @SentinelResource(value="paymentSQL",blockHandlerClass = BlockHandlers.class,blockHandler = "paymentSQLBlockHandler",fallbackClass = FallBacks.class,fallback = "paymentSQLFallBack")
    public CommonResult<Payment> paymentSQL(@PathVariable Long id)
    {
        return paymentService.paymentSQL(id);
    }

主函数入口开启Feign客户端

@EnableFeignClients 

在这里插入图片描述

限流测试

触发限流后,blockException并没有被友好处理,而是直接抛出了Error页面!
找了好久原因,竟然是因为传入的参数 id 的类型不一致:
public String xxxBlockHandler(@PathVariable long id, BlockException exception){…}
实际上应该传入Long,而不是long。
在给定blockHandler之后,如果没有找到合适的方法供调用,并没有友好提示,有点坑的。
在这里插入图片描述
再次测试成功限流

在这里插入图片描述

熔断测试

停掉其它服务
在这里插入图片描述
访问后给出熔断降级提示
在这里插入图片描述

运行时异常测试

添加制造异常的语句
在这里插入图片描述
成功处理运行时异常
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值