@SpringBootApplication @EnableEurekaClient @EnableCircuitBreaker @ServletComponentScan public class SaleApp { @Bean @LoadBalanced public RestTemplate getRestTemplate() { return new RestTemplate(); } public static void main(String[] args) { new SpringApplicationBuilder(SaleApp.class).web(true).run(args); } }
@Service //@DefaultProperties(defaultFallback = "getMemberFallback") public class MemberService { @Autowired private RestTemplate restTpl; @HystrixCommand(fallbackMethod = "getMemberFallback", groupKey = "MemberGroup", commandKey = "MemberCommandKey", commandProperties = { @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000") }, threadPoolProperties = { @HystrixProperty(name = "coreSize", value = "2") }) public Member getMember(Integer id) { try { Thread.sleep(2000); } catch (Exception e) { } Member member = restTpl.getForObject( "http://spring-hy-member/member/{id}", Member.class, id); return member; } public Member getMemberFallback(Integer id) { Member m = new Member(); m.setId(1); m.setName("error member"); return m; } }
原文地址:https://www.cnblogs.com/zfzf1/p/8550706.html
时间: 2024-11-08 21:39:56