sentinel实现对openfeign保护

  1. 引入依赖
    <dependency>
        <groupId>com.alibaba.cloudgroupId>
        <artifactId>spring-cloud-starter-alibaba-sentinelartifactId>
    dependency>
    
  2. yml添加配置
    feign:
      sentinel:
        enabled: true
    
  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() ;
    }
    
  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";
        }
    }
    

你可能感兴趣的:(sentinel,java)