|
|
@@ -1,9 +1,12 @@
|
|
|
package com.ytpm.utils;
|
|
|
|
|
|
+import com.ytpm.handle.DynamicFeignClientFactory;
|
|
|
+import com.ytpm.question.base.BaseFeign;
|
|
|
import org.springframework.cloud.openfeign.FeignClient;
|
|
|
import org.springframework.context.ApplicationContext;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
import java.lang.reflect.Method;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
@@ -19,6 +22,8 @@ public class FeignClientInvoker {
|
|
|
private final ApplicationContext applicationContext;
|
|
|
private final Map<String, Object> feignClientCache = new HashMap<>();
|
|
|
private final Map<String, Method> methodCache = new HashMap<>();
|
|
|
+ @Resource
|
|
|
+ private DynamicFeignClientFactory feignClientFactory;
|
|
|
|
|
|
public FeignClientInvoker(ApplicationContext applicationContext) {
|
|
|
this.applicationContext = applicationContext;
|
|
|
@@ -51,19 +56,37 @@ public class FeignClientInvoker {
|
|
|
if (feignClientCache.containsKey(serviceName)) {
|
|
|
return feignClientCache.get(serviceName);
|
|
|
}
|
|
|
-
|
|
|
- // 动态查找标注@FeignClient的Bean
|
|
|
- Map<String, Object> feignClients = applicationContext.getBeansWithAnnotation(FeignClient.class);
|
|
|
- for (Object bean : feignClients.values()) {
|
|
|
- FeignClient annotation = bean.getClass().getInterfaces()[0].getAnnotation(FeignClient.class);
|
|
|
- if (annotation != null && serviceName.equals(annotation.name())) {
|
|
|
- feignClientCache.put(serviceName, bean);
|
|
|
- return bean;
|
|
|
+ Object feignClient = null;
|
|
|
+ try {
|
|
|
+ // 默认查询声明实例
|
|
|
+ // 动态查找标注@FeignClient的Bean
|
|
|
+ Map<String, Object> feignClients = applicationContext.getBeansWithAnnotation(FeignClient.class);
|
|
|
+ for (Object bean : feignClients.values()) {
|
|
|
+ FeignClient annotation = bean.getClass().getInterfaces()[0].getAnnotation(FeignClient.class);
|
|
|
+ if (annotation != null && serviceName.equals(annotation.name())) {
|
|
|
+ feignClientCache.put(serviceName, bean);
|
|
|
+ feignClient = bean;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 构建动态FeignClient
|
|
|
+ if (feignClient == null) {
|
|
|
+ Class<?> clazz = com.ytpm.question.base.BaseFeign.class;
|
|
|
+ if (serviceName.contains("ios")) {
|
|
|
+ clazz = com.ytpm.lemonios.feign.base.BaseFeign.class;
|
|
|
+ }
|
|
|
+ feignClient = feignClientFactory.getClient(serviceName, clazz);
|
|
|
}
|
|
|
+ feignClientCache.put(serviceName, feignClient);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new IllegalArgumentException("未找到服务: " + serviceName);
|
|
|
+ }
|
|
|
+ if (feignClient == null) {
|
|
|
+ throw new IllegalArgumentException("未找到服务: " + serviceName);
|
|
|
}
|
|
|
- throw new IllegalArgumentException("未找到服务: " + serviceName);
|
|
|
+ return feignClient;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
private Method getTargetMethod(Object feignClient, String methodName, Object[] args)
|
|
|
throws NoSuchMethodException {
|
|
|
|