spring cloud feign+hystrix

Posted zfzf1

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring cloud feign+hystrix相关的知识,希望对你有一定的参考价值。

server:
  port: 8081
spring:
  application:
    name: spring-hy-sale
feign:
  hystrix:
    enabled: true
hystrix:
  command:
    HelloClient#toHello():
      execution:
        isolation:
          thread: 
            timeoutInMilliseconds: 500
      circuitBreaker:
        requestVolumeThreshold: 3
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

 

@FeignClient(name = "spring-hy-member", fallback = HelloClientFallback.class)
public interface HelloClient {

	@RequestMapping(method = RequestMethod.GET, value = "/hello")
	public String hello();
	
	@RequestMapping(method = RequestMethod.GET, value = "/toHello")
	public String toHello();
}

  

@Component
public class HelloClientFallback implements HelloClient {

	public String hello() {
		return "fallback hello";
	}

	public String toHello() {
		return "fallback timeout hello";
	}

}

  

@RestController
public class FeignController {
	
	@Autowired
	private HelloClient helloClient;

	@RequestMapping(method = RequestMethod.GET, value = "/hello")
	public String hello() {
		return helloClient.hello();
	}
	
	@RequestMapping(method = RequestMethod.GET, value = "/toHello")
	public String toHello() throws InterruptedException {
		Thread.sleep(500);
		String result = helloClient.toHello();
		HystrixCircuitBreaker breaker = HystrixCircuitBreaker.Factory
				.getInstance(HystrixCommandKey.Factory
						.asKey("HelloClient#toHello()"));	
		System.out.println("断路器状态:" + breaker.isOpen());
		return result;
	}


}

  

 

以上是关于spring cloud feign+hystrix的主要内容,如果未能解决你的问题,请参考以下文章

Spring Cloud Feign 组成和配置

6Sping Cloud Feign

spring cloud feign遇到的问题

Spring-Cloud之Feign

spring cloud学习--Feign

Spring Cloud官方文档中文版-声明式Rest客户端:Feign