openfeign配置sentinel进行服务降级

2020-12-11 15:42:45 浏览数 (2)

1.pom

代码语言:javascript复制
<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>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

2.yml

代码语言:javascript复制
feign:
  sentinel:
    enabled: true

3.业务类

代码语言:javascript复制
@Component
@FeignClient(name = "nacos-payment-provider",fallback = PaymentServiceFallback.class)
public interface PaymentService {

    @GetMapping(value = "/payment/nacos/{id}")
    public String getPayment(@PathVariable("id") Long id);

}
代码语言:javascript复制
@Component
public class PaymentServiceFallback implements PaymentService {
    @Override
    public String getPayment(Long id) {
        return "服务降级";
    }
}

0 人点赞