ConsumerController
package com.mindasoft.consumer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.circuitbreaker.CircuitBreakerFactory; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; /** * Created by huangmin on 2017/11/10 14:50. */ @RestController public class ConsumerController { private static final Logger LOGGER = LoggerFactory.getLogger(ConsumerController.class); @Autowired private RestTemplate restTemplate; // HTTP 访问操作类 @Autowired private CircuitBreakerFactory circuitBreakerFactory; @RequestMapping("/hello") public String hello() { String providerMsg = circuitBreakerFactory.create("hello") .run(() -> restTemplate.getForObject("http://PROVIDER-SERVICE/hello", String.class), throwable -> "CircuitBreaker fallback msg"); return "Hello,I'm Customer! msg from provider : <br/><br/> " + providerMsg; } }启动eureka server和本项目,访问:9004/hello
页面打印如下:
说明进入到了断路throwable 处理逻辑。