一、概述
1容握、分布式面臨的問(wèn)題
?? 復(fù)雜分布式體系結(jié)構(gòu)中的應(yīng)用程序有數(shù)十個(gè)依賴關(guān)系宣脉,每個(gè)依賴關(guān)系在某些時(shí)候?qū)⒉豢杀苊獾厥 ?/strong>
2、服務(wù)雪崩
?? 多個(gè)微服務(wù)之間調(diào)用的時(shí)候剔氏,假設(shè)微服務(wù)A調(diào)用微服務(wù)B和微服務(wù)C塑猖,微服務(wù)B和微服務(wù)C又調(diào)用其它的微服務(wù)竹祷,這就是所謂的“扇出”。如果扇出的鏈路上某個(gè)微服務(wù)的調(diào)用響應(yīng)時(shí)間過(guò)長(zhǎng)或者不可用羊苟,對(duì)微服務(wù)A的調(diào)用就會(huì)占用越來(lái)越多的系統(tǒng)資源塑陵,進(jìn)而引起系統(tǒng)崩潰,所謂的“雪崩效應(yīng)”蜡励。
?? 對(duì)于高流量的應(yīng)用來(lái)說(shuō)令花,單一的后端依賴可能會(huì)導(dǎo)致所有服務(wù)器上的所有資源都在幾秒鐘內(nèi)飽和。比失敗更糟糕的是凉倚,這些應(yīng)用程序還可能導(dǎo)致服務(wù)之間的延遲增加兼都,備份隊(duì)列,線程和其他系統(tǒng)資源緊張占遥,導(dǎo)致整個(gè)系統(tǒng)發(fā)生更多的級(jí)聯(lián)故障俯抖。這些都表示需要對(duì)故障和延遲進(jìn)行隔離和管理,以便單個(gè)依賴關(guān)系的失敗瓦胎,不能取消整個(gè)應(yīng)用程序或系統(tǒng)芬萍。
備注:一般情況對(duì)于服務(wù)依賴的保護(hù)主要有3中解決方案:
?(1)熔斷模式:這種模式主要是參考電路熔斷,如果一條線路電壓過(guò)高搔啊,保險(xiǎn)絲會(huì)熔斷柬祠,防止火災(zāi)。放到我們的系統(tǒng)中负芋,如果某個(gè)目標(biāo)服務(wù)調(diào)用慢或者有大量超時(shí)漫蛔,此時(shí),熔斷該服務(wù)的調(diào)用旧蛾,對(duì)于后續(xù)調(diào)用請(qǐng)求莽龟,不在繼續(xù)調(diào)用目標(biāo)服務(wù),直接返回锨天,快速釋放資源毯盈。如果目標(biāo)服務(wù)情況好轉(zhuǎn)則恢復(fù)調(diào)用。
? (2)隔離模式:這種模式就像對(duì)系統(tǒng)請(qǐng)求按類型劃分成一個(gè)個(gè)小島的一樣病袄,當(dāng)某個(gè)小島被火少光了搂赋,不會(huì)影響到其他的小島。例如可以對(duì)不同類型的請(qǐng)求使用線程池來(lái)資源隔離益缠,每種類型的請(qǐng)求互不影響脑奠,如果一種類型的請(qǐng)求線程資源耗盡,則對(duì)后續(xù)的該類型請(qǐng)求直接返回幅慌,不再調(diào)用后續(xù)資源宋欺。這種模式使用場(chǎng)景非常多,例如將一個(gè)服務(wù)拆開(kāi),對(duì)于重要的服務(wù)使用單獨(dú)服務(wù)器來(lái)部署迄靠,再或者公司最近推廣的多中心秒咨。
? (3)限流模式:上述的熔斷模式和隔離模式都屬于出錯(cuò)后的容錯(cuò)處理機(jī)制喇辽,而限流模式則可以稱為預(yù)防模式掌挚。限流模式主要是提前對(duì)各個(gè)類型的請(qǐng)求設(shè)置最高的QPS閾值,若高于設(shè)置的閾值則對(duì)該請(qǐng)求直接返回菩咨,不再調(diào)用后續(xù)資源吠式。這種模式不能解決服務(wù)依賴的問(wèn)題,只能解決系統(tǒng)整體資源分配問(wèn)題抽米,因?yàn)闆](méi)有被限流的請(qǐng)求依然有可能造成雪崩效應(yīng)特占。
3、是什么云茸?
?? Hystrix是一個(gè)用于處理分布式系統(tǒng)的延遲和容錯(cuò)的開(kāi)源庫(kù)是目,在分布式系統(tǒng)里,許多依賴不可避免的會(huì)調(diào)用失敗标捺,比如超時(shí)懊纳、異常等,Hystrix能夠保證在一個(gè)依賴出問(wèn)題的情況下亡容,不會(huì)導(dǎo)致整體服務(wù)失敗嗤疯,避免級(jí)聯(lián)故障,以提高分布式系統(tǒng)的彈性闺兢。
?? “斷路器”本身是一種開(kāi)關(guān)裝置茂缚,當(dāng)某個(gè)服務(wù)單元發(fā)生故障之后,通過(guò)斷路器的故障監(jiān)控(類似熔斷保險(xiǎn)絲)屋谭,向調(diào)用方返回一個(gè)符合預(yù)期的脚囊、可處理的備選響應(yīng)(FallBack),而不是長(zhǎng)時(shí)間的等待或者拋出調(diào)用方無(wú)法處理的異常桐磁,這樣就保證了服務(wù)調(diào)用方的線程不會(huì)被長(zhǎng)時(shí)間悔耘、不必要地占用,從而避免了故障在分布式系統(tǒng)中的蔓延所意,乃至雪崩淮逊。
4、能干嗎扶踊?
(1)泄鹏、服務(wù)降級(jí):
?? 備注:Hystrix服務(wù)降級(jí),其實(shí)就是線程池中單個(gè)線程障處理秧耗,防止單個(gè)線程請(qǐng)求時(shí)間太長(zhǎng)备籽,導(dǎo)致資源長(zhǎng)期被占有而得不到釋放,從而導(dǎo)致線程池被快速占用完,導(dǎo)致服務(wù)崩潰车猬。
Hystrix能解決如下問(wèn)題:
?? a霉猛、請(qǐng)求超時(shí)降級(jí),線程資源不足降級(jí)珠闰,降級(jí)之后可以返回自定義數(shù)據(jù)惜浅;
?? b、線程池隔離降級(jí)伏嗜,分布式服務(wù)可以針對(duì)不同的服務(wù)使用不同的線程池坛悉,從而互不影響;
?? c承绸、自動(dòng)觸發(fā)降級(jí)與恢復(fù)裸影;
?? d、實(shí)現(xiàn)請(qǐng)求緩存和請(qǐng)求合并军熏;
(2)轩猩、服務(wù)熔斷:
?? 備注:熔斷模式:這種模式主要是參考電路熔斷,如果一條線路電壓過(guò)高荡澎,保險(xiǎn)絲會(huì)熔斷均践,防止火災(zāi)。放到我們的系統(tǒng)中衔瓮,如果某個(gè)目標(biāo)服務(wù)調(diào)用慢或者有大量超時(shí)浊猾,此時(shí),熔斷該服務(wù)的調(diào)用热鞍,對(duì)于后續(xù)調(diào)用請(qǐng)求葫慎,不在繼續(xù)調(diào)用目標(biāo)服務(wù),直接返回薇宠,快速釋放資源偷办。如果目標(biāo)服務(wù)情況好轉(zhuǎn)則恢復(fù)調(diào)用。
(3)澄港、服務(wù)限流:
?? 備注:限流模式主要是提前對(duì)各個(gè)類型的請(qǐng)求設(shè)置最高的QPS閾值椒涯,若高于設(shè)置的閾值則對(duì)該請(qǐng)求直接返回,不再調(diào)用后續(xù)資源回梧。這種模式不能解決服務(wù)依賴的問(wèn)題废岂,只能解決系統(tǒng)整體資源分配問(wèn)題,因?yàn)闆](méi)有被限流的請(qǐng)求依然有可能造成雪崩效應(yīng)狱意。
官網(wǎng)資料:https://github.com/Netflix/Hystrix/wiki |
---|
二湖苞、服務(wù)熔斷
1、是什么详囤?
? 熔斷機(jī)制十應(yīng)對(duì)雪崩效應(yīng)的一種微服務(wù)鏈路保護(hù)機(jī)制财骨。
?當(dāng)扇形出鏈路的某個(gè)微服務(wù)不可用或者相應(yīng)時(shí)間太長(zhǎng)時(shí),會(huì)進(jìn)行服務(wù)的降級(jí),進(jìn)而熔斷該節(jié)點(diǎn)微服務(wù)的調(diào)用隆箩,快速返回“錯(cuò)誤”的相應(yīng)信息该贾。當(dāng)檢測(cè)到該節(jié)點(diǎn)微服務(wù)調(diào)用響應(yīng)正常后恢復(fù)調(diào)用鏈路。
?? Hystrix會(huì)監(jiān)控微服務(wù)間調(diào)用的狀況捌臊,當(dāng)失敗的調(diào)用到一定閾值杨蛋,缺省是5秒內(nèi)20次調(diào)用失敗就會(huì)啟動(dòng)熔斷機(jī)制。熔斷機(jī)制的注解是@HystrixCommand娃属。
?? 一般是某個(gè)服務(wù)故障或者異常引起六荒,類似現(xiàn)實(shí)世界中的“保險(xiǎn)絲”护姆,當(dāng)某個(gè)異常條件被觸發(fā)矾端,直接熔斷整個(gè)服務(wù),而不是一直等到此服務(wù)超時(shí)卵皂。
2秩铆、參考microservicecloud-provider-dept-8001新建microservicecloud-provider-dept-hystrix-8001
(1)、pom.xml文件
- 修改內(nèi)容
<!-- 引入hystrix -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
- 全部?jī)?nèi)容
<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>
<parent>
<groupId>cn.smilexl.springcloud</groupId>
<artifactId>microservicecloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>microservicecloud-provider-dept-hystrix-8001</artifactId>
<dependencies>
<!-- 引入自己定義的api通用包灯变,可以使用Dept部門Entity -->
<dependency>
<groupId>cn.smilexl.springcloud</groupId>
<artifactId>microservicecloud-api</artifactId>
<version>${project.version}</version>
</dependency>
<!-- 引入hystrix -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!-- 將微服務(wù)provider注冊(cè)進(jìn)eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- actuator監(jiān)控信息完善 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 修改后立即生效殴玛,熱部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>
(2)、application.yml文件
server:
port: 8001
mybatis:
# config-location: classpath:mybatis/mybatis.cfg.xml # mybatis配置文件所在路徑
type-aliases-package: cn.smilexl.springcloud.entities # 所有Entity別名類所在包
mapper-locations: classpath:mapper/**/*.xml # mapper映射文件
spring:
application:
name: microservicecloud-dept # 微服務(wù)名
datasource:
type: com.alibaba.druid.pool.DruidDataSource # 當(dāng)前數(shù)據(jù)源操作類型
driver-class-name: org.gjt.mm.mysql.Driver # mysql驅(qū)動(dòng)包
url: jdbc:mysql://47.99.218.123:3306/clouddb01 # 數(shù)據(jù)庫(kù)名稱
username: root
password: 123456
dbcp2:
min-idle: 5 # 數(shù)據(jù)庫(kù)連接池的最小維持連接數(shù)
initial-size: 5 # 初始化連接數(shù)
max-total: 5 # 最大連接數(shù)
max-wait-millis: 200 # 等待連接獲取的最大超時(shí)時(shí)間
eureka:
client: #客戶端注冊(cè)進(jìn)eureka服務(wù)列表內(nèi)
service-url:
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka
instance:
instance-id: microservicecloud-provider-dept-hystrix-8001 #自定義服務(wù)名稱信息
prefer-ip-address: true #訪問(wèn)路徑可以顯示IP地址
#點(diǎn)擊超鏈接后顯示信息
info:
app.name: microservicecloud
company.name: www.smilexl.cn
build.artifactId: $project.artifactId$
build.version: $project.version$
將服務(wù)名修改為: instance-id: microservicecloud-provider-dept-hystrix-8001 #自定義服務(wù)名稱信息
(3)添祸、將啟動(dòng)類修改為:DeptProvider8001_hystrix_App.java
添加 @EnableCircuitBreaker 注解滚粟,具體代碼如下:
package cn.smilexl.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
@SpringBootApplication
@EnableCircuitBreaker //對(duì)hystrix熔斷機(jī)制的支持
public class DeptProvider8001_hystrix_App {
public static void main(String[] args) {
SpringApplication.run(DeptProvider8001_hystrix_App.class, args);
}
}
(4)、修改 DeptController.java
對(duì)于 @HystrixCommand 報(bào)異常后如何處理刃泌,如下所示:
一旦調(diào)用服務(wù)方法失敗并拋出了錯(cuò)誤信息后凡壤,會(huì)自動(dòng)調(diào)用 @HystrixCommand標(biāo)準(zhǔn)好的fallbackMethod調(diào)用類中的指定方法
代碼內(nèi)容如下:
package cn.smilexl.springcloud.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import cn.smilexl.springcloud.entities.Dept;
import cn.smilexl.springcloud.service.DeptService;
@RestController
public class DeptController {
@Autowired
private DeptService deptService;
// 添加
@PostMapping("/dept/add")
public Boolean addDept(Dept dept) {
return deptService.addDept(dept);
}
// 查詢?nèi)? @GetMapping("/dept/list")
public List<Dept> findAll() {
return deptService.findAll();
}
// 根據(jù)id查詢
@GetMapping("/dept/get/{id}")
//一旦調(diào)用服務(wù)方法失敗并拋出了錯(cuò)誤信息后,會(huì)自動(dòng)調(diào)用 @HystrixCommand標(biāo)準(zhǔn)好的fallbackMethod調(diào)用類中的指定方法
@HystrixCommand(fallbackMethod="processHystrix_Get")
public Dept findById(@PathVariable("id") Long id) {
Dept dept = deptService.findById(id);
if(dept == null) {
throw new RuntimeException("該ID"+id+"沒(méi)有對(duì)應(yīng)的信息");
}
return dept;
}
//處理@HystrixCommand對(duì)應(yīng)的錯(cuò)誤信息
public Dept processHystrix_Get(@PathVariable("id") Long id) {
return new Dept().setDeptno(id)
.setDname("該ID"+id+"沒(méi)有對(duì)應(yīng)的信息耙替,null---@HystrixCommand")
.setDb_source("no this datasource in Mysql");
}
}
(5)亚侠、測(cè)試
(1)、先啟動(dòng)3個(gè) eureka俗扇;
當(dāng)查詢的 id 數(shù)據(jù)庫(kù)中不存在次 id 信息時(shí):
(2)硝烂、在啟動(dòng)主啟動(dòng)類 microservicecloud-provider-dept-hystrix-8001
(3)、最后啟動(dòng)consumer類 microservicecloud-consumer-dept-80
3铜幽、存在的問(wèn)題
- 每對(duì)應(yīng)一個(gè)方法滞谢,都會(huì)對(duì)應(yīng)一個(gè)兜底的異常處理的方法,容易造成方法膨脹除抛;
- 處理異常和業(yè)務(wù)邏輯綁定再一起高耦合狮杨,對(duì)程序的維護(hù)性、健壯性镶殷、美觀性都不好禾酱;
解決辦法,采用服務(wù)降級(jí),對(duì)接口進(jìn)行處理颤陶。
三颗管、服務(wù)降級(jí)
?? 所謂降級(jí),一般是從整個(gè)負(fù)荷考慮滓走。就是當(dāng)某個(gè)服務(wù)熔斷之后垦江,服務(wù)器將不再被調(diào)用;此時(shí)搅方,客戶端可以自己準(zhǔn)備一個(gè)本地的fallback返回比吭,返回一個(gè)缺省值。這樣做姨涡,雖然服務(wù)水平下降衩藤,但好歹可用,比直接掛掉要強(qiáng)涛漂。
1赏表、是什么?
?整體資源快不夠了匈仗,忍痛將某些服務(wù)先關(guān)掉瓢剿,待渡過(guò)難關(guān),再開(kāi)啟回來(lái)悠轩。
?? 服務(wù)降級(jí)處理是在客戶端實(shí)現(xiàn)完成的间狂,與服務(wù)端沒(méi)有關(guān)系。
2火架、修改microservicecloud-api工程
(1)鉴象、根據(jù)已經(jīng)有的 DeptClientService.java 接口,新建一個(gè)實(shí)現(xiàn)了 FallbackFactory接口的類DeptClientServiceFallbackFactory
具體代碼如下:
package cn.smilexl.springcloud.service;
import java.util.List;
import org.springframework.stereotype.Component;
import cn.smilexl.springcloud.entities.Dept;
import feign.hystrix.FallbackFactory;
@Component //交給spring;不要忘了 ,不要忘了
public class DeptClientServiceFallbackFactory implements FallbackFactory<DeptClientService>{
@Override
public DeptClientService create(Throwable arg0) {
return new DeptClientService() {
@Override
public Dept findById(Long id) {
return new Dept().setDeptno(id)
.setDname("該ID:"+id+"沒(méi)有沒(méi)有對(duì)應(yīng)的信息,Consumer客戶端提供的降級(jí)信息,此刻服務(wù)Provider已經(jīng)關(guān)閉")
.setDb_source("no this database in MySQL");
}
@Override
public Boolean addDept(Dept dept) {
return null;
}
@Override
public List<Dept> findAll() {
return null;
}
};
}
}
(2)距潘、在DeptClientService接口上的注解@FeignClient中添加 fallbackFactory 屬性值
具體代碼如下:
package cn.smilexl.springcloud.service;
import java.util.List;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import cn.smilexl.springcloud.entities.Dept;
//@FeignClient(value="MICROSERVICECLOUD-DEPT")
@FeignClient(value="MICROSERVICECLOUD-DEPT", fallbackFactory=DeptClientServiceFallbackFactory.class)
public interface DeptClientService {
// 根據(jù)id查詢
@GetMapping("/dept/get/{id}")
public Dept findById(@PathVariable("id") Long id);
// 添加
@GetMapping("/dept/add")
public Boolean addDept(Dept dept);
// 根據(jù)id查詢
@GetMapping("/dept/list")
public List<Dept> findAll();
}
修改完成之后進(jìn)行: mvn clean install
3炼列、修改 microservicecloud-consumer-dept-feign 工程的 yml 配置文件
? application.yml文件修改如下:
server:
port: 80
feign:
hystrix:
enabled: true
eureka:
client:
register-with-eureka: false
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
4、測(cè)試
(1)音比、先啟動(dòng)3個(gè)eureka
(6)、再次訪問(wèn) http:// localhost/consumer/dept/get/1骚亿,客戶端自己調(diào)用提示
(2)俭尖、在啟動(dòng)微服務(wù):microservicecloud-provider-dept-8001
(3)、最后啟動(dòng)微服務(wù):microservicecloud-consumer-dept-feign
(4)洞翩、正常訪問(wèn):http:// localhost/consumer/dept/get/1
(5)稽犁、故意關(guān)掉微服務(wù):microservicecloud-provider-dept-8001
- 此時(shí)服務(wù)端privoder已經(jīng)down了已亥,但是我們做了服務(wù)降級(jí)處理,讓客戶端不可用時(shí)也會(huì)獲得提示信息而不會(huì)掛起耗死服務(wù)器来屠。
5虑椎、小結(jié)
【服務(wù)熔斷】
?? 一般是某個(gè)服務(wù)故障或者異常引起震鹉,類似現(xiàn)實(shí)世界中的“保險(xiǎn)絲”,當(dāng)某個(gè)異常條件被觸發(fā)捆姜,直接熔斷整個(gè)服務(wù)传趾,而不是一直等到此服務(wù)超時(shí)導(dǎo)致系統(tǒng)資源大面積的被占用。
【服務(wù)降級(jí)】
?? 所謂降級(jí)泥技,一般是從整體負(fù)荷考慮浆兰。就是某個(gè)服務(wù)熔斷之后,服務(wù)器將不再被調(diào)用珊豹,此時(shí)客戶端可以自己準(zhǔn)備一個(gè)本地的 fallback 回調(diào)簸呈,返回一個(gè)缺省值病蛉。這樣做蒿涎,雖然服務(wù)水平下降,但好歹可用塑荒,比直接掛掉要強(qiáng)忽妒。
四玩裙、服務(wù)監(jiān)控 hystrixDashboard
1、概述
? 除了隔離依賴服務(wù)的調(diào)用以外段直,Hystrix還提供了準(zhǔn)實(shí)時(shí)的調(diào)用監(jiān)控(Hystrix Dashboard),Hystrix會(huì)持續(xù)的記錄所有通過(guò)Hystrix發(fā)起的請(qǐng)求的執(zhí)行信息,并以統(tǒng)計(jì)報(bào)表和圖形的形式展現(xiàn)給用戶溶诞,包括每秒執(zhí)行多少次請(qǐng)求鸯檬、多少次成功、多少次失敗等螺垢,Netfilx通過(guò) hystrix-metrics-event-stream 項(xiàng)目實(shí)現(xiàn)了對(duì)以上指示的監(jiān)控喧务。Spring Cloud 也提供了 Hystrix Dashboard 整合,對(duì)監(jiān)控內(nèi)容轉(zhuǎn)化成可視化界面枉圃。
2功茴、配置步驟
(1)、新建工程 microservicecloud-consumer-hystrix-dashboard
(2)孽亲、修改工程pom文件
- pom.xml修改內(nèi)容
<!-- 引入hystrix和 hystrixDashboard相關(guān) -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
- pom.xml全部?jī)?nèi)容
<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>
<parent>
<groupId>cn.smilexl.springcloud</groupId>
<artifactId>microservicecloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>microservicecloud-consumer-hystrix-dashboard</artifactId>
<dependencies>
<dependency><!-- 自己定義的api -->
<groupId>cn.smilexl.springcloud</groupId>
<artifactId>microservicecloud-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Ribbon相關(guān) -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<!-- feign相關(guān) -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- 引入hystrix和 hystrixDashboard相關(guān) -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 修改后立即生效坎穿,熱部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>
(3)、修改工程yml配置文件
- application.yml文件
server:
port: 9001
eureka:
client:
registerWithEureka: false
fetchRegistry: false
(4)返劲、新建主啟動(dòng)類 DeptConsumer_HystrixDashboard_App.java 并添加注解@EnableHystrixDashboard
package cn.smilexl.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
@SpringBootApplication
//@EnableCircuitBreaker
@EnableHystrixDashboard //開(kāi)啟儀表盤
public class DeptConsumer_HystrixDashboard_App {
public static void main(String[] args) {
SpringApplication.run(DeptConsumer_HystrixDashboard_App.class, args);
}
}
(5)玲昧、所有provider微服務(wù)提供者(8001/8002/8003)都需要監(jiān)控依賴配置
<!-- actuator監(jiān)控信息完善 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
(6)、在工程microservicecloud-provider-dept-hystrix-8001主啟動(dòng)類中添加服務(wù)監(jiān)控配置
注意:如果沒(méi)有此配置下面打開(kāi)的
http:// localhost:9001/hystrix
中查看的監(jiān)控?cái)?shù)據(jù)將會(huì)連接不上篮绿,打開(kāi)的http:// localhost:8001/hystrix.stream
將會(huì)報(bào)404孵延。
package cn.smilexl.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.context.annotation.Bean;
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
@SpringBootApplication
//@EnableDiscoveryClient //服務(wù)發(fā)現(xiàn)
@EnableCircuitBreaker //對(duì)hystrix熔斷機(jī)制的支持
public class DeptProvider8001_hystrix_App {
public static void main(String[] args) {
SpringApplication.run(DeptProvider8001_hystrix_App.class, args);
}
// 此配置是為了服務(wù)監(jiān)控而配置,與服務(wù)容錯(cuò)本身無(wú)關(guān)亲配,
// ServletRegistrationBean因?yàn)閟pringboot的默認(rèn)路徑不是"/hystrix.stream"尘应,
// 只要在自己的項(xiàng)目里配置上下面的servlet就可以了
@Bean
public ServletRegistrationBean<HystrixMetricsStreamServlet> getServlet() {
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean<HystrixMetricsStreamServlet> registrationBean = new ServletRegistrationBean<>(streamServlet);
registrationBean.setLoadOnStartup(1);//系統(tǒng)啟動(dòng)時(shí)加載順序
registrationBean.addUrlMappings("/hystrix.stream");//路徑
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
}
(7)惶凝、啟動(dòng)相關(guān)工程
- 先啟動(dòng)3個(gè)eureka集群
- 在啟動(dòng):microservicecloud-provider-dept-hystrix-8001
- 接著啟動(dòng):microservicecloud-consumer-hystrix-dashboard
- 先測(cè)試數(shù)據(jù)8001是否以通:http://localhost:8001/dept/get/1
- 打開(kāi)儀表盤界面:http://localhost:9001/hystrix
- 檢查數(shù)據(jù)是否以通:http://localhost:8001/hystrix.stream
(8)、監(jiān)控測(cè)試
- 多次刷新 http://localhost:8001/dept/get/1
- 觀察檢測(cè)窗口
?? |--- 填寫(xiě)監(jiān)控地址
?? |--- 監(jiān)控結(jié)果
?? |--- 如何看犬钢?
?? ?? |--- 7色
?? ?? |--- 1圈:實(shí)心圓梨睁,共有兩種含義。它通過(guò)顏色的變化代表了實(shí)例的健康程度娜饵,它的健康度:綠色<黃色<橙色<紅色遞減坡贺。
?? ???? ?? 該實(shí)心圓除了顏色的變化之外,它的大小也會(huì)根據(jù)實(shí)例的請(qǐng)求流量發(fā)生變化箱舞,流量越大該實(shí)心圓就越大遍坟。所以通過(guò)該實(shí)心圓的展示,就可以在大量的實(shí)例中快速的發(fā)現(xiàn)**故障實(shí)例和高壓實(shí)例晴股。
?? ?? |--- 1線:曲線用來(lái)記錄2分鐘內(nèi)流量的相對(duì)變化愿伴,可以通過(guò)它來(lái)觀察到流量的上升和下降趨勢(shì)。
?? ?? |--- 整圖說(shuō)明:
?? |--- 搞懂一個(gè)才能看懂復(fù)雜的: