fix guava version conflict bug & fix router strong dependency on LoadBalancerClientFilter (#236)

pull/264/head
lepdou 2 years ago committed by GitHub
parent 80cc2882c6
commit d49520dd11
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -3,3 +3,4 @@
[UT: Add config module unit test](https://github.com/Tencent/spring-cloud-tencent/pull/229) [UT: Add config module unit test](https://github.com/Tencent/spring-cloud-tencent/pull/229)
[Feature: Add config change listener feature support](https://github.com/Tencent/spring-cloud-tencent/pull/220) [Feature: Add config change listener feature support](https://github.com/Tencent/spring-cloud-tencent/pull/220)
[Feature: Support spring cloud gateway routers](https://github.com/Tencent/spring-cloud-tencent/pull/230) [Feature: Support spring cloud gateway routers](https://github.com/Tencent/spring-cloud-tencent/pull/230)
[Bugfix: fix guava version conflict bug & fix router strong dependency on LoadBalancerClientFilter](https://github.com/Tencent/spring-cloud-tencent/pull/236)

@ -0,0 +1,66 @@
/*
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. 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 com.tencent.cloud.polaris.router.beanprocessor;
import java.util.List;
import com.tencent.cloud.common.metadata.config.MetadataLocalProperties;
import com.tencent.cloud.common.util.BeanFactoryUtils;
import com.tencent.cloud.polaris.router.RouterRuleLabelResolver;
import com.tencent.cloud.polaris.router.scg.PolarisLoadBalancerClientFilter;
import com.tencent.cloud.polaris.router.spi.RouterLabelResolver;
import org.springframework.beans.BeansException;
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.LoadBalancerClient;
import org.springframework.cloud.gateway.config.LoadBalancerProperties;
import org.springframework.cloud.gateway.filter.LoadBalancerClientFilter;
/**
* Replace LoadBalancerClientFilter with PolarisLoadBalancerClientFilter.
*@author lepdou 2022-06-15
*/
public class LoadBalancerClientFilterBeanPostProcessor implements BeanPostProcessor, BeanFactoryAware {
private BeanFactory factory;
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.factory = beanFactory;
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof LoadBalancerClientFilter) {
// Support spring cloud gateway router.
// Replaces the default LoadBalancerClientFilter implementation and returns a custom PolarisLoadBalancerClientFilter
LoadBalancerClient loadBalancerClient = this.factory.getBean(LoadBalancerClient.class);
LoadBalancerProperties loadBalancerProperties = this.factory.getBean(LoadBalancerProperties.class);
List<RouterLabelResolver> routerLabelResolvers = BeanFactoryUtils.getBeans(factory, RouterLabelResolver.class);
MetadataLocalProperties metadataLocalProperties = this.factory.getBean(MetadataLocalProperties.class);
RouterRuleLabelResolver routerRuleLabelResolver = this.factory.getBean(RouterRuleLabelResolver.class);
return new PolarisLoadBalancerClientFilter(loadBalancerClient, loadBalancerProperties,
metadataLocalProperties, routerRuleLabelResolver, routerLabelResolvers);
}
return bean;
}
}

@ -16,14 +16,14 @@
* *
*/ */
package com.tencent.cloud.polaris.router; package com.tencent.cloud.polaris.router.beanprocessor;
import java.util.List; import java.util.List;
import com.tencent.cloud.common.metadata.config.MetadataLocalProperties; import com.tencent.cloud.common.metadata.config.MetadataLocalProperties;
import com.tencent.cloud.common.util.BeanFactoryUtils; import com.tencent.cloud.common.util.BeanFactoryUtils;
import com.tencent.cloud.polaris.router.RouterRuleLabelResolver;
import com.tencent.cloud.polaris.router.resttemplate.PolarisLoadBalancerInterceptor; import com.tencent.cloud.polaris.router.resttemplate.PolarisLoadBalancerInterceptor;
import com.tencent.cloud.polaris.router.scg.PolarisLoadBalancerClientFilter;
import com.tencent.cloud.polaris.router.spi.RouterLabelResolver; import com.tencent.cloud.polaris.router.spi.RouterLabelResolver;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
@ -33,17 +33,13 @@ import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerInterceptor; import org.springframework.cloud.client.loadbalancer.LoadBalancerInterceptor;
import org.springframework.cloud.client.loadbalancer.LoadBalancerRequestFactory; import org.springframework.cloud.client.loadbalancer.LoadBalancerRequestFactory;
import org.springframework.cloud.gateway.config.LoadBalancerProperties;
import org.springframework.cloud.gateway.filter.LoadBalancerClientFilter;
/** /**
* Replace LoadBalancerInterceptor with PolarisLoadBalancerInterceptor. * Replace LoadBalancerInterceptor with PolarisLoadBalancerInterceptor.
* PolarisLoadBalancerInterceptor can pass routing context information. * PolarisLoadBalancerInterceptor can pass routing context information.
*<br/>
* Replace LoadBalancerClientFilter with PolarisLoadBalancerClientFilter.
*@author lepdou 2022-05-18 *@author lepdou 2022-05-18
*/ */
public class PolarisLoadBalancerBeanPostProcessor implements BeanPostProcessor, BeanFactoryAware { public class LoadBalancerInterceptorBeanPostProcessor implements BeanPostProcessor, BeanFactoryAware {
private BeanFactory factory; private BeanFactory factory;
@ -66,18 +62,6 @@ public class PolarisLoadBalancerBeanPostProcessor implements BeanPostProcessor,
return new PolarisLoadBalancerInterceptor(loadBalancerClient, requestFactory, return new PolarisLoadBalancerInterceptor(loadBalancerClient, requestFactory,
routerLabelResolvers, metadataLocalProperties, routerRuleLabelResolver); routerLabelResolvers, metadataLocalProperties, routerRuleLabelResolver);
} }
else if (bean instanceof LoadBalancerClientFilter) {
// Support spring cloud gateway router.
// Replaces the default LoadBalancerClientFilter implementation and returns a custom PolarisLoadBalancerClientFilter
LoadBalancerClient loadBalancerClient = this.factory.getBean(LoadBalancerClient.class);
LoadBalancerProperties loadBalancerProperties = this.factory.getBean(LoadBalancerProperties.class);
List<RouterLabelResolver> routerLabelResolvers = BeanFactoryUtils.getBeans(factory, RouterLabelResolver.class);
MetadataLocalProperties metadataLocalProperties = this.factory.getBean(MetadataLocalProperties.class);
RouterRuleLabelResolver routerRuleLabelResolver = this.factory.getBean(RouterRuleLabelResolver.class);
return new PolarisLoadBalancerClientFilter(loadBalancerClient, loadBalancerProperties,
metadataLocalProperties, routerRuleLabelResolver, routerLabelResolvers);
}
return bean; return bean;
} }
} }

@ -19,12 +19,16 @@
package com.tencent.cloud.polaris.router.config; package com.tencent.cloud.polaris.router.config;
import com.tencent.cloud.polaris.context.ServiceRuleManager; import com.tencent.cloud.polaris.context.ServiceRuleManager;
import com.tencent.cloud.polaris.router.PolarisLoadBalancerBeanPostProcessor;
import com.tencent.cloud.polaris.router.RouterRuleLabelResolver; import com.tencent.cloud.polaris.router.RouterRuleLabelResolver;
import com.tencent.cloud.polaris.router.beanprocessor.LoadBalancerClientFilterBeanPostProcessor;
import com.tencent.cloud.polaris.router.beanprocessor.LoadBalancerInterceptorBeanPostProcessor;
import com.tencent.cloud.polaris.router.config.properties.PolarisMetadataRouterProperties; import com.tencent.cloud.polaris.router.config.properties.PolarisMetadataRouterProperties;
import com.tencent.cloud.polaris.router.config.properties.PolarisNearByRouterProperties; import com.tencent.cloud.polaris.router.config.properties.PolarisNearByRouterProperties;
import com.tencent.cloud.polaris.router.config.properties.PolarisRuleBasedRouterProperties; import com.tencent.cloud.polaris.router.config.properties.PolarisRuleBasedRouterProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.cloud.client.loadbalancer.LoadBalancerInterceptor;
import org.springframework.cloud.gateway.filter.LoadBalancerClientFilter;
import org.springframework.cloud.netflix.ribbon.RibbonClients; import org.springframework.cloud.netflix.ribbon.RibbonClients;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@ -45,8 +49,16 @@ public class RouterAutoConfiguration {
@Bean @Bean
@Order(HIGHEST_PRECEDENCE) @Order(HIGHEST_PRECEDENCE)
public PolarisLoadBalancerBeanPostProcessor polarisLoadBalancerBeanPostProcessor() { @ConditionalOnClass(LoadBalancerInterceptor.class)
return new PolarisLoadBalancerBeanPostProcessor(); public LoadBalancerInterceptorBeanPostProcessor loadBalancerInterceptorBeanPostProcessor() {
return new LoadBalancerInterceptorBeanPostProcessor();
}
@Bean
@Order(HIGHEST_PRECEDENCE)
@ConditionalOnClass(LoadBalancerClientFilter.class)
public LoadBalancerClientFilterBeanPostProcessor loadBalancerClientFilterBeanPostProcessor() {
return new LoadBalancerClientFilterBeanPostProcessor();
} }
@Bean @Bean

@ -20,8 +20,8 @@ package com.tencent.cloud.polaris.router.resttemplate;
import com.tencent.cloud.common.metadata.config.MetadataLocalProperties; import com.tencent.cloud.common.metadata.config.MetadataLocalProperties;
import com.tencent.cloud.common.util.BeanFactoryUtils; import com.tencent.cloud.common.util.BeanFactoryUtils;
import com.tencent.cloud.polaris.router.PolarisLoadBalancerBeanPostProcessor;
import com.tencent.cloud.polaris.router.RouterRuleLabelResolver; import com.tencent.cloud.polaris.router.RouterRuleLabelResolver;
import com.tencent.cloud.polaris.router.beanprocessor.LoadBalancerInterceptorBeanPostProcessor;
import com.tencent.cloud.polaris.router.spi.RouterLabelResolver; import com.tencent.cloud.polaris.router.spi.RouterLabelResolver;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
@ -39,7 +39,7 @@ import org.springframework.cloud.client.loadbalancer.LoadBalancerRequestFactory;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
/** /**
* Test for ${@link PolarisLoadBalancerBeanPostProcessor} * Test for ${@link LoadBalancerInterceptorBeanPostProcessor}
* @author lepdou 2022-05-26 * @author lepdou 2022-05-26
*/ */
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
@ -68,7 +68,7 @@ public class PolarisLoadBalancerBeanPostProcessorTest {
.thenReturn(null); .thenReturn(null);
LoadBalancerInterceptor loadBalancerInterceptor = new LoadBalancerInterceptor(loadBalancerClient, loadBalancerRequestFactory); LoadBalancerInterceptor loadBalancerInterceptor = new LoadBalancerInterceptor(loadBalancerClient, loadBalancerRequestFactory);
PolarisLoadBalancerBeanPostProcessor processor = new PolarisLoadBalancerBeanPostProcessor(); LoadBalancerInterceptorBeanPostProcessor processor = new LoadBalancerInterceptorBeanPostProcessor();
processor.setBeanFactory(beanFactory); processor.setBeanFactory(beanFactory);
Object bean = processor.postProcessBeforeInitialization(loadBalancerInterceptor, ""); Object bean = processor.postProcessBeforeInitialization(loadBalancerInterceptor, "");
@ -79,7 +79,7 @@ public class PolarisLoadBalancerBeanPostProcessorTest {
@Test @Test
public void testNotWrapperLoadBalancerInterceptor() { public void testNotWrapperLoadBalancerInterceptor() {
PolarisLoadBalancerBeanPostProcessor processor = new PolarisLoadBalancerBeanPostProcessor(); LoadBalancerInterceptorBeanPostProcessor processor = new LoadBalancerInterceptorBeanPostProcessor();
processor.setBeanFactory(beanFactory); processor.setBeanFactory(beanFactory);
OtherBean otherBean = new OtherBean(); OtherBean otherBean = new OtherBean();

@ -75,6 +75,7 @@
<logback.version>1.2.7</logback.version> <logback.version>1.2.7</logback.version>
<mocktio.version>4.5.1</mocktio.version> <mocktio.version>4.5.1</mocktio.version>
<byte-buddy.version>1.12.10</byte-buddy.version> <byte-buddy.version>1.12.10</byte-buddy.version>
<guava.version>31.0.1-jre</guava.version>
<!-- Maven Plugin Versions --> <!-- Maven Plugin Versions -->
<maven-source-plugin.version>3.2.0</maven-source-plugin.version> <maven-source-plugin.version>3.2.0</maven-source-plugin.version>
@ -147,6 +148,27 @@
<version>${revision}</version> <version>${revision}</version>
</dependency> </dependency>
<!-- third part framework dependencies -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
<exclusions>
<exclusion>
<artifactId>jsr305</artifactId>
<groupId>com.google.code.findbugs</groupId>
</exclusion>
<exclusion>
<artifactId>animal-sniffer-annotations</artifactId>
<groupId>org.codehaus.mojo</groupId>
</exclusion>
<exclusion>
<artifactId>error_prone_annotations</artifactId>
<groupId>com.google.errorprone</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency> <dependency>
<groupId>ch.qos.logback</groupId> <groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId> <artifactId>logback-classic</artifactId>

@ -18,6 +18,7 @@
<module>gateway-zuul-service</module> <module>gateway-zuul-service</module>
<module>gateway-scg-service</module> <module>gateway-scg-service</module>
<module>gateway-callee-service</module> <module>gateway-callee-service</module>
<module>gateway-callee-service2</module>
</modules> </modules>
<build> <build>

Loading…
Cancel
Save