服务消费者
pom文件
导入hystrix场景启动器
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
yml文件 配置hystrix超时时间
enabled: true #feign作为客户端使用hystrix必须开启,服务端没有feign所以没有这个
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 3000 #毫秒为单位的超时时间
主启动类注解
@EnableHystrix 开启hystrix注解
编写代码实现OpenFeignService接口 实现消费者端服务熔断
服务提供者
pom文件
导入hystrix场景启动器、actuator依赖
<!--hystrix-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!-- 引入Hystrix dashboard依赖和 actuator依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
yml文件 配置hystrix监控
management:
endpoints:
web:
exposure:
include: hystrix.stream
主启动类
/**
*此配置是为了服务监控而配置,与服务降级熔断本身无关,springcloud升级后的坑
*ServletRegistrationBean因为springboot的默认路径不是"/hystrix.stream",
*只要在自己的项目里配置上下面的servlet就可以了
*/
@Bean
public ServletRegistrationBean getServlet() {
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
控制层
默认熔断
编写熔断后的方法
public Result myFallback(Integer id){
return new Result(200,"findPaymentById输入的id为: "+ id);
}
在需要熔断后方法体上加注解
@HystrixCommand(fallbackMethod = "myFallback") fallbackMethod处理熔断服务时执行的方法
自定义熔断
编写熔断后的方法
public Result circuitFallback(@RequestParam(value="id")Integer id){
return new Result<Object>(200,"delete熔断"+id);
}
在需要熔断后方法体上加注解
@HystrixCommand(fallbackMethod = "circuitFallback",commandProperties = {
@HystrixProperty(name = "circuitBreaker.enabled",value = "true"),// 是否开启断路器
@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"),// 请求次数
@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "10000"), // 时间窗口期
@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "50")// 失败率达到多少后跳闸
})
创建仪表盘子项目
在pom文件中导入
<dependencies>
<!-- 引入Hystrix dashboard依赖和 actuator依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
在yml配置相关设置
server:
port: 9000
management:
enabled: false
endpoints:
web:
exposure:
include: hystrix.stream
base-path: /
主启动类中注解
@SpringBootApplication
@EnableHystrixDashboard