sentinel降级熔断

sentinel 页面配置结合Java代码

关于微服务架构的降级熔断有疑问的请移步我的其他博客
服务流程大意:

  1. 客户端(消费者)通过ribbon负载均衡远程调用服务端(生产者),在客户端配置了sentinel内容,达到微服务熔断降级
  2. 客户端(消费者)通过open feign负载均衡远程调用服务端(生产者),在客户端配置了sentinel内容,达到微服务熔断降级
    两种类型: 都是sentinel页面配置的降级,Java业务程序异常降级,两个维度分析

客户端代码

maven

 <!--nacos-->
 <dependency>
      <groupId>com.alibaba.cloud</groupId>
      <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  </dependency>
  <dependency>
      <groupId>com.alibaba.cloud</groupId>
      <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
  </dependency>
  <dependency>
      <groupId>com.alibaba.cloud</groupId>
      <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
  </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>

yml

server:
  port: 83
spring:
  application:
    name: nacos-consumer-order
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 #nacos地址,将服务注册到nacos中
    sentinel:
      transport:
        dashboard: localhost:8080
        port: 8719
management:
  endpoints:
    web:
      exposure:
        include: '*'
service-url:
  nacos-user-service: http://nacos-provider-payment #生产者服务名,用于远程调用

主启动类

/**
 * @author 小鱼
 * @version 1.0
 * @date 2021/8/4 5:16 下午
 * @EnableDiscoveryClient:开启服务注册
 */
@SpringBootApplication
@EnableDiscoveryClient
public class ConsumerMain83 {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerMain83.class);
    }
}

资源请求类
里面的请求都是去远程调用了服务端(生产者)

/**
 * @author 小鱼
 * @version 1.0
 * @date 2021/8/4 5:20 下午
 */
@RestController
@Slf4j
public class OrderController {

    @Value("${service-url.nacos-user-service}")
    private String url;

    @Resource
    private RestTemplate restTemplate;

    @GetMapping("/getPort")
    public String getPort(){
        return restTemplate.getForObject(url+"/getPort",String.class);
    }

    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult getPaymentById(@PathVariable("id") Long id) {
        return restTemplate.getForObject(url+"/payment/get/"+id,CommonResult.class);
    }

    /**
     * 测试sentinel页面配置的规则
     *  @SentinelResource:
     *      blockHandler:contentTestBlockHandler方法用于 超过规则时候调用
     * @return
     */
    @GetMapping("/consumer/payment/testBlockHandler")
    @SentinelResource(value = "testBlockHandler",blockHandler = "contentTestBlockHandler")
    public CommonResult testBlockHandler() {
        return restTemplate.getForObject(url+"/payment/testBlockHandler",CommonResult.class);
    }
    public CommonResult contentTestBlockHandler(BlockException blockException) {
        return new CommonResult(444, "我是sentinel的控制台违规处理:"+blockException.getMessage() , null);
    }

    /**
     * @SentinelResource:
     *      fallback:java业务代码出现异常的时候出现调用的降级方法
     * @return
     */
    @GetMapping("/consumer/payment/testFallback")
    @SentinelResource(value = "testFallback",fallback = "contentTestFallback")
    public CommonResult testFallback() {
        return restTemplate.getForObject(url+"/payment/testFallback",CommonResult.class);
    }
    public CommonResult contentTestFallback(Throwable throwable) {
        return new CommonResult(444, "我是业务异常处理降级:"+throwable.getMessage() , null);
    }

    @GetMapping("/consumer/payment/testAll")
    @SentinelResource(value = "testAll",fallback = "contentTestFallback",blockHandler = "contentTestBlockHandler" )
    public CommonResult testAll() {
        return restTemplate.getForObject(url+"/payment/testFallback",CommonResult.class);
    }
}

生产者代码

@GetMapping("/payment/testBlockHandler")
public CommonResult testBlockHandler() {
     return new CommonResult(444, "我是正常测试testBlockHandler:" +  "端口:" + port, null);
 }

@GetMapping("/payment/testFallback")
public CommonResult testFallback() {
    int num=10/0;
    return new CommonResult(444, "我是正常测试testFallback:" +  "端口:" + port, null);
}

验证

1.浏览器访问:http://localhost:83/consumer/payment/testBlockHandler。sentinel了流控规则:QPS的阈值为1。浏览器刷新很快就会出现下图,调用了contentTestBlockHandler方法
在这里插入图片描述
2.浏览器访问:http://localhost:83/consumer/payment/testFallback。Java运行时异常,业务逻辑写错了,调用了contentTestFallback方法
在这里插入图片描述
2.浏览器访问:http://localhost:83/consumer/payment/testAll。这个配置了fallback和blockHandler的时候会出现两种情况。如果不符合blockHandler的时候出现了fallback的情况会去fallback里面的方法。但是如果符合blockHandler的规则,那么会运行
blockHandler的方法。效果如下图
在这里插入图片描述
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值