<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR3</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> </dependencies>
@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; } }
@RestController public class TestController { @Autowired private MemberService memberService; @RequestMapping(value = "/router", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) public Member router() { return memberService.getMember(1); } }