Merge pull request #70 from SkyeBeFreeman/main

feat:optimize metadata module.
pull/71/head
lepdou 3 years ago committed by GitHub
commit 1f81434c33
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -48,6 +48,7 @@
<dependency> <dependency>
<groupId>org.springframework.cloud</groupId> <groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId> <artifactId>spring-cloud-starter-openfeign</artifactId>
<optional>true</optional>
</dependency> </dependency>
<!-- powermock-module-junit4 --> <!-- powermock-module-junit4 -->

@ -21,22 +21,14 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import com.netflix.zuul.ZuulFilter; import com.netflix.zuul.ZuulFilter;
import com.tencent.cloud.common.constant.MetadataConstant; import com.tencent.cloud.metadata.core.filter.gateway.Metadata2HeaderScgFilter;
import com.tencent.cloud.metadata.core.filter.gateway.scg.Metadata2HeaderScgFilter; import com.tencent.cloud.metadata.core.filter.gateway.Metadata2HeaderZuulFilter;
import com.tencent.cloud.metadata.core.filter.gateway.scg.MetadataFirstScgFilter; import com.tencent.cloud.metadata.core.interceptor.Metadata2HeaderFeignInterceptor;
import com.tencent.cloud.metadata.core.filter.gateway.zuul.Metadata2HeaderZuulFilter; import com.tencent.cloud.metadata.core.interceptor.Metadata2HeaderRestTemplateInterceptor;
import com.tencent.cloud.metadata.core.filter.gateway.zuul.MetadataFirstZuulFilter;
import com.tencent.cloud.metadata.core.filter.web.MetadataReactiveFilter;
import com.tencent.cloud.metadata.core.filter.web.MetadataServletFilter;
import com.tencent.cloud.metadata.core.interceptor.feign.Metadata2HeaderFeignInterceptor;
import com.tencent.cloud.metadata.core.interceptor.feign.MetadataFirstFeignInterceptor;
import com.tencent.cloud.metadata.core.interceptor.resttemplate.MetadataRestTemplateInterceptor;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.cloud.gateway.filter.GlobalFilter; import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationContextAware;
@ -46,12 +38,6 @@ import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import static javax.servlet.DispatcherType.ASYNC;
import static javax.servlet.DispatcherType.ERROR;
import static javax.servlet.DispatcherType.FORWARD;
import static javax.servlet.DispatcherType.INCLUDE;
import static javax.servlet.DispatcherType.REQUEST;
/** /**
* Metadata transfer auto configuration. * Metadata transfer auto configuration.
* *
@ -60,56 +46,12 @@ import static javax.servlet.DispatcherType.REQUEST;
@Configuration @Configuration
public class MetadataTransferAutoConfiguration { public class MetadataTransferAutoConfiguration {
/**
* Create when web application type is SERVLET.
*/
@Configuration
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
static class MetadataServletFilterConfig {
@Bean
public FilterRegistrationBean<MetadataServletFilter> metadataServletFilterRegistrationBean(
MetadataServletFilter metadataServletFilter) {
FilterRegistrationBean<MetadataServletFilter> filterRegistrationBean = new FilterRegistrationBean<>(
metadataServletFilter);
filterRegistrationBean.setDispatcherTypes(ASYNC, ERROR, FORWARD, INCLUDE,
REQUEST);
filterRegistrationBean.setOrder(MetadataConstant.OrderConstant.FILTER_ORDER);
return filterRegistrationBean;
}
@Bean
public MetadataServletFilter metadataServletFilter() {
return new MetadataServletFilter();
}
}
/**
* Create when web application type is REACTIVE.
*/
@Configuration
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
static class MetadataReactiveFilterConfig {
@Bean
public MetadataReactiveFilter metadataReactiveFilter() {
return new MetadataReactiveFilter();
}
}
/** /**
* Create when gateway application is Zuul. * Create when gateway application is Zuul.
*/ */
@Configuration @Configuration
@ConditionalOnClass(name = "com.netflix.zuul.http.ZuulServlet") @ConditionalOnClass(name = "com.netflix.zuul.http.ZuulServlet")
static class MetadataZuulFilterConfig { static class MetadataTransferZuulFilterConfig {
@Bean
public ZuulFilter metadataFirstZuulFilter() {
return new MetadataFirstZuulFilter();
}
@Bean @Bean
public ZuulFilter metadata2HeaderZuulFilter() { public ZuulFilter metadata2HeaderZuulFilter() {
@ -123,12 +65,7 @@ public class MetadataTransferAutoConfiguration {
*/ */
@Configuration @Configuration
@ConditionalOnClass(name = "org.springframework.cloud.gateway.filter.GlobalFilter") @ConditionalOnClass(name = "org.springframework.cloud.gateway.filter.GlobalFilter")
static class MetadataScgFilterConfig { static class MetadataTransferScgFilterConfig {
@Bean
public GlobalFilter metadataFirstScgFilter() {
return new MetadataFirstScgFilter();
}
@Bean @Bean
public GlobalFilter metadata2HeaderScgFilter() { public GlobalFilter metadata2HeaderScgFilter() {
@ -142,15 +79,10 @@ public class MetadataTransferAutoConfiguration {
*/ */
@Configuration @Configuration
@ConditionalOnClass(name = "feign.Feign") @ConditionalOnClass(name = "feign.Feign")
static class MetadataFeignPluginConfig { static class MetadataTransferFeignInterceptorConfig {
@Bean
public MetadataFirstFeignInterceptor metadataFirstFeignInterceptor() {
return new MetadataFirstFeignInterceptor();
}
@Bean @Bean
public Metadata2HeaderFeignInterceptor metadataFeignInterceptor() { public Metadata2HeaderFeignInterceptor metadata2HeaderFeignInterceptor() {
return new Metadata2HeaderFeignInterceptor(); return new Metadata2HeaderFeignInterceptor();
} }
@ -161,18 +93,18 @@ public class MetadataTransferAutoConfiguration {
*/ */
@Configuration @Configuration
@ConditionalOnClass(name = "org.springframework.web.client.RestTemplate") @ConditionalOnClass(name = "org.springframework.web.client.RestTemplate")
static class MetadataRestTemplateConfig implements ApplicationContextAware { static class MetadataTransferRestTemplateConfig implements ApplicationContextAware {
private ApplicationContext context; private ApplicationContext context;
@Bean @Bean
public MetadataRestTemplateInterceptor metadataRestTemplateInterceptor() { public Metadata2HeaderRestTemplateInterceptor metadata2HeaderRestTemplateInterceptor() {
return new MetadataRestTemplateInterceptor(); return new Metadata2HeaderRestTemplateInterceptor();
} }
@Bean @Bean
BeanPostProcessor metadataRestTemplatePostProcessor( BeanPostProcessor metadata2HeaderRestTemplatePostProcessor(
MetadataRestTemplateInterceptor metadataRestTemplateInterceptor) { Metadata2HeaderRestTemplateInterceptor metadata2HeaderRestTemplateInterceptor) {
// Coping with multiple bean injection scenarios // Coping with multiple bean injection scenarios
Map<String, RestTemplate> beans = this.context Map<String, RestTemplate> beans = this.context
.getBeansOfType(RestTemplate.class); .getBeansOfType(RestTemplate.class);
@ -184,14 +116,15 @@ public class MetadataTransferAutoConfiguration {
List<ClientHttpRequestInterceptor> interceptors = restTemplate List<ClientHttpRequestInterceptor> interceptors = restTemplate
.getInterceptors(); .getInterceptors();
// Avoid setting interceptor repeatedly. // Avoid setting interceptor repeatedly.
if (null != interceptors if (null != interceptors && !interceptors
&& !interceptors.contains(metadataRestTemplateInterceptor)) { .contains(metadata2HeaderRestTemplateInterceptor)) {
interceptors.add(metadataRestTemplateInterceptor); interceptors.add(metadata2HeaderRestTemplateInterceptor);
restTemplate.setInterceptors(interceptors); restTemplate.setInterceptors(interceptors);
} }
} }
} }
return new MetadataRestTemplatePostProcessor(metadataRestTemplateInterceptor); return new Metadata2HeaderRestTemplatePostProcessor(
metadata2HeaderRestTemplateInterceptor);
} }
@Override @Override
@ -200,14 +133,14 @@ public class MetadataTransferAutoConfiguration {
this.context = applicationContext; this.context = applicationContext;
} }
public static class MetadataRestTemplatePostProcessor public static class Metadata2HeaderRestTemplatePostProcessor
implements BeanPostProcessor { implements BeanPostProcessor {
private MetadataRestTemplateInterceptor metadataRestTemplateInterceptor; private Metadata2HeaderRestTemplateInterceptor metadata2HeaderRestTemplateInterceptor;
MetadataRestTemplatePostProcessor( Metadata2HeaderRestTemplatePostProcessor(
MetadataRestTemplateInterceptor metadataRestTemplateInterceptor) { Metadata2HeaderRestTemplateInterceptor metadata2HeaderRestTemplateInterceptor) {
this.metadataRestTemplateInterceptor = metadataRestTemplateInterceptor; this.metadata2HeaderRestTemplateInterceptor = metadata2HeaderRestTemplateInterceptor;
} }
@Override @Override
@ -222,9 +155,9 @@ public class MetadataTransferAutoConfiguration {
List<ClientHttpRequestInterceptor> interceptors = restTemplate List<ClientHttpRequestInterceptor> interceptors = restTemplate
.getInterceptors(); .getInterceptors();
// Avoid setting interceptor repeatedly. // Avoid setting interceptor repeatedly.
if (null != interceptors if (null != interceptors && !interceptors
&& !interceptors.contains(metadataRestTemplateInterceptor)) { .contains(metadata2HeaderRestTemplateInterceptor)) {
interceptors.add(this.metadataRestTemplateInterceptor); interceptors.add(this.metadata2HeaderRestTemplateInterceptor);
restTemplate.setInterceptors(interceptors); restTemplate.setInterceptors(interceptors);
} }
} }

@ -15,7 +15,7 @@
* specific language governing permissions and limitations under the License. * specific language governing permissions and limitations under the License.
*/ */
package com.tencent.cloud.metadata.core.filter.gateway.scg; package com.tencent.cloud.metadata.core.filter.gateway;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLEncoder; import java.net.URLEncoder;

@ -15,7 +15,7 @@
* specific language governing permissions and limitations under the License. * specific language governing permissions and limitations under the License.
*/ */
package com.tencent.cloud.metadata.core.filter.gateway.zuul; package com.tencent.cloud.metadata.core.filter.gateway;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLEncoder; import java.net.URLEncoder;

@ -15,7 +15,7 @@
* specific language governing permissions and limitations under the License. * specific language governing permissions and limitations under the License.
*/ */
package com.tencent.cloud.metadata.core.interceptor.feign; package com.tencent.cloud.metadata.core.interceptor;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLEncoder; import java.net.URLEncoder;
@ -48,7 +48,7 @@ public class Metadata2HeaderFeignInterceptor implements RequestInterceptor, Orde
@Override @Override
public int getOrder() { public int getOrder() {
return MetadataConstant.OrderConstant.METADATA_2_HEADER_FEIGN_INTERCEPTOR_ORDER; return MetadataConstant.OrderConstant.METADATA_2_HEADER_INTERCEPTOR_ORDER;
} }
@Override @Override

@ -15,7 +15,7 @@
* specific language governing permissions and limitations under the License. * specific language governing permissions and limitations under the License.
*/ */
package com.tencent.cloud.metadata.core.interceptor.resttemplate; package com.tencent.cloud.metadata.core.interceptor;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
@ -41,12 +41,12 @@ import org.springframework.util.StringUtils;
* *
* @author Haotian Zhang * @author Haotian Zhang
*/ */
public class MetadataRestTemplateInterceptor public class Metadata2HeaderRestTemplateInterceptor
implements ClientHttpRequestInterceptor, Ordered { implements ClientHttpRequestInterceptor, Ordered {
@Override @Override
public int getOrder() { public int getOrder() {
return MetadataConstant.OrderConstant.INTERCEPTOR_ORDER; return MetadataConstant.OrderConstant.METADATA_2_HEADER_INTERCEPTOR_ORDER;
} }
@Override @Override

@ -17,18 +17,15 @@
package com.tencent.cloud.metadata.config; package com.tencent.cloud.metadata.config;
import com.tencent.cloud.metadata.core.filter.web.MetadataReactiveFilter; import com.tencent.cloud.metadata.core.filter.gateway.Metadata2HeaderZuulFilter;
import com.tencent.cloud.metadata.core.filter.web.MetadataServletFilter; import com.tencent.cloud.metadata.core.interceptor.Metadata2HeaderFeignInterceptor;
import com.tencent.cloud.metadata.core.interceptor.feign.Metadata2HeaderFeignInterceptor; import com.tencent.cloud.metadata.core.interceptor.Metadata2HeaderRestTemplateInterceptor;
import com.tencent.cloud.metadata.core.interceptor.feign.MetadataFirstFeignInterceptor;
import com.tencent.cloud.metadata.core.interceptor.resttemplate.MetadataRestTemplateInterceptor;
import org.assertj.core.api.Assertions; import org.assertj.core.api.Assertions;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner; import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
/** /**
* Test for {@link MetadataTransferAutoConfiguration} * Test for {@link MetadataTransferAutoConfiguration}
@ -39,10 +36,6 @@ public class MetadataTransferAutoConfigurationTest {
private final ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner(); private final ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner();
private final WebApplicationContextRunner webApplicationContextRunner = new WebApplicationContextRunner();
private final ReactiveWebApplicationContextRunner reactiveWebApplicationContextRunner = new ReactiveWebApplicationContextRunner();
/** /**
* No any web application. * No any web application.
*/ */
@ -52,90 +45,23 @@ public class MetadataTransferAutoConfigurationTest {
.withConfiguration( .withConfiguration(
AutoConfigurations.of(MetadataTransferAutoConfiguration.class)) AutoConfigurations.of(MetadataTransferAutoConfiguration.class))
.run(context -> { .run(context -> {
Assertions.assertThat(context).doesNotHaveBean(
MetadataTransferAutoConfiguration.MetadataReactiveFilterConfig.class);
Assertions.assertThat(context)
.doesNotHaveBean(MetadataReactiveFilter.class);
Assertions.assertThat(context).doesNotHaveBean(
MetadataTransferAutoConfiguration.MetadataServletFilterConfig.class);
Assertions.assertThat(context)
.doesNotHaveBean(MetadataServletFilter.class);
Assertions.assertThat(context).hasSingleBean(
MetadataTransferAutoConfiguration.MetadataFeignPluginConfig.class);
Assertions.assertThat(context)
.hasSingleBean(MetadataFirstFeignInterceptor.class);
Assertions.assertThat(context)
.hasSingleBean(Metadata2HeaderFeignInterceptor.class);
Assertions.assertThat(context).hasSingleBean(
MetadataTransferAutoConfiguration.MetadataRestTemplateConfig.class);
Assertions.assertThat(context)
.hasSingleBean(MetadataRestTemplateInterceptor.class);
Assertions.assertThat(context).hasSingleBean(
MetadataTransferAutoConfiguration.MetadataRestTemplateConfig.MetadataRestTemplatePostProcessor.class);
});
}
/**
* web application.
*/
@Test
public void test2() {
this.webApplicationContextRunner
.withConfiguration(
AutoConfigurations.of(MetadataTransferAutoConfiguration.class))
.run(context -> {
Assertions.assertThat(context).doesNotHaveBean(
MetadataTransferAutoConfiguration.MetadataReactiveFilterConfig.class);
Assertions.assertThat(context)
.doesNotHaveBean(MetadataReactiveFilter.class);
Assertions.assertThat(context).hasSingleBean(
MetadataTransferAutoConfiguration.MetadataServletFilterConfig.class);
Assertions.assertThat(context)
.hasSingleBean(MetadataServletFilter.class);
Assertions.assertThat(context).hasSingleBean( Assertions.assertThat(context).hasSingleBean(
MetadataTransferAutoConfiguration.MetadataFeignPluginConfig.class); MetadataTransferAutoConfiguration.MetadataTransferFeignInterceptorConfig.class);
Assertions.assertThat(context)
.hasSingleBean(MetadataFirstFeignInterceptor.class);
Assertions.assertThat(context) Assertions.assertThat(context)
.hasSingleBean(Metadata2HeaderFeignInterceptor.class); .hasSingleBean(Metadata2HeaderFeignInterceptor.class);
Assertions.assertThat(context).hasSingleBean( Assertions.assertThat(context).hasSingleBean(
MetadataTransferAutoConfiguration.MetadataRestTemplateConfig.class); MetadataTransferAutoConfiguration.MetadataTransferRestTemplateConfig.class);
Assertions.assertThat(context)
.hasSingleBean(MetadataRestTemplateInterceptor.class);
Assertions.assertThat(context).hasSingleBean(
MetadataTransferAutoConfiguration.MetadataRestTemplateConfig.MetadataRestTemplatePostProcessor.class);
});
}
/**
* reactive web application.
*/
@Test
public void test3() {
this.reactiveWebApplicationContextRunner
.withConfiguration(
AutoConfigurations.of(MetadataTransferAutoConfiguration.class))
.run(context -> {
Assertions.assertThat(context).hasSingleBean(
MetadataTransferAutoConfiguration.MetadataReactiveFilterConfig.class);
Assertions.assertThat(context)
.hasSingleBean(MetadataReactiveFilter.class);
Assertions.assertThat(context).doesNotHaveBean(
MetadataTransferAutoConfiguration.MetadataServletFilterConfig.class);
Assertions.assertThat(context) Assertions.assertThat(context)
.doesNotHaveBean(MetadataServletFilter.class); .hasSingleBean(Metadata2HeaderRestTemplateInterceptor.class);
Assertions.assertThat(context).hasSingleBean( Assertions.assertThat(context).hasSingleBean(
MetadataTransferAutoConfiguration.MetadataFeignPluginConfig.class); MetadataTransferAutoConfiguration.MetadataTransferRestTemplateConfig.Metadata2HeaderRestTemplatePostProcessor.class);
Assertions.assertThat(context)
.hasSingleBean(MetadataFirstFeignInterceptor.class);
Assertions.assertThat(context)
.hasSingleBean(Metadata2HeaderFeignInterceptor.class);
Assertions.assertThat(context).hasSingleBean( Assertions.assertThat(context).hasSingleBean(
MetadataTransferAutoConfiguration.MetadataRestTemplateConfig.class); MetadataTransferAutoConfiguration.MetadataTransferZuulFilterConfig.class);
Assertions.assertThat(context) Assertions.assertThat(context)
.hasSingleBean(MetadataRestTemplateInterceptor.class); .hasSingleBean(Metadata2HeaderZuulFilter.class);
Assertions.assertThat(context).hasSingleBean( Assertions.assertThat(context).hasSingleBean(
MetadataTransferAutoConfiguration.MetadataRestTemplateConfig.MetadataRestTemplatePostProcessor.class); MetadataTransferAutoConfiguration.MetadataTransferScgFilterConfig.class);
Assertions.assertThat(context).hasSingleBean(GlobalFilter.class);
}); });
} }

@ -15,18 +15,16 @@
* specific language governing permissions and limitations under the License. * specific language governing permissions and limitations under the License.
*/ */
package com.tencent.cloud.metadata.core.intercepter.feign; package com.tencent.cloud.metadata.core.intercepter;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLDecoder; import java.net.URLDecoder;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tencent.cloud.common.constant.MetadataConstant; import com.tencent.cloud.common.constant.MetadataConstant;
import com.tencent.cloud.common.metadata.MetadataContextHolder; import com.tencent.cloud.common.metadata.MetadataContextHolder;
import com.tencent.cloud.common.metadata.MetadataLocalProperties; import com.tencent.cloud.common.metadata.config.MetadataLocalProperties;
import com.tencent.cloud.common.util.JacksonUtils; import com.tencent.cloud.common.util.JacksonUtils;
import com.tencent.cloud.metadata.core.interceptor.feign.Metadata2HeaderFeignInterceptor; import com.tencent.cloud.metadata.core.interceptor.Metadata2HeaderFeignInterceptor;
import feign.RequestInterceptor; import feign.RequestInterceptor;
import feign.RequestTemplate; import feign.RequestTemplate;
import org.assertj.core.api.Assertions; import org.assertj.core.api.Assertions;
@ -65,12 +63,10 @@ public class Metadata2HeaderFeignInterceptorTest {
private TestApplication.TestFeign testFeign; private TestApplication.TestFeign testFeign;
@Test @Test
public void test1() throws JsonProcessingException { public void test1() {
String metadata = testFeign.test(); String metadata = testFeign.test();
ObjectMapper mapper = new ObjectMapper(); Assertions.assertThat(metadata)
Assertions.assertThat(metadata).isEqualTo( .isEqualTo("{\"a\":\"11\",\"b\":\"22\",\"c\":\"33\"}{}");
"{\"a\":\"11\",\"b\":\"22\",\"c\":\"33\"}{\"LOCAL_SERVICE\":\"test"
+ "\",\"LOCAL_PATH\":\"/test\",\"LOCAL_NAMESPACE\":\"default\"}");
Assertions.assertThat(metadataLocalProperties.getContent().get("a")) Assertions.assertThat(metadataLocalProperties.getContent().get("a"))
.isEqualTo("1"); .isEqualTo("1");
Assertions.assertThat(metadataLocalProperties.getContent().get("b")) Assertions.assertThat(metadataLocalProperties.getContent().get("b"))

@ -15,16 +15,16 @@
* specific language governing permissions and limitations under the License. * specific language governing permissions and limitations under the License.
*/ */
package com.tencent.cloud.metadata.core.intercepter.resttemplate; package com.tencent.cloud.metadata.core.intercepter;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLDecoder; import java.net.URLDecoder;
import com.tencent.cloud.common.constant.MetadataConstant; import com.tencent.cloud.common.constant.MetadataConstant;
import com.tencent.cloud.common.metadata.MetadataContextHolder; import com.tencent.cloud.common.metadata.MetadataContextHolder;
import com.tencent.cloud.common.metadata.MetadataLocalProperties; import com.tencent.cloud.common.metadata.config.MetadataLocalProperties;
import com.tencent.cloud.common.util.JacksonUtils; import com.tencent.cloud.common.util.JacksonUtils;
import com.tencent.cloud.metadata.core.interceptor.resttemplate.MetadataRestTemplateInterceptor; import com.tencent.cloud.metadata.core.interceptor.Metadata2HeaderRestTemplateInterceptor;
import org.assertj.core.api.Assertions; import org.assertj.core.api.Assertions;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -46,15 +46,15 @@ import org.springframework.web.client.RestTemplate;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
/** /**
* Test for {@link MetadataRestTemplateInterceptor} * Test for {@link Metadata2HeaderRestTemplateInterceptor}
* *
* @author Haotian Zhang * @author Haotian Zhang
*/ */
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT, @SpringBootTest(webEnvironment = RANDOM_PORT,
classes = MetadataRestTemplateInterceptorTest.TestApplication.class, classes = Metadata2HeaderRestTemplateInterceptorTest.TestApplication.class,
properties = { "spring.config.location = classpath:application-test.yml" }) properties = { "spring.config.location = classpath:application-test.yml" })
public class MetadataRestTemplateInterceptorTest { public class Metadata2HeaderRestTemplateInterceptorTest {
@Autowired @Autowired
private MetadataLocalProperties metadataLocalProperties; private MetadataLocalProperties metadataLocalProperties;
@ -75,9 +75,8 @@ public class MetadataRestTemplateInterceptorTest {
.exchange("http://localhost:" + localServerPort + "/test", HttpMethod.GET, .exchange("http://localhost:" + localServerPort + "/test", HttpMethod.GET,
httpEntity, String.class) httpEntity, String.class)
.getBody(); .getBody();
Assertions.assertThat(metadata).isEqualTo( Assertions.assertThat(metadata)
"{\"a\":\"11\",\"b\":\"22\",\"c\":\"33\"}{\"LOCAL_SERVICE\":\"test" .isEqualTo("{\"a\":\"11\",\"b\":\"22\",\"c\":\"33\"}{}");
+ "\",\"LOCAL_PATH\":\"/test\",\"LOCAL_NAMESPACE\":\"default\"}");
Assertions.assertThat(metadataLocalProperties.getContent().get("a")) Assertions.assertThat(metadataLocalProperties.getContent().get("a"))
.isEqualTo("1"); .isEqualTo("1");
Assertions.assertThat(metadataLocalProperties.getContent().get("b")) Assertions.assertThat(metadataLocalProperties.getContent().get("b"))

@ -23,11 +23,6 @@
<groupId>com.tencent.cloud</groupId> <groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-polaris-context</artifactId> <artifactId>spring-cloud-tencent-polaris-context</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-metadata-transfer</artifactId>
</dependency>
<!-- Spring Cloud Tencent dependencies end --> <!-- Spring Cloud Tencent dependencies end -->
<!-- Polaris dependencies start --> <!-- Polaris dependencies start -->

@ -84,16 +84,13 @@ public class PolarisFeignClient implements Client {
String method = metadataContext.getSystemMetadata(SystemMetadataKey.PEER_PATH); String method = metadataContext.getSystemMetadata(SystemMetadataKey.PEER_PATH);
resultRequest.setMethod(method); resultRequest.setMethod(method);
resultRequest.setRetStatus(RetStatus.RetSuccess); resultRequest.setRetStatus(RetStatus.RetSuccess);
String sourceNamespace = metadataContext String sourceNamespace = MetadataContext.LOCAL_NAMESPACE;
.getSystemMetadata(SystemMetadataKey.LOCAL_NAMESPACE); String sourceService = MetadataContext.LOCAL_SERVICE;
String sourceService = metadataContext
.getSystemMetadata(SystemMetadataKey.LOCAL_SERVICE);
if (StringUtils.isNotBlank(sourceNamespace) if (StringUtils.isNotBlank(sourceNamespace)
&& StringUtils.isNotBlank(sourceService)) { && StringUtils.isNotBlank(sourceService)) {
resultRequest resultRequest
.setCallerService(new ServiceKey(sourceNamespace, sourceService)); .setCallerService(new ServiceKey(sourceNamespace, sourceService));
} }
URI uri = URI.create(request.url()); URI uri = URI.create(request.url());
resultRequest.setHost(uri.getHost()); resultRequest.setHost(uri.getHost());
resultRequest.setPort(uri.getPort()); resultRequest.setPort(uri.getPort());

@ -20,6 +20,7 @@ package com.tencent.cloud.polaris.discovery;
import java.util.Map; import java.util.Map;
import com.tencent.cloud.common.constant.MetadataConstant.SystemMetadataKey; import com.tencent.cloud.common.constant.MetadataConstant.SystemMetadataKey;
import com.tencent.cloud.common.metadata.MetadataContext;
import com.tencent.cloud.common.metadata.MetadataContextHolder; import com.tencent.cloud.common.metadata.MetadataContextHolder;
import com.tencent.cloud.polaris.PolarisProperties; import com.tencent.cloud.polaris.PolarisProperties;
import com.tencent.polaris.api.core.ConsumerAPI; import com.tencent.polaris.api.core.ConsumerAPI;
@ -65,10 +66,8 @@ public class PolarisDiscoveryHandler {
String method = MetadataContextHolder.get() String method = MetadataContextHolder.get()
.getSystemMetadata(SystemMetadataKey.PEER_PATH); .getSystemMetadata(SystemMetadataKey.PEER_PATH);
getInstancesRequest.setMethod(method); getInstancesRequest.setMethod(method);
String localNamespace = MetadataContextHolder.get() String localNamespace = MetadataContext.LOCAL_NAMESPACE;
.getSystemMetadata(SystemMetadataKey.LOCAL_NAMESPACE); String localService = MetadataContext.LOCAL_SERVICE;
String localService = MetadataContextHolder.get()
.getSystemMetadata(SystemMetadataKey.LOCAL_SERVICE);
Map<String, String> allTransitiveCustomMetadata = MetadataContextHolder.get() Map<String, String> allTransitiveCustomMetadata = MetadataContextHolder.get()
.getAllTransitiveCustomMetadata(); .getAllTransitiveCustomMetadata();
if (StringUtils.isNotBlank(localNamespace) || StringUtils.isNotBlank(localService) if (StringUtils.isNotBlank(localNamespace) || StringUtils.isNotBlank(localService)

@ -21,7 +21,7 @@ import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import com.tencent.cloud.common.metadata.MetadataLocalProperties; import com.tencent.cloud.common.metadata.config.MetadataLocalProperties;
import com.tencent.cloud.polaris.PolarisProperties; import com.tencent.cloud.polaris.PolarisProperties;
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler; import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler;
import com.tencent.cloud.polaris.util.OkHttpUtil; import com.tencent.cloud.polaris.util.OkHttpUtil;

@ -17,7 +17,7 @@
package com.tencent.cloud.polaris.registry; package com.tencent.cloud.polaris.registry;
import com.tencent.cloud.common.metadata.MetadataLocalProperties; import com.tencent.cloud.common.metadata.config.MetadataLocalProperties;
import com.tencent.cloud.polaris.PolarisProperties; import com.tencent.cloud.polaris.PolarisProperties;
import com.tencent.cloud.polaris.discovery.ConditionalOnPolarisDiscoveryEnabled; import com.tencent.cloud.polaris.discovery.ConditionalOnPolarisDiscoveryEnabled;
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryAutoConfiguration; import com.tencent.cloud.polaris.discovery.PolarisDiscoveryAutoConfiguration;

@ -24,11 +24,6 @@
<groupId>com.tencent.cloud</groupId> <groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-polaris-context</artifactId> <artifactId>spring-cloud-tencent-polaris-context</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-metadata-transfer</artifactId>
</dependency>
<!-- Spring Cloud Tencent dependencies end --> <!-- Spring Cloud Tencent dependencies end -->
<!-- Polaris dependencies start --> <!-- Polaris dependencies start -->

@ -21,9 +21,7 @@ import java.nio.charset.StandardCharsets;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import com.tencent.cloud.common.constant.MetadataConstant;
import com.tencent.cloud.common.metadata.MetadataContext; import com.tencent.cloud.common.metadata.MetadataContext;
import com.tencent.cloud.common.metadata.MetadataContextHolder;
import com.tencent.cloud.polaris.ratelimit.constant.RateLimitConstant; import com.tencent.cloud.polaris.ratelimit.constant.RateLimitConstant;
import com.tencent.cloud.polaris.ratelimit.utils.QuotaCheckUtils; import com.tencent.cloud.polaris.ratelimit.utils.QuotaCheckUtils;
import com.tencent.polaris.ratelimit.api.core.LimitAPI; import com.tencent.polaris.ratelimit.api.core.LimitAPI;
@ -66,22 +64,9 @@ public class QuotaCheckReactiveFilter implements WebFilter, Ordered {
@Override @Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) { public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
// get metadata of current thread String localNamespace = MetadataContext.LOCAL_NAMESPACE;
MetadataContext metadataContext = exchange String localService = MetadataContext.LOCAL_SERVICE;
.getAttribute(MetadataConstant.HeaderName.METADATA_CONTEXT); String method = exchange.getRequest().getURI().getPath();
if (metadataContext == null) {
metadataContext = MetadataContextHolder.get();
}
String localNamespace = metadataContext
.getSystemMetadata(MetadataConstant.SystemMetadataKey.LOCAL_NAMESPACE);
String localService = metadataContext
.getSystemMetadata(MetadataConstant.SystemMetadataKey.LOCAL_SERVICE);
// TODO Get path
String method = metadataContext
.getSystemMetadata(MetadataConstant.SystemMetadataKey.LOCAL_PATH);
Map<String, String> labels = null; Map<String, String> labels = null;
if (StringUtils.isNotBlank(method)) { if (StringUtils.isNotBlank(method)) {
labels = new HashMap<>(); labels = new HashMap<>();

@ -26,8 +26,7 @@ import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import com.tencent.cloud.common.constant.MetadataConstant.SystemMetadataKey; import com.tencent.cloud.common.metadata.MetadataContext;
import com.tencent.cloud.common.metadata.MetadataContextHolder;
import com.tencent.cloud.polaris.ratelimit.constant.RateLimitConstant; import com.tencent.cloud.polaris.ratelimit.constant.RateLimitConstant;
import com.tencent.cloud.polaris.ratelimit.utils.QuotaCheckUtils; import com.tencent.cloud.polaris.ratelimit.utils.QuotaCheckUtils;
import com.tencent.polaris.ratelimit.api.core.LimitAPI; import com.tencent.polaris.ratelimit.api.core.LimitAPI;
@ -63,14 +62,9 @@ public class QuotaCheckServletFilter extends OncePerRequestFilter {
protected void doFilterInternal(HttpServletRequest request, protected void doFilterInternal(HttpServletRequest request,
HttpServletResponse response, FilterChain filterChain) HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException { throws ServletException, IOException {
String localNamespace = MetadataContextHolder.get() String localNamespace = MetadataContext.LOCAL_NAMESPACE;
.getSystemMetadata(SystemMetadataKey.LOCAL_NAMESPACE); String localService = MetadataContext.LOCAL_SERVICE;
String localService = MetadataContextHolder.get() String method = request.getRequestURI();
.getSystemMetadata(SystemMetadataKey.LOCAL_SERVICE);
// TODO Get path
String method = MetadataContextHolder.get()
.getSystemMetadata(SystemMetadataKey.LOCAL_PATH);
Map<String, String> labels = null; Map<String, String> labels = null;
if (StringUtils.isNotBlank(method)) { if (StringUtils.isNotBlank(method)) {
labels = new HashMap<>(); labels = new HashMap<>();

@ -24,11 +24,6 @@
<groupId>com.tencent.cloud</groupId> <groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-polaris-context</artifactId> <artifactId>spring-cloud-tencent-polaris-context</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-metadata-transfer</artifactId>
</dependency>
<!-- Spring Cloud Tencent dependencies end --> <!-- Spring Cloud Tencent dependencies end -->
<!-- Polaris dependencies start --> <!-- Polaris dependencies start -->

@ -29,6 +29,7 @@ import com.netflix.loadbalancer.PollingServerListUpdater;
import com.netflix.loadbalancer.Server; import com.netflix.loadbalancer.Server;
import com.netflix.loadbalancer.ServerList; import com.netflix.loadbalancer.ServerList;
import com.tencent.cloud.common.constant.MetadataConstant.SystemMetadataKey; import com.tencent.cloud.common.constant.MetadataConstant.SystemMetadataKey;
import com.tencent.cloud.common.metadata.MetadataContext;
import com.tencent.cloud.common.metadata.MetadataContextHolder; import com.tencent.cloud.common.metadata.MetadataContextHolder;
import com.tencent.cloud.common.pojo.PolarisServer; import com.tencent.cloud.common.pojo.PolarisServer;
import com.tencent.polaris.api.pojo.DefaultInstance; import com.tencent.polaris.api.pojo.DefaultInstance;
@ -72,8 +73,7 @@ public class PolarisRoutingLoadBalancer extends DynamicServerListLoadBalancer<Se
else { else {
String serviceName; String serviceName;
// notice the difference between different service registries // notice the difference between different service registries
if (StringUtils.isNotBlank( if (StringUtils.isNotBlank(allServers.get(0).getMetaInfo().getServiceIdForDiscovery())) {
allServers.get(0).getMetaInfo().getServiceIdForDiscovery())) {
serviceName = allServers.get(0).getMetaInfo().getServiceIdForDiscovery(); serviceName = allServers.get(0).getMetaInfo().getServiceIdForDiscovery();
} }
else { else {
@ -83,12 +83,11 @@ public class PolarisRoutingLoadBalancer extends DynamicServerListLoadBalancer<Se
throw new IllegalStateException( throw new IllegalStateException(
"PolarisRoutingLoadBalancer only Server with AppName or ServiceIdForDiscovery attribute"); "PolarisRoutingLoadBalancer only Server with AppName or ServiceIdForDiscovery attribute");
} }
ServiceKey serviceKey = new ServiceKey(MetadataContextHolder.LOCAL_NAMESPACE, ServiceKey serviceKey = new ServiceKey(MetadataContext.LOCAL_NAMESPACE, serviceName);
serviceName);
List<Instance> instances = new ArrayList<>(8); List<Instance> instances = new ArrayList<>(8);
for (Server server : allServers) { for (Server server : allServers) {
DefaultInstance instance = new DefaultInstance(); DefaultInstance instance = new DefaultInstance();
instance.setNamespace(MetadataContextHolder.LOCAL_NAMESPACE); instance.setNamespace(MetadataContext.LOCAL_NAMESPACE);
instance.setService(serviceName); instance.setService(serviceName);
instance.setHealthy(server.isAlive()); instance.setHealthy(server.isAlive());
instance.setProtocol(server.getScheme()); instance.setProtocol(server.getScheme());
@ -103,14 +102,10 @@ public class PolarisRoutingLoadBalancer extends DynamicServerListLoadBalancer<Se
} }
ProcessRoutersRequest processRoutersRequest = new ProcessRoutersRequest(); ProcessRoutersRequest processRoutersRequest = new ProcessRoutersRequest();
processRoutersRequest.setDstInstances(serviceInstances); processRoutersRequest.setDstInstances(serviceInstances);
String srcNamespace = MetadataContextHolder.get() String srcNamespace = MetadataContext.LOCAL_NAMESPACE;
.getSystemMetadata(SystemMetadataKey.LOCAL_NAMESPACE); String srcService = MetadataContext.LOCAL_SERVICE;
String srcService = MetadataContextHolder.get() Map<String, String> transitiveCustomMetadata = MetadataContextHolder.get().getAllTransitiveCustomMetadata();
.getSystemMetadata(SystemMetadataKey.LOCAL_SERVICE); String method = MetadataContextHolder.get().getSystemMetadata(SystemMetadataKey.PEER_PATH);
Map<String, String> transitiveCustomMetadata = MetadataContextHolder.get()
.getAllTransitiveCustomMetadata();
String method = MetadataContextHolder.get()
.getSystemMetadata(SystemMetadataKey.PEER_PATH);
processRoutersRequest.setMethod(method); processRoutersRequest.setMethod(method);
if (StringUtils.isNotBlank(srcNamespace) && StringUtils.isNotBlank(srcService)) { if (StringUtils.isNotBlank(srcNamespace) && StringUtils.isNotBlank(srcService)) {
ServiceInfo serviceInfo = new ServiceInfo(); ServiceInfo serviceInfo = new ServiceInfo();

@ -75,6 +75,24 @@
<version>${commons.io.version}</version> <version>${commons.io.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<optional>true</optional>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>

@ -31,21 +31,6 @@ public final class MetadataConstant {
*/ */
public static class SystemMetadataKey { public static class SystemMetadataKey {
/**
* Local namespace.
*/
public static String LOCAL_NAMESPACE = "LOCAL_NAMESPACE";
/**
* Local service.
*/
public static String LOCAL_SERVICE = "LOCAL_SERVICE";
/**
* Local path.
*/
public static String LOCAL_PATH = "LOCAL_PATH";
/** /**
* Peer namespace. * Peer namespace.
*/ */
@ -71,13 +56,7 @@ public final class MetadataConstant {
/** /**
* Order of filter. * Order of filter.
*/ */
public static final int FILTER_ORDER = Ordered.HIGHEST_PRECEDENCE + 13; public static final int WEB_FILTER_ORDER = Ordered.HIGHEST_PRECEDENCE + 13;
/**
* Order of MetadataFirstFeignPlugin.
*/
public static int METADATA_FIRST_FEIGN_PLUGIN_ORDER = Ordered.HIGHEST_PRECEDENCE
+ 1;
/** /**
* Order of MetadataFirstFeignInterceptor. * Order of MetadataFirstFeignInterceptor.
@ -86,14 +65,9 @@ public final class MetadataConstant {
+ 1; + 1;
/** /**
* Order of Metadata2HeaderFeignInterceptor. * Order of Metadata2HeaderInterceptor.
*/
public static int METADATA_2_HEADER_FEIGN_INTERCEPTOR_ORDER = Ordered.LOWEST_PRECEDENCE;
/**
* Order of interceptor.
*/ */
public static int INTERCEPTOR_ORDER = Ordered.LOWEST_PRECEDENCE; public static int METADATA_2_HEADER_INTERCEPTOR_ORDER = Ordered.LOWEST_PRECEDENCE;
} }

@ -1,23 +0,0 @@
package com.tencent.cloud.common.metadata;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Metadata auto configuration.
*
* @author Haotian Zhang
*/
@Configuration
public class MetadataAutoConfiguration {
/**
* metadata properties.
* @return metadata properties
*/
@Bean
public MetadataLocalProperties metadataLocalProperties() {
return new MetadataLocalProperties();
}
}

@ -21,15 +21,29 @@ import java.util.Collections;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
import com.tencent.cloud.common.util.JacksonUtils; import com.tencent.cloud.common.util.JacksonUtils;
/** /**
* Transitive Metadata Context. * Metadata Context.
* *
* @author Haotian Zhang * @author Haotian Zhang
*/ */
public class MetadataContext { public class MetadataContext {
/**
* Namespace of local instance.
*/
public static final String LOCAL_NAMESPACE = ApplicationContextAwareUtils
.getProperties("spring.cloud.polaris.discovery.namespace", "default");
/**
* Service name of local instance.
*/
public static final String LOCAL_SERVICE = ApplicationContextAwareUtils.getProperties(
"spring.cloud.polaris.discovery.service",
ApplicationContextAwareUtils.getProperties("spring.application.name", null));
/** /**
* Transitive custom metadata content. * Transitive custom metadata content.
*/ */

@ -21,7 +21,7 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import com.tencent.cloud.common.constant.MetadataConstant; import com.tencent.cloud.common.metadata.config.MetadataLocalProperties;
import com.tencent.cloud.common.util.ApplicationContextAwareUtils; import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
@ -33,23 +33,8 @@ import org.springframework.util.CollectionUtils;
*/ */
public final class MetadataContextHolder { public final class MetadataContextHolder {
/**
* Namespace of local instance.
*/
public static final String LOCAL_NAMESPACE = ApplicationContextAwareUtils
.getProperties("spring.cloud" + ".polaris.discovery.namespace", "default");
private static final ThreadLocal<MetadataContext> METADATA_CONTEXT = new InheritableThreadLocal<>(); private static final ThreadLocal<MetadataContext> METADATA_CONTEXT = new InheritableThreadLocal<>();
private static final String LOCAL_SPRING_APPLICATION_NAME = ApplicationContextAwareUtils
.getProperties("spring.application.name", null);
/**
* Service name of local instance.
*/
public static final String LOCAL_SERVICE = ApplicationContextAwareUtils.getProperties(
"spring.cloud.polaris" + ".discovery.service", LOCAL_SPRING_APPLICATION_NAME);
private static MetadataLocalProperties metadataLocalProperties; private static MetadataLocalProperties metadataLocalProperties;
private MetadataContextHolder() { private MetadataContextHolder() {
@ -74,12 +59,6 @@ public final class MetadataContextHolder {
metadataLocalProperties.getTransitive()); metadataLocalProperties.getTransitive());
metadataContext.putAllTransitiveCustomMetadata(transitiveMetadataMap); metadataContext.putAllTransitiveCustomMetadata(transitiveMetadataMap);
// init system metadata
metadataContext.putSystemMetadata(
MetadataConstant.SystemMetadataKey.LOCAL_NAMESPACE, LOCAL_NAMESPACE);
metadataContext.putSystemMetadata(
MetadataConstant.SystemMetadataKey.LOCAL_SERVICE, LOCAL_SERVICE);
METADATA_CONTEXT.set(metadataContext); METADATA_CONTEXT.set(metadataContext);
} }
return METADATA_CONTEXT.get(); return METADATA_CONTEXT.get();

@ -0,0 +1,140 @@
/*
* 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.config;
import com.netflix.zuul.ZuulFilter;
import com.tencent.cloud.common.constant.MetadataConstant;
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;
import com.tencent.cloud.common.metadata.filter.web.MetadataServletFilter;
import com.tencent.cloud.common.metadata.interceptor.feign.MetadataFirstFeignInterceptor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import static javax.servlet.DispatcherType.ASYNC;
import static javax.servlet.DispatcherType.ERROR;
import static javax.servlet.DispatcherType.FORWARD;
import static javax.servlet.DispatcherType.INCLUDE;
import static javax.servlet.DispatcherType.REQUEST;
/**
* Metadata auto configuration.
*
* @author Haotian Zhang
*/
@Configuration
public class MetadataAutoConfiguration {
/**
* metadata properties.
* @return metadata properties
*/
@Bean
public MetadataLocalProperties metadataLocalProperties() {
return new MetadataLocalProperties();
}
/**
* Create when web application type is SERVLET.
*/
@Configuration
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
static class MetadataServletFilterConfig {
@Bean
public FilterRegistrationBean<MetadataServletFilter> metadataServletFilterRegistrationBean(
MetadataServletFilter metadataServletFilter) {
FilterRegistrationBean<MetadataServletFilter> filterRegistrationBean = new FilterRegistrationBean<>(
metadataServletFilter);
filterRegistrationBean.setDispatcherTypes(ASYNC, ERROR, FORWARD, INCLUDE,
REQUEST);
filterRegistrationBean
.setOrder(MetadataConstant.OrderConstant.WEB_FILTER_ORDER);
return filterRegistrationBean;
}
@Bean
public MetadataServletFilter metadataServletFilter() {
return new MetadataServletFilter();
}
}
/**
* Create when web application type is REACTIVE.
*/
@Configuration
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
static class MetadataReactiveFilterConfig {
@Bean
public MetadataReactiveFilter metadataReactiveFilter() {
return new MetadataReactiveFilter();
}
}
/**
* Create when Feign exists.
*/
@Configuration
@ConditionalOnClass(name = "feign.Feign")
static class MetadataFeignInterceptorConfig {
@Bean
public MetadataFirstFeignInterceptor metadataFirstFeignInterceptor() {
return new MetadataFirstFeignInterceptor();
}
}
/**
* Create when gateway application is Zuul.
*/
@Configuration
@ConditionalOnClass(name = "com.netflix.zuul.http.ZuulServlet")
static class MetadataZuulFilterConfig {
@Bean
public ZuulFilter metadataFirstZuulFilter() {
return new MetadataFirstZuulFilter();
}
}
/**
* Create when gateway application is SCG.
*/
@Configuration
@ConditionalOnClass(name = "org.springframework.cloud.gateway.filter.GlobalFilter")
static class MetadataScgFilterConfig {
@Bean
public GlobalFilter metadataFirstScgFilter() {
return new MetadataFirstScgFilter();
}
}
}

@ -15,7 +15,7 @@
* specific language governing permissions and limitations under the License. * specific language governing permissions and limitations under the License.
*/ */
package com.tencent.cloud.common.metadata; package com.tencent.cloud.common.metadata.config;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;

@ -16,7 +16,7 @@
* *
*/ */
package com.tencent.cloud.metadata.core.filter.gateway.scg; package com.tencent.cloud.common.metadata.filter.gateway;
import com.tencent.cloud.common.constant.MetadataConstant; import com.tencent.cloud.common.constant.MetadataConstant;
import com.tencent.cloud.common.metadata.MetadataContext; import com.tencent.cloud.common.metadata.MetadataContext;
@ -57,12 +57,14 @@ public class MetadataFirstScgFilter implements GlobalFilter, Ordered {
// get metadata of current thread // get metadata of current thread
MetadataContext metadataContext = exchange MetadataContext metadataContext = exchange
.getAttribute(MetadataConstant.HeaderName.METADATA_CONTEXT); .getAttribute(MetadataConstant.HeaderName.METADATA_CONTEXT);
if (metadataContext == null) {
metadataContext = MetadataContextHolder.get();
}
// TODO The peer namespace is temporarily the same as the local namespace // TODO The peer namespace is temporarily the same as the local namespace
metadataContext.putSystemMetadata( metadataContext.putSystemMetadata(
MetadataConstant.SystemMetadataKey.PEER_NAMESPACE, MetadataConstant.SystemMetadataKey.PEER_NAMESPACE,
MetadataContextHolder.get().getSystemMetadata( MetadataContext.LOCAL_NAMESPACE);
MetadataConstant.SystemMetadataKey.LOCAL_NAMESPACE));
metadataContext.putSystemMetadata(MetadataConstant.SystemMetadataKey.PEER_SERVICE, metadataContext.putSystemMetadata(MetadataConstant.SystemMetadataKey.PEER_SERVICE,
route.getId()); route.getId());
metadataContext.putSystemMetadata(MetadataConstant.SystemMetadataKey.PEER_PATH, metadataContext.putSystemMetadata(MetadataConstant.SystemMetadataKey.PEER_PATH,

@ -14,11 +14,12 @@
* CONDITIONS OF ANY KIND, either express or implied. See the License for the * CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License. * specific language governing permissions and limitations under the License.
*/ */
package com.tencent.cloud.metadata.core.filter.gateway.zuul; package com.tencent.cloud.common.metadata.filter.gateway;
import com.netflix.zuul.ZuulFilter; import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext; import com.netflix.zuul.context.RequestContext;
import com.tencent.cloud.common.constant.MetadataConstant; import com.tencent.cloud.common.constant.MetadataConstant;
import com.tencent.cloud.common.metadata.MetadataContext;
import com.tencent.cloud.common.metadata.MetadataContextHolder; import com.tencent.cloud.common.metadata.MetadataContextHolder;
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.PRE_DECORATION_FILTER_ORDER; import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.PRE_DECORATION_FILTER_ORDER;
@ -56,8 +57,7 @@ public class MetadataFirstZuulFilter extends ZuulFilter {
// TODO The peer namespace is temporarily the same as the local namespace // TODO The peer namespace is temporarily the same as the local namespace
MetadataContextHolder.get().putSystemMetadata( MetadataContextHolder.get().putSystemMetadata(
MetadataConstant.SystemMetadataKey.PEER_NAMESPACE, MetadataConstant.SystemMetadataKey.PEER_NAMESPACE,
MetadataContextHolder.get().getSystemMetadata( MetadataContext.LOCAL_NAMESPACE);
MetadataConstant.SystemMetadataKey.LOCAL_NAMESPACE));
MetadataContextHolder.get().putSystemMetadata( MetadataContextHolder.get().putSystemMetadata(
MetadataConstant.SystemMetadataKey.PEER_SERVICE, MetadataConstant.SystemMetadataKey.PEER_SERVICE,
(String) requestContext.get(SERVICE_ID_KEY)); (String) requestContext.get(SERVICE_ID_KEY));

@ -15,11 +15,10 @@
* specific language governing permissions and limitations under the License. * specific language governing permissions and limitations under the License.
*/ */
package com.tencent.cloud.metadata.core.filter.web; package com.tencent.cloud.common.metadata.filter.web;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import com.tencent.cloud.common.constant.MetadataConstant; import com.tencent.cloud.common.constant.MetadataConstant;
@ -37,10 +36,6 @@ import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter; import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain; import org.springframework.web.server.WebFilterChain;
import static com.tencent.cloud.common.constant.MetadataConstant.SystemMetadataKey.LOCAL_NAMESPACE;
import static com.tencent.cloud.common.constant.MetadataConstant.SystemMetadataKey.LOCAL_PATH;
import static com.tencent.cloud.common.constant.MetadataConstant.SystemMetadataKey.LOCAL_SERVICE;
/** /**
* Filter used for storing the metadata from upstream temporarily when web application is * Filter used for storing the metadata from upstream temporarily when web application is
* REACTIVE. * REACTIVE.
@ -54,7 +49,7 @@ public class MetadataReactiveFilter implements WebFilter, Ordered {
@Override @Override
public int getOrder() { public int getOrder() {
return MetadataConstant.OrderConstant.FILTER_ORDER; return MetadataConstant.OrderConstant.WEB_FILTER_ORDER;
} }
@Override @Override
@ -79,13 +74,7 @@ public class MetadataReactiveFilter implements WebFilter, Ordered {
Map<String, String> upstreamCustomMetadataMap = JacksonUtils Map<String, String> upstreamCustomMetadataMap = JacksonUtils
.deserialize2Map(customMetadataStr); .deserialize2Map(customMetadataStr);
// create system metadata. MetadataContextHolder.init(upstreamCustomMetadataMap, null);
Map<String, String> systemMetadataMap = new HashMap<>();
systemMetadataMap.put(LOCAL_NAMESPACE, MetadataContextHolder.LOCAL_NAMESPACE);
systemMetadataMap.put(LOCAL_SERVICE, MetadataContextHolder.LOCAL_SERVICE);
systemMetadataMap.put(LOCAL_PATH, serverHttpRequest.getURI().getPath());
MetadataContextHolder.init(upstreamCustomMetadataMap, systemMetadataMap);
// Save to ServerWebExchange. // Save to ServerWebExchange.
serverWebExchange.getAttributes().put( serverWebExchange.getAttributes().put(

@ -15,12 +15,11 @@
* specific language governing permissions and limitations under the License. * specific language governing permissions and limitations under the License.
*/ */
package com.tencent.cloud.metadata.core.filter.web; package com.tencent.cloud.common.metadata.filter.web;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import javax.servlet.FilterChain; import javax.servlet.FilterChain;
@ -38,17 +37,13 @@ import org.springframework.core.annotation.Order;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.web.filter.OncePerRequestFilter; import org.springframework.web.filter.OncePerRequestFilter;
import static com.tencent.cloud.common.constant.MetadataConstant.SystemMetadataKey.LOCAL_NAMESPACE;
import static com.tencent.cloud.common.constant.MetadataConstant.SystemMetadataKey.LOCAL_PATH;
import static com.tencent.cloud.common.constant.MetadataConstant.SystemMetadataKey.LOCAL_SERVICE;
/** /**
* Filter used for storing the metadata from upstream temporarily when web application is * Filter used for storing the metadata from upstream temporarily when web application is
* SERVLET. * SERVLET.
* *
* @author Haotian Zhang * @author Haotian Zhang
*/ */
@Order(MetadataConstant.OrderConstant.FILTER_ORDER) @Order(MetadataConstant.OrderConstant.WEB_FILTER_ORDER)
public class MetadataServletFilter extends OncePerRequestFilter { public class MetadataServletFilter extends OncePerRequestFilter {
private static final Logger LOG = LoggerFactory private static final Logger LOG = LoggerFactory
@ -75,14 +70,8 @@ public class MetadataServletFilter extends OncePerRequestFilter {
Map<String, String> upstreamCustomMetadataMap = JacksonUtils Map<String, String> upstreamCustomMetadataMap = JacksonUtils
.deserialize2Map(customMetadataStr); .deserialize2Map(customMetadataStr);
// create system metadata.
Map<String, String> systemMetadataMap = new HashMap<>();
systemMetadataMap.put(LOCAL_NAMESPACE, MetadataContextHolder.LOCAL_NAMESPACE);
systemMetadataMap.put(LOCAL_SERVICE, MetadataContextHolder.LOCAL_SERVICE);
systemMetadataMap.put(LOCAL_PATH, httpServletRequest.getRequestURI());
try { try {
MetadataContextHolder.init(upstreamCustomMetadataMap, systemMetadataMap); MetadataContextHolder.init(upstreamCustomMetadataMap, null);
filterChain.doFilter(httpServletRequest, httpServletResponse); filterChain.doFilter(httpServletRequest, httpServletResponse);
} }

@ -15,7 +15,7 @@
* specific language governing permissions and limitations under the License. * specific language governing permissions and limitations under the License.
*/ */
package com.tencent.cloud.metadata.core.interceptor.feign; package com.tencent.cloud.common.metadata.interceptor.feign;
import com.tencent.cloud.common.constant.MetadataConstant; import com.tencent.cloud.common.constant.MetadataConstant;
import com.tencent.cloud.common.metadata.MetadataContext; import com.tencent.cloud.common.metadata.MetadataContext;
@ -43,10 +43,8 @@ public class MetadataFirstFeignInterceptor implements RequestInterceptor, Ordere
MetadataContext metadataContext = MetadataContextHolder.get(); MetadataContext metadataContext = MetadataContextHolder.get();
// TODO The peer namespace is temporarily the same as the local namespace // TODO The peer namespace is temporarily the same as the local namespace
metadataContext.putSystemMetadata( metadataContext.putSystemMetadata(MetadataConstant.SystemMetadataKey.PEER_NAMESPACE,
MetadataConstant.SystemMetadataKey.PEER_NAMESPACE, MetadataContext.LOCAL_NAMESPACE);
MetadataContextHolder.get().getSystemMetadata(
MetadataConstant.SystemMetadataKey.LOCAL_NAMESPACE));
metadataContext.putSystemMetadata(MetadataConstant.SystemMetadataKey.PEER_SERVICE, metadataContext.putSystemMetadata(MetadataConstant.SystemMetadataKey.PEER_SERVICE,
requestTemplate.feignTarget().name()); requestTemplate.feignTarget().name());
metadataContext.putSystemMetadata(MetadataConstant.SystemMetadataKey.PEER_PATH, metadataContext.putSystemMetadata(MetadataConstant.SystemMetadataKey.PEER_PATH,

@ -1,3 +1,3 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.tencent.cloud.common.util.ApplicationContextAwareUtils,\ com.tencent.cloud.common.util.ApplicationContextAwareUtils,\
com.tencent.cloud.common.metadata.MetadataAutoConfiguration com.tencent.cloud.common.metadata.config.MetadataAutoConfiguration

@ -1,45 +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.common.metadata;
import org.assertj.core.api.Assertions;
import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
/**
* Test for {@link MetadataAutoConfiguration}
*
* @author Haotian Zhang
*/
public class MetadataAutoConfigurationTest {
private final ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner();
@Test
public void test1() {
this.applicationContextRunner
.withConfiguration(AutoConfigurations.of(MetadataAutoConfiguration.class))
.run(context -> {
Assertions.assertThat(context)
.hasSingleBean(MetadataLocalProperties.class);
});
}
}

@ -28,8 +28,6 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import static com.tencent.cloud.common.constant.MetadataConstant.SystemMetadataKey.LOCAL_NAMESPACE;
/** /**
* Test for {@link MetadataContextHolder} * Test for {@link MetadataContextHolder}
* *
@ -61,21 +59,19 @@ public class MetadataContextHolderTest {
customMetadata.put("b", "22"); customMetadata.put("b", "22");
customMetadata.put("c", "3"); customMetadata.put("c", "3");
Map<String, String> systemMetadata = new HashMap<>(); Map<String, String> systemMetadata = new HashMap<>();
systemMetadata.put(LOCAL_NAMESPACE, "namespace");
MetadataContextHolder.init(customMetadata, systemMetadata); MetadataContextHolder.init(customMetadata, systemMetadata);
metadataContext = MetadataContextHolder.get(); metadataContext = MetadataContextHolder.get();
customMetadata = metadataContext.getAllTransitiveCustomMetadata(); customMetadata = metadataContext.getAllTransitiveCustomMetadata();
systemMetadata = metadataContext.getAllSystemMetadata();
Assertions.assertThat(customMetadata.get("a")).isEqualTo("1"); Assertions.assertThat(customMetadata.get("a")).isEqualTo("1");
Assertions.assertThat(customMetadata.get("b")).isEqualTo("22"); Assertions.assertThat(customMetadata.get("b")).isEqualTo("22");
Assertions.assertThat(customMetadata.get("c")).isEqualTo("3"); Assertions.assertThat(customMetadata.get("c")).isEqualTo("3");
Assertions.assertThat(systemMetadata.get(LOCAL_NAMESPACE)).isEqualTo("namespace"); Assertions.assertThat(MetadataContext.LOCAL_NAMESPACE).isEqualTo("default");
} }
@Test @Test
public void test2() { public void test2() {
Assertions.assertThat(MetadataContextHolder.LOCAL_NAMESPACE).isEqualTo("default"); Assertions.assertThat(MetadataContext.LOCAL_NAMESPACE).isEqualTo("default");
Assertions.assertThat(MetadataContextHolder.LOCAL_SERVICE).isEqualTo("test"); Assertions.assertThat(MetadataContext.LOCAL_SERVICE).isEqualTo("test");
} }
@SpringBootApplication @SpringBootApplication

@ -0,0 +1,145 @@
/*
* 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.config;
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;
import com.tencent.cloud.common.metadata.filter.web.MetadataServletFilter;
import com.tencent.cloud.common.metadata.interceptor.feign.MetadataFirstFeignInterceptor;
import org.assertj.core.api.Assertions;
import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
/**
* Test for {@link MetadataAutoConfiguration}
*
* @author Haotian Zhang
*/
public class MetadataAutoConfigurationTest {
private final ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner();
private final WebApplicationContextRunner webApplicationContextRunner = new WebApplicationContextRunner();
private final ReactiveWebApplicationContextRunner reactiveWebApplicationContextRunner = new ReactiveWebApplicationContextRunner();
/**
* No any web application.
*/
@Test
public void test1() {
this.applicationContextRunner
.withConfiguration(AutoConfigurations.of(MetadataAutoConfiguration.class))
.run(context -> {
Assertions.assertThat(context)
.hasSingleBean(MetadataLocalProperties.class);
Assertions.assertThat(context).doesNotHaveBean(
MetadataAutoConfiguration.MetadataServletFilterConfig.class);
Assertions.assertThat(context)
.doesNotHaveBean(MetadataServletFilter.class);
Assertions.assertThat(context).doesNotHaveBean(
MetadataAutoConfiguration.MetadataReactiveFilterConfig.class);
Assertions.assertThat(context)
.doesNotHaveBean(MetadataReactiveFilter.class);
Assertions.assertThat(context).hasSingleBean(
MetadataAutoConfiguration.MetadataFeignInterceptorConfig.class);
Assertions.assertThat(context)
.hasSingleBean(MetadataFirstFeignInterceptor.class);
Assertions.assertThat(context).hasSingleBean(
MetadataAutoConfiguration.MetadataZuulFilterConfig.class);
Assertions.assertThat(context)
.hasSingleBean(MetadataFirstZuulFilter.class);
Assertions.assertThat(context).hasSingleBean(
MetadataAutoConfiguration.MetadataScgFilterConfig.class);
Assertions.assertThat(context)
.hasSingleBean(MetadataFirstScgFilter.class);
});
}
/**
* web application.
*/
@Test
public void test2() {
this.webApplicationContextRunner
.withConfiguration(AutoConfigurations.of(MetadataAutoConfiguration.class))
.run(context -> {
Assertions.assertThat(context)
.hasSingleBean(MetadataLocalProperties.class);
Assertions.assertThat(context).hasSingleBean(
MetadataAutoConfiguration.MetadataServletFilterConfig.class);
Assertions.assertThat(context)
.hasSingleBean(MetadataServletFilter.class);
Assertions.assertThat(context).doesNotHaveBean(
MetadataAutoConfiguration.MetadataReactiveFilterConfig.class);
Assertions.assertThat(context)
.doesNotHaveBean(MetadataReactiveFilter.class);
Assertions.assertThat(context).hasSingleBean(
MetadataAutoConfiguration.MetadataFeignInterceptorConfig.class);
Assertions.assertThat(context)
.hasSingleBean(MetadataFirstFeignInterceptor.class);
Assertions.assertThat(context).hasSingleBean(
MetadataAutoConfiguration.MetadataZuulFilterConfig.class);
Assertions.assertThat(context)
.hasSingleBean(MetadataFirstZuulFilter.class);
Assertions.assertThat(context).hasSingleBean(
MetadataAutoConfiguration.MetadataScgFilterConfig.class);
Assertions.assertThat(context)
.hasSingleBean(MetadataFirstScgFilter.class);
});
}
/**
* reactive web application.
*/
@Test
public void test3() {
this.reactiveWebApplicationContextRunner
.withConfiguration(AutoConfigurations.of(MetadataAutoConfiguration.class))
.run(context -> {
Assertions.assertThat(context)
.hasSingleBean(MetadataLocalProperties.class);
Assertions.assertThat(context).doesNotHaveBean(
MetadataAutoConfiguration.MetadataServletFilterConfig.class);
Assertions.assertThat(context)
.doesNotHaveBean(MetadataServletFilter.class);
Assertions.assertThat(context).hasSingleBean(
MetadataAutoConfiguration.MetadataReactiveFilterConfig.class);
Assertions.assertThat(context)
.hasSingleBean(MetadataReactiveFilter.class);
Assertions.assertThat(context).hasSingleBean(
MetadataAutoConfiguration.MetadataFeignInterceptorConfig.class);
Assertions.assertThat(context)
.hasSingleBean(MetadataFirstFeignInterceptor.class);
Assertions.assertThat(context).hasSingleBean(
MetadataAutoConfiguration.MetadataZuulFilterConfig.class);
Assertions.assertThat(context)
.hasSingleBean(MetadataFirstZuulFilter.class);
Assertions.assertThat(context).hasSingleBean(
MetadataAutoConfiguration.MetadataScgFilterConfig.class);
Assertions.assertThat(context)
.hasSingleBean(MetadataFirstScgFilter.class);
});
}
}

@ -15,7 +15,7 @@
* specific language governing permissions and limitations under the License. * specific language governing permissions and limitations under the License.
*/ */
package com.tencent.cloud.common.metadata; package com.tencent.cloud.common.metadata.config;
import org.assertj.core.api.Assertions; import org.assertj.core.api.Assertions;
import org.junit.Test; import org.junit.Test;

@ -15,10 +15,10 @@
* specific language governing permissions and limitations under the License. * specific language governing permissions and limitations under the License.
*/ */
package com.tencent.cloud.metadata.core.filter.web; package com.tencent.cloud.common.metadata.filter.web;
import com.tencent.cloud.common.constant.MetadataConstant; import com.tencent.cloud.common.constant.MetadataConstant;
import com.tencent.cloud.common.metadata.MetadataLocalProperties; import com.tencent.cloud.common.metadata.config.MetadataLocalProperties;
import org.assertj.core.api.Assertions; import org.assertj.core.api.Assertions;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -59,7 +59,7 @@ public class MetadataReactiveFilterTest {
@Test @Test
public void test1() { public void test1() {
Assertions.assertThat(this.metadataReactiveFilter.getOrder()) Assertions.assertThat(this.metadataReactiveFilter.getOrder())
.isEqualTo(MetadataConstant.OrderConstant.FILTER_ORDER); .isEqualTo(MetadataConstant.OrderConstant.WEB_FILTER_ORDER);
} }
@Test @Test

@ -15,7 +15,7 @@
* specific language governing permissions and limitations under the License. * specific language governing permissions and limitations under the License.
*/ */
package com.tencent.cloud.metadata.core.filter.web; package com.tencent.cloud.common.metadata.filter.web;
import java.io.IOException; import java.io.IOException;
@ -23,7 +23,7 @@ import javax.servlet.FilterChain;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import com.tencent.cloud.common.constant.MetadataConstant; import com.tencent.cloud.common.constant.MetadataConstant;
import com.tencent.cloud.common.metadata.MetadataLocalProperties; import com.tencent.cloud.common.metadata.config.MetadataLocalProperties;
import org.assertj.core.api.Assertions; import org.assertj.core.api.Assertions;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;

@ -1,6 +1,10 @@
spring: spring:
application: application:
name: test name: test
autoconfigure:
exclude:
- org.springframework.cloud.gateway.config.GatewayAutoConfiguration
- org.springframework.cloud.netflix.zuul.ZuulServerAutoConfiguration
cloud: cloud:
tencent: tencent:
metadata: metadata:

@ -1,22 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent> <parent>
<artifactId>polaris-circuitbreaker-example</artifactId> <artifactId>polaris-circuitbreaker-example</artifactId>
<groupId>com.tencent.cloud</groupId> <groupId>com.tencent.cloud</groupId>
<version>${revision}</version> <version>${revision}</version>
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>polaris-circuitbreaker-example-a</artifactId> <artifactId>polaris-circuitbreaker-example-a</artifactId>
<name>Polaris Circuit Breaker Example A</name>
<properties> <dependencies>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
@ -26,6 +23,16 @@
<groupId>com.tencent.cloud</groupId> <groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-polaris-circuitbreaker</artifactId> <artifactId>spring-cloud-starter-tencent-polaris-circuitbreaker</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
</dependencies> </dependencies>
<build> <build>

@ -11,12 +11,9 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>polaris-circuitbreaker-example-b</artifactId> <artifactId>polaris-circuitbreaker-example-b</artifactId>
<name>Polaris Circuit Breaker Example B</name>
<properties> <dependencies>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId> <artifactId>spring-boot-starter-webflux</artifactId>
@ -51,5 +48,4 @@
</plugin> </plugin>
</plugins> </plugins>
</build> </build>
</project> </project>

@ -31,7 +31,7 @@ import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/example/service/b") @RequestMapping("/example/service/b")
public class ServiceBController { public class ServiceBController {
@Value("${is-throw-runtime-exception:#{true}}") @Value("${is-throw-runtime-exception:#{false}}")
private boolean isThrowRuntimeException; private boolean isThrowRuntimeException;
/** /**

@ -11,6 +11,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>polaris-circuitbreaker-example</artifactId> <artifactId>polaris-circuitbreaker-example</artifactId>
<name>Polaris Circuit Breaker Example</name>
<packaging>pom</packaging> <packaging>pom</packaging>
<modules> <modules>
@ -20,18 +21,8 @@
<dependencies> <dependencies>
<dependency> <dependency>
<artifactId>spring-cloud-starter-tencent-polaris-discovery</artifactId>
<groupId>com.tencent.cloud</groupId> <groupId>com.tencent.cloud</groupId>
</dependency> <artifactId>spring-cloud-starter-tencent-polaris-discovery</artifactId>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency> </dependency>
</dependencies> </dependencies>

@ -17,6 +17,10 @@
package com.tencent.cloud.polaris.discovery.service.callee; package com.tencent.cloud.polaris.discovery.service.callee;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
@ -31,13 +35,19 @@ import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/discovery/service/callee") @RequestMapping("/discovery/service/callee")
public class DiscoveryCalleeController { public class DiscoveryCalleeController {
private static Logger LOG = LoggerFactory.getLogger(DiscoveryCalleeController.class);
@Value("${server.port:0}")
private int port;
/** /**
* Get information of callee. * Get information of callee.
* @return information of callee * @return information of callee
*/ */
@GetMapping("/info") @GetMapping("/info")
public String info() { public String info() {
return "Discovery Service Callee"; LOG.info("Discovery Service Callee [{}] is called.", port);
return String.format("Discovery Service Callee [%s] is called.", port);
} }
/** /**
@ -48,6 +58,7 @@ public class DiscoveryCalleeController {
*/ */
@GetMapping("/sum") @GetMapping("/sum")
public int sum(@RequestParam int value1, @RequestParam int value2) { public int sum(@RequestParam int value1, @RequestParam int value2) {
LOG.info("Discovery Service Callee is called and sum is {}.", value1 + value2);
return value1 + value2; return value1 + value2;
} }

@ -1,3 +1,9 @@
global:
system:
discoverCluster:
sameAsBuiltin: true
healthCheckCluster:
sameAsBuiltin: true
#global: #global:
# system: # system:
# discoverCluster: # discoverCluster:

@ -23,21 +23,21 @@
<artifactId>spring-cloud-starter-tencent-polaris-discovery</artifactId> <artifactId>spring-cloud-starter-tencent-polaris-discovery</artifactId>
</dependency> </dependency>
<!-- <dependency>--> <!-- <dependency>-->
<!-- <groupId>org.springframework.cloud</groupId>--> <!-- <groupId>org.springframework.cloud</groupId>-->
<!-- <artifactId>spring-cloud-starter-consul-discovery</artifactId>--> <!-- <artifactId>spring-cloud-starter-consul-discovery</artifactId>-->
<!-- </dependency>--> <!-- </dependency>-->
<!-- <dependency>--> <!-- <dependency>-->
<!-- <groupId>org.springframework.cloud</groupId>--> <!-- <groupId>org.springframework.cloud</groupId>-->
<!-- <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>--> <!-- <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>-->
<!-- </dependency>--> <!-- </dependency>-->
<!-- <dependency>--> <dependency>
<!-- <groupId>com.tencent.cloud</groupId>--> <groupId>com.tencent.cloud</groupId>
<!-- <artifactId>spring-cloud-starter-tencent-polaris-router</artifactId>--> <artifactId>spring-cloud-starter-tencent-polaris-router</artifactId>
<!-- </dependency>--> </dependency>
</dependencies> </dependencies>
<build> <build>
<plugins> <plugins>
<plugin> <plugin>

@ -19,11 +19,6 @@
<artifactId>spring-cloud-starter-tencent-polaris-discovery</artifactId> <artifactId>spring-cloud-starter-tencent-polaris-discovery</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-metadata-transfer</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>

@ -24,6 +24,7 @@ import com.tencent.cloud.common.constant.MetadataConstant;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@ -37,7 +38,10 @@ import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/gateway/example/callee") @RequestMapping("/gateway/example/callee")
public class GatewayCalleeController { public class GatewayCalleeController {
private static Logger logger = LoggerFactory.getLogger(GatewayCalleeController.class); private static Logger LOG = LoggerFactory.getLogger(GatewayCalleeController.class);
@Value("${server.port:0}")
private int port;
/** /**
* Get information of callee. * Get information of callee.
@ -45,7 +49,8 @@ public class GatewayCalleeController {
*/ */
@RequestMapping("/info") @RequestMapping("/info")
public String info() { public String info() {
return "Gateway Example Callee"; LOG.info("Gateway Example Callee [{}] is called.", port);
return String.format("Gateway Example Callee [%s] is called.", port);
} }
/** /**
@ -55,7 +60,7 @@ public class GatewayCalleeController {
public String echoHeader( public String echoHeader(
@RequestHeader(MetadataConstant.HeaderName.CUSTOM_METADATA) String metadataStr) @RequestHeader(MetadataConstant.HeaderName.CUSTOM_METADATA) String metadataStr)
throws UnsupportedEncodingException { throws UnsupportedEncodingException {
logger.info(URLDecoder.decode(metadataStr, "UTF-8")); LOG.info(URLDecoder.decode(metadataStr, "UTF-8"));
return URLDecoder.decode(metadataStr, "UTF-8"); return URLDecoder.decode(metadataStr, "UTF-8");
} }

Loading…
Cancel
Save