Feign 集成 Hystrix實(shí)現(xiàn)不同的調(diào)用接口不同的設(shè)置方式
問題描述
小編在寫項(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。。。。
本來以為是很簡單的事,但是面向百度編程時(shí)發(fā)現(xiàn)沒有搜索到,官網(wǎng)也沒有,這就難受了,小編屬于那種不會(huì)主動(dòng)研究源碼的,都是項(xiàng)目有需要或者說看到別人改造了啥玩意特別有意思,否則都不去喵一眼,現(xiàn)在沒辦法只能研究一波源碼,手動(dòng)改造。
正文
正文分為三個(gè)部分描述
- 源碼研究
- 提出方案
- 方案實(shí)現(xiàn)
源碼研究
先說說如果找到關(guān)鍵的源代碼,如果對hystrix feign 集成比較熟悉的朋友,可以略過,直接看方案,如果希望知道源碼怎么走的朋友建議看下,這個(gè)花了我挺長時(shí)間的,網(wǎng)上的源碼解析都是只有關(guān)鍵代碼展示,但是具體細(xì)節(jié)怎么走,沒有描述,要不然我也不會(huì)花很長時(shí)間進(jìn)行研究閱讀。
Hystrix、feign 簡單介紹
首先我們知道 feign 是spring cloud 中用來進(jìn)行服務(wù)之間的調(diào)用,openFeign 當(dāng)中集成了 ribbon實(shí)現(xiàn)了負(fù)載均衡的實(shí)際的請求
Hystrix是個(gè)熔斷器,就是在某些任務(wù)執(zhí)行時(shí)及時(shí)的失敗返回而不是掛著線程,造成服務(wù)器的級聯(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)鍵的配置類
com.netflix.hystrix.HystrixCommandProperties

可以看到這里屬性都是final,就是說不能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)來的問題,這時(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è)類看代碼就知道,對應(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 配置類
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)感覺了,就是說關(guān)鍵配置信息的 HystrixCommandKey 是這里指定的
是不是真的,可以驗(yàn)證下,打個(gè)斷點(diǎn),然后手動(dòng)把 commandKey 修改了,然后上面 HystrixCommandProperties 斷點(diǎn)處驗(yàn)證就行,我這里不貼代碼了
提出方案
那結(jié)合代碼發(fā)現(xiàn) SetterFactory 這接口是關(guā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
如果是基于方法級別的不同配置,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)展,再寫一篇
但是不能基于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 {
// 對應(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) {
// 如果指定方法沒有 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));
}
}
測試頁面就不貼了,老方法,萬能的斷點(diǎn)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
java設(shè)計(jì)模式:建造者模式之生產(chǎn)線
這篇文章主要介紹了Java設(shè)計(jì)模式之建造者模式,結(jié)合具體實(shí)例形式分析了建造者模式的概念、原理、實(shí)現(xiàn)方法與相關(guān)使用注意事項(xiàng),需要的朋友可以參考下2021-08-08
java?web實(shí)現(xiàn)簡單登錄注冊功能全過程(eclipse,mysql)
前期我們學(xué)習(xí)了javaweb項(xiàng)目用JDBC連接數(shù)據(jù)庫,還有數(shù)據(jù)庫的建表功能,下面這篇文章主要給大家介紹了關(guān)于java?web實(shí)現(xiàn)簡單登錄注冊功能的相關(guān)資料,需要的朋友可以參考下2022-07-07
Mybatis以main方法形式調(diào)用dao層執(zhí)行代碼實(shí)例
這篇文章主要介紹了Mybatis以main方法形式調(diào)用dao層執(zhí)行代碼實(shí)例,MyBatis 是一款優(yōu)秀的持久層框架,MyBatis 免除了幾乎所有的 JDBC 代碼以及設(shè)置參數(shù)和獲取結(jié)果集的工作,需要的朋友可以參考下2023-08-08
SpringCloud gateway request的body驗(yàn)證或修改方式
這篇文章主要介紹了SpringCloud gateway request的body驗(yàn)證或修改方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
SpringBoot整合Thymeleaf小項(xiàng)目及詳細(xì)流程
這篇文章主要介紹了SpringBoot整合Thymeleaf小項(xiàng)目,本項(xiàng)目使用SpringBoot開發(fā),jdbc5.1.48,主要涉及到Mybatis的使用,Thymeleaf的使用,用戶密碼加密,驗(yàn)證碼的設(shè)計(jì),圖片的文件上傳(本文件上傳到本地,沒有傳到數(shù)據(jù)庫)登錄過濾,需要的朋友可以參考下2022-03-03
基于spring boot 1.5.4 集成 jpa+hibernate+jdbcTemplate(詳解)
下面小編就為大家?guī)硪黄趕pring boot 1.5.4 集成 jpa+hibernate+jdbcTemplate(詳解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06
Mybatis 中的sql批量修改方法實(shí)現(xiàn)
在項(xiàng)目中遇到需要批量更新的功能,原本想的是在Java中用循環(huán)訪問數(shù)據(jù)庫去更新,但是心里總覺得這樣做會(huì)不會(huì)太頻繁了,太耗費(fèi)資源了,效率也很低,查了下mybatis的批量操作,原來確實(shí)有<foreach>標(biāo)簽可以做到,下面通過本文給大家介紹下2017-01-01

