Hystrix
Hystrix 能够保证在一个依赖出现问题情况下。 不会导致我们的服务失败。 避免一个级联的故障。 来提高我们分布式的系统的弹性。
熔断器 本身是一个开关装置。 当某个服务发生故障之后。 通过断路器的故障监控。 返回一个符合预期的可处理的备用的响应。
作用:
1.服务降级fallback(友好页面) 2.服务熔断break(服务器达到最大访问量后,直接拒绝访问,然后调用服务降级的方法并返回友好提示,最后再恢复服务)
3.服务限流flowlimit(限制高并发)
操作:
1.新建子项目
2.改pom.xml
<dependencies> <!--hystrix--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <!--eureka client--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>com.etc.cloud</groupId> <artifactId>cloud-api-commons</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</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>
3.新建main类
@EnableCircuitBreaker @EnableEurekaClient @SpringBootApplication @Slf4j public class PaymentHystrixMain8001 { public static void main(String[] args) { SpringApplication.run( PaymentHystrixMain8001.class,args); log.info("****************PaymentHystrixMain8001 启动 ************ "); }
如果服务要整合openfeign
用@EnableHystrix注解代替@EnableCircuitBreaker
@EnableHystrix @EnableFeignClients @SpringBootApplication @EnableEurekaClient @Slf4j public class OrderHysitxMain8000 { public static void main(String[] args) { SpringApplication.run(OrderHysitxMain8000.class, args); log.info("****************OrderHysitxMain8000 启动 ************ "); } }
@EnableHystrix 开启hystrix
@EnableCircuitBreaker spring cloud 的hystrix
4.新建业务类
@Service public class PaymentService { public String paymentinfo_ok(Integer id) { return "线程池:" + Thread.currentThread().getName() + "paymentinfo ok--------------" + id + "------0(n_n)0"; } @HystrixCommand(fallbackMethod = "paymentinfo_timeoutHandler", commandProperties = { @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")}) public String paymentinfo_timeout(Integer id) { int tiemNumber = 5000; try { TimeUnit.MILLISECONDS.sleep(tiemNumber); } catch (InterruptedException e) { e.printStackTrace(); } return "线程池:" + Thread.currentThread().getName() + "paymentinfo_timeout-------" + id + "-----------0(n_n)0" + "耗时:" + tiemNumber + "秒"; } /** * 兜底方法,降级方法 * @param id * @return */ public String paymentinfo_timeoutHandler(Integer id) { return "线程池:" + Thread.currentThread().getName() + "8001系统繁忙稍后在试试"; } //=====服务熔断 @HystrixCommand(fallbackMethod = "paymentCircuitBreaker_fallback", commandProperties = { @HystrixProperty(name = "circuitBreaker.enabled", value = "true"),// 是否开启断路器 @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "5"),// 请求次数 @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"), // 时间窗口期 @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60"),// 失败率达到多少后跳闸 }) public String paymentCircuitBreaker(Integer id) { if (id < 0) { throw new RuntimeException("id 不能为负数"); } String uuid = UUID.randomUUID().toString(); return Thread.currentThread().getName() + "\t" + "调用成功" + uuid; } public String paymentCircuitBreaker_fallback(Integer id) { return "id 不能负数,请稍后再试,/(ㄒoㄒ)/~~ id: " + id; } }
@HystrixCommand是指定需要降级服务的方法,括号里写兜底的方法
@HystrixProperty是指定降级的条件
熔断器是指在指定时间内,比如10秒内如果够了5个请求,而且请求失败率达到60%时,拒绝访问并跳到友好页面(服务降级),等10
秒以后,熔断器转换到半开状态,当熔断器处于半开状态时,当前只能有一个请求被放行;
这个被放行的请求获得远端服务的响应后,假如是成功的,熔断器转换为关闭状态,重新计算,否则转换到打开状态,等10秒后继续半开。
5.新建controller测试
@RestController @Slf4j public class PaymentController { @Autowired private PaymentService paymentService; @GetMapping("/payment/hystrix/ok/{id}") public String payment_ok(@PathVariable("id") Integer id){ return paymentService.paymentinfo_ok(id); } @GetMapping("/payment/hystrix/timeout/{id}") public String payment_timeout(@PathVariable("id") Integer id){ return paymentService.paymentinfo_timeout(id); } /** * 熔断器 * @param id * @return */ @GetMapping("/payment/circuit/{id}") public String paymentCircuitBreaker(@PathVariable("id") Integer id) { return paymentService.paymentCircuitBreaker(id); }
server:
port: 8000
spring:
application:
name: cloud-consumer-feign-hystrix
eureka:
client:
register-with-eureka: false
fetch-registry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka
feign:
hystrix:
enabled: true
feign: hystrix: enabled: true
开启feign对hystrix的支持