spring boot 启动类 添加组件
@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; } }