問題描述
小編在寫項目時遇到一個需求:
假設現在有三個項目A、B、C,其中A、B作為服務提供方,C作為調用方,需要實現C在調用A、B時實現不同的超時時間,比如C調用A時超時是2s,調用B時超時是3s。。。。
本來以為是很簡單的事,但是面向百度編程時發現沒有搜索到,官網也沒有,這就難受了,小編屬于那種不會主動研究源碼的,都是項目有需要或者說看到別人改造了啥玩意特別有意思,否則都不去喵一眼,現在沒辦法只能研究一波源碼,手動改造。
正文
正文分為三個部分描述
- 源碼研究
- 提出方案
- 方案實現
源碼研究
先說說如果找到關鍵的源代碼,如果對hystrix feign 集成比較熟悉的朋友,可以略過,直接看方案,如果希望知道源碼怎么走的朋友建議看下,這個花了我挺長時間的,網上的源碼解析都是只有關鍵代碼展示,但是具體細節怎么走,沒有描述,要不然我也不會花很長時間進行研究閱讀。
Hystrix、feign 簡單介紹
首先我們知道 feign 是spring cloud 中用來進行服務之間的調用,openFeign 當中集成了 ribbon實現了負載均衡的實際的請求
Hystrix是個熔斷器,就是在某些任務執行時及時的失敗返回而不是掛著線程,造成服務器的級聯癱瘓,feign在進行微服務之間調用時如果出現了服務超時,Hystrix進行熔斷,立馬返回結果。
關鍵代碼
如果大家上網去搜 Hystrix 超時配置,應該都是這樣
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=3000
那么 Hystrix 組件工作時肯定需要解析這個配置,調用它
spring boot 配置有個特點,命名方式都是 {組件名}Properties 這種形式,那么搜索下就找到了關鍵的配置類
com.netflix.hystrix.HystrixCommandProperties
可以看到這里屬性都是final,就是說不能set,那么只有構造函數或者靜態代碼塊可以改,后者明顯不合適,找找構造方法就能看到
這就很像了嘛!隨便點一個進去看
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(); }
像不像拼接上面配置,那么關鍵是這個 HystrixCommandKey 怎么傳進來的問題,這時候打個斷點就行,啟動項目,進行調用
這時候就有調用方法棧了,可以直接看 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調用feign,最主要的是 setterMethodMap 從哪里設置的,
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屬性,那么也是構造函數賦值的,一樣的打個斷點,重新啟動下項目
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); // 默認實現 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)); } } }
看到這里結合上面 HystrixCommandProperties 代碼 是不是有點感覺了,就是說關鍵配置信息的 HystrixCommandKey 是這里指定的
是不是真的,可以驗證下,打個斷點,然后手動把 commandKey 修改了,然后上面 HystrixCommandProperties 斷點處驗證就行,我這里不貼代碼了
提出方案
那結合代碼發現 SetterFactory 這接口是關鍵,而這又是注入的那么簡單了只要我們手動實現這接口并且注入到 spring 容器中就行
在 feign.hystrix.SetterFactory.Default#create 方法中手動實現不同的feign 接口不同的配置,甚至不同的feign
我這里目前是用注解實現的,大家也可以用方法名等規則實現
最終目的就是讓指定的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:我用了發現不生效,目前還在調試,有了進展,再寫一篇
但是不能基于feign 調用不同接口實現,所以如果想基于方法實現不同配置用官方這個就行,如果想一個接口下所有方法一樣配置,不同接口實現不同,那么用我下面這方式也行。
具體實現
指定注解
@Target({TYPE, METHOD}) @Retention(RUNTIME) public @interface CusHystrixCommandKey { // 對應 commandKey String name(); }
實現 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)); } }
測試頁面就不貼了,老方法,萬能的斷點
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/weixin_39660224/article/details/109477040