兩者都能實(shí)現(xiàn)降級(jí),但fallbackFactory能得到服務(wù)降級(jí)的原因。
1.2 fallback使用FeignAPI和Fallback代碼如下:
@FeignClient(name = "shared-geoip", fallback = GeoIPRemote.HystrixFallback.class)
public interface GeoIPRemote {
@RequestMapping(method = RequestMethod.GET, value = "/geo/get_ip_info")
ResponseMessagegetIpInfo(@RequestParam("host") String host);
@Component
class HystrixFallback implements GeoIPRemote {
@Override
public ResponseMessagegetIpInfo(String ip) {
return ResponseMessage.buildFail(ResponseCode.SERVICE_BUSY, "IP分析服務(wù)");
}
}
}
當(dāng)服務(wù)降級(jí)的時(shí)候,fallback中只能打印此次調(diào)用失敗的log,而無(wú)法打印詳細(xì)的異常信息,這明顯不符合我們的要求。
1.3 fallbackFactory使用@FeignClient(name = "shared-geoip", fallbackFactory = GeoIPRemote.HystrixFallbackFactory.class)
public interface GeoIPRemote {
@RequestMapping(method = RequestMethod.GET, value = "/geo/get_ip_info")
ResponseMessagegetIpInfo(@RequestParam("host") String host);
@Component
class HystrixFallbackFactory implements FallbackFactory{
@Override
public GeoIPRemote create(Throwable throwable) {
//此處可以得知異常原因,比如遠(yuǎn)程服務(wù)掉線,服務(wù)異常等原因
LogUtils.error(throwable, "errorMsg:{}", throwable.getMessage());
return host ->ResponseMessage.buildFail(ResponseCode.SERVICE_BUSY, "IP分析服務(wù)");
}
}
}
2021-06-10 11:14:22.832 [INFO BEGIN] [PollingServerListUpdater-0]:com.netflix.config.ChainedDynamicProperty - [checkAndFlip:115] Flipping property: shared-geoip.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647 [INFO END]
2021-06-10 11:14:22.832.912 [ERROR BEGIN] [hystrix-shared-geoip-1]:c.t.t.p.r.h.HystrixFallbackFactory - [create:26] Connection refused: connect executing POST http://shared-geoip/geo/get_ip_info [ERROR END]
2020-10-30 15:14:22.912 [ERROR BEGIN] [hystrix-shared-geoip-1-1]:c.t.t.p.r.h.FallbackFactory- [GeoIPRemote :60] errorMsg:Connection refused [ERROR END]
當(dāng)服務(wù)降級(jí)的時(shí)候,fallbackFactory中既打印此次調(diào)用失敗的log,也可以打印詳細(xì)的異常信息,符合要求。
2.hystrix線程池配置和超時(shí)機(jī)制重點(diǎn)關(guān)注hystrix的隔離策略有兩種:threadpool和semaphore
對(duì)比如下:
應(yīng)用提供外網(wǎng)訪問(wèn),對(duì)于高并發(fā)下、機(jī)器性能比較好的情況下推薦使用threadpool隔離方式。
詳細(xì)配置如下,調(diào)用方微服務(wù)和網(wǎng)關(guān)服務(wù)可根據(jù)實(shí)際情況進(jìn)行適當(dāng)修改。
feign:
hystrix:
enabled: true #開(kāi)啟降級(jí)
ribbon:
#Ribbon允許大連接數(shù),即所有后端微服務(wù)實(shí)例請(qǐng)求并發(fā)數(shù)之和的大值。
MaxTotalConnections: 500
#單個(gè)后端微服務(wù)實(shí)例能接收的大請(qǐng)求并發(fā)數(shù)
MaxConnectionsPerHost: 500
#建議設(shè)置超時(shí)時(shí)間,以免因?yàn)榈却龝r(shí)間過(guò)長(zhǎng)造成請(qǐng)求處理失?。ㄒ唬? #Http請(qǐng)求中的socketTimeout
ReadTimeout: 5000
#Http請(qǐng)求中的connectTimeout
ConnectTimeout: 10000
hystrix:
threadpool:
default:
coreSize: 20 #核心線程數(shù)量
maximumSize: 100 #大線程數(shù)量
allowMaximumSizeToDivergeFromCoreSize: true
command:
default:
fallback:
isolation:
thread:
timeoutInMilliseconds: 10000 #命令執(zhí)行超時(shí)時(shí)間
execution:
isolation:
thread:
timeoutInMilliseconds: 10000 #請(qǐng)求的超時(shí)
timeout:
enabled: true #斷路器超時(shí)設(shè)置
circuitBreaker:
requestVolumeThreshold: 1000 #設(shè)置熔斷器失敗的個(gè)數(shù)
配置解釋鏈接:https://blog.csdn.net/harris135/article/details/77879148?utm_source=debugrun&utm_medium=distribute.pc_relevant.none-task-blog-title-3&spm=1001.2101.3001.4242
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購(gòu),新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧