Springboot-微服務-微服務元件之服務管理-服務熔斷雪崩-Hystrix
Springboot-微服務-微服務元件之服務管理-服務熔斷雪崩-Hystrix
1.Hystrix
1.1.簡介
Hystrix,即熔斷器。
Hystix是Netflix開源的一個延遲和容錯庫,用於隔離訪問遠端服務、第三方庫,防止出現級聯失敗。
1.2.熔斷器的工作機制:
正常工作的情況下,客戶端請求呼叫服務API介面:
當有服務出現異常時,直接進行失敗回滾,服務降級處理:
當服務繁忙時,如果服務出現異常,不是粗暴的直接報錯,而是返回一個友好的提示,雖然拒絕了使用者的訪問,但是會返回一個結果。
這就好比去買魚,平常超市買魚會額外贈送殺魚的服務。等到逢年過節,超時繁忙時,可能就不提供殺魚服務了,這就是服務的降級。
系統特別繁忙時,一些次要服務暫時中斷,優先保證主要服務的暢通,一切資源優先讓給主要服務來使用,在雙十一、618時,京東天貓都會採用這樣的策略。
Hystrix 解決服務雪崩問題,主要手段有兩個:
- 執行緒隔離
- 服務熔斷和降級
原理分系:
Hystix 為每一個依賴的服務呼叫分配一個小的執行緒池,如果執行緒池滿了,則呼叫會立即拒絕。預設不採用排隊,加速失敗判定時間
使用者的請求不再直接訪問服務,而是通過執行緒池中的空閒執行緒來訪問服務,如果執行緒池滿,或者請求超時,則會降級處理。
———什麼是服務的降級?———
服務降級:優先保證核心服務,而非核心服務不可用或者弱可用。
使用者請求故障時,不會被阻塞,也不會永遠等待,或者你看到了一個系統奔潰的資訊,這些都不會出現,唯一出現就是你設定的一個友好的返回資訊。
服務降級,雖然會導致請求失敗,但是不會阻塞,最多就是依賴這個服務對應的執行緒資源,對其它服務沒有響應。
降級一般使用的地方
一般在服務的消費方,會去做服務的降級處理
入門操作
要點
-
引入Hystrix相關依賴
-
添加註解 @EnableHystrix / @EnableCircuitBreaker 這個兩個註解都可以
-
在control 配置相關引數的配置,例如配置註解,超時時長。
引入引入Hystrix相關依賴
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> <version>2.0.1.RELEASE</version> </dependency>
在Spring啟動類上添加註解
一個標準的eureka標準的客戶端都需要配置以上三個註解,所以Spring 提供一個新的註解SpringCloudApplication包含的以上三個註解
package com.caicai;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
//@EnableDiscoveryClient//開啟eureka服務端的呼叫
//@SpringBootApplication
//@EnableCircuitBreaker //新增Hystrix
//@EnableHystrix
@SpringCloudApplication // 一個標準的eureka標準的客戶端都需要配置以上三個註解,所以Spring 提供一個新的註解SpringCloudApplication包含的以上三個註解
public class ConsumerServiceApplication {
//Spring boot 呼叫的介面都是rest風格的介面,這裡我們使用,Spring 自帶的resTemplate 去呼叫,它包含了:http,post 等等
@Bean
@LoadBalanced//開啟負載均衡註解
public RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(ConsumerServiceApplication.class);
}
}
在control 配置相關引數的配置
配置要點
-
編寫超時回撥方法
-
配置所有方法的預設降級邏輯,在類上配置@DefaultProperties註解
-
回撥方法型別,要和呼叫方法的型別要保持一致
-
針對某一個方法,配置單一的超時時長
-
配置全域性的預設超時,時長。(在yml配置檔案中,配置)
package com.caicai.Consumer.Control;
import com.caicai.Consumer.po.User;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.List;
@RestController
@RequestMapping("consumer")
@DefaultProperties(defaultFallback = "default_Fallback")//為所有方法新增降級處理邏輯
public class ConsumerControl {
@Autowired
private RestTemplate restTemplate;
@GetMapping("{id}")
// @HystrixCommand(fallbackMethod = "queryById_Fallback")
@HystrixCommand // 開啟降級處理邏輯
// @HystrixCommand(commandProperties = {
//
// @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "3000")//設定超時時長
// }) //配置單個超時,時長
public String queryById(@PathVariable("id") Long id)
{
String url = "http://user-service/user/"+id;
String user = restTemplate.getForObject(url,String.class);
return user;
}
//一對一的處理邏輯方法
public String queryById_Fallback( Long id)
{
return "系統繁忙!,請稍後再試!";
}
//通用的降級處理邏輯方法
public String default_Fallback()
{
return "系統繁忙!,請稍後再試!";
}
}
配置預設全域性超時,時長
要點1: 該段時長,預設沒有提示,需要手工去填寫。
要點2: 可以針對某個服務或者方法配置相關時長
hystrix:
command:
default: #這裡,填寫服務名稱/方法
execution:
isolation:
thread:
timeoutInMilliseconds: 3000
開開心心,上班!
快快樂樂,遊玩!
及時行樂!