天天看點

Spring Cloud Alibaba Sentinel整合Feign

服務提供方

建立一個sentinel-feign-provider服務并注冊到Nacos

@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class FeignProviderApplication {

    public static void main(String[] args) {
        SpringApplication.run(FeignProviderApplication.class, args);
    }

    @GetMapping("hello")
    public String hello() {
        return "hello";
    }

}
           

服務消費方

1. 引入依賴

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- feigns -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!-- sentinel -->
        <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-nacos-discovery</artifactId>
        </dependency>
    </dependencies>
           

2. application.yml

server:
  port: 8060

spring:
  application:
    name: sentinel-feign-consumer
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848

feign:
  sentinel:
    # 開啟feign sentinel熔斷降級支援
    enabled: true
           

3. Feign

使用方法和hystrix熔斷差不多,需要指定fallback屬性,fallback屬性的類必須實作feign接口

  • 接口
@FeignClient(value = "sentinel-feign-provider", fallback = TestFeignImpl.class)
public interface TestFeign {

    @GetMapping("hello")
    String hello();
}
           
  • 實作類
@Component
public class TestFeignImpl implements TestFeign{
    @Override
    public String hello() {
        return "feign fallback";
    }
}
           

4. controller

@RestController
public class TestController {

    @Autowired
    private TestFeign testFeign;

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("hello-feign")
    public String helloFeign(){
        return testFeign.hello();
    }

    // fallback屬性指定降級的方法,預設為目前類的方法
    @SentinelResource(value = "hello-rest", fallback = "helloRestFallback")
    @GetMapping("hello-rest")
    public String helloRest(){
        return restTemplate.getForObject("http://sentinel-feign-provider/hello", String.class);
    }

    public String helloRestFallback(){
        return "rest fallback";
    }
}
           

5. 啟動類

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients //開啟feign
public class FeignConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(FeignConsumerApplication.class, args);
    }

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}
           

測試

啟動服務提供方和消費方

curl http://localhost:8060/hello-rest

hello

curl http://localhost:8060/hello-feign

hello

關閉服務提供方

再次

curl http://localhost:8060/hello-rest

rest fallback

再次

curl http://localhost:8060/hello-feign

feign fallback

如果feign接口指定了fallback,并且@SentinelResource注解中也指定了fallback,優先使用feign接口中指定的

示例代碼

作者部落格

繼續閱讀