1. 程式人生 > >spring cloud feign+hystrix

spring cloud feign+hystrix

exe interface hystrix rup rest log println exec pen

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