前言:在上一章節中我們創建了服務消費者模塊,而本節內容則是使用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方法進行兜底處理