前面分别讲过了 “Spring Cloud Feign 声明式REST客户端”“Spring Cloud Hystrix 断路器”,其实在Feign中已经集成了对Hystrix的配置。

基于之前的Feign工程,做如下改造:

  1. application.yml添加配置:
feign:
  hystrix:
    enabled: true
  1. 修改Feign client
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@FeignClient(value = "${provider.hostname}", fallback = HelloClient.HelloClientFallback.class)
public interface HelloClient {

    @GetMapping("hello/{name}")
    String hello(@PathVariable(value = "name") String name);

    @Component
    class HelloClientFallback implements HelloClient {
        @Override
        public String hello(String name) {
            return "Fallback: " + name;
        }
    }

}

转载请注明出处:http://www.iyujian.me/java/spring-cloud-feign-hystrix.html