在微服務中,我們將系統拆分為很多個服務單元,各單元之間通過服務註冊和訂閱消費的方式進行相互依賴。但是如果有一些服務出現問題了會怎麼樣? 比如說有三個服務(ABC),A調用B,B調用C。由於網路延遲或C本身代碼有問題導致B遲遲得不到回應,這樣B調用C的請求就會被掛起,等待。 在高併發的訪問的情況下,這 ...
在微服務中,我們將系統拆分為很多個服務單元,各單元之間通過服務註冊和訂閱消費的方式進行相互依賴。但是如果有一些服務出現問題了會怎麼樣?
比如說有三個服務(ABC),A調用B,B調用C。由於網路延遲或C本身代碼有問題導致B遲遲得不到回應,這樣B調用C的請求就會被掛起,等待。
在高併發的訪問的情況下,這些掛起的線程得不到釋放,使後續的請求阻塞,最終導致B也掛掉了。依次類推,A可能也會掛掉,進而使整個系統全部崩潰。
為瞭解決整個問題,Spring Cloud 使用Hystrix進行服務容錯保護,包括斷路器、線程隔離等一系列的保護功能,今天我們就來看下如何通過Hystrix實現斷路器。
一、什麼是Spring Cloud Hystrix?什麼是斷路器?
Spring Cloud Hystrix是基於Netflix的開源框架Hystrix實現的,其目的是為了通過控制那些訪問遠程系統、服務和第三方的節點,從而對延遲和故障提供強大的容錯能力。
斷路器類似於我們家裡面強電箱裡面用到的漏電斷路保護器,當服務單元出現故障(類似於電器發生短路),通過斷路器的故障監控功能(類似於保險絲),向調用方返回一個錯誤響應,避免長時間等待,從而避免故障蔓延到整個系統。
二、沒有斷路器的情況下,頁面展示
還記得我們前面寫的spring cloud 入門系列二:使用Eureka 進行服務治理裡面的三個服務(eureka/hello-service/hello-consumer)嗎?我們基於這個進行實驗。
- 啟動eureka服務註冊中心,埠號1111
- 啟動hello-service服務提供者,這裡我們啟動兩個服務,埠號分別為9090,9091
- 啟動hello-consumer服務消費者,埠號為9999;這個時候我們多次訪問http://localhost:9999/hello-consumer是沒有問題的
- 將hello-service埠號為9091的服務關掉,再去多次訪問http://localhost:9999/hello-consumer,報錯了
PS:這裡說明下,為什麼要多次訪問,是因為我們通過ribbon實現了負載均衡,訪問http://localhost:9999/hello-consumer的時候,會輪詢訪問hello-service的兩個服務,當訪問到埠號是9091的服務時才報錯,訪問9090的服務就不會有問題。
三、斷路器代碼實現
接下來我們看下如何進行代碼實現,我們不去修改服務註冊中心和服務提供者,只需要修改服務消費者hello-consumer。
- 修改POM文件,引入Hystrix依賴
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.sam</groupId> <artifactId>hello-consumer</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.1.RELEASE</version> </parent> <properties> <javaVersion>1.8</javaVersion> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR6</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- 引入eureka 客戶端依賴 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <!-- 引入ribbon 依賴 ,用來實現負載均衡,我們這裡只是使用先不作其他介紹 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> </dependency> <!-- 引入hystrix 依賴 ,用來實現服務容錯保護--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> </dependencies> </project>
- 修改啟動類,追加註解@EnableCircuitBreaker,開啟斷路器
@EnableDiscoveryClient @SpringBootApplication @EnableCircuitBreaker public class ConsumerApp { //@Bean 應用在方法上,用來將方法返回值設為為bean @Bean @LoadBalanced //@LoadBalanced實現負載均衡 public RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(ConsumerApp.class, args); } }
這個時候你會發現,這個啟動類加了三個註解,這個是不是很麻煩?沒關係,我們可以使用註解@SpringCloudApplication
@SpringCloudApplication public class ConsumerApp { //@Bean 應用在方法上,用來將方法返回值設為為bean @Bean @LoadBalanced //@LoadBalanced實現負載均衡 public RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(ConsumerApp.class, args); } }
@SpringCloudApplication = @EnableDiscoveryClient +@SpringBootApplication+@EnableCircuitBreaker,從源碼就能看出來:
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @SpringBootApplication @EnableDiscoveryClient @EnableCircuitBreaker public @interface SpringCloudApplication { }
- 追加service
@Service public class ConsumerService { @Autowired RestTemplate restTemplate; @HystrixCommand(fallbackMethod = "errorMsg") public String consumer() { // 調用hello-service服務,註意這裡用的是服務名,而不是具體的ip+port restTemplate.getForObject("http://hello-service/hello", String.class); return "hello consumer finish !!!"; } public String errorMsg() { return "error!!!"; } }
我們把原來controller裡面的調用RestTemplate的實現放到service裡面,並且通過@HystrixCommand來指定回調方法,當出現錯誤時調用該方法。
- 修改controller
/** *這裡不再直接調用restTemplate, *而是通過調用service進行實現 * */ @RestController public class ConsumerController { @Autowired // RestTemplate restTemplate; ConsumerService service; @RequestMapping("/hello-consumer") public String helloConsumer() { // //調用hello-service服務,註意這裡用的是服務名,而不是具體的ip+port // restTemplate.getForObject("http://hello-service/hello", String.class); return service.consumer(); } }
- 測試,多次訪問,當報錯的時候,會顯示如下內容
大功告成!