idea创建Hystrix入门实例

此案例为替换原有的消费者,注册中心和消息提供者延用之前的

注册中心的案例   https://www.cnblogs.com/songlove/p/14793575.html

消息提供者:https://www.cnblogs.com/songlove/p/14794021.html

消息消费者在Feign案例上修改而得 Feign入门实例  https://www.cnblogs.com/songlove/p/14828920.html

第一步引入maven的依赖

1 <dependency>
2       <groupId>org.springframework.cloud</groupId>
3       <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
4       <version>2.2.8.RELEASE</version>
5 </dependency>

第二步 启动类上添加@EnableHystrix注解

 第三步在controller类中添加@HystrixCommand注解,指定默认错误返回方法reFallBack

1 @HystrixCommand(fallbackMethod = "reFallBack")
2     @GetMapping(value = "/callHello")
3     public String calloHello(){
4         //此时的调用相当于 http://eureka-user-provide/user/hello
5         String result=userRemoteClient.hello();
6         System.out.println("调用结果:"+result);
7         return result;
8     }

第四步 编写reFallBack方法

1    public String reFallBack(){
2         return "默认失败了";
3     }

第五步,启动消息注册中心和消息提供者,访问http://localhost:8098/callHello地址

 第六步,停掉消息提供者,再此访问此地址

 使用 @HystrixCommand注解配置属性如

 1 @HystrixCommand(fallbackMethod = "reFallBack",commandProperties = {
 2             @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "5000"),
 3             @HystrixProperty(name="metrics.rollingStats.timeInMilliseconds",value = "100000")
 4     },threadPoolProperties = {
 5             @HystrixProperty(name="coreSize",value = "1"),
 6             @HystrixProperty(name = "maxQueueSize",value="10")
 7     })
 8     @GetMapping(value = "/callHello_02")
 9     public String callHello_02(){
10         String result=userRemoteClient.hello();
11         System.out.println("调用结果:"+result);
12         return result;
13     }
execution.isolation.thread.timeoutInMilliseconds  超时时间  单位毫秒
metrics.rollingStats.timeInMilliseconds  设置统计时间的时间窗口值
调用此方法

 项目引用 Spring Boot Actuator监控

1、先引入maven的项目依赖

1 <dependency>
2       <groupId>org.springframework.boot</groupId>
3       <artifactId>spring-boot-starter-actuator</artifactId>
4 </dependency>

2、调用一下服务 ,然后再查看调用状态 http://localhost:8098/actuator/health

gitee地址:Hystrix_demo: 断路器模式案例 (gitee.com)
生于忧患,死于安乐
原文地址:https://www.cnblogs.com/songlove/p/14917643.html