Hystrix 熔断机制
1 导入依赖
org.springframework.cloud
spring-cloud-netflix-hystrix</artifactId》
2 启动类加注解
@SpringBootApplication
@EnableDiscoveryClient //服务发现注解
@EnableFeignClients //feingn远程调用注解·
@EnableCircuitBreaker //Hystrix注解
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
3 控制器
@RestController
@Slf4j
public class HystrixController {
@Autowired
private ProductClient productClient;
@HystrixCommand(fallbackMethod = "fallback")
@GetMapping("/getHystrix")
public String getProductMeg4() {
RestTemplate restTemplate = new RestTemplate();
return restTemplate.postForObject("http://localhost:8089/product/listForOrder", Arrays.asList("157875196366160022"), String.class);
}
/**
* 回调
* @return
*/
private String fallback() {
return "哎呀 ,地球怎么找不到你了" ;
}
}
注意 版本不同 HystrixCommand 注解没有 + 依赖
com.netflix.hystrix
hystrix-javanica
RELEASE
4 浏览器中 查找
当发生雪崩情况下会调用回滚函数