diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/eager/instrument/feign/FeignEagerLoadSmartLifecycle.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/eager/instrument/feign/FeignEagerLoadSmartLifecycle.java index 0f7804cc9..87ce27905 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/eager/instrument/feign/FeignEagerLoadSmartLifecycle.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/eager/instrument/feign/FeignEagerLoadSmartLifecycle.java @@ -28,6 +28,8 @@ import feign.Target; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.aop.framework.AopProxy; +import org.springframework.aop.framework.JdkDynamicAopProxyUtils; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.context.ApplicationContext; import org.springframework.context.SmartLifecycle; @@ -93,7 +95,17 @@ public class FeignEagerLoadSmartLifecycle implements SmartLifecycle { public static Target.HardCodedTarget getHardCodedTarget(Object proxy) { try { - Object invocationHandler = Proxy.getInvocationHandler(proxy); + int count = 0; + Object invocationHandler = proxy; + // 避免死循环 + while (count++ < 100) { + invocationHandler = Proxy.getInvocationHandler(invocationHandler); + if (invocationHandler instanceof AopProxy) { + invocationHandler = JdkDynamicAopProxyUtils.getTarget(invocationHandler); + continue; + } + break; + } for (Field field : invocationHandler.getClass().getDeclaredFields()) { field.setAccessible(true); diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/org/springframework/aop/framework/JdkDynamicAopProxyUtils.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/org/springframework/aop/framework/JdkDynamicAopProxyUtils.java new file mode 100644 index 000000000..b67a769e9 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/org/springframework/aop/framework/JdkDynamicAopProxyUtils.java @@ -0,0 +1,50 @@ +/* + * Tencent is pleased to support the open source community by making spring-cloud-tencent available. + * + * Copyright (C) 2021 Tencent. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package org.springframework.aop.framework; + +import java.lang.reflect.Field; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class JdkDynamicAopProxyUtils { + + private static final Logger LOGGER = LoggerFactory.getLogger(JdkDynamicAopProxyUtils.class); + + private JdkDynamicAopProxyUtils() { + } + + public static Object getTarget(Object invocationHandler) { + if (invocationHandler instanceof JdkDynamicAopProxy) { + try { + JdkDynamicAopProxy jdkDynamicAopProxy = (JdkDynamicAopProxy) invocationHandler; + Field advisedField = JdkDynamicAopProxy.class.getDeclaredField("advised"); + advisedField.setAccessible(true); + AdvisedSupport advisedSupport = (AdvisedSupport) advisedField.get(jdkDynamicAopProxy); + + if (advisedSupport != null && advisedSupport.getTargetSource() != null) { + return advisedSupport.getTargetSource().getTarget(); + } + } + catch (Exception e) { + LOGGER.error("Unexpected error occurred while getting target from JdkDynamicAopProxy", e); + } + } + return null; + } +}