hystrix 给方法加断路器

Posted 奋小斗Struggle Young

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hystrix 给方法加断路器相关的知识,希望对你有一定的参考价值。

 

添加依赖

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>

1.启动类

@SpringBootApplication
@EnableDiscoveryClient
@ComponentScan("Cloud_Hystrix.*")
@EnableCircuitBreaker
public class HystrixController {
public static void main(String[] args) {
SpringApplication.run(HystrixController.class, args);
}
}

 

 

2,controller

@RestController
@RequestMapping(value="/reqmap",produces={"application/json;charaset=utf-8"},method=RequestMethod.GET)
public class HystrixReqMap {
@Autowired
HystrixService hystrixService;
@RequestMapping("")
public String reqmap(String param){
return hystrixService.reqmap(param);
}
}

 

 

3.service

@Service
public class HystrixService {
@HystrixCommand(fallbackMethod="fail")
public String reqmap(String param){
if(param.equals("aaa")){
throw new RuntimeException();
}
return param+"--hystrix";
}
public String fail(String param){
return "runtimeException";
}
}

以上是关于hystrix 给方法加断路器的主要内容,如果未能解决你的问题,请参考以下文章

试驾 Hystrix 断路器配置

SpringCloud断路器Hystrix全面解析

Hystrix:自定义断路器和恢复逻辑

Spring Cloud笔记 断路器-hystrix

Hystrix Dashboard:断路器执行监控

Spring Cloud入门教程-Hystrix断路器实现容错和降级