springcloud熔斷器Hystrix與監控
服務雪崩效應
當一個請求依賴多個服務的時候:
正常情況下的訪問
但是,當請求的服務中出現無法訪問、異常、超時等問題時(圖中的I),那麼用戶的請求將會被阻塞。
如果多個用戶的請求中,都存在無法訪問的服務,那麼他們都將陷入阻塞的狀態中。
Hystrix的引入,可以通過服務熔斷和服務降級來解決這個問題。
上面的圖解可能大家看不明白,沒事。在springcloud的中如果很多很多的請求來訪問這個項目那麼這個服務器肯定是扛不住這個壓力的那麼就需要多個服務一起來抗,如果其中的一個服務沒有抗住導致宕機,那麼原本它要做的事情就需要分佈到其他的服務上面去(負載均衡),其他的服務的壓力就會加大然後其中又有服務抗不住,它要承擔的事情就需要分佈到其他的服務上面去,這樣一來服務器的壓力就會越來越大,從而導致整個項目崩塌----這就是雪崩效應。。。
服務熔斷服務降級
Hystrix斷路器簡介
hystrix對應的中文名字是“豪豬”,豪豬周身長滿了刺,能保護自己不受天敵的傷害,代表了一種防禦機制,這與hystrix本身的功能不謀而合,因此Netflix團隊將該框架命名爲Hystrix,並使用了對應的卡通形象做作爲logo。
在一個分佈式系統裏,許多依賴不可避免的會調用失敗,比如超時、異常等,如何能夠保證在一個依賴出問題的情況下,不會導致整體服務失敗,這個就是Hystrix需要做的事情。Hystrix提供了熔斷、隔離、Fallback、cache、監控等功能,能夠在一個、或多個依賴同時出現問題時保證系統依然可用。
Hystrix服務熔斷服務降級@HystrixCommand fallbackMethod
熔斷機制是應對雪崩效應的一種微服務鏈路保護機制。
當某個服務不可用或者響應時間超時,會進行服務降級,進而熔斷該節點的服務調用,快速返回自定義的錯誤影響頁面信息。
我們寫個項目來測試下;
我們寫一個新的帶服務熔斷的服務提供者項目 microservice-student-provider-hystrix-1004
把 配置和 代碼 都複製一份到這個項目裏;
然後修改;
1,pom.xml加下 hystrix支持
<!--Hystrix相關依賴-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
2,application.yml修改下端口和實例名稱
server:
port: 1004
context-path: /
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/tb_stu?useUnicode=true&characterEncoding=utf8
username: root
password: 123
jpa:
hibernate:
ddl-auto: update
show-sql: true
application:
name: microservice-student
profiles: provider-hystrix-1004
eureka:
instance:
hostname: localhost
appname: microservice-student
instance-id: microservice-student:1004
prefer-ip-address: true
client:
service-url:
defaultZone: http://eureka2001.hu.com:2001/eureka/,http://eureka2002.hu.com:2002/eureka/,http://eureka2003.hu.com:2003/eureka/
info:
groupId: com.hu.t224Springcloud
artifactId: microservice-student-provider-hystrix-1004
version: 1.0-SNAPSHOT
userName: http://hu.com
phone: 123456123456
啓動類StudentProviderHystrixApplication_1004加下註解支持 @EnableCircuitBreaker
package com.hu.microservicestudentproviderhystrix;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableCircuitBreaker
@EntityScan("com.hu.*.*")
@EnableEurekaClient
@SpringBootApplication
public class MicroserviceStudentProviderHystrixApplication {
public static void main(String[] args) {
SpringApplication.run(MicroserviceStudentProviderHystrixApplication.class, args);
}
}
服務提供者1004中controller新增
@Value("${server.port}")
private String port;
/**
* 測試Hystrix服務降級
* @return
* @throws InterruptedException
*/
@ResponseBody
@GetMapping(value="/hystrix")
@HystrixCommand(fallbackMethod="hystrixFallback")
public Map<String,Object> hystrix() throws InterruptedException{
Thread.sleep(2000);
Map<String,Object> map=new HashMap<String,Object>();
map.put("code", 200);
map.put("info","工號【"+port+"】正在爲您服務");
return map;
}
public Map<String,Object> hystrixFallback() throws InterruptedException{
Map<String,Object> map=new HashMap<String,Object>();
map.put("code", 500);
map.put("info", "系統【"+port+"】繁忙,稍後重試");
return map;
}
這裏我正常訪問 返回的是 200 業務數據xxxxx
但是我們這裏Thread.sleep(2000) 模擬超時;
這裏的話 我們加上@HystrixCommand註解 以及 fallbackMethod
表明這個方法我們再 沒有異常以及沒有超時(hystrix默認1秒算超時)的情況,才返回正常的業務數據;
否則,進入我們fallback指定的本地方法,我們搞的是500 系統出錯,稍後重試,有效的解決雪崩效應,以及返回給用戶界面很好的報錯提示信息;
microservice-student-consumer-80項目也要對應的加個方法
/**
* 測試Hystrix服務降級
* @return
*/
@GetMapping(value="/hystrix")
@ResponseBody
public Map<String,Object> hystrix(){
return restTemplate.getForObject(SERVER_IP_PORT+"/student/hystrix/", Map.class);
}
然後我們來測試下
先啓動三個eureka,再啓動帶hystrix的provider,最後啓動普通的consumer;
瀏覽器:http://localhost/student/hystrix
返回:
因爲 Hystrix默認1算超時,所以 sleep了2秒 所以進入自定義fallback方法,防止服務雪崩;
我們這裏將那個休眠註銷就行
Hystrix默認超時時間設置
Hystrix默認超時時間是1秒,我們可以通過hystrix源碼看到,
找到 hystrix-core.jar com.netflix.hystrix包下的HystrixCommandProperties類
default_executionTimeoutInMilliseconds屬性局勢默認的超時時間
默認1000毫秒 1秒
我們系統裏假如要自定義設置hystrix的默認時間的話;
application.yml配置文件加上
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 3000
注意:這段配置idea居然沒有提示功能,我比較鬱悶;
測試OK
Hystrix服務監控Dashboard
Hystrix服務監控Dashboard儀表盤
Hystrix提供了 準實時的服務調用監控項目Dashboard,能夠實時記錄通過Hystrix發起的請求執行情況,
可以通過圖表的形式展現給用戶看。
我們新建項目:microservice-student-consumer-hystrix-dashboard-90
加依賴:
<!--Hystrix服務監控Dashboard依賴-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
application.yml配置
server:
port: 90
context-path: /
新建啓動類:StudentConsumerDashBoardApplication_90
加註解:@EnableHystrixDashboard
package com.hu.microservicestudentconsumerhystrixdashboard90;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
@EnableHystrixDashboard
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
public class MicroserviceStudentConsumerHystrixDashboard90Application {
public static void main(String[] args) {
SpringApplication.run(MicroserviceStudentConsumerHystrixDashboard90Application.class, args);
}
}
這樣就完事了。
我們啓動這個項目;
然後瀏覽器輸入:http://localhost:90/hystrix
出現這個 就說明OK;
然後我們來測試下;
我們啓動三個eureka,然後再啓動microservice-student-provider-hystrix-1004
我們直接請求http://localhost:1004/student/hystrix
返回正常業務
我們監控的話,http://localhost:1004/hystrix.stream 這個路徑即可;
一直是ping,然後data返回數據;
用圖形化的話
指標含義:
各種情況: