Spring-cloud-hystrix-容错机制(当服务调用异常时进行响应)
1.在App.java中开启容错保护(加入此注解开启容错机制@EnableCircuitBreaker)
@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker
public class RequestApp {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SpringApplication.run(RequestApp.class, args);
}
@Bean
@LoadBalanced
public RestTemplate newRestTemplate(){
return new RestTemplate();
}
}
2.pom.xml中引用hystrix依赖
org.springframework.boot
spring-boot-starter-parent
1.3.7.RELEASE
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-starter-eureka
org.springframework.cloud
spring-cloud-starter-hystrix
org.springframework.cloud
spring-cloud-dependencies
Camden.SR3
pom
import
3。Controller
@RequestMapping(value = "CircuitBreakerInfo")
public List circuitBreakerInfo(){
return circuitBreakerserver.circuitBreakerInfoService();
}
Service
@HystrixCommand(fallbackMethod = "circuiterrorMethod")
public List circuitBreakerInfoService(){
System.out.println("测试Hystrix / circuitBreaker 方法");
Map maps = new HashMap();
maps.put("name", "request");
return restTemplate.getForObject(ribbonUrl, List.class,maps);
}