Feign 集成 Hystrix實現(xiàn)不同的調(diào)用接口不同的設(shè)置方式
問題描述
小編在寫項目時遇到一個需求:
假設(shè)現(xiàn)在有三個項目A、B、C,其中A、B作為服務提供方,C作為調(diào)用方,需要實現(xiàn)C在調(diào)用A、B時實現(xiàn)不同的超時時間,比如C調(diào)用A時超時是2s,調(diào)用B時超時是3s。。。。
本來以為是很簡單的事,但是面向百度編程時發(fā)現(xiàn)沒有搜索到,官網(wǎng)也沒有,這就難受了,小編屬于那種不會主動研究源碼的,都是項目有需要或者說看到別人改造了啥玩意特別有意思,否則都不去喵一眼,現(xiàn)在沒辦法只能研究一波源碼,手動改造。
正文
正文分為三個部分描述
- 源碼研究
- 提出方案
- 方案實現(xiàn)
源碼研究
先說說如果找到關(guān)鍵的源代碼,如果對hystrix feign 集成比較熟悉的朋友,可以略過,直接看方案,如果希望知道源碼怎么走的朋友建議看下,這個花了我挺長時間的,網(wǎng)上的源碼解析都是只有關(guān)鍵代碼展示,但是具體細節(jié)怎么走,沒有描述,要不然我也不會花很長時間進行研究閱讀。
Hystrix、feign 簡單介紹
首先我們知道 feign 是spring cloud 中用來進行服務之間的調(diào)用,openFeign 當中集成了 ribbon實現(xiàn)了負載均衡的實際的請求
Hystrix是個熔斷器,就是在某些任務執(zhí)行時及時的失敗返回而不是掛著線程,造成服務器的級聯(lián)癱瘓,feign在進行微服務之間調(diào)用時如果出現(xiàn)了服務超時,Hystrix進行熔斷,立馬返回結(jié)果。
關(guān)鍵代碼
如果大家上網(wǎng)去搜 Hystrix 超時配置,應該都是這樣
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=3000
那么 Hystrix 組件工作時肯定需要解析這個配置,調(diào)用它
spring boot 配置有個特點,命名方式都是 {組件名}Properties 這種形式,那么搜索下就找到了關(guān)鍵的配置類
com.netflix.hystrix.HystrixCommandProperties

可以看到這里屬性都是final,就是說不能set,那么只有構(gòu)造函數(shù)或者靜態(tài)代碼塊可以改,后者明顯不合適,找找構(gòu)造方法就能看到

這就很像了嘛!隨便點一個進去看
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)鍵是這個 HystrixCommandKey 怎么傳進來的問題,這時候打個斷點就行,啟動項目,進行調(diào)用

這時候就有調(diào)用方法棧了,可以直接看 invoke:104, HystrixInvocationHandler (feign.hystrix)
@Override
public Object invoke(final Object proxy, final Method method, final Object[] args)
throws Throwable {
.............
// setterMethodMap 封裝 hystrixCommand 配置信息(超時時間、是否重試.....)
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();
}
大致就這樣,其實就是用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ù)賦值的,一樣的打個斷點,重新啟動下項目

target:56, HystrixTargeter (org.springframework.cloud.openfeign)
看到這里代碼 核心部分
// HystrixTargeter 這個類看代碼就知道,對應這 @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;
// 這里其實是容器中拿到 SetterFactory 配置類
SetterFactory setterFactory = getOptional(factory.getName(), context,
SetterFactory.class);
if (setterFactory != null) {
builder.setterFactory(setterFactory);
}
// 從 @FeignClient 注解中讀取或者默認
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);
// 默認實現(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 代碼 是不是有點感覺了,就是說關(guān)鍵配置信息的 HystrixCommandKey 是這里指定的
是不是真的,可以驗證下,打個斷點,然后手動把 commandKey 修改了,然后上面 HystrixCommandProperties 斷點處驗證就行,我這里不貼代碼了
提出方案
那結(jié)合代碼發(fā)現(xiàn) SetterFactory 這接口是關(guān)鍵,而這又是注入的那么簡單了只要我們手動實現(xiàn)這接口并且注入到 spring 容器中就行
在 feign.hystrix.SetterFactory.Default#create 方法中手動實現(xiàn)不同的feign 接口不同的配置,甚至不同的feign
我這里目前是用注解實現(xiàn)的,大家也可以用方法名等規(guī)則實現(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
如果是基于方法級別的不同配置,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)試,有了進展,再寫一篇
但是不能基于feign 調(diào)用不同接口實現(xiàn),所以如果想基于方法實現(xiàn)不同配置用官方這個就行,如果想一個接口下所有方法一樣配置,不同接口實現(xiàn)不同,那么用我下面這方式也行。
具體實現(xiàn)
指定注解
@Target({TYPE, METHOD})
@Retention(RUNTIME)
public @interface CusHystrixCommandKey {
// 對應 commandKey
String name();
}
實現(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) {
// 如果指定方法沒有 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));
}
}
測試頁面就不貼了,老方法,萬能的斷點
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
IntelliJ IDEA使用maven實現(xiàn)tomcat的熱部署
這篇文章主要介紹了IntelliJ IDEA使用maven實現(xiàn)tomcat的熱部署,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07
IDEA實現(xiàn) springmvc的簡單注冊登錄功能的示例代碼
這篇文章主要介紹了IDEA實現(xiàn) springmvc的簡單注冊登錄功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06
Java 微信公眾號開發(fā)相關(guān)總結(jié)
公眾號作為主流的自媒體平臺,有著不少人使用。這次以文本回復作為案例來講解Java相關(guān)的微信公眾號開發(fā)2021-05-05
elasticsearch通過guice注入Node組裝啟動過程
這篇文章主要為大家介紹了?elasticsearch通過guice注入Node組裝啟動過程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-04-04
SpringBoot集成JWT實現(xiàn)Token登錄驗證的示例代碼
隨著技術(shù)的發(fā)展,分布式web應用的普及,通過session管理用戶登錄狀態(tài)成本越來越高,因此慢慢發(fā)展成為token的方式做登錄身份校驗,本文就來介紹一下SpringBoot集成JWT實現(xiàn)Token登錄驗證的示例代碼,感興趣的可以了解一下2023-12-12
淺談Java中常用數(shù)據(jù)結(jié)構(gòu)的實現(xiàn)類 Collection和Map
下面小編就為大家?guī)硪黄獪\談Java中常用數(shù)據(jù)結(jié)構(gòu)的實現(xiàn)類 Collection和Map。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-09-09

