前言
在分布式微服務(wù)的項(xiàng)目中,常常會(huì)有多個(gè)服務(wù)復(fù)用,產(chǎn)生多個(gè)服務(wù)調(diào)用的情況。比如A服務(wù)調(diào)用B服務(wù),B服務(wù)調(diào)用C服務(wù)。服務(wù)調(diào)用鏈路長(zhǎng)了必然會(huì)增加服務(wù)超時(shí)的概率,服務(wù)的超時(shí)阻塞會(huì)一直占用線程資源,大量的阻塞會(huì)直接消耗完服務(wù)線程,嚴(yán)重情況下會(huì)導(dǎo)致服務(wù)直接宕機(jī)從而引發(fā)調(diào)用鏈路的服務(wù)雪崩。那么,有沒(méi)有一種方式可以實(shí)現(xiàn)服務(wù)熔斷、降低、隔離呢?今天我們介紹Netflix公司研發(fā)的Hystrix框架。
知識(shí)積累
Hystrix是一個(gè)延遲和容錯(cuò)庫(kù),旨在隔離遠(yuǎn)程系統(tǒng)、服務(wù)和第三方庫(kù)的訪問(wèn)點(diǎn),停止級(jí)聯(lián)故障,并在故障不可避免的復(fù)雜分布式系統(tǒng)中實(shí)現(xiàn)彈性。
服務(wù)熔斷是在服務(wù)達(dá)到某些特定指標(biāo)主動(dòng)進(jìn)行斷開(kāi)的一種操作,而服務(wù)降級(jí)則是在服務(wù)調(diào)用過(guò)程中對(duì)后續(xù)服務(wù)進(jìn)行評(píng)估比如達(dá)到超時(shí)時(shí)間未響應(yīng)就會(huì)進(jìn)行后續(xù)冗余邏輯處理。服務(wù)熔斷了必定會(huì)進(jìn)行服務(wù)降級(jí),但服務(wù)降級(jí)了并不一定是服務(wù)熔斷觸發(fā)。
服務(wù)雪崩是業(yè)務(wù)流程中多個(gè)服務(wù)進(jìn)行鏈路調(diào)用,如果后續(xù)鏈路服務(wù)發(fā)生故障阻塞會(huì)影響調(diào)用方服務(wù)阻塞,嚴(yán)重情況下會(huì)直接將整個(gè)鏈路服務(wù)拖死,從而造成服務(wù)雪崩的現(xiàn)象。
Hystrix提供了信號(hào)量、線程兩種隔離模式。信號(hào)量隔離直接可以限制服務(wù)的并發(fā)線程數(shù)目,而線程隔離則直接依靠線程池來(lái)限制當(dāng)前負(fù)載直接使用當(dāng)前線程池線程,從而避免消耗完服務(wù)的其他線程造成服務(wù)不可用的情況。默認(rèn)情況下,Hystrix使用線程隔離,線程隔離才是真正意義上的服務(wù)隔離。
在實(shí)際的項(xiàng)目開(kāi)發(fā)中,Hystrix與openfeign能夠友好的集成。我們?cè)赟pringboot、spring cloud項(xiàng)目中可以直接引入Hystrix、feign依賴(lài),并且完成相應(yīng)的配置即可達(dá)到我們需要的結(jié)果。
Springboot集成Hystrix
在微服務(wù)的開(kāi)發(fā)中為了服務(wù)間的互相復(fù)用與調(diào)用,我們需要引入注冊(cè)中心來(lái)幫助實(shí)現(xiàn)服的發(fā)布、訂閱與維護(hù)。對(duì)于注冊(cè)中心中間件的選擇有nacos、consul等等,其中搭建都相對(duì)簡(jiǎn)單這里不再講述。
當(dāng)我們引入注冊(cè)中心后我們直接可以使用feign框架通過(guò)RestTemplate直接調(diào)用服務(wù),我們不再關(guān)心內(nèi)部怎么調(diào)用,只需要按照特定的規(guī)則封裝接口即可。
以下重點(diǎn)介紹Hystrix的相關(guān)集成和配置
1、maven依賴(lài)引入
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.12.RELEASE</version>
<relativePath/>
</parent>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR10</spring-cloud.version>
</properties>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2、application開(kāi)啟feign的hystrix支持(客戶端配置限流降級(jí)熔斷)
#hystrix
feign:
hystrix:
enabled: true
circuitbreaker:
enabled: true
hystrix:
threadpool:
#默認(rèn)配置
default:
#動(dòng)態(tài)調(diào)整線程數(shù)
allowMaximumSizeToDivergeFromCoreSize: false
#核心線程數(shù)
coreSize: 10
#最大線程數(shù)
maximumSize: 10
#空閑存活時(shí)間min
keepAliveTimeMinutes: 1
#隊(duì)列長(zhǎng)度 設(shè)置置為-1時(shí),隊(duì)列會(huì)使用 SynchronousQueue,此時(shí)其 size 為0,Hystrix 不會(huì)向隊(duì)列內(nèi)存放作業(yè)。
maxQueueSize: -1
#如果需要?jiǎng)討B(tài)修改隊(duì)列長(zhǎng)度的話可以設(shè)置此值,即使隊(duì)列未滿,隊(duì)列內(nèi)作業(yè)達(dá)到此值時(shí)同樣會(huì)拒絕請(qǐng)求。此值默認(rèn)是 5
queueSizeRejectionThreshold: 5
command:
default:
#線程池key
threadPoolKeyOverride: default
#熔斷器
circuitBreaker:
enabled: true
#錯(cuò)誤占比
errorThresholdPercentage: 50
#窗口時(shí)間內(nèi)最小請(qǐng)求數(shù)目
requestVolumeThreshold: 20
#休眠時(shí)間
sleepWindowInMilliseconds: 5000
execution:
#隔離
isolation:
strategy: THREAD
thread:
#線程超時(shí)時(shí)間
timeoutInMilliseconds: 5000
timeout:
enabled: true
#統(tǒng)計(jì)器
metrics:
rollingStats:
#窗口大小
timeInMilliseconds: 10000
#桶數(shù)目需要保證與timeInMilliseconds整除
numBuckets: 10
3、入口類(lèi)增加@EnableFeignClients @EnableHystrix 開(kāi)啟feign與hystrix
@EnableFeignClients
@EnableHystrix
@Slf4j
public class TestDemoApplication {
public static void main(String[] args) {
SpringApplication.run(TestDemoApplication.class, args);
}
}
4、feign調(diào)用增加降級(jí)方法
/**
* @author senfel
* @version 1.0
* @date 2023/7/03 15:04
*/
@Service
@FeignClient(value = "test-demo",fallback = FallbackService.class)
public interface TestService {
/**
* 測(cè)試hystrix
*/
@GetMapping("/feign")
String feign(@RequestParam String str);
}
/**
* FallbackService
* @author senfel
* @version 1.0
* @date 2023/7/3 15:26
*/
@Service
public class FallbackService implements TestService {
@Override
public String feign(String str) {
return ">>>>>客戶端服務(wù)降級(jí)>>>>>";
}
}
服務(wù)端配置限流降級(jí)熔斷(選擇使用)
線程隔離、
服務(wù)調(diào)用超時(shí)5s降級(jí)、
10s內(nèi)最低20個(gè)請(qǐng)求進(jìn)入規(guī)則驗(yàn)證超過(guò)50%錯(cuò)誤率直接熔斷5s
@GetMapping("/feign")
@HystrixCommand(
// 標(biāo)識(shí)線程池 保持唯一
threadPoolKey = "threadPoolKeyByFeign",
// 線程池細(xì)節(jié)屬性配置
threadPoolProperties = {
// 線程數(shù)
@HystrixProperty(name="coreSize",value = "10"),
//最大線程數(shù)量
@HystrixProperty(name="maximumSize",value = "10"),
// 等待隊(duì)列長(zhǎng)度
@HystrixProperty(name="maxQueueSize",value="-1"),
//如果需要?jiǎng)討B(tài)修改隊(duì)列長(zhǎng)度的話可以設(shè)置此值,即使隊(duì)列未滿,隊(duì)列內(nèi)作業(yè)達(dá)到此值時(shí)同樣會(huì)拒絕請(qǐng)求。此值默認(rèn)是 5
@HystrixProperty(name="queueSizeRejectionThreshold",value="5"),
//空閑時(shí)間1min
@HystrixProperty(name="keepAliveTimeMinutes",value="1")
},
// 熔斷的一些細(xì)節(jié)屬性配置
commandProperties = {
// 調(diào)用服務(wù)超時(shí)時(shí)間
@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="5000"),
// 統(tǒng)計(jì)時(shí)間窗口定義
@HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds",value = "10000"),
//桶數(shù)量 保證與統(tǒng)計(jì)時(shí)間窗口整除
@HystrixProperty(name = "metrics.rollingPercentile.numBuckets",value = "10"),
// 統(tǒng)計(jì)時(shí)間窗口內(nèi)的最小請(qǐng)求數(shù)
@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "20"),
// 統(tǒng)計(jì)時(shí)間窗口內(nèi)的錯(cuò)誤數(shù)量百分比閾值
@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "50"),
// 自我修復(fù)時(shí)的活動(dòng)窗口長(zhǎng)度
@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "5000")
},
// 降級(jí)方法
fallbackMethod = "myFallBack"
)
public String feign(String str){
if(str.contains("s")){
throw new RuntimeException();
}
return str+"ssss";
}
private String myFallBack(String str){
return ">>>>>服務(wù)端服務(wù)降級(jí)>>>>>";
}
Springboot集成Hystrix可視化
1、增加maven依賴(lài)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
2、application配置監(jiān)控
hystrix:
dashboard:
proxy-stream-allow-list: "127.0.0.1"
management:
endpoints:
web:
exposure:
include: health,hystrix.stream
3、頁(yè)面訪問(wèn)hystrix-dashboard
http://127.0.0.1:9999/hystrix
http://127.0.0.1:9999/actuator/hystrix.stream文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-532657.html
寫(xiě)在最后
Springboot集成Hystrix實(shí)現(xiàn)熔斷、降級(jí)、隔離較為簡(jiǎn)單,并且提供了hystrix-dashboard可視化儀表盤(pán)。Hystrix框架已經(jīng)實(shí)現(xiàn)了熔斷降級(jí)、隔離策略,我們集成后只需要根據(jù)自身情況進(jìn)行配置選用即可。一般在微服務(wù)架構(gòu)優(yōu)先選擇客戶端熔斷降級(jí),當(dāng)然也可在服務(wù)端進(jìn)行注解配置。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-532657.html
到了這里,關(guān)于微服務(wù):Springboot集成Hystrix實(shí)現(xiàn)熔斷、降級(jí)、隔離的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!