分布式系统的延时和故障容错之Spring Cloud Hystrix (2)

服务层HelloService.java

package com.easy.serviceConsumer.service; import com.easy.serviceConsumer.exception.NotFallbackException; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; @Service public class HelloService { @Autowired RestTemplate restTemplate; private static final String HELLO_SERVICE = "http://hystrix-service-provider/"; @HystrixCommand(fallbackMethod = "helloFallback", ignoreExceptions = {NotFallbackException.class} , groupKey = "hello", commandKey = "str", threadPoolKey = "helloStr") public String hello(String p1, String p2) { return restTemplate.getForObject(HELLO_SERVICE + "hello?p1=" + p1 + "&p2=" + p2, String.class); } private String helloFallback(String p1, String p2, Throwable e) { System.out.println("class: " + e.getClass()); return "error, " + p1 + ", " + p2; } }

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

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