Feign 集成 Hystrix實(shí)現(xiàn)不同的調(diào)用接口不同的設(shè)置方式
問(wèn)題描述
小編在寫(xiě)項(xiàng)目時(shí)遇到一個(gè)需求:
假設(shè)現(xiàn)在有三個(gè)項(xiàng)目A、B、C,其中A、B作為服務(wù)提供方,C作為調(diào)用方,需要實(shí)現(xiàn)C在調(diào)用A、B時(shí)實(shí)現(xiàn)不同的超時(shí)時(shí)間,比如C調(diào)用A時(shí)超時(shí)是2s,調(diào)用B時(shí)超時(shí)是3s。。。。
本來(lái)以為是很簡(jiǎn)單的事,但是面向百度編程時(shí)發(fā)現(xiàn)沒(méi)有搜索到,官網(wǎng)也沒(méi)有,這就難受了,小編屬于那種不會(huì)主動(dòng)研究源碼的,都是項(xiàng)目有需要或者說(shuō)看到別人改造了啥玩意特別有意思,否則都不去喵一眼,現(xiàn)在沒(méi)辦法只能研究一波源碼,手動(dòng)改造。
正文
正文分為三個(gè)部分描述
- 源碼研究
- 提出方案
- 方案實(shí)現(xiàn)
源碼研究
先說(shuō)說(shuō)如果找到關(guān)鍵的源代碼,如果對(duì)hystrix feign 集成比較熟悉的朋友,可以略過(guò),直接看方案,如果希望知道源碼怎么走的朋友建議看下,這個(gè)花了我挺長(zhǎng)時(shí)間的,網(wǎng)上的源碼解析都是只有關(guān)鍵代碼展示,但是具體細(xì)節(jié)怎么走,沒(méi)有描述,要不然我也不會(huì)花很長(zhǎng)時(shí)間進(jìn)行研究閱讀。
Hystrix、feign 簡(jiǎn)單介紹
首先我們知道 feign 是spring cloud 中用來(lái)進(jìn)行服務(wù)之間的調(diào)用,openFeign 當(dāng)中集成了 ribbon實(shí)現(xiàn)了負(fù)載均衡的實(shí)際的請(qǐng)求
Hystrix是個(gè)熔斷器,就是在某些任務(wù)執(zhí)行時(shí)及時(shí)的失敗返回而不是掛著線程,造成服務(wù)器的級(jí)聯(lián)癱瘓,feign在進(jìn)行微服務(wù)之間調(diào)用時(shí)如果出現(xiàn)了服務(wù)超時(shí),Hystrix進(jìn)行熔斷,立馬返回結(jié)果。
關(guān)鍵代碼
如果大家上網(wǎng)去搜 Hystrix 超時(shí)配置,應(yīng)該都是這樣
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=3000
那么 Hystrix 組件工作時(shí)肯定需要解析這個(gè)配置,調(diào)用它
spring boot 配置有個(gè)特點(diǎn),命名方式都是 {組件名}Properties 這種形式,那么搜索下就找到了關(guān)鍵的配置類(lèi)
com.netflix.hystrix.HystrixCommandProperties
可以看到這里屬性都是final,就是說(shuō)不能set,那么只有構(gòu)造函數(shù)或者靜態(tài)代碼塊可以改,后者明顯不合適,找找構(gòu)造方法就能看到
這就很像了嘛!隨便點(diǎn)一個(gè)進(jìn)去看
private static HystrixProperty<Boolean> getProperty(String propertyPrefix, HystrixCommandKey key, String instanceProperty, Boolean builderOverrideValue, Boolean defaultValue) { return forBoolean() .add(propertyPrefix + ".command." + key.name() + "." + instanceProperty, builderOverrideValue) .add(propertyPrefix + ".command.default." + instanceProperty, defaultValue) .build(); }
像不像拼接上面配置,那么關(guān)鍵是這個(gè) HystrixCommandKey 怎么傳進(jìn)來(lái)的問(wèn)題,這時(shí)候打個(gè)斷點(diǎn)就行,啟動(dòng)項(xiàng)目,進(jìn)行調(diào)用
這時(shí)候就有調(diào)用方法棧了,可以直接看 invoke:104, HystrixInvocationHandler (feign.hystrix)
@Override public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable { ............. // setterMethodMap 封裝 hystrixCommand 配置信息(超時(shí)時(shí)間、是否重試.....) HystrixCommand<Object> hystrixCommand = new HystrixCommand<Object>(setterMethodMap.get(method)) { @Override protected Object run() throws Exception { .... HystrixInvocationHandler.this.dispatch.get(method).invoke(args); .... } @Override protected Object getFallback() { ......... } }; ...... return hystrixCommand.execute(); }
大致就這樣,其實(shí)就是用hystrixCommand調(diào)用feign,最主要的是 setterMethodMap 從哪里設(shè)置的,
final class HystrixInvocationHandler implements InvocationHandler { private final Target<?> target; private final Map<Method, MethodHandler> dispatch; private final FallbackFactory<?> fallbackFactory; // Nullable private final Map<Method, Method> fallbackMethodMap; private final Map<Method, Setter> setterMethodMap; HystrixInvocationHandler(Target<?> target, Map<Method, MethodHandler> dispatch, SetterFactory setterFactory, FallbackFactory<?> fallbackFactory) { this.target = checkNotNull(target, "target"); this.dispatch = checkNotNull(dispatch, "dispatch"); this.fallbackFactory = fallbackFactory; this.fallbackMethodMap = toFallbackMethod(dispatch); this.setterMethodMap = toSetters(setterFactory, target, dispatch.keySet()); } }
一樣也是final屬性,那么也是構(gòu)造函數(shù)賦值的,一樣的打個(gè)斷點(diǎn),重新啟動(dòng)下項(xiàng)目
target:56, HystrixTargeter (org.springframework.cloud.openfeign)
看到這里代碼 核心部分
// HystrixTargeter 這個(gè)類(lèi)看代碼就知道,對(duì)應(yīng)這 @FeignClient 注解的接口 @SuppressWarnings("unchecked") class HystrixTargeter implements Targeter { @Override public <T> T target(FeignClientFactoryBean factory, Feign.Builder feign, FeignContext context, Target.HardCodedTarget<T> target) { ..... feign.hystrix.HystrixFeign.Builder builder = (feign.hystrix.HystrixFeign.Builder) feign; // 這里其實(shí)是容器中拿到 SetterFactory 配置類(lèi) SetterFactory setterFactory = getOptional(factory.getName(), context, SetterFactory.class); if (setterFactory != null) { builder.setterFactory(setterFactory); } // 從 @FeignClient 注解中讀取或者默認(rèn) Class<?> fallback = factory.getFallback(); if (fallback != void.class) { return targetWithFallback(factory.getName(), context, target, builder, fallback); } Class<?> fallbackFactory = factory.getFallbackFactory(); if (fallbackFactory != void.class) { return targetWithFallbackFactory(factory.getName(), context, target, builder, fallbackFactory); } return feign.target(target); } ..... private <T> T getOptional(String feignClientName, FeignContext context, Class<T> beanType) { return context.getInstance(feignClientName, beanType); } }
看下 feign.hystrix.SetterFactory
public interface SetterFactory { HystrixCommand.Setter create(Target<?> target, Method method); // 默認(rèn)實(shí)現(xiàn) final class Default implements SetterFactory { @Override public HystrixCommand.Setter create(Target<?> target, Method method) { String groupKey = target.name(); String commandKey = Feign.configKey(target.type(), method); // HystrixCommandKey、group 賦值 return HystrixCommand.Setter .withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKey)) .andCommandKey(HystrixCommandKey.Factory.asKey(commandKey)); } } }
看到這里結(jié)合上面 HystrixCommandProperties 代碼 是不是有點(diǎn)感覺(jué)了,就是說(shuō)關(guān)鍵配置信息的 HystrixCommandKey 是這里指定的
是不是真的,可以驗(yàn)證下,打個(gè)斷點(diǎn),然后手動(dòng)把 commandKey 修改了,然后上面 HystrixCommandProperties 斷點(diǎn)處驗(yàn)證就行,我這里不貼代碼了
提出方案
那結(jié)合代碼發(fā)現(xiàn) SetterFactory 這接口是關(guān)鍵,而這又是注入的那么簡(jiǎn)單了只要我們手動(dòng)實(shí)現(xiàn)這接口并且注入到 spring 容器中就行
在 feign.hystrix.SetterFactory.Default#create 方法中手動(dòng)實(shí)現(xiàn)不同的feign 接口不同的配置,甚至不同的feign
我這里目前是用注解實(shí)現(xiàn)的,大家也可以用方法名等規(guī)則實(shí)現(xiàn)
最終目的就是讓指定的feign 方法獲取指定的配置
@FeignClient(value = "itemRobot", path = "cloud/device") public interface DeviceApi { @RequestMapping(value = "/login", method = RequestMethod.GET) ServerResponse<String> login(@RequestParam String appId); @RequestMapping(value = "/logout", method = RequestMethod.GET) ServerResponse<String> logout(@RequestParam String appId); }
# login() 方法映射 hystrix.command.login.execution.isolation.thread.timeoutInMilliseconds=10000 # logout() 方法映射 hystrix.command.logout.execution.isolation.thread.timeoutInMilliseconds=10000
如果是基于方法級(jí)別的不同配置,hystrix 官方有給這樣的注解
com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand
@HystrixCommand(groupKey="accountPayGroup",commandKey="accountPay",threadPoolKey="account",threadPoolProperties= { @HystrixProperty(name="coreSize",value="20"), @HystrixProperty(name="maxQueueSize",value="50") },commandProperties={ @HystrixProperty(name="circuitBreaker.sleepWindowInMilliseconds",value="3000"), @HystrixProperty(name="circuitBreaker.errorThresholdPercentage",value="40") })
ps:我用了發(fā)現(xiàn)不生效,目前還在調(diào)試,有了進(jìn)展,再寫(xiě)一篇
但是不能基于feign 調(diào)用不同接口實(shí)現(xiàn),所以如果想基于方法實(shí)現(xiàn)不同配置用官方這個(gè)就行,如果想一個(gè)接口下所有方法一樣配置,不同接口實(shí)現(xiàn)不同,那么用我下面這方式也行。
具體實(shí)現(xiàn)
指定注解
@Target({TYPE, METHOD}) @Retention(RUNTIME) public @interface CusHystrixCommandKey { // 對(duì)應(yīng) commandKey String name(); }
實(shí)現(xiàn) SetterFactory
import com.netflix.hystrix.HystrixCommand; import com.netflix.hystrix.HystrixCommandGroupKey; import com.netflix.hystrix.HystrixCommandKey; import feign.Feign; import feign.Target; import feign.hystrix.SetterFactory; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.stereotype.Component; import java.lang.reflect.Method; @Component public class MyHystrixFactory implements SetterFactory { @Override public HystrixCommand.Setter create(Target<?> target, Method method) { String groupKey = target.name(); String commandKey = Feign.configKey(target.type(), method); CusHystrixCommandKey annotation = method.getAnnotation(CusHystrixCommandKey.class); if (annotation == null) { // 如果指定方法沒(méi)有 CusHystrixCommandKey 注解,用 FeignClient.value() 作為key FeignClient feignClient = method.getDeclaringClass().getAnnotation(FeignClient.class); commandKey = feignClient.value(); } else { // 否則獲取指定的name() 作為key commandKey = annotation.name(); } return HystrixCommand.Setter .withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKey)) .andCommandKey(HystrixCommandKey.Factory.asKey(commandKey)); } }
測(cè)試頁(yè)面就不貼了,老方法,萬(wàn)能的斷點(diǎn)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
JavaWeb項(xiàng)目Servlet無(wú)法訪問(wèn)問(wèn)題解決
這篇文章主要介紹了JavaWeb項(xiàng)目Servlet無(wú)法訪問(wèn)問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11IntelliJ IDEA使用maven實(shí)現(xiàn)tomcat的熱部署
這篇文章主要介紹了IntelliJ IDEA使用maven實(shí)現(xiàn)tomcat的熱部署,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07IDEA實(shí)現(xiàn) springmvc的簡(jiǎn)單注冊(cè)登錄功能的示例代碼
這篇文章主要介紹了IDEA實(shí)現(xiàn) springmvc的簡(jiǎn)單注冊(cè)登錄功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06Java 微信公眾號(hào)開(kāi)發(fā)相關(guān)總結(jié)
公眾號(hào)作為主流的自媒體平臺(tái),有著不少人使用。這次以文本回復(fù)作為案例來(lái)講解Java相關(guān)的微信公眾號(hào)開(kāi)發(fā)2021-05-05elasticsearch通過(guò)guice注入Node組裝啟動(dòng)過(guò)程
這篇文章主要為大家介紹了?elasticsearch通過(guò)guice注入Node組裝啟動(dòng)過(guò)程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04淺談Spring如何解決循環(huán)依賴的問(wèn)題
這篇文章主要介紹了淺談Spring如何解決循環(huán)依賴的問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09SpringBoot集成JWT實(shí)現(xiàn)Token登錄驗(yàn)證的示例代碼
隨著技術(shù)的發(fā)展,分布式web應(yīng)用的普及,通過(guò)session管理用戶登錄狀態(tài)成本越來(lái)越高,因此慢慢發(fā)展成為token的方式做登錄身份校驗(yàn),本文就來(lái)介紹一下SpringBoot集成JWT實(shí)現(xiàn)Token登錄驗(yàn)證的示例代碼,感興趣的可以了解一下2023-12-12淺談Java中常用數(shù)據(jù)結(jié)構(gòu)的實(shí)現(xiàn)類(lèi) Collection和Map
下面小編就為大家?guī)?lái)一篇淺談Java中常用數(shù)據(jù)結(jié)構(gòu)的實(shí)現(xiàn)類(lèi) Collection和Map。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-09-09