目录
Hystrix-1.1 Hystrix fallbackMethod进入
Hystrix-2.1 Health——commandProperties
-
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.setName("xuhy"); System.out.println("断啦"); return user; } }
- 4、其他内容
- 实体类User.java
//因为通过RestTemplate调用不需要jpa的注解内容 public class User { private Long id; private String username; private String name; private Short age; private BigDecimal balance; public Long getId() { return this.id; } public void setId(Long id) { this.id = id; } public String getUsername() { return this.username; } public void setUsername(String username) { this.username = username; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public Short getAge() { return this.age; } public void setAge(Short age) { this.age = age; } public BigDecimal getBalance() { return this.balance; } public void setBalance(BigDecimal balance) { this.balance = balance; } }
- application.yml
server: port: 7901 user: userServicePath: http://localhost:7900/simple/ eureka: client: serviceUrl: defaultZone: http://user:123@localhost:8761/eureka instance: prefer-ip-address: true instance-id: ${spring.application.name}:${spring.application.instance-id:${server.port}} spring: application: name: hystrix-001
{
"id": 0,
"username": null,
"name": "xuhy",
"age": null,
"balance": null
}
-
Hystrix-1.1 Hystrix fallbackMethod进入
将用户服务等其中一份服务断掉,则会直接进入fallbackMethod方法中,控制台会打印断啦两个字。
-
Hystrix-2.1 Health——commandProperties
@HystrixCommand(fallbackMethod = "stubMyService",
commandProperties = {
@HystrixProperty(name="execution.isolation.strategy", value="SEMAPHORE")
}
)
- 2、内容演示
-
Hystrix-2.2 Health——autuactor
-
Indicator及Metrics Streamss
- 未完待续