Hystrix命令fallbackmethod示例

时间:2019-12-23 14:43:05

标签: java spring hystrix netflix

我正在尝试学习使用netflix hystrix为我的服务创建后备方法。下面是一个restTemplate,可以访问另一个显示http请求的(microservice4)。但是,当我运行代码时,即使microservice4运行良好,microservice3也会实现fallbackmethod而不是main方法。我尝试放置Order注释,但是没有用。请让我知道是否缺少一些东西。

@RestController
public class Microservice3Controller {

    private final Logger LOG = LoggerFactory.getLogger(this.getClass());

    @Autowired
    RestTemplate restTemplate;

        @Order(1)
        @HystrixCommand(fallbackMethod = "MicroError")
    @GetMapping(value = "/microservice3")
    public String method3() {
        LOG.info("Inside method3");
        String baseUrl = "http://localhost:8004/microservice4";
        try {
                    Thread.sleep(1000);
        } catch (InterruptedException ex) {

        }                
        String response = (String) restTemplate.exchange(baseUrl, HttpMethod.GET, null, String.class).getBody();
        LOG.info("The response received by method3 is " + response);
        return response;
    }

        @Order(2)
        public String MicroError() {
            return "Microservice 4 Error";
        }
}

0 个答案:

没有答案