前言:在上一章节中我们创建了服务消费者模块,而本节内容则是使用Hystrix对服务进行服务降级处理。
1、首先我们先对服务提供者的服务进行服务降级处理
(1)修改cloud-provider-hystrix-payment8001子模块的PaymentServiceImpl类
注:@HystrixProperty注解的更多key/value参数设置可以查看HystrixCommandProperties 类
package com.ken.springcloud.service.impl;import com.ken.springcloud.service.PaymentService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.stereotype.Service;import java.util.concurrent.TimeUnit;@Service
public class PaymentServiceImpl implements PaymentService {@Overridepublic String paymentInfoOK(Integer id) {return "线程池:" + Thread.currentThread().getName() + "paymentInfoOK,id: " + id;}@Override//一旦调用服务方法失败并抛出了错误信息后,会自动调用@HystrixCommand标注好的fallbackMethod调用类中的指定方法,这里设置服务降级的条件为连接超时超过3秒,即3秒内走paymentInfoTimeOut方法内的业务逻辑,超过3秒走paymentInfoTimeOutHandler方法@HystrixCommand(fallbackMethod = "paymentInfoTimeOutHandler",commandProperties = {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000")})public String paymentInfoTimeOut(Integer id) {int timeNumber = 5;try {TimeUnit.SECONDS.sleep(timeNumber);} catch (InterruptedException e) {e.printStackTrace();}return "线程池:" + Thread.currentThread().getName() + "paymentInfoTimeOut,id: " + id + "耗时" + timeNumber + "秒";}public String paymentInfoTimeOutHandler(Integer id) {return "系统繁忙,请稍后再试";}}
(2)修改cloud-provider-hystrix-payment8001子模块的PaymentHystrixMain8001启动类
package com.ken.springcloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication
//使用Feign,激活并开启
@EnableEurekaClient
//开启断路器功能
@EnableCircuitBreaker
public class PaymentHystrixMain8001 {public static void main(String[] args) {SpringApplication.run(PaymentHystrixMain8001.class, args);}}
(3) 分别启动eureka-server7001、cloud-provider-hystrix-payment8001服务
效果图:
(4) 在浏览器的地址栏里分别输入http://localhost:8001/payment/hystrix/timeout/1通过调用这个接口查看服务提供者的服务降级功能是否正常运行
效果图:
由图可知服务提供者的服务降级成功,客户端连接cloud-provider-hystrix-payment8001服务超时后最后执行了paymentInfoTimeOutHandler方法,由paymentInfoTimeOutHandler方法进行兜底处理,而且值得注意的是正常调用服务使用到的线程池和服务降级时使用到的线程池不是同一个,这意味着hystrix用单独的一个线程池作服务降级处理,起到了隔离的效果
正常的:
降级处理的:
2、在上述的操作中我们对服务提供者的服务成功地进行了服务降级处理,接下来我们对服务消费者的服务同样进行服务降级处理
(1)修改cloud-consumer-feign-hystrix-order80子模块的application.yml文件,加上feign:hystrix:enabled:true的配置,使feign调用启用hystrix
关于feign:hystrix:enabled:true配置的相关官方文档,官方文档介绍如果配置feign.hystrix.Enabled=true,则Feign将使用断路器包装所有方法https://cloud.spring.io/spring-cloud-openfeign/reference/html/#spring-cloud-feign-hystrix-fallback
server:port: 8080
eureka:client:#表示是否将自己注册进Eureka Server里,默认为trueregister-with-eureka: falseservice-url:defaultZone: http://eureka7001.com:7001/eureka/feign:client:config:default:#指的是建立连接所用的时间,适用于网络状况正常的情况下,两端连接所用的时间connect-timeout: 5000#指的是建立连接后从服务器读取到可用资源所用的时间read-timeout: 5000#feign调用启用hystrix,启用后feign调用可以使用hystrix的服务降级、服务熔断、服务隔离等,从上述的设置可知feign请求超过5s就会进行服务降级、服务熔断、服务隔离等相关操作hystrix:enabled: true#设置命令执行的超时时间,默认1000ms,因为开启了feign:hystrix:enabled:true,这里要设置为5000ms,否则设置ribbon的超时时间也没用,ribbon的超时时间会被强制改为了1000ms
hystrix:command:default:execution:isolation:thread:timeoutInMilliseconds: 5000
(2)修改cloud-consumer-feign-hystrix-order80子模块的OrderHystrixMain80启动类,开启断路器功能
package com.ken.springcloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.openfeign.EnableFeignClients;@SpringBootApplication
//使用Feign,激活并开启
@EnableFeignClients
//开启断路器功能
@EnableCircuitBreaker
public class OrderHystrixMain80 {public static void main(String[] args) {SpringApplication.run(OrderHystrixMain80.class, args);}}
(3)修改cloud-provider-hystrix-payment8001子模块的PaymentServiceImpl类,把睡眠时间调为2秒,模拟服务提供者正常运行
package com.ken.springcloud.service.impl;import com.ken.springcloud.service.PaymentService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.stereotype.Service;import java.util.concurrent.TimeUnit;@Service
public class PaymentServiceImpl implements PaymentService {@Overridepublic String paymentInfoOK(Integer id) {return "线程池:" + Thread.currentThread().getName() + "paymentInfoOK,id: " + id;}@Override//一旦调用服务方法失败并抛出了错误信息后,会自动调用@HystrixCommand标注好的fallbackMethod调用类中的指定方法,这里设置服务降级的条件为连接超时超过3秒,即3秒内走paymentInfoTimeOut方法内的业务逻辑,超过3秒走paymentInfoTimeOutHandler方法@HystrixCommand(fallbackMethod = "paymentInfoTimeOutHandler",commandProperties = {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000")})public String paymentInfoTimeOut(Integer id) {int timeNumber = 2;try {TimeUnit.SECONDS.sleep(timeNumber);} catch (InterruptedException e) {e.printStackTrace();}return "线程池:" + Thread.currentThread().getName() + "paymentInfoTimeOut,id: " + id + "耗时" + timeNumber + "秒";}public String paymentInfoTimeOutHandler(Integer id) {return "线程池:" + Thread.currentThread().getName() + "系统繁忙,请稍后再试";}}
(4)修改cloud-consumer-feign-hystrix-order80子模块的OrderHystrixController类,设置超时时间为1.5秒,这里是模拟服务消费者端的响应时间短但是服务提供者端返回资源的时间长,从而观察服务消费者端在超时后会作出什么反应
package com.ken.springcloud.controller;import com.ken.springcloud.service.PaymentHystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;@RestController
public class OrdertHystrixController {@Resourceprivate PaymentHystrixService paymentHystrixService;@GetMapping("/consumer/payment/hystrix/ok/{id}")public String paymentInfoOK(@PathVariable("id") Integer id) {String result = paymentHystrixService.paymentInfoOK(id);return result;}@GetMapping("/consumer/payment/hystrix/timeout/{id}")//一旦调用服务方法失败并抛出了错误信息后,会自动调用@HystrixCommand标注好的fallbackMethod调用类中的指定方法,这里设置服务降级的条件为连接超时超过3秒,即3秒内走paymentInfoTimeOut方法内的业务逻辑,超过3秒走paymentInfoTimeOutHandler方法@HystrixCommand(fallbackMethod = "paymentInfoTimeOutHandler",commandProperties = {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1500")})public String paymentInfoTimeOut(@PathVariable("id") Integer id) {String result = paymentHystrixService.paymentInfoTimeOut(id);return result;}public String paymentInfoTimeOutHandler(Integer id) {return "服务提供者繁忙,请稍后再试";}}
(5) 分别启动eureka-server7001、cloud-provider-hystrix-payment8001服务、cloud-consumer-feign-hystrix-order80服务
效果图:
(6) 在浏览器的地址栏里分别输入http://localhost:8080/consumer/payment/hystrix/timeout/1通过调用这个接口查看服务消费者的服务降级功能是否正常运行
由图可知服务消费者的服务降级成功,服务消费者连接服务提供者的服务超过1.5秒后超时,最后执行了paymentInfoTimeOutHandler方法,由paymentInfoTimeOutHandler方法进行兜底处理