• Sentinel整合OpenFeign


    基本使用
    1. 引入依赖
      <dependency>
          <groupId>com.alibaba.cloudgroupId>
          <artifactId>spring-cloud-starter-alibaba-sentinelartifactId>
      dependency>
      
      • 1
      • 2
      • 3
      • 4
    2. 添加配置
      # 1. sentinel保护feign开关
      feign.sentinel.enabled=true
      # 2. sentinel dashboard 配置
      spring.cloud.sentinel.transport.dashboard=localhost:8858
      spring.cloud.sentinel.transport.port=8729
      # 3. 基础配置
      spring.application.name=hello-sentinel-feign
      spring.cloud.nacos.discovery.server-addr=localhost:8848
      spring.cloud.loadbalancer.nacos.enabled=true
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
    3. 编写feign接口并配置fallback属性
      @FeignClient(value = "nacos-client-app"
              , contextId = "nacosHelloClient"
              , fallback = NacosHelloClientFallback.class
      )
      public interface NacosHelloClient {
      
          @GetMapping("/hello/index")
          String hello() ;
      
          @GetMapping("/hello/exception")
          String exception() ;
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
    4. 编写fallback实现代码
      @Component
      public class NacosHelloClientFallback implements NacosHelloClient {
      
          @Override
          public String hello() {
              return "fallback hello ret value";
          }
          @Override
          public String exception() {
              return "fallback exception ret value";
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
    配置降级规则
    1. 添加配置
      # 配置降级规则
      spring.cloud.sentinel.datasource.ds1.file.dataType=json
      spring.cloud.sentinel.datasource.ds1.file.file=classpath:sentinel/feign-degrade-sentinel.json
      spring.cloud.sentinel.datasource.ds1.file.rule-type=degrade
      
      • 1
      • 2
      • 3
      • 4
    2. 编写sentinel/feign-degrade-sentinel.json降级规则 (慢调用降级规则)
      [{
          "resource": "GET:http://nacos-client-app/hello/index",
          "grade": 0,
          "count": 200,
          "timeWindow": 11,
          "minRequestAmount": 6,
          "statIntervalMs": 10000,
          "slowRatio":0.3
      }]
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
  • 相关阅读:
    抖音获得抖音商品详情 API 返回值说明
    基于采样的规划算法之RRT家族(三):RRT*
    xilinx的原语的使用
    QCefView入门示例程序简介
    c语言实现通讯录
    C++ 多态之虚函数表
    java开发之个人微信助手的开发
    Jmeter-非GUI模式下运行jmeter脚本-适用于服务器上持续集成测试
    在 Android 应用程序开发期间减少 Android 应用程序大小的 9 种方法
    每日三题 8.11
  • 原文地址:https://blog.csdn.net/yichengjie_c/article/details/132833352