目录
Hystrix-1.1 Hystrix fallbackMethod进入
Hystrix-2.1 Health——commandProperties
Hystrix——feign和fallback方法的@Component
Feign使用fallbackFactory属性打印fallback异常
-
Hystrix- 注意
使用Hystrix必须注入autuactor依赖,不然启动不起来
-
Hystrix-0 Hystrix 的官方简介
- 官方文档:https://projects.spring.io/spring-cloud/spring-cloud.html#_circuit_breaker_hystrix_clients
- javanica文档:https://github.com/Netflix/Hystrix/tree/master/hystrix-contrib/hystrix-javanica
-
Hystrix-1 Hystrix 的开发步骤
- 1、添加pom.xml依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>
- 2、 启动类添加注解@EnableCircuitBreaker
@SpringBootApplication @EnableEurekaClient @EnableCircuitBreaker public class Hystrix001Application { @Bean public RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(Hystrix001Application.class, args); } }
- 3、方法名称中添加注解@HystrixCommand,并且自定义返回方法【返回方法的参数返回值、入参一定要和注解相同】
@RestController public class MovieController { @Autowired private RestTemplate restTemplate; @Value("${user.userServicePath}") private String userServicePath; @GetMapping("/movie/{id}") @HystrixCommand(fallbackMethod = "findByIdfallback") public User findById(@PathVariable Long id) { return this.restTemplate.getForObject(this.userServicePath + id, User.class); } public User findByIdfallback() { User user = new User(); user.setId(1110504125L); user.