feat:optimize metadata module.

pull/70/head
SkyeBeFreeman 3 years ago
parent fc16d17206
commit 9802f97cfb

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

@ -21,22 +21,14 @@ import java.util.List;
import java.util.Map;
import com.netflix.zuul.ZuulFilter;
import com.tencent.cloud.common.constant.MetadataConstant;
import com.tencent.cloud.metadata.core.filter.gateway.scg.Metadata2HeaderScgFilter;
import com.tencent.cloud.metadata.core.filter.gateway.scg.MetadataFirstScgFilter;
import com.tencent.cloud.metadata.core.filter.gateway.zuul.Metadata2HeaderZuulFilter;
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 com.tencent.cloud.metadata.core.filter.gateway.Metadata2HeaderScgFilter;
import com.tencent.cloud.metadata.core.filter.gateway.Metadata2HeaderZuulFilter;
import com.tencent.cloud.metadata.core.interceptor.Metadata2HeaderFeignInterceptor;
import com.tencent.cloud.metadata.core.interceptor.Metadata2HeaderRestTemplateInterceptor;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
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.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
@ -46,12 +38,6 @@ import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.util.CollectionUtils;
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.
*
@ -60,56 +46,12 @@ import static javax.servlet.DispatcherType.REQUEST;
@Configuration
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.
*/
@Configuration
@ConditionalOnClass(name = "com.netflix.zuul.http.ZuulServlet")
static class MetadataZuulFilterConfig {
@Bean
public ZuulFilter metadataFirstZuulFilter() {
return new MetadataFirstZuulFilter();
}
static class MetadataTransferZuulFilterConfig {
@Bean
public ZuulFilter metadata2HeaderZuulFilter() {
@ -123,12 +65,7 @@ public class MetadataTransferAutoConfiguration {
*/
@Configuration
@ConditionalOnClass(name = "org.springframework.cloud.gateway.filter.GlobalFilter")
static class MetadataScgFilterConfig {
@Bean
public GlobalFilter metadataFirstScgFilter() {
return new MetadataFirstScgFilter();
}
static class MetadataTransferScgFilterConfig {
@Bean
public GlobalFilter metadata2HeaderScgFilter() {
@ -142,15 +79,10 @@ public class MetadataTransferAutoConfiguration {
*/
@Configuration
@ConditionalOnClass(name = "feign.Feign")
static class MetadataFeignPluginConfig {
@Bean
public MetadataFirstFeignInterceptor metadataFirstFeignInterceptor() {
return new MetadataFirstFeignInterceptor();
}
static class MetadataTransferFeignInterceptorConfig {
@Bean
public Metadata2HeaderFeignInterceptor metadataFeignInterceptor() {
public Metadata2HeaderFeignInterceptor metadata2HeaderFeignInterceptor() {
return new Metadata2HeaderFeignInterceptor();
}
@ -161,18 +93,18 @@ public class MetadataTransferAutoConfiguration {
*/
@Configuration
@ConditionalOnClass(name = "org.springframework.web.client.RestTemplate")
static class MetadataRestTemplateConfig implements ApplicationContextAware {
static class MetadataTransferRestTemplateConfig implements ApplicationContextAware {
private ApplicationContext context;
@Bean
public MetadataRestTemplateInterceptor metadataRestTemplateInterceptor() {
return new MetadataRestTemplateInterceptor();
public Metadata2HeaderRestTemplateInterceptor metadata2HeaderRestTemplateInterceptor() {
return new Metadata2HeaderRestTemplateInterceptor();
}
@Bean
BeanPostProcessor metadataRestTemplatePostProcessor(
MetadataRestTemplateInterceptor metadataRestTemplateInterceptor) {
BeanPostProcessor metadata2HeaderRestTemplatePostProcessor(
Metadata2HeaderRestTemplateInterceptor metadata2HeaderRestTemplateInterceptor) {
// Coping with multiple bean injection scenarios
Map<String, RestTemplate> beans = this.context
.getBeansOfType(RestTemplate.class);
@ -184,14 +116,15 @@ public class MetadataTransferAutoConfiguration {
List<ClientHttpRequestInterceptor> interceptors = restTemplate
.getInterceptors();
// Avoid setting interceptor repeatedly.
if (null != interceptors
&& !interceptors.contains(metadataRestTemplateInterceptor)) {
interceptors.add(metadataRestTemplateInterceptor);
if (null != interceptors && !interceptors
.contains(metadata2HeaderRestTemplateInterceptor)) {
interceptors.add(metadata2HeaderRestTemplateInterceptor);
restTemplate.setInterceptors(interceptors);
}
}
}
return new MetadataRestTemplatePostProcessor(metadataRestTemplateInterceptor);
return new Metadata2HeaderRestTemplatePostProcessor(
metadata2HeaderRestTemplateInterceptor);
}
@Override
@ -200,14 +133,14 @@ public class MetadataTransferAutoConfiguration {
this.context = applicationContext;
}
public static class MetadataRestTemplatePostProcessor
public static class Metadata2HeaderRestTemplatePostProcessor
implements BeanPostProcessor {
private MetadataRestTemplateInterceptor metadataRestTemplateInterceptor;
private Metadata2HeaderRestTemplateInterceptor metadata2HeaderRestTemplateInterceptor;
MetadataRestTemplatePostProcessor(
MetadataRestTemplateInterceptor metadataRestTemplateInterceptor) {
this.metadataRestTemplateInterceptor = metadataRestTemplateInterceptor;
Metadata2HeaderRestTemplatePostProcessor(
Metadata2HeaderRestTemplateInterceptor metadata2HeaderRestTemplateInterceptor) {
this.metadata2HeaderRestTemplateInterceptor = metadata2HeaderRestTemplateInterceptor;
}
@Override
@ -222,9 +155,9 @@ public class MetadataTransferAutoConfiguration {
List<ClientHttpRequestInterceptor> interceptors = restTemplate
.getInterceptors();
// Avoid setting interceptor repeatedly.
if (null != interceptors
&& !interceptors.contains(metadataRestTemplateInterceptor)) {
interceptors.add(this.metadataRestTemplateInterceptor);
if (null != interceptors && !interceptors
.contains(metadata2HeaderRestTemplateInterceptor)) {
interceptors.add(this.metadata2HeaderRestTemplateInterceptor);
restTemplate.setInterceptors(interceptors);
}
}

@ -15,7 +15,7 @@
* 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.net.URLEncoder;

@ -15,7 +15,7 @@
* 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.net.URLEncoder;

@ -15,7 +15,7 @@
* 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.net.URLEncoder;
@ -48,7 +48,7 @@ public class Metadata2HeaderFeignInterceptor implements RequestInterceptor, Orde
@Override
public int getOrder() {
return MetadataConstant.OrderConstant.METADATA_2_HEADER_FEIGN_INTERCEPTOR_ORDER;
return MetadataConstant.OrderConstant.METADATA_2_HEADER_INTERCEPTOR_ORDER;
}
@Override

@ -15,7 +15,7 @@
* 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.UnsupportedEncodingException;
@ -41,12 +41,12 @@ import org.springframework.util.StringUtils;
*
* @author Haotian Zhang
*/
public class MetadataRestTemplateInterceptor
public class Metadata2HeaderRestTemplateInterceptor
implements ClientHttpRequestInterceptor, Ordered {
@Override
public int getOrder() {
return MetadataConstant.OrderConstant.INTERCEPTOR_ORDER;
return MetadataConstant.OrderConstant.METADATA_2_HEADER_INTERCEPTOR_ORDER;
}
@Override

@ -17,18 +17,15 @@
package com.tencent.cloud.metadata.config;
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 com.tencent.cloud.metadata.core.filter.gateway.Metadata2HeaderZuulFilter;
import com.tencent.cloud.metadata.core.interceptor.Metadata2HeaderFeignInterceptor;
import com.tencent.cloud.metadata.core.interceptor.Metadata2HeaderRestTemplateInterceptor;
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;
import org.springframework.cloud.gateway.filter.GlobalFilter;
/**
* Test for {@link MetadataTransferAutoConfiguration}
@ -39,10 +36,6 @@ public class MetadataTransferAutoConfigurationTest {
private final ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner();
private final WebApplicationContextRunner webApplicationContextRunner = new WebApplicationContextRunner();
private final ReactiveWebApplicationContextRunner reactiveWebApplicationContextRunner = new ReactiveWebApplicationContextRunner();
/**
* No any web application.
*/
@ -52,90 +45,23 @@ public class MetadataTransferAutoConfigurationTest {
.withConfiguration(
AutoConfigurations.of(MetadataTransferAutoConfiguration.class))
.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(
MetadataTransferAutoConfiguration.MetadataFeignPluginConfig.class);
Assertions.assertThat(context)
.hasSingleBean(MetadataFirstFeignInterceptor.class);
MetadataTransferAutoConfiguration.MetadataTransferFeignInterceptorConfig.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);
});
}
/**
* 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);
MetadataTransferAutoConfiguration.MetadataTransferRestTemplateConfig.class);
Assertions.assertThat(context)
.doesNotHaveBean(MetadataServletFilter.class);
.hasSingleBean(Metadata2HeaderRestTemplateInterceptor.class);
Assertions.assertThat(context).hasSingleBean(
MetadataTransferAutoConfiguration.MetadataFeignPluginConfig.class);
Assertions.assertThat(context)
.hasSingleBean(MetadataFirstFeignInterceptor.class);
Assertions.assertThat(context)
.hasSingleBean(Metadata2HeaderFeignInterceptor.class);
MetadataTransferAutoConfiguration.MetadataTransferRestTemplateConfig.Metadata2HeaderRestTemplatePostProcessor.class);
Assertions.assertThat(context).hasSingleBean(
MetadataTransferAutoConfiguration.MetadataRestTemplateConfig.class);
MetadataTransferAutoConfiguration.MetadataTransferZuulFilterConfig.class);
Assertions.assertThat(context)
.hasSingleBean(MetadataRestTemplateInterceptor.class);
.hasSingleBean(Metadata2HeaderZuulFilter.class);
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.
*/
package com.tencent.cloud.metadata.core.intercepter.feign;
package com.tencent.cloud.metadata.core.intercepter;
import java.io.UnsupportedEncodingException;
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.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.metadata.core.interceptor.feign.Metadata2HeaderFeignInterceptor;
import com.tencent.cloud.metadata.core.interceptor.Metadata2HeaderFeignInterceptor;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.assertj.core.api.Assertions;
@ -65,12 +63,10 @@ public class Metadata2HeaderFeignInterceptorTest {
private TestApplication.TestFeign testFeign;
@Test
public void test1() throws JsonProcessingException {
public void test1() {
String metadata = testFeign.test();
ObjectMapper mapper = new ObjectMapper();
Assertions.assertThat(metadata).isEqualTo(
"{\"a\":\"11\",\"b\":\"22\",\"c\":\"33\"}{\"LOCAL_SERVICE\":\"test"
+ "\",\"LOCAL_PATH\":\"/test\",\"LOCAL_NAMESPACE\":\"default\"}");
Assertions.assertThat(metadata)
.isEqualTo("{\"a\":\"11\",\"b\":\"22\",\"c\":\"33\"}{}");
Assertions.assertThat(metadataLocalProperties.getContent().get("a"))
.isEqualTo("1");
Assertions.assertThat(metadataLocalProperties.getContent().get("b"))

@ -15,16 +15,16 @@
* 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.net.URLDecoder;
import com.tencent.cloud.common.constant.MetadataConstant;
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.metadata.core.interceptor.resttemplate.MetadataRestTemplateInterceptor;
import com.tencent.cloud.metadata.core.interceptor.Metadata2HeaderRestTemplateInterceptor;
import org.assertj.core.api.Assertions;
import org.junit.Test;
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;
/**
* Test for {@link MetadataRestTemplateInterceptor}
* Test for {@link Metadata2HeaderRestTemplateInterceptor}
*
* @author Haotian Zhang
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT,
classes = MetadataRestTemplateInterceptorTest.TestApplication.class,
classes = Metadata2HeaderRestTemplateInterceptorTest.TestApplication.class,
properties = { "spring.config.location = classpath:application-test.yml" })
public class MetadataRestTemplateInterceptorTest {
public class Metadata2HeaderRestTemplateInterceptorTest {
@Autowired
private MetadataLocalProperties metadataLocalProperties;
@ -75,9 +75,8 @@ public class MetadataRestTemplateInterceptorTest {
.exchange("http://localhost:" + localServerPort + "/test", HttpMethod.GET,
httpEntity, String.class)
.getBody();
Assertions.assertThat(metadata).isEqualTo(
"{\"a\":\"11\",\"b\":\"22\",\"c\":\"33\"}{\"LOCAL_SERVICE\":\"test"
+ "\",\"LOCAL_PATH\":\"/test\",\"LOCAL_NAMESPACE\":\"default\"}");
Assertions.assertThat(metadata)
.isEqualTo("{\"a\":\"11\",\"b\":\"22\",\"c\":\"33\"}{}");
Assertions.assertThat(metadataLocalProperties.getContent().get("a"))
.isEqualTo("1");
Assertions.assertThat(metadataLocalProperties.getContent().get("b"))

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

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

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

@ -17,7 +17,7 @@
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.discovery.ConditionalOnPolarisDiscoveryEnabled;
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryAutoConfiguration;

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

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

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

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

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

@ -75,6 +75,24 @@
<version>${commons.io.version}</version>
</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>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>

@ -31,21 +31,6 @@ public final class MetadataConstant {
*/
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.
*/
@ -71,13 +56,7 @@ public final class MetadataConstant {
/**
* Order of filter.
*/
public static final int FILTER_ORDER = Ordered.HIGHEST_PRECEDENCE + 13;
/**
* Order of MetadataFirstFeignPlugin.
*/
public static int METADATA_FIRST_FEIGN_PLUGIN_ORDER = Ordered.HIGHEST_PRECEDENCE
+ 1;
public static final int WEB_FILTER_ORDER = Ordered.HIGHEST_PRECEDENCE + 13;
/**
* Order of MetadataFirstFeignInterceptor.
@ -86,14 +65,9 @@ public final class MetadataConstant {
+ 1;
/**
* Order of Metadata2HeaderFeignInterceptor.
*/
public static int METADATA_2_HEADER_FEIGN_INTERCEPTOR_ORDER = Ordered.LOWEST_PRECEDENCE;
/**
* Order of interceptor.
* Order of Metadata2HeaderInterceptor.
*/
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.concurrent.ConcurrentHashMap;
import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
import com.tencent.cloud.common.util.JacksonUtils;
/**
* Transitive Metadata Context.
* Metadata Context.
*
* @author Haotian Zhang
*/
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.
*/

@ -21,7 +21,7 @@ import java.util.HashMap;
import java.util.List;
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 org.springframework.util.CollectionUtils;
@ -33,23 +33,8 @@ import org.springframework.util.CollectionUtils;
*/
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 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 MetadataContextHolder() {
@ -74,12 +59,6 @@ public final class MetadataContextHolder {
metadataLocalProperties.getTransitive());
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);
}
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.
*/
package com.tencent.cloud.common.metadata;
package com.tencent.cloud.common.metadata.config;
import java.util.ArrayList;
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.metadata.MetadataContext;
@ -57,12 +57,14 @@ public class MetadataFirstScgFilter implements GlobalFilter, Ordered {
// get metadata of current thread
MetadataContext metadataContext = exchange
.getAttribute(MetadataConstant.HeaderName.METADATA_CONTEXT);
if (metadataContext == null) {
metadataContext = MetadataContextHolder.get();
}
// TODO The peer namespace is temporarily the same as the local namespace
metadataContext.putSystemMetadata(
MetadataConstant.SystemMetadataKey.PEER_NAMESPACE,
MetadataContextHolder.get().getSystemMetadata(
MetadataConstant.SystemMetadataKey.LOCAL_NAMESPACE));
MetadataContext.LOCAL_NAMESPACE);
metadataContext.putSystemMetadata(MetadataConstant.SystemMetadataKey.PEER_SERVICE,
route.getId());
metadataContext.putSystemMetadata(MetadataConstant.SystemMetadataKey.PEER_PATH,

@ -14,11 +14,12 @@
* 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.metadata.core.filter.gateway.zuul;
package com.tencent.cloud.common.metadata.filter.gateway;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.tencent.cloud.common.constant.MetadataConstant;
import com.tencent.cloud.common.metadata.MetadataContext;
import com.tencent.cloud.common.metadata.MetadataContextHolder;
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
MetadataContextHolder.get().putSystemMetadata(
MetadataConstant.SystemMetadataKey.PEER_NAMESPACE,
MetadataContextHolder.get().getSystemMetadata(
MetadataConstant.SystemMetadataKey.LOCAL_NAMESPACE));
MetadataContext.LOCAL_NAMESPACE);
MetadataContextHolder.get().putSystemMetadata(
MetadataConstant.SystemMetadataKey.PEER_SERVICE,
(String) requestContext.get(SERVICE_ID_KEY));

@ -15,11 +15,10 @@
* 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.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
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.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
* REACTIVE.
@ -54,7 +49,7 @@ public class MetadataReactiveFilter implements WebFilter, Ordered {
@Override
public int getOrder() {
return MetadataConstant.OrderConstant.FILTER_ORDER;
return MetadataConstant.OrderConstant.WEB_FILTER_ORDER;
}
@Override
@ -79,13 +74,7 @@ public class MetadataReactiveFilter implements WebFilter, Ordered {
Map<String, String> upstreamCustomMetadataMap = JacksonUtils
.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, serverHttpRequest.getURI().getPath());
MetadataContextHolder.init(upstreamCustomMetadataMap, systemMetadataMap);
MetadataContextHolder.init(upstreamCustomMetadataMap, null);
// Save to ServerWebExchange.
serverWebExchange.getAttributes().put(

@ -15,12 +15,11 @@
* 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.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.FilterChain;
@ -38,17 +37,13 @@ import org.springframework.core.annotation.Order;
import org.springframework.util.StringUtils;
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
* SERVLET.
*
* @author Haotian Zhang
*/
@Order(MetadataConstant.OrderConstant.FILTER_ORDER)
@Order(MetadataConstant.OrderConstant.WEB_FILTER_ORDER)
public class MetadataServletFilter extends OncePerRequestFilter {
private static final Logger LOG = LoggerFactory
@ -75,14 +70,8 @@ public class MetadataServletFilter extends OncePerRequestFilter {
Map<String, String> upstreamCustomMetadataMap = JacksonUtils
.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 {
MetadataContextHolder.init(upstreamCustomMetadataMap, systemMetadataMap);
MetadataContextHolder.init(upstreamCustomMetadataMap, null);
filterChain.doFilter(httpServletRequest, httpServletResponse);
}

@ -15,7 +15,7 @@
* 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.metadata.MetadataContext;
@ -43,10 +43,8 @@ public class MetadataFirstFeignInterceptor implements RequestInterceptor, Ordere
MetadataContext metadataContext = MetadataContextHolder.get();
// TODO The peer namespace is temporarily the same as the local namespace
metadataContext.putSystemMetadata(
MetadataConstant.SystemMetadataKey.PEER_NAMESPACE,
MetadataContextHolder.get().getSystemMetadata(
MetadataConstant.SystemMetadataKey.LOCAL_NAMESPACE));
metadataContext.putSystemMetadata(MetadataConstant.SystemMetadataKey.PEER_NAMESPACE,
MetadataContext.LOCAL_NAMESPACE);
metadataContext.putSystemMetadata(MetadataConstant.SystemMetadataKey.PEER_SERVICE,
requestTemplate.feignTarget().name());
metadataContext.putSystemMetadata(MetadataConstant.SystemMetadataKey.PEER_PATH,

@ -1,3 +1,3 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
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.test.context.junit4.SpringRunner;
import static com.tencent.cloud.common.constant.MetadataConstant.SystemMetadataKey.LOCAL_NAMESPACE;
/**
* Test for {@link MetadataContextHolder}
*
@ -61,21 +59,19 @@ public class MetadataContextHolderTest {
customMetadata.put("b", "22");
customMetadata.put("c", "3");
Map<String, String> systemMetadata = new HashMap<>();
systemMetadata.put(LOCAL_NAMESPACE, "namespace");
MetadataContextHolder.init(customMetadata, systemMetadata);
metadataContext = MetadataContextHolder.get();
customMetadata = metadataContext.getAllTransitiveCustomMetadata();
systemMetadata = metadataContext.getAllSystemMetadata();
Assertions.assertThat(customMetadata.get("a")).isEqualTo("1");
Assertions.assertThat(customMetadata.get("b")).isEqualTo("22");
Assertions.assertThat(customMetadata.get("c")).isEqualTo("3");
Assertions.assertThat(systemMetadata.get(LOCAL_NAMESPACE)).isEqualTo("namespace");
Assertions.assertThat(MetadataContext.LOCAL_NAMESPACE).isEqualTo("default");
}
@Test
public void test2() {
Assertions.assertThat(MetadataContextHolder.LOCAL_NAMESPACE).isEqualTo("default");
Assertions.assertThat(MetadataContextHolder.LOCAL_SERVICE).isEqualTo("test");
Assertions.assertThat(MetadataContext.LOCAL_NAMESPACE).isEqualTo("default");
Assertions.assertThat(MetadataContext.LOCAL_SERVICE).isEqualTo("test");
}
@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.
*/
package com.tencent.cloud.common.metadata;
package com.tencent.cloud.common.metadata.config;
import org.assertj.core.api.Assertions;
import org.junit.Test;

@ -15,10 +15,10 @@
* 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.metadata.MetadataLocalProperties;
import com.tencent.cloud.common.metadata.config.MetadataLocalProperties;
import org.assertj.core.api.Assertions;
import org.junit.Before;
import org.junit.Test;
@ -59,7 +59,7 @@ public class MetadataReactiveFilterTest {
@Test
public void test1() {
Assertions.assertThat(this.metadataReactiveFilter.getOrder())
.isEqualTo(MetadataConstant.OrderConstant.FILTER_ORDER);
.isEqualTo(MetadataConstant.OrderConstant.WEB_FILTER_ORDER);
}
@Test

@ -15,7 +15,7 @@
* 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;
@ -23,7 +23,7 @@ import javax.servlet.FilterChain;
import javax.servlet.ServletException;
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.junit.Test;
import org.junit.runner.RunWith;

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

@ -11,10 +11,7 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>polaris-circuitbreaker-example-a</artifactId>
<properties>
<java.version>1.8</java.version>
</properties>
<name>Polaris Circuit Breaker Example A</name>
<dependencies>
<dependency>
@ -26,6 +23,16 @@
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-polaris-circuitbreaker</artifactId>
</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>
<build>

@ -11,10 +11,7 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>polaris-circuitbreaker-example-b</artifactId>
<properties>
<java.version>1.8</java.version>
</properties>
<name>Polaris Circuit Breaker Example B</name>
<dependencies>
<dependency>
@ -51,5 +48,4 @@
</plugin>
</plugins>
</build>
</project>

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

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

@ -17,6 +17,10 @@
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@ -31,13 +35,19 @@ import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/discovery/service/callee")
public class DiscoveryCalleeController {
private static Logger LOG = LoggerFactory.getLogger(DiscoveryCalleeController.class);
@Value("${server.port:0}")
private int port;
/**
* Get information of callee.
* @return information of callee
*/
@GetMapping("/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")
public int sum(@RequestParam int value1, @RequestParam int value2) {
LOG.info("Discovery Service Callee is called and sum is {}.", value1 + value2);
return value1 + value2;
}

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

@ -33,10 +33,10 @@
<!-- <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>com.tencent.cloud</groupId>-->
<!-- <artifactId>spring-cloud-starter-tencent-polaris-router</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-polaris-router</artifactId>
</dependency>
</dependencies>
<build>
<plugins>

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

@ -24,6 +24,7 @@ import com.tencent.cloud.common.constant.MetadataConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@ -37,7 +38,10 @@ import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/gateway/example/callee")
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.
@ -45,7 +49,8 @@ public class GatewayCalleeController {
*/
@RequestMapping("/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(
@RequestHeader(MetadataConstant.HeaderName.CUSTOM_METADATA) String metadataStr)
throws UnsupportedEncodingException {
logger.info(URLDecoder.decode(metadataStr, "UTF-8"));
LOG.info(URLDecoder.decode(metadataStr, "UTF-8"));
return URLDecoder.decode(metadataStr, "UTF-8");
}

Loading…
Cancel
Save