目录
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.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
- 启动项目
- 访问health——http://192.168.4.160:7900/health
{ "description": "Spring Cloud Eureka Discovery Client", "status": "UP", "discoveryComposite": { "description": "Spring Cloud Eureka Discovery Client", "status": "UP", "discoveryClient": { "description": "Spring Cloud Eureka Discovery Client", "status": "UP", "services": [ "microservice-provider-user", "microservice-consumer-movie-ribbon-with-hystrix" ] }, "eureka": { "description": "Remote status from Eureka server", "status": "UP", "applications": { "MICROSERVICE-CONSUMER-MOVIE-RIBBON-WITH-HYSTRIX": 1, "MICROSERVICE-PROVIDER-USER": 1 } } }, "diskSpace": { "status": "UP", "total": 446141005824, "free": 395148431360, "threshold": 10485760 }, "db": { "status": "UP", "database": "H2", "hello": 1 }, "refreshScope": { "status": "UP" }, "hystrix": { "status": "UP" } }
- 把用户微服务停掉——http://192.168.4.160:8010/movie/1
{ "id": 0, "username": null, "name": null, "age": null, "balance": null }
- 多次请求到不再请求的状态
- 我这边怎么请求都是up状态,所以直接用老师的事例图片了。openC..表示当前打开的断路器是findById
- 要想使用health必须是要有autuactor的支持,如果pom文件没有添加,启动都启动不起来哦!!!!
-
Indicator及Metrics Stream
- 官方文档:https://projects.spring.io/spring-cloud/spring-cloud.html#_circuit_breaker_hystrix_clients
- MICROSERVICE-CONSUMER-MOVIE-RIBBON-WITH-HYSTRIX方访问/hystrix.stream发现浏览器一直旋转
- 现在用户微服务启动成功之后,访问movie/1再次访问/hystrix.stream就有啦
-
Hystrix——feign和fallback方法的@Component
- 1 官方例子
@FeignClient(name = "hello", fallback = HystrixClientFallback.class)
protected interface HystrixClient {
@RequestMapping(method = RequestMethod.GET, value = "/hello")
Hello iFailSometimes();
}
static class HystrixClientFallback implements HystrixClient {
@Override
public Hello iFailSometimes() {
return new Hello("fallback");
}
}
- 2 改编自己非public(写在一起或者不一起都行)
写在外部public
注意:如果你没有添加@Component在HystrixClientFallback中会报错
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'movieController': Unsatisfied dependency expressed through field 'userFeignClient'; nested exception is
-
如何禁用单个FegionClient的Hystrix的支持
-
Feign使用fallbackFactory属性打印fallback异常