Spring Cloud Alibaba

【Spring Cloud Alibaba】Feign中使用Se

2019-11-04  本文已影响0人  扮鬼之梦

《Feign中使用Sentinel断路器》

1.添加依赖

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>

2.添加配置

feign:
  sentinel: 
    enabled: true

3.创建ProviderClientFallback类

import org.springframework.stereotype.Component;
import com.thy.feign.ProviderClient;

@Component
public class ProviderClientFallback implements ProviderClient{

    @Override
    public String findMember(String string) {
        return "访问失败";
    }

}

4.ProviderClient中的@FeignClient添加fallback属性ProviderClientFallback.class

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import com.thy.feign.fallback.ProviderClientFallback;

@FeignClient(name="service-provider",fallback = ProviderClientFallback.class)
public interface ProviderClient {
    @GetMapping("/echo/{string}")
    String findMember(@PathVariable String string);
}

5.测试

此时我们关闭服务提供者,请求 http://localhost:8080/echo/hi 浏览器会显示:

访问失败
上一篇下一篇

猜你喜欢

热点阅读