本系列對(duì)應(yīng)的是尚硅谷周陽(yáng)Spring Cloud的思維導(dǎo)圖整理的筆記澎办,用來方便自己后面的知識(shí)點(diǎn)回顧嘲碱。分別以每個(gè)知識(shí)點(diǎn)作為一篇文章詳細(xì)講述。
知識(shí)點(diǎn)傳送門:
項(xiàng)目源碼
- SpringCloud01:微服務(wù)概述與SpringCloud
- SpringCloud02:Rest微服務(wù)構(gòu)建案例工程模塊
- SpringCloud03:Eureka服務(wù)注冊(cè)與發(fā)現(xiàn)
- SpringCloud04:Ribbon負(fù)載均衡
- SpringCloud05:Feign負(fù)載均衡
- SpringCloud06:Hystrix斷路器
- SpringCloud07:zuul路由網(wǎng)關(guān)
- SpringCloud08:SpringCloud Config分布式配置中心
一局蚀、概述
1.分布式系統(tǒng)面臨的問題
分布式系統(tǒng)面臨的問題
復(fù)雜分布式體系結(jié)構(gòu)中的應(yīng)用程序有數(shù)十個(gè)依賴關(guān)系麦锯,每個(gè)依賴關(guān)系在某些時(shí)候?qū)⒉豢杀苊獾厥 ?/strong>
服務(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í)間過長(zhǎng)或者不可用千扶,對(duì)微服務(wù)A的調(diào)用就會(huì)占用越來越多的系統(tǒng)資源料祠,進(jìn)而引起系統(tǒng)崩潰,所謂的“雪崩效應(yīng)”.
對(duì)于高流量的應(yīng)用來說澎羞,單一的后端依賴可能會(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)巷帝。
2.Hystirx是什么
Hystrix是一個(gè)用于處理分布式系統(tǒng)的延遲和容錯(cuò)的開源庫(kù),在分布式系統(tǒng)里扫夜,許多依賴不可避免的會(huì)調(diào)用失敗楞泼,比如超時(shí)、異常等笤闯,Hystrix能夠保證在一個(gè)依賴出問題的情況下堕阔,不會(huì)導(dǎo)致整體服務(wù)失敗,避免級(jí)聯(lián)故障颗味,以提高分布式系統(tǒng)的彈性超陆。
“斷路器”本身是一種開關(guān)裝置,當(dāng)某個(gè)服務(wù)單元發(fā)生故障之后浦马,通過斷路器的故障監(jiān)控(類似熔斷保險(xiǎn)絲)时呀,向調(diào)用方返回一個(gè)符合預(yù)期的、可處理的備選響應(yīng)(FallBack)晶默,而不是長(zhǎng)時(shí)間的等待或者拋出調(diào)用方無法處理的異常谨娜,這樣就保證了服務(wù)調(diào)用方的線程不會(huì)被長(zhǎng)時(shí)間、不必要地占用磺陡,從而避免了故障在分布式系統(tǒng)中的蔓延趴梢,乃至雪崩漠畜。
3.Hystrix能干嘛
- 服務(wù)降級(jí)
- 服務(wù)熔斷
- 服務(wù)限流
- 接近實(shí)時(shí)的監(jiān)控。坞靶。憔狞。
二、服務(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)用鏈路。在SpringCloud框架里熔斷機(jī)制通過Hystrix實(shí)現(xiàn)妻味。Hystrix會(huì)監(jiān)控微服務(wù)間調(diào)用的狀況正压,當(dāng)失敗的調(diào)用到一定閾值,缺省是5秒內(nèi)20次調(diào)用失敗就會(huì)啟動(dòng)熔斷機(jī)制责球。熔斷機(jī)制的注解是@HystrixCommand焦履。
2.參考microservicecloud-provider-dept-8001,新建microservicecloud-provider-dept-hystrix-8001
3.修改POM文件
<!-- hystrix -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
4.YML文件
server:
port: 8001
mybatis:
config-location: classpath:mybatis/mybatis.cfg.xml #mybatis所在路徑
type-aliases-package: com.atguigu.springcloud.entities #entity別名類
mapper-locations:
- classpath:mybatis/mapper/**/*.xml #mapper映射文件
spring:
application:
name: microservicecloud-dept
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: org.gjt.mm.mysql.Driver
url: jdbc:mysql://localhost:3306/cloudDB01
username: root
password: 123456
dbcp2:
min-idle: 5
initial-size: 5
max-total: 5
max-wait-millis: 200
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-dept8001-hystrix #自定義服務(wù)名稱信息
prefer-ip-address: true #訪問路徑可以顯示IP地址
info:
app.name: atguigu-microservicecloud
company.name: www.atguigu.com
build.artifactId: $project.artifactId$
build.version: $project.version$
5.修改DeptController
@HystrixCommand報(bào)異常后如何處理
一旦調(diào)用服務(wù)方法失敗并拋出了錯(cuò)誤信息后雏逾,會(huì)自動(dòng)調(diào)用@HystrixCommand標(biāo)注好的fallbackMethod調(diào)用類中的指定方法
package com.atguigu.springcloud.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.atguigu.springcloud.entities.Dept;
import com.atguigu.springcloud.service.DeptService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
@RestController
public class DeptController
{
@Autowired
private DeptService service = null;
@RequestMapping(value="/dept/get/{id}",method=RequestMethod.GET)
@HystrixCommand(fallbackMethod = "processHystrix_Get")
public Dept get(@PathVariable("id") Long id)
{
Dept dept = this.service.get(id);
if(null == dept)
{
throw new RuntimeException("該ID:"+id+"沒有沒有對(duì)應(yīng)的信息");
}
return dept;
}
public Dept processHystrix_Get(@PathVariable("id") Long id)
{
return new Dept().setDeptno(id)
.setDname("該ID:"+id+"沒有沒有對(duì)應(yīng)的信息,null--@HystrixCommand")
.setDb_source("no this database in MySQL");
}
}
6.修改主啟動(dòng)類DeptProvider8001_Hystrix_App并添加新注解@EnableCircuitBreaker
package com.atguigu.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient //本服務(wù)啟動(dòng)后會(huì)自動(dòng)注冊(cè)進(jìn)eureka服務(wù)中
@EnableCircuitBreaker//對(duì)hystrixR熔斷機(jī)制的支持
public class DeptProvider8001_Hystrix_App
{
public static void main(String[] args)
{
SpringApplication.run(DeptProvider8001_Hystrix_App.class, args);
}
}
7.測(cè)試
- 3個(gè)eureka先啟動(dòng)
- 主啟動(dòng)類DeptProvider8001_Hystrix_App
- Consumer啟動(dòng)microservicecloud-consumer-dept-80
-
http://localhost/consumer/dept/get/112
如果對(duì)應(yīng)的ID:112嘉裤,數(shù)據(jù)庫(kù)里面沒有這個(gè)記錄,我們報(bào)錯(cuò)后統(tǒng)一返回栖博。
三屑宠、服務(wù)降級(jí)
1.是什么
整體資源快不夠了,忍痛將某些服務(wù)先關(guān)掉仇让,待渡過難關(guān)典奉,再開啟回來。
服務(wù)降級(jí)處理是在客戶端實(shí)現(xiàn)完成的丧叽,與服務(wù)端沒有關(guān)系
2.修改microservicecloud-api工程卫玖,
根據(jù)已經(jīng)有的DeptClientService接口新建一個(gè)實(shí)現(xiàn)了
FallbackFactory接口的類DeptClientServiceFallbackFactory
千萬(wàn)不要忘記在類上面新增@Component注解,大坑S淮尽<偎病!
package com.atguigu.springcloud.service;
import java.util.List;
import org.springframework.stereotype.Component;
import com.atguigu.springcloud.entities.Dept;
import feign.hystrix.FallbackFactory;
@Component//不要忘記添加迂尝,不要忘記添加
public class DeptClientServiceFallbackFactory implements FallbackFactory<DeptClientService>
{
@Override
public DeptClientService create(Throwable throwable)
{
return new DeptClientService() {
@Override
public Dept get(long id)
{
return new Dept().setDeptno(id)
.setDname("該ID:"+id+"沒有沒有對(duì)應(yīng)的信息,Consumer客戶端提供的降級(jí)信息,此刻服務(wù)Provider已經(jīng)關(guān)閉")
.setDb_source("no this database in MySQL");
}
@Override
public List<Dept> list()
{
return null;
}
@Override
public boolean add(Dept dept)
{
return false;
}
};
}
}
3.修改microservicecloud-api工程脱茉,DeptClientService接口在注解@FeignClient中添加fallbackFactory屬性值
package com.atguigu.springcloud.service;
import java.util.List;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.atguigu.springcloud.entities.Dept;
@FeignClient(value = "MICROSERVICECLOUD-DEPT",fallbackFactory=DeptClientServiceFallbackFactory.class)
public interface DeptClientService
{
@RequestMapping(value = "/dept/get/{id}",method = RequestMethod.GET)
public Dept get(@PathVariable("id") long id);
@RequestMapping(value = "/dept/list",method = RequestMethod.GET)
public List<Dept> list();
@RequestMapping(value = "/dept/add",method = RequestMethod.POST)
public boolean add(Dept dept);
}
4.microservicecloud-api工程
mvn clean install
5.microservicecloud-consumer-dept-feign工程修改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/
6.測(cè)試
- 3個(gè)eureka先啟動(dòng)
- 微服務(wù)microservicecloud-provider-dept-8001啟動(dòng)
- microservicecloud-consumer-dept-feign啟動(dòng)
- 正常訪問測(cè)試 http://localhost/consumer/dept/get/1
- 故意關(guān)閉微服務(wù)microservicecloud-provider-dept-8001
- 客戶端自己調(diào)用提示 http://localhost/consumer/dept/get/1
此時(shí)服務(wù)端provider已經(jīng)down了,但是我們做了服務(wù)降級(jí)處理雹舀,讓客戶端在服務(wù)端不可用時(shí)也會(huì)獲得提示信息而不會(huì)掛起耗死服務(wù)器
四芦劣、服務(wù)監(jiān)控hystrixDashboard
1.概述
除了隔離依賴服務(wù)的調(diào)用以外,Hystrix還提供了準(zhǔn)實(shí)時(shí)的調(diào)用監(jiān)控(Hystrix Dashboard)说榆,Hystrix會(huì)持續(xù)地記錄所有通過Hystrix發(fā)起的請(qǐng)求的執(zhí)行信息虚吟,并以統(tǒng)計(jì)報(bào)表和圖形的形式展示給用戶寸认,包括每秒執(zhí)行多少請(qǐng)求多少成功,多少失敗等串慰。Netflix通過hystrix-metrics-event-stream項(xiàng)目實(shí)現(xiàn)了對(duì)以上指標(biāo)的監(jiān)控偏塞。Spring Cloud也提供了Hystrix Dashboard的整合,對(duì)監(jiān)控內(nèi)容轉(zhuǎn)化成可視化界面邦鲫。
2.新建工程microservicecloud-consumer-hystrix-dashboard
3.修改POM文件
<!-- hystrix和 hystrix-dashboard相關(guān)-->
<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>
4.修改YML文件
server:
port: 9001
5.主啟動(dòng)類改名+新注解@EnableHystrixDashboard
package com.atguigu.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
@SpringBootApplication
@EnableHystrixDashboard
public class DeptConsumer_DashBoard_App
{
public static void main(String[] args)
{
SpringApplication.run(DeptConsumer_DashBoard_App.class,args);
}
}
6.所有Provider微服務(wù)提供類(8001/8002/8003)都需要監(jiān)控依賴配置
<!-- actuator監(jiān)控信息完善 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
7.啟動(dòng)microservicecloud-consumer-hystrix-dashboard該微服務(wù)監(jiān)控消費(fèi)端
8.啟動(dòng)3個(gè)eureka集群
9.啟動(dòng)microservicecloud-provider-dept-hystrix-8001
http://localhost:8001/dept/get/1
http://localhost:8001/hystrix.stream
10.啟動(dòng)的相關(guān)微服務(wù)工程
11.監(jiān)控測(cè)試
1)多次刷新http://localhost:8001/dept/get/1
2)觀察監(jiān)控窗口
- 填寫監(jiān)控地址
- Delay:該參數(shù)用來控制服務(wù)器上輪詢監(jiān)控信息的延遲時(shí)間灸叼,默認(rèn)為2000毫秒,可以通過配置該屬性來降低客戶端的網(wǎng)絡(luò)和CPU消耗庆捺。
- Title:該參數(shù)對(duì)應(yīng)了頭部標(biāo)題Hystrix Stream之后的內(nèi)容古今,默認(rèn)會(huì)使用具體監(jiān)控實(shí)例的URL,可以通過配置該信息來展示更合適的標(biāo)題滔以。
- 查看監(jiān)控結(jié)果
7色 1圈 1線
- 實(shí)心圓:共有兩種含義捉腥。它通過顏色的變化代表了實(shí)例的健康程度谴垫,它的健康度從綠色<黃色<橙色<紅色遞減此迅。
該實(shí)心圓除了顏色的變化之外,它的大小也會(huì)根據(jù)實(shí)例的請(qǐng)求流量發(fā)生變化贷盲,流量越大該實(shí)心圓就越大坏匪。所以通過該實(shí)心圓的展示拟逮,就可以在大量的實(shí)例中快速的發(fā)現(xiàn)故障實(shí)例和高壓力實(shí)例。 - 曲線:用來記錄2分鐘內(nèi)流量的相對(duì)變化适滓,可以通過它來觀察到流量的上升和下降趨勢(shì)敦迄。