Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix:fix feign url bug when using sleuth. #1119

Merged
merged 2 commits into from
Sep 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@

- [fix:the polaris config relation non-daemon thread should stop when application fails to start.](https://github.com/Tencent/spring-cloud-tencent/pull/1110)
- [Refactoring:remove invalid @AutoConfigureAfter and @AutoConfigureBefore from discovery client automatic configuration.](https://github.com/Tencent/spring-cloud-tencent/pull/1118)
- [fix:fix feign url bug when using sleuth.](https://github.com/Tencent/spring-cloud-tencent/pull/1119)
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.cloud.client.loadbalancer.LoadBalancedRetryFactory;
import org.springframework.cloud.loadbalancer.blocking.client.BlockingLoadBalancerClient;
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
import org.springframework.cloud.openfeign.loadbalancer.FeignBlockingLoadBalancerClient;
Expand All @@ -40,7 +41,6 @@
public class EnhancedFeignBeanPostProcessor implements BeanPostProcessor, BeanFactoryAware {

private final EnhancedPluginRunner pluginRunner;

private BeanFactory factory;

public EnhancedFeignBeanPostProcessor(EnhancedPluginRunner pluginRunner) {
Expand All @@ -54,24 +54,28 @@ public Object postProcessBeforeInitialization(@NonNull Object bean, @NonNull Str

private Object wrapper(Object bean) {
if (isNeedWrap(bean)) {
Client delegate;
if (bean instanceof LoadBalancerFeignClient) {
LoadBalancerFeignClient client = ((LoadBalancerFeignClient) bean);
return new EnhancedLoadBalancerFeignClient(
createPolarisFeignClient(client.getDelegate()),
factory(),
clientFactory());
}
if (bean instanceof RetryableFeignBlockingLoadBalancerClient
|| bean instanceof FeignBlockingLoadBalancerClient) {
Client delegate;
if (bean instanceof RetryableFeignBlockingLoadBalancerClient) {
delegate = ((RetryableFeignBlockingLoadBalancerClient) bean).getDelegate();
delegate = ((LoadBalancerFeignClient) bean).getDelegate();
if (delegate != null && !(delegate instanceof EnhancedFeignClient)) {
return new LoadBalancerFeignClient(createPolarisFeignClient(delegate),
factory(),
clientFactory());
}
else {
delegate = ((FeignBlockingLoadBalancerClient) bean).getDelegate();
}
if (bean instanceof RetryableFeignBlockingLoadBalancerClient) {
delegate = ((RetryableFeignBlockingLoadBalancerClient) bean).getDelegate();
if (delegate != null && !(delegate instanceof EnhancedFeignClient)) {
EnhancedFeignClient client = createPolarisFeignClient(delegate);
return new RetryableFeignBlockingLoadBalancerClient(client,
blockingLoadBalancerClient(),
loadBalancedRetryFactory());
}
if (delegate != null) {
return new EnhancedFeignBlockingLoadBalancerClient(createPolarisFeignClient(delegate),
}
if (bean instanceof FeignBlockingLoadBalancerClient) {
delegate = ((FeignBlockingLoadBalancerClient) bean).getDelegate();
if (delegate != null && !(delegate instanceof EnhancedFeignClient)) {
return new FeignBlockingLoadBalancerClient(createPolarisFeignClient(delegate),
factory.getBean(BlockingLoadBalancerClient.class));
}
}
Expand All @@ -81,9 +85,7 @@ private Object wrapper(Object bean) {
}

private boolean isNeedWrap(Object bean) {
return bean instanceof Client && !(bean instanceof EnhancedFeignClient)
&& !(bean instanceof EnhancedFeignBlockingLoadBalancerClient)
&& !(bean instanceof EnhancedLoadBalancerFeignClient);
return bean instanceof Client && !(bean instanceof EnhancedFeignClient);
}

private EnhancedFeignClient createPolarisFeignClient(Client delegate) {
Expand All @@ -102,4 +104,12 @@ CachingSpringLoadBalancerFactory factory() {
SpringClientFactory clientFactory() {
return this.factory.getBean(SpringClientFactory.class);
}

LoadBalancedRetryFactory loadBalancedRetryFactory() {
return this.factory.getBean(LoadBalancedRetryFactory.class);
}

BlockingLoadBalancerClient blockingLoadBalancerClient() {
return this.factory.getBean(BlockingLoadBalancerClient.class);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ public void testPostProcessBeforeInitialization() {
Object bean = enhancedFeignBeanPostProcessor.postProcessBeforeInitialization(bean1, "bean1");
assertThat(bean).isNotInstanceOfAny(
EnhancedFeignClient.class,
EnhancedLoadBalancerFeignClient.class,
EnhancedFeignBlockingLoadBalancerClient.class);

// bean instanceOf Client.class
Expand All @@ -83,12 +82,14 @@ public void testPostProcessBeforeInitialization() {
LoadBalancerFeignClient bean3 = mock(LoadBalancerFeignClient.class);
doReturn(mock(Client.class)).when(bean3).getDelegate();
bean = enhancedFeignBeanPostProcessor.postProcessBeforeInitialization(bean3, "bean3");
assertThat(bean).isInstanceOf(EnhancedLoadBalancerFeignClient.class);
assertThat(bean).isInstanceOf(LoadBalancerFeignClient.class);
assertThat(((LoadBalancerFeignClient) bean).getDelegate()).isInstanceOf(EnhancedFeignClient.class);

// bean instanceOf FeignBlockingLoadBalancerClient.class
FeignBlockingLoadBalancerClient bean4 = mock(FeignBlockingLoadBalancerClient.class);
doReturn(mock(Client.class)).when(bean4).getDelegate();
bean = enhancedFeignBeanPostProcessor.postProcessBeforeInitialization(bean4, "bean4");
assertThat(bean).isInstanceOf(EnhancedFeignBlockingLoadBalancerClient.class);
assertThat(bean).isInstanceOf(FeignBlockingLoadBalancerClient.class);
assertThat(((FeignBlockingLoadBalancerClient) bean).getDelegate()).isInstanceOf(EnhancedFeignClient.class);
}
}

This file was deleted.

This file was deleted.