06-CircuitBreaker断路器 (2)

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
页面打印如下:

Hello,I'm Customer! msg from provider : CircuitBreaker fallback msg

说明进入到了断路throwable 处理逻辑。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zgzwdy.html