feat:feat:add spring cloud Greenwich support

pull/83/head
skyehtzhang 3 years ago
parent 9c99474e84
commit 8c9da70750

@ -5,9 +5,9 @@ name: Test with Junit
on:
push:
branches: [ main ]
branches: [ greenwich-new ]
pull_request:
branches: [ main ]
branches: [ greenwich-new ]
jobs:
build:

1
.gitignore vendored

@ -51,6 +51,7 @@ applog/
.flattened-pom.xml
# Polaris
polaris
*/backup
/backup
backup

@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-build</artifactId>
<version>2.3.4.RELEASE</version>
<version>2.1.11.RELEASE</version>
<relativePath/>
</parent>
<modelVersion>4.0.0</modelVersion>
@ -77,13 +77,14 @@
<properties>
<!-- Project revision -->
<revision>1.2.0-Hoxton.SR9-SNAPSHOT</revision>
<revision>1.2.0-Greenwich-SNAPSHOT</revision>
<!-- Spring Cloud -->
<spring.cloud.version>Hoxton.SR9</spring.cloud.version>
<spring.cloud.version>Greenwich.SR6</spring.cloud.version>
<!-- Dependencies -->
<logback.version>1.2.7</logback.version>
<guava.version>23.0</guava.version>
<!-- Maven Plugin Versions -->
<jacoco.version>0.8.3</jacoco.version>
@ -122,6 +123,12 @@
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

@ -70,10 +70,11 @@ public class Metadata2HeaderScgFilter implements GlobalFilter, Ordered {
String metadataStr = JacksonUtils.serialize2Json(customMetadata);
try {
builder.header(MetadataConstant.HeaderName.CUSTOM_METADATA,
URLEncoder.encode(metadataStr, "UTF-8"));
new String[] { URLEncoder.encode(metadataStr, "UTF-8") });
}
catch (UnsupportedEncodingException e) {
builder.header(MetadataConstant.HeaderName.CUSTOM_METADATA, metadataStr);
builder.header(MetadataConstant.HeaderName.CUSTOM_METADATA,
new String[] { metadataStr });
}
}

@ -19,6 +19,7 @@ package com.tencent.cloud.metadata.core.interceptor;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Collections;
import java.util.Map;
import com.tencent.cloud.common.constant.MetadataConstant;
@ -74,7 +75,8 @@ public class Metadata2HeaderFeignInterceptor implements RequestInterceptor, Orde
.getAllTransitiveCustomMetadata();
if (!CollectionUtils.isEmpty(customMetadata)) {
String metadataStr = JacksonUtils.serialize2Json(customMetadata);
requestTemplate.removeHeader(CUSTOM_METADATA);
// empty value, clear the existing values
requestTemplate.header(CUSTOM_METADATA, Collections.emptyList());
try {
requestTemplate.header(CUSTOM_METADATA,
URLEncoder.encode(metadataStr, "UTF-8"));

@ -50,10 +50,8 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
* @author Haotian Zhang
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = DEFINED_PORT,
classes = Metadata2HeaderFeignInterceptorTest.TestApplication.class,
properties = { "server.port=8081",
"spring.config.location = classpath:application-test.yml" })
@SpringBootTest(webEnvironment = DEFINED_PORT, classes = Metadata2HeaderFeignInterceptorTest.TestApplication.class, properties = {
"server.port=8081", "spring.config.location = classpath:application-test.yml" })
public class Metadata2HeaderFeignInterceptorTest {
@Autowired
@ -99,9 +97,9 @@ public class Metadata2HeaderFeignInterceptorTest {
@FeignClient(name = "test-feign", url = "http://localhost:8081")
public interface TestFeign {
@RequestMapping(value = "/test",
headers = { MetadataConstant.HeaderName.CUSTOM_METADATA
+ "={\"a\":\"11" + "\",\"b\":\"22\",\"c\":\"33\"}" })
@RequestMapping(value = "/test", headers = {
MetadataConstant.HeaderName.CUSTOM_METADATA + "={\"a\":\"11"
+ "\",\"b\":\"22\",\"c\":\"33\"}" })
String test();
}

@ -51,9 +51,8 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
* @author Haotian Zhang
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT,
classes = Metadata2HeaderRestTemplateInterceptorTest.TestApplication.class,
properties = { "spring.config.location = classpath:application-test.yml" })
@SpringBootTest(webEnvironment = RANDOM_PORT, classes = Metadata2HeaderRestTemplateInterceptorTest.TestApplication.class, properties = {
"spring.config.location = classpath:application-test.yml" })
public class Metadata2HeaderRestTemplateInterceptorTest {
@Autowired

@ -30,9 +30,8 @@ import org.springframework.context.annotation.Configuration;
*
* @author lepdou 2022-03-29
*/
@ConditionalOnProperty(value = "spring.cloud.polaris.circuitbreaker.enabled",
havingValue = "true", matchIfMissing = true)
@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty(value = "spring.cloud.polaris.circuitbreaker.enabled", havingValue = "true", matchIfMissing = true)
@Configuration
public class PolarisCircuitBreakerBootstrapConfiguration {
@Bean

@ -39,9 +39,8 @@ import static org.springframework.core.Ordered.HIGHEST_PRECEDENCE;
*
* @author Haotian Zhang
*/
@ConditionalOnProperty(value = "spring.cloud.polaris.circuitbreaker.enabled",
havingValue = "true", matchIfMissing = true)
@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty(value = "spring.cloud.polaris.circuitbreaker.enabled", havingValue = "true", matchIfMissing = true)
@Configuration
@AutoConfigureAfter(PolarisContextConfiguration.class)
@AutoConfigureBefore(FeignAutoConfiguration.class)
public class PolarisFeignClientAutoConfiguration {

@ -24,9 +24,7 @@ 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.loadbalancer.blocking.client.BlockingLoadBalancerClient;
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
import org.springframework.cloud.openfeign.loadbalancer.FeignBlockingLoadBalancerClient;
import org.springframework.cloud.openfeign.ribbon.CachingSpringLoadBalancerFactory;
import org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient;
@ -60,12 +58,6 @@ public class PolarisFeignBeanPostProcessor
createPolarisFeignClient(client.getDelegate()), factory(),
clientFactory());
}
if (bean instanceof FeignBlockingLoadBalancerClient) {
FeignBlockingLoadBalancerClient client = (FeignBlockingLoadBalancerClient) bean;
return new PolarisFeignBlockingLoadBalancerClient(
createPolarisFeignClient(client.getDelegate()),
factory.getBean(BlockingLoadBalancerClient.class));
}
return createPolarisFeignClient((Client) bean);
}
return bean;
@ -73,7 +65,6 @@ public class PolarisFeignBeanPostProcessor
private boolean isNeedWrap(Object bean) {
return bean instanceof Client && !(bean instanceof PolarisFeignClient)
&& !(bean instanceof PolarisFeignBlockingLoadBalancerClient)
&& !(bean instanceof PolarisLoadBalancerFeignClient);
}

@ -1,38 +0,0 @@
/*
* 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.circuitbreaker.feign;
import feign.Client;
import org.springframework.cloud.loadbalancer.blocking.client.BlockingLoadBalancerClient;
import org.springframework.cloud.openfeign.loadbalancer.FeignBlockingLoadBalancerClient;
/**
* Wrap for {@link FeignBlockingLoadBalancerClient}.
*
* @author Haotian Zhang
*/
public class PolarisFeignBlockingLoadBalancerClient
extends FeignBlockingLoadBalancerClient {
public PolarisFeignBlockingLoadBalancerClient(Client delegate,
BlockingLoadBalancerClient loadBalancerClient) {
super(delegate, loadBalancerClient);
}
}

@ -21,7 +21,6 @@ import com.tencent.cloud.polaris.circuitbreaker.PolarisFeignClientAutoConfigurat
import com.tencent.cloud.polaris.context.PolarisContextConfiguration;
import feign.Client;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
@ -30,6 +29,8 @@ import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Test for {@link PolarisFeignClient}.
*
@ -48,7 +49,7 @@ public class PolarisFeignClientTest {
public void testPolarisFeignBeanPostProcessor() {
final PolarisFeignBeanPostProcessor postProcessor = springCtx
.getBean(PolarisFeignBeanPostProcessor.class);
Assertions.assertNotNull(postProcessor, "PolarisFeignBeanPostProcessor");
assertThat(postProcessor).isNotNull();
}
@Test
@ -60,9 +61,6 @@ public class PolarisFeignClientTest {
if (client instanceof PolarisLoadBalancerFeignClient) {
return;
}
if (client instanceof PolarisFeignBlockingLoadBalancerClient) {
return;
}
throw new IllegalStateException("Polaris burying failed");
}

@ -37,8 +37,7 @@ public class TestPolarisFeignApp {
SpringApplication.run(TestPolarisFeignApp.class);
}
@FeignClient(name = "feign-service-polaris",
fallback = TestPolarisServiceFallback.class)
@FeignClient(name = "feign-service-polaris", fallback = TestPolarisServiceFallback.class)
public interface TestPolarisService {
/**

@ -32,9 +32,8 @@ import org.springframework.context.annotation.Configuration;
*
* @author lepdou 2022-03-28
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty(value = "spring.cloud.polaris.config.enabled",
matchIfMissing = true)
@Configuration
@ConditionalOnProperty(value = "spring.cloud.polaris.config.enabled", matchIfMissing = true)
public class PolarisConfigAutoConfiguration {
@Bean

@ -36,9 +36,8 @@ import org.springframework.context.annotation.Import;
*
* @author lepdou 2022-03-10
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty(value = "spring.cloud.polaris.config.enabled",
matchIfMissing = true)
@Configuration
@ConditionalOnProperty(value = "spring.cloud.polaris.config.enabled", matchIfMissing = true)
@Import(PolarisContextConfiguration.class)
public class PolarisConfigBootstrapAutoConfiguration {

@ -31,8 +31,7 @@ import org.springframework.cloud.client.ConditionalOnDiscoveryEnabled;
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
@ConditionalOnDiscoveryEnabled
@ConditionalOnProperty(value = "spring.cloud.polaris.discovery.enabled",
matchIfMissing = true)
@ConditionalOnProperty(value = "spring.cloud.polaris.discovery.enabled", matchIfMissing = true)
public @interface ConditionalOnPolarisDiscoveryEnabled {
}

@ -19,7 +19,6 @@
package com.tencent.cloud.polaris.discovery;
import com.tencent.cloud.polaris.PolarisDiscoveryProperties;
import com.tencent.cloud.polaris.discovery.reactive.PolarisReactiveDiscoveryClientConfiguration;
import com.tencent.cloud.polaris.extend.consul.ConsulContextProperties;
import com.tencent.polaris.api.core.ConsumerAPI;
import com.tencent.polaris.api.core.ProviderAPI;
@ -37,11 +36,9 @@ import org.springframework.context.annotation.Import;
*
* @author Haotian Zhang, Andrew Shan, Jie Cheng
*/
@Configuration(proxyBeanMethods = false)
@Configuration
@ConditionalOnPolarisDiscoveryEnabled
@Import({ PolarisDiscoveryClientConfiguration.class,
PolarisReactiveDiscoveryClientConfiguration.class,
ConsulContextProperties.class })
@Import({ PolarisDiscoveryClientConfiguration.class, ConsulContextProperties.class })
public class PolarisDiscoveryAutoConfiguration {
@Bean

@ -20,7 +20,6 @@ package com.tencent.cloud.polaris.discovery;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.cloud.client.CommonsClientAutoConfiguration;
import org.springframework.cloud.client.ConditionalOnBlockingDiscoveryEnabled;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.simple.SimpleDiscoveryClientAutoConfiguration;
import org.springframework.context.annotation.Bean;
@ -31,8 +30,7 @@ import org.springframework.context.annotation.Configuration;
*
* @author Haotian Zhang, Andrew Shan, Jie Cheng
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnBlockingDiscoveryEnabled
@Configuration
@AutoConfigureBefore({ SimpleDiscoveryClientAutoConfiguration.class,
CommonsClientAutoConfiguration.class })
@AutoConfigureAfter(PolarisDiscoveryAutoConfiguration.class)

@ -1,88 +0,0 @@
/*
* 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.discovery.reactive;
import java.util.function.Function;
import com.tencent.cloud.polaris.discovery.PolarisServiceDiscovery;
import com.tencent.polaris.api.exception.PolarisException;
import org.reactivestreams.Publisher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.ReactiveDiscoveryClient;
/**
* Reactive Discovery Client for Polaris.
*
* @author Haotian Zhang, Andrew Shan, Jie Cheng
*/
public class PolarisReactiveDiscoveryClient implements ReactiveDiscoveryClient {
private static final Logger log = LoggerFactory
.getLogger(PolarisReactiveDiscoveryClient.class);
private PolarisServiceDiscovery polarisServiceDiscovery;
public PolarisReactiveDiscoveryClient(
PolarisServiceDiscovery polarisServiceDiscovery) {
this.polarisServiceDiscovery = polarisServiceDiscovery;
}
@Override
public String description() {
return "Spring Cloud Polaris Reactive Discovery Client";
}
@Override
public Flux<ServiceInstance> getInstances(String serviceId) {
return Mono.justOrEmpty(serviceId).flatMapMany(loadInstancesFromPolaris())
.subscribeOn(Schedulers.boundedElastic());
}
private Function<String, Publisher<ServiceInstance>> loadInstancesFromPolaris() {
return serviceId -> {
try {
return Flux.fromIterable(polarisServiceDiscovery.getInstances(serviceId));
}
catch (PolarisException e) {
log.error("get service instance[{}] from polaris error!", serviceId, e);
return Flux.empty();
}
};
}
@Override
public Flux<String> getServices() {
return Flux.defer(() -> {
try {
return Flux.fromIterable(polarisServiceDiscovery.getServices());
}
catch (Exception e) {
log.error("get services from polaris server fail,", e);
return Flux.empty();
}
}).subscribeOn(Schedulers.boundedElastic());
}
}

@ -1,51 +0,0 @@
/*
* 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.discovery.reactive;
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryAutoConfiguration;
import com.tencent.cloud.polaris.discovery.PolarisServiceDiscovery;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cloud.client.ConditionalOnReactiveDiscoveryEnabled;
import org.springframework.cloud.client.ReactiveCommonsClientAutoConfiguration;
import org.springframework.cloud.client.discovery.composite.reactive.ReactiveCompositeDiscoveryClientAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Reactive Discovery Client Configuration for Polaris.
*
* @author Haotian Zhang, Andrew Shan, Jie Cheng
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnReactiveDiscoveryEnabled
@AutoConfigureAfter({ PolarisDiscoveryAutoConfiguration.class,
ReactiveCompositeDiscoveryClientAutoConfiguration.class })
@AutoConfigureBefore({ ReactiveCommonsClientAutoConfiguration.class })
public class PolarisReactiveDiscoveryClientConfiguration {
@Bean
@ConditionalOnMissingBean
public PolarisReactiveDiscoveryClient polarisReactiveDiscoveryClient(
PolarisServiceDiscovery polarisServiceDiscovery) {
return new PolarisReactiveDiscoveryClient(polarisServiceDiscovery);
}
}

@ -40,11 +40,10 @@ import org.springframework.context.annotation.Configuration;
*
* @author Haotian Zhang, Andrew Shan, Jie Cheng
*/
@Configuration(proxyBeanMethods = false)
@Configuration
@EnableConfigurationProperties
@ConditionalOnPolarisDiscoveryEnabled
@ConditionalOnProperty(value = "spring.cloud.service-registry.auto-registration.enabled",
matchIfMissing = true)
@ConditionalOnProperty(value = "spring.cloud.service-registry.auto-registration.enabled", matchIfMissing = true)
@AutoConfigureAfter({ AutoServiceRegistrationConfiguration.class,
AutoServiceRegistrationAutoConfiguration.class,
PolarisDiscoveryAutoConfiguration.class })

@ -30,7 +30,7 @@ import org.springframework.context.annotation.Configuration;
*
* @author Haotian Zhang, Andrew Shan, Jie Cheng
*/
@Configuration(proxyBeanMethods = false)
@Configuration
@EnableConfigurationProperties
@ConditionalOnPolarisDiscoveryEnabled
@AutoConfigureAfter(RibbonAutoConfiguration.class)

@ -70,8 +70,7 @@ public class PolarisDiscoveryClientConfigurationTest {
@Test
public void testDiscoveryBlockingDisabled() {
this.contextRunner
.withPropertyValues("spring.cloud.discovery.blocking.enabled=false")
this.contextRunner.withPropertyValues("spring.cloud.discovery.enabled=false")
.run(context -> assertThat(context)
.doesNotHaveBean(PolarisDiscoveryClient.class));
}

@ -1,80 +0,0 @@
/*
* 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.discovery.reactive;
import com.tencent.cloud.polaris.context.PolarisContextConfiguration;
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryClientConfiguration;
import com.tencent.polaris.test.mock.discovery.NamingServer;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Configuration;
import static com.tencent.polaris.test.common.Consts.PORT;
import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Test for {@link PolarisReactiveDiscoveryClientConfiguration}
*
* @author Haotian Zhang
*/
public class PolarisReactiveDiscoveryClientConfigurationTest {
private static NamingServer namingServer;
private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(PolarisContextConfiguration.class,
PolarisReactiveDiscoveryClientConfiguration.class,
PolarisDiscoveryClientConfiguration.class,
PolarisContextConfiguration.class))
.withPropertyValues("spring.application.name=" + SERVICE_PROVIDER)
.withPropertyValues("server.port=" + PORT)
.withPropertyValues("spring.cloud.polaris.address=grpc://127.0.0.1:10081");
@BeforeClass
public static void beforeClass() throws Exception {
namingServer = NamingServer.startNamingServer(10081);
}
@AfterClass
public static void afterClass() throws Exception {
if (null != namingServer) {
namingServer.terminate();
}
}
@Test
public void testDefaultInitialization() {
this.contextRunner.run(context -> assertThat(context)
.hasSingleBean(PolarisReactiveDiscoveryClient.class));
}
@Configuration
@EnableAutoConfiguration
@EnableDiscoveryClient
static class PolarisReactiveDiscoveryClientConfiguration {
}
}

@ -1,81 +0,0 @@
/*
* 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.discovery.reactive;
import java.util.Arrays;
import com.tencent.cloud.polaris.discovery.PolarisServiceDiscovery;
import com.tencent.polaris.api.exception.PolarisException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.modules.junit4.PowerMockRunner;
import reactor.core.publisher.Flux;
import reactor.test.StepVerifier;
import org.springframework.cloud.client.ServiceInstance;
import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
import static java.util.Collections.singletonList;
import static org.mockito.Mockito.when;
/**
* Test for {@link PolarisReactiveDiscoveryClient}
*
* @author Haotian Zhang
*/
@RunWith(PowerMockRunner.class)
@PowerMockIgnore("javax.management.*")
public class PolarisReactiveDiscoveryClientTest {
@Mock
private PolarisServiceDiscovery serviceDiscovery;
@Mock
private ServiceInstance serviceInstance;
@InjectMocks
private PolarisReactiveDiscoveryClient client;
@Test
public void testGetInstances() throws PolarisException {
when(serviceDiscovery.getInstances(SERVICE_PROVIDER))
.thenReturn(singletonList(serviceInstance));
Flux<ServiceInstance> instances = this.client.getInstances(SERVICE_PROVIDER);
StepVerifier.create(instances).expectNextCount(1).expectComplete().verify();
}
@Test
public void testGetServices() throws PolarisException {
when(serviceDiscovery.getServices())
.thenReturn(Arrays.asList(SERVICE_PROVIDER + 1, SERVICE_PROVIDER + 2));
Flux<String> services = this.client.getServices();
StepVerifier.create(services)
.expectNext(SERVICE_PROVIDER + 1, SERVICE_PROVIDER + 2).expectComplete()
.verify();
}
}

@ -41,8 +41,7 @@ import static javax.servlet.DispatcherType.REQUEST;
* @author Haotian Zhang
*/
@Configuration
@ConditionalOnProperty(name = "spring.cloud.polaris.ratelimit.enabled",
matchIfMissing = true)
@ConditionalOnProperty(name = "spring.cloud.polaris.ratelimit.enabled", matchIfMissing = true)
public class RateLimitConfiguration {
@Bean

@ -55,9 +55,9 @@ import static org.mockito.Mockito.when;
* @author Haotian Zhang
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
classes = { CalleeControllerTests.Config.class, TestController.class },
properties = { "spring.application.name=java_provider_test",
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = {
CalleeControllerTests.Config.class, TestController.class }, properties = {
"spring.application.name=java_provider_test",
"spring.cloud.polaris.discovery.namespace=Test",
"spring.cloud.polaris.address=grpc://127.0.0.1:10081" })
public class CalleeControllerTests {

@ -36,10 +36,9 @@ import org.springframework.context.annotation.Configuration;
*
* @author Haotian Zhang
*/
@Configuration(proxyBeanMethods = false)
@Configuration
@EnableConfigurationProperties
@ConditionalOnProperty(value = "spring.cloud.polaris.loadbalancer.enabled",
matchIfMissing = true)
@ConditionalOnProperty(value = "spring.cloud.polaris.loadbalancer.enabled", matchIfMissing = true)
@AutoConfigureAfter(RibbonAutoConfiguration.class)
@RibbonClients(defaultConfiguration = PolarisRibbonClientConfiguration.class)
public class PolarisRibbonAutoConfiguration {

@ -0,0 +1,53 @@
/*
* 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.common.metadata.aop;
import java.net.URI;
import com.tencent.cloud.common.constant.MetadataConstant;
import com.tencent.cloud.common.metadata.MetadataContextHolder;
import feign.Request;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
/**
* Aspect for getting service name of peer-service in Feign of Greenwich.
*
* @author Haotian Zhang
*/
@Aspect
public class MetadataFeignAspect {
@Pointcut("execution(* feign.Client.execute(..))")
public void execute() {
}
/**
* Get service name before execution of Feign client.
*
* @param joinPoint join point
*/
@Before("execute()")
public void doBefore(JoinPoint joinPoint) {
Request request = (Request) joinPoint.getArgs()[0];
MetadataContextHolder.get().putSystemMetadata(MetadataConstant.SystemMetadataKey.PEER_SERVICE,
URI.create(request.url()).getAuthority());
}
}

@ -19,6 +19,7 @@ package com.tencent.cloud.common.metadata.config;
import com.netflix.zuul.ZuulFilter;
import com.tencent.cloud.common.constant.MetadataConstant;
import com.tencent.cloud.common.metadata.aop.MetadataFeignAspect;
import com.tencent.cloud.common.metadata.filter.gateway.MetadataFirstScgFilter;
import com.tencent.cloud.common.metadata.filter.gateway.MetadataFirstZuulFilter;
import com.tencent.cloud.common.metadata.filter.web.MetadataReactiveFilter;
@ -107,6 +108,11 @@ public class MetadataAutoConfiguration {
return new MetadataFirstFeignInterceptor();
}
@Bean
public MetadataFeignAspect metadataFeignAspect() {
return new MetadataFeignAspect();
}
}
/**

@ -66,7 +66,7 @@ public class MetadataFirstScgFilter implements GlobalFilter, Ordered {
MetadataConstant.SystemMetadataKey.PEER_NAMESPACE,
MetadataContext.LOCAL_NAMESPACE);
metadataContext.putSystemMetadata(MetadataConstant.SystemMetadataKey.PEER_SERVICE,
route.getId());
route.getUri().getAuthority());
metadataContext.putSystemMetadata(MetadataConstant.SystemMetadataKey.PEER_PATH,
exchange.getRequest().getURI().getPath());

@ -46,8 +46,9 @@ public class MetadataFirstFeignInterceptor implements RequestInterceptor, Ordere
metadataContext.putSystemMetadata(
MetadataConstant.SystemMetadataKey.PEER_NAMESPACE,
MetadataContext.LOCAL_NAMESPACE);
metadataContext.putSystemMetadata(MetadataConstant.SystemMetadataKey.PEER_SERVICE,
requestTemplate.feignTarget().name());
// Cannot get service name of peer-service in Feign interceptor of Greenwich.
// metadataContext.putSystemMetadata(MetadataConstant.SystemMetadataKey.PEER_SERVICE,
// requestTemplate.feignTarget().name());
metadataContext.putSystemMetadata(MetadataConstant.SystemMetadataKey.PEER_PATH,
requestTemplate.path());
}

@ -17,6 +17,7 @@
package com.tencent.cloud.common.util;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@ -73,7 +74,7 @@ public final class JacksonUtils {
}
return new HashMap<>();
}
catch (JsonProcessingException e) {
catch (IOException e) {
LOG.error(
"Json to map failed. check if the format of the json string[{}] is correct.",
jsonStr, e);

@ -34,9 +34,8 @@ import org.springframework.test.context.junit4.SpringRunner;
* @author Haotian Zhang
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
classes = MetadataContextHolderTest.TestApplication.class,
properties = { "spring.config.location = classpath:application-test.yml" })
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = MetadataContextHolderTest.TestApplication.class, properties = {
"spring.config.location = classpath:application-test.yml" })
public class MetadataContextHolderTest {
@Test

@ -32,9 +32,8 @@ import org.springframework.test.context.junit4.SpringRunner;
* @author Haotian Zhang
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
classes = MetadataLocalPropertiesTest.TestApplication.class,
properties = { "spring.config.location = classpath:application-test.yml" })
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = MetadataLocalPropertiesTest.TestApplication.class, properties = {
"spring.config.location = classpath:application-test.yml" })
public class MetadataLocalPropertiesTest {
@Autowired

@ -41,9 +41,8 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
* @author Haotian Zhang
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = MOCK,
classes = MetadataServletFilterTest.TestApplication.class,
properties = { "spring.config.location = classpath:application-test.yml" })
@SpringBootTest(webEnvironment = MOCK, classes = MetadataServletFilterTest.TestApplication.class, properties = {
"spring.config.location = classpath:application-test.yml" })
public class MetadataReactiveFilterTest {
@Autowired

@ -43,9 +43,8 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
* @author Haotian Zhang
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT,
classes = MetadataServletFilterTest.TestApplication.class,
properties = { "spring.config.location = classpath:application-test.yml" })
@SpringBootTest(webEnvironment = RANDOM_PORT, classes = MetadataServletFilterTest.TestApplication.class, properties = {
"spring.config.location = classpath:application-test.yml" })
public class MetadataServletFilterTest {
@Autowired

@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies-parent</artifactId>
<version>2.3.1.RELEASE</version>
<version>2.1.11.RELEASE</version>
<relativePath/>
</parent>
<modelVersion>4.0.0</modelVersion>
@ -62,7 +62,7 @@
</developers>
<properties>
<revision>1.2.0-Hoxton.SR9-SNAPSHOT</revision>
<revision>1.2.0-Greenwich-SNAPSHOT</revision>
<polaris.version>1.3.1</polaris.version>
<powermock.version>2.0.0</powermock.version>

@ -25,8 +25,7 @@ import org.springframework.web.bind.annotation.GetMapping;
*
* @author Haotian Zhang
*/
@FeignClient(name = "polaris-circuitbreaker-example-b",
fallback = ProviderBFallback.class)
@FeignClient(name = "polaris-circuitbreaker-example-b", fallback = ProviderBFallback.class)
public interface ProviderB {
/**

@ -26,8 +26,7 @@ import org.springframework.web.bind.annotation.RequestParam;
*
* @author Haotian Zhang
*/
@FeignClient(value = "DiscoveryCalleeService",
fallback = DiscoveryCalleeServiceFallback.class)
@FeignClient(value = "DiscoveryCalleeService", fallback = DiscoveryCalleeServiceFallback.class)
public interface DiscoveryCalleeService {
/**

@ -30,6 +30,7 @@ import com.tencent.polaris.factory.config.ConfigurationImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.core.env.Environment;
import org.springframework.util.CollectionUtils;
@ -50,6 +51,7 @@ public class PolarisContextProperties {
/**
* current server local ip address.
*/
@Value("${spring.cloud.polaris.local-ip-address:}")
private String localIpAddress;
/**

@ -18,9 +18,9 @@
package com.tencent.cloud.polaris.context;
import com.tencent.polaris.client.api.SDKContext;
import org.apache.commons.lang.StringUtils;
import org.junit.Assert;
import org.junit.Test;
import org.junit.platform.commons.util.StringUtils;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
@ -29,8 +29,8 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = PolarisContextApplication.class,
properties = { "spring.config.location = classpath:bootstrap.yml" })
@SpringBootTest(classes = PolarisContextApplication.class, properties = {
"spring.config.location = classpath:bootstrap.yml" })
@ImportAutoConfiguration({ PolarisContextConfiguration.class })
public class PolarisContextGetHostTest {

Loading…
Cancel
Save