gateway集成hystrix全局?jǐn)嗦菲?/h2>
pom.xml添加依賴
1
2
3
4
|
< dependency > < groupId >org.springframework.cloud</ groupId > < artifactId >spring-cloud-starter-netflix-hystrix</ artifactId > </ dependency > |
在配置文件中,增加spring.cloud.gateway.default-filters:
1
2
3
4
5
|
default -filters: - name: Hystrix args: name: fallbackcmd fallbackUri: forward:/fallbackcontroller |
一定要注意是spring.cloud.gateway.default-filters這個(gè)配置節(jié)。
如上的配置,將會(huì)使用HystrixCommand打包剩余的過(guò)濾器,并命名為fallbackcmd,我們還配置了可選的參數(shù)fallbackUri,降級(jí)邏輯被調(diào)用,請(qǐng)求將會(huì)被轉(zhuǎn)發(fā)到URI為/fallbackcontroller的控制器處理。
定義降級(jí)處理如下:
1
2
3
4
5
6
7
|
@RequestMapping (value = "/fallbackcontroller" ) public Map<String, String> fallBackController() { Map<String, String> res = new HashMap(); res.put( "code" , "-100" ); res.put( "data" , "service not available" ); return res; } |
此時(shí)可以設(shè)置hystrix超時(shí)時(shí)間(毫秒) ,默認(rèn)只有2秒
1
2
3
4
5
6
7
|
hystrix: command: default : execution: isolation: thread: timeoutInMilliseconds: 30000 |
示例代碼:
https://github.com/wanghongqi/springcloudconsul_test/tree/master/springtest_gateway
spring cloud gateway 全局熔斷
熔斷主要保護(hù)的是調(diào)用方服務(wù),如某A服務(wù)調(diào)用B服務(wù)的rpc接口,突然B服務(wù)接口不穩(wěn)定,表現(xiàn)為接口延遲或者失敗率變大。
這個(gè)時(shí)候如果對(duì)B服務(wù)調(diào)用不能快速失敗,那么會(huì)導(dǎo)致A服務(wù)大量線程資源無(wú)法釋放導(dǎo)致最終A服務(wù)不穩(wěn)定,故障點(diǎn)由B服務(wù)傳遞到A服務(wù),故障擴(kuò)大。
熔斷就是在B服務(wù)出現(xiàn)故障的情況下,斷開(kāi)對(duì)B的調(diào)用,通過(guò)快速失敗來(lái)保證A服務(wù)的穩(wěn)定。
一:Gateway項(xiàng)目maven引入依賴包
Spring Cloud Gateway 利用 Hystrix 的熔斷特性,在流量過(guò)大時(shí)進(jìn)行服務(wù)降級(jí),同樣我們還是首先給項(xiàng)目添加上依賴
1
2
3
4
|
< dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-actuator</ artifactId > </ dependency > |
二:創(chuàng)建熔斷后轉(zhuǎn)發(fā)的請(qǐng)求
1
2
3
4
5
6
7
8
9
|
@RestController public class FallbackController { private Logger log= LoggerFactory.getLogger(getClass()); @RequestMapping ( "/error/fallback" ) public Object fallacak(){ log.info( "熔斷處理!??!" ); return "Service Error?。?!" ; } } |
三:yml配置
1
2
3
4
5
6
7
8
9
10
11
|
#全局熔斷攔截器 default -filters: - name: Hystrix args: name: fallbackcmd fallbackUri: forward:/error/fallback - name: Retry args: retries: 3 statuses: BAD_GATEWAY,BAD_REQUEST methods: GET,POST |
Hystrix是Gateway以封裝好的過(guò)濾器。如果不了解請(qǐng)查看:GatwayFilter工廠
name
:即HystrixCommand的名字
fallbackUri
:forward:/error/fallback 配置了 fallback 時(shí)要會(huì)調(diào)的路徑,當(dāng)調(diào)用 Hystrix 的 fallback 被調(diào)用時(shí),請(qǐng)求將轉(zhuǎn)發(fā)到/error/fallback 這個(gè) URI
Retry 通過(guò)這四個(gè)參數(shù)來(lái)控制重試機(jī)制: retries, statuses, methods, 和 series
retries
:重試次數(shù),默認(rèn)值是 3 次
statuses
:HTTP 的狀態(tài)返回碼,取值請(qǐng)參考:org.springframework.http.HttpStatus
methods
:指定哪些方法的請(qǐng)求需要進(jìn)行重試邏輯,默認(rèn)值是 GET 方法,取值參考:org.springframework.http.HttpMethod
series
:一些列的狀態(tài)碼配置,取值參考:org.springframework.http.HttpStatus.Series。符合的某段狀態(tài)碼才會(huì)進(jìn)行重試邏輯,默認(rèn)值是 SERVER_ERROR,值是 5,也就是 5XX(5 開(kāi)頭的狀態(tài)碼),共有5 個(gè)值。
1
2
3
4
5
6
7
8
9
|
# hystrix 信號(hào)量隔離, 3 秒后自動(dòng)超時(shí) hystrix: command: fallbackcmd: execution: isolation: strategy: SEMAPHORE thread: timeoutInMilliseconds: 3000 |
fallbackcmd 此處需要和上面設(shè)置的HystrixCommand一致
timeoutInMilliseconds 設(shè)置超時(shí)時(shí)間
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://wanghq.blog.csdn.net/article/details/88179419