From bb6b4837e4a0fe404b340222056d18f03cf1ceca Mon Sep 17 00:00:00 2001 From: lbw Date: Sat, 30 Jan 2021 23:26:04 +0800 Subject: [PATCH] =?UTF-8?q?:bookmark:=20:bookmark:=20:bookmark:=20v3.0.4?= =?UTF-8?q?=20spring=20cloud=202020.0.1=20=E9=87=8D=E6=9E=84=20archetype?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/feign/ext/PigSentinelFeign.java | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/pig-common/pig-common-feign/src/main/java/com/pig4cloud/pig/common/feign/ext/PigSentinelFeign.java b/pig-common/pig-common-feign/src/main/java/com/pig4cloud/pig/common/feign/ext/PigSentinelFeign.java index 50fb6333..f5d56aed 100644 --- a/pig-common/pig-common-feign/src/main/java/com/pig4cloud/pig/common/feign/ext/PigSentinelFeign.java +++ b/pig-common/pig-common-feign/src/main/java/com/pig4cloud/pig/common/feign/ext/PigSentinelFeign.java @@ -16,6 +16,11 @@ package com.pig4cloud.pig.common.feign.ext; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; +import java.util.Map; + import com.alibaba.cloud.sentinel.feign.SentinelContractHolder; import feign.Contract; import feign.Feign; @@ -23,17 +28,14 @@ import feign.InvocationHandlerFactory; import feign.Target; import org.springframework.beans.BeansException; import org.springframework.cloud.openfeign.FallbackFactory; +import org.springframework.cloud.openfeign.FeignClient; import org.springframework.cloud.openfeign.FeignContext; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; +import org.springframework.core.annotation.AnnotationUtils; import org.springframework.util.ReflectionUtils; import org.springframework.util.StringUtils; -import java.lang.reflect.Field; -import java.lang.reflect.InvocationHandler; -import java.lang.reflect.Method; -import java.util.Map; - /** * 支持自动降级注入 重写 {@link com.alibaba.cloud.sentinel.feign.SentinelFeign} * @@ -74,27 +76,25 @@ public final class PigSentinelFeign { super.invocationHandlerFactory(new InvocationHandlerFactory() { @Override public InvocationHandler create(Target target, Map dispatch) { - // using reflect get fallback and fallbackFactory properties from - // FeignClientFactoryBean because FeignClientFactoryBean is a package - // level class, we can not use it in our package - Object feignClientFactoryBean = PigSentinelFeign.Builder.this.applicationContext - .getBean("&" + target.type().getName()); - Class fallback = (Class) getFieldValue(feignClientFactoryBean, "fallback"); - Class fallbackFactory = (Class) getFieldValue(feignClientFactoryBean, "fallbackFactory"); - String beanName = (String) getFieldValue(feignClientFactoryBean, "contextId"); + // 查找 FeignClient 上的 降级策略 + FeignClient feignClient = AnnotationUtils.findAnnotation(target.type(), FeignClient.class); + Class fallback = feignClient.fallback(); + Class fallbackFactory = feignClient.fallbackFactory(); + + String beanName = feignClient.contextId(); if (!StringUtils.hasText(beanName)) { - beanName = (String) getFieldValue(feignClientFactoryBean, "name"); + beanName = feignClient.name(); } Object fallbackInstance; FallbackFactory fallbackFactoryInstance; - // check fallback and fallbackFactory properties if (void.class != fallback) { fallbackInstance = getFromContext(beanName, "fallback", fallback, target.type()); return new PigSentinelInvocationHandler(target, dispatch, new FallbackFactory.Default(fallbackInstance)); } + if (void.class != fallbackFactory) { fallbackFactoryInstance = (FallbackFactory) getFromContext(beanName, "fallbackFactory", fallbackFactory, FallbackFactory.class);