fix:fix wrong context data storage.

pull/170/head
SkyeBeFreeman 3 years ago
parent 36cb6f874e
commit 266ee13bd2

@ -1,5 +1,4 @@
# Change Log # Change Log
--- ---
- [fix:fix routes of gateway doesn't refresh bug.](https://github.com/Tencent/spring-cloud-tencent/pull/158) - [fix:fix wrong context data storage.](https://github.com/Tencent/spring-cloud-tencent/pull/170)
- [fix:Turn off automatic injection of Polars rule.](https://github.com/Tencent/spring-cloud-tencent/pull/162)

@ -0,0 +1,5 @@
# Change Log
---
- [fix:fix routes of gateway doesn't refresh bug.](https://github.com/Tencent/spring-cloud-tencent/pull/158)
- [fix:Turn off automatic injection of Polars rule.](https://github.com/Tencent/spring-cloud-tencent/pull/162)

@ -86,7 +86,7 @@
<properties> <properties>
<!-- Project revision --> <!-- Project revision -->
<revision>1.4.2-Hoxton.SR9</revision> <revision>1.4.3-Hoxton.SR9-SNAPSHOT</revision>
<!-- Spring Cloud --> <!-- Spring Cloud -->
<spring.cloud.version>Hoxton.SR9</spring.cloud.version> <spring.cloud.version>Hoxton.SR9</spring.cloud.version>

@ -75,7 +75,7 @@ public class DecodeTransferMetadataReactiveFilter implements WebFilter, Ordered
Map<String, String> upstreamCustomMetadataMap = JacksonUtils Map<String, String> upstreamCustomMetadataMap = JacksonUtils
.deserialize2Map(customMetadataStr); .deserialize2Map(customMetadataStr);
MetadataContextHolder.init(upstreamCustomMetadataMap, null); MetadataContextHolder.init(upstreamCustomMetadataMap);
// Save to ServerWebExchange. // Save to ServerWebExchange.
serverWebExchange.getAttributes().put( serverWebExchange.getAttributes().put(

@ -72,7 +72,7 @@ public class DecodeTransferMetadataServletFilter extends OncePerRequestFilter {
.deserialize2Map(customMetadataStr); .deserialize2Map(customMetadataStr);
try { try {
MetadataContextHolder.init(upstreamCustomMetadataMap, null); MetadataContextHolder.init(upstreamCustomMetadataMap);
filterChain.doFilter(httpServletRequest, httpServletResponse); filterChain.doFilter(httpServletRequest, httpServletResponse);
} }

@ -24,7 +24,6 @@ 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.config.MetadataLocalProperties; import com.tencent.cloud.common.metadata.config.MetadataLocalProperties;
import com.tencent.cloud.common.util.JacksonUtils;
import com.tencent.cloud.metadata.core.EncodeTransferMedataFeignInterceptor; import com.tencent.cloud.metadata.core.EncodeTransferMedataFeignInterceptor;
import feign.RequestInterceptor; import feign.RequestInterceptor;
import feign.RequestTemplate; import feign.RequestTemplate;
@ -67,7 +66,7 @@ public class EncodeTransferMedataFeignInterceptorTest {
public void test1() { public void test1() {
String metadata = testFeign.test(); String metadata = testFeign.test();
Assertions.assertThat(metadata) Assertions.assertThat(metadata)
.isEqualTo("{\"a\":\"11\",\"b\":\"22\",\"c\":\"33\"}{}"); .isEqualTo("{\"a\":\"11\",\"b\":\"22\",\"c\":\"33\"}");
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"))
@ -92,9 +91,7 @@ public class EncodeTransferMedataFeignInterceptorTest {
public String test( public String test(
@RequestHeader(MetadataConstant.HeaderName.CUSTOM_METADATA) String customMetadataStr) @RequestHeader(MetadataConstant.HeaderName.CUSTOM_METADATA) String customMetadataStr)
throws UnsupportedEncodingException { throws UnsupportedEncodingException {
String systemMetadataStr = JacksonUtils return URLDecoder.decode(customMetadataStr, "UTF-8");
.serialize2Json(MetadataContextHolder.get().getAllSystemMetadata());
return URLDecoder.decode(customMetadataStr, "UTF-8") + systemMetadataStr;
} }
@FeignClient(name = "test-feign", url = "http://localhost:8081") @FeignClient(name = "test-feign", url = "http://localhost:8081")

@ -24,7 +24,6 @@ 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.config.MetadataLocalProperties; import com.tencent.cloud.common.metadata.config.MetadataLocalProperties;
import com.tencent.cloud.common.util.JacksonUtils;
import com.tencent.cloud.metadata.core.EncodeTransferMedataRestTemplateInterceptor; import com.tencent.cloud.metadata.core.EncodeTransferMedataRestTemplateInterceptor;
import org.assertj.core.api.Assertions; import org.assertj.core.api.Assertions;
import org.junit.Test; import org.junit.Test;
@ -77,7 +76,7 @@ public class EncodeTransferMedataRestTemplateInterceptorTest {
httpEntity, String.class) httpEntity, String.class)
.getBody(); .getBody();
Assertions.assertThat(metadata) Assertions.assertThat(metadata)
.isEqualTo("{\"a\":\"11\",\"b\":\"22\",\"c\":\"33\"}{}"); .isEqualTo("{\"a\":\"11\",\"b\":\"22\",\"c\":\"33\"}");
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"))
@ -106,9 +105,7 @@ public class EncodeTransferMedataRestTemplateInterceptorTest {
public String test( public String test(
@RequestHeader(MetadataConstant.HeaderName.CUSTOM_METADATA) String customMetadataStr) @RequestHeader(MetadataConstant.HeaderName.CUSTOM_METADATA) String customMetadataStr)
throws UnsupportedEncodingException { throws UnsupportedEncodingException {
String systemMetadataStr = JacksonUtils return URLDecoder.decode(customMetadataStr, "UTF-8");
.serialize2Json(MetadataContextHolder.get().getAllSystemMetadata());
return URLDecoder.decode(customMetadataStr, "UTF-8") + systemMetadataStr;
} }
} }

@ -20,9 +20,7 @@ package com.tencent.cloud.polaris.circuitbreaker.feign;
import java.io.IOException; import java.io.IOException;
import java.net.URI; import java.net.URI;
import com.tencent.cloud.common.constant.MetadataConstant.SystemMetadataKey;
import com.tencent.cloud.common.metadata.MetadataContext; import com.tencent.cloud.common.metadata.MetadataContext;
import com.tencent.cloud.common.metadata.MetadataContextHolder;
import com.tencent.polaris.api.core.ConsumerAPI; import com.tencent.polaris.api.core.ConsumerAPI;
import com.tencent.polaris.api.pojo.RetStatus; import com.tencent.polaris.api.pojo.RetStatus;
import com.tencent.polaris.api.pojo.ServiceKey; import com.tencent.polaris.api.pojo.ServiceKey;
@ -32,6 +30,8 @@ import feign.Request;
import feign.Request.Options; import feign.Request.Options;
import feign.Response; import feign.Response;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static feign.Util.checkNotNull; import static feign.Util.checkNotNull;
@ -42,6 +42,9 @@ import static feign.Util.checkNotNull;
*/ */
public class PolarisFeignClient implements Client { public class PolarisFeignClient implements Client {
private static final Logger LOG = LoggerFactory.getLogger(PolarisFeignClient.class);
private final Client delegate; private final Client delegate;
private final ConsumerAPI consumerAPI; private final ConsumerAPI consumerAPI;
@ -64,6 +67,7 @@ public class PolarisFeignClient implements Client {
} }
catch (IOException origin) { catch (IOException origin) {
resultRequest.setRetStatus(RetStatus.RetFail); resultRequest.setRetStatus(RetStatus.RetFail);
throw origin; throw origin;
} }
finally { finally {
@ -74,24 +78,17 @@ public class PolarisFeignClient implements Client {
private ServiceCallResult createServiceCallResult(final Request request) { private ServiceCallResult createServiceCallResult(final Request request) {
ServiceCallResult resultRequest = new ServiceCallResult(); ServiceCallResult resultRequest = new ServiceCallResult();
MetadataContext metadataContext = MetadataContextHolder.get(); resultRequest.setNamespace(MetadataContext.LOCAL_NAMESPACE);
String namespace = metadataContext String serviceName = request.requestTemplate().feignTarget().name();
.getSystemMetadata(SystemMetadataKey.PEER_NAMESPACE);
resultRequest.setNamespace(namespace);
String serviceName = metadataContext
.getSystemMetadata(SystemMetadataKey.PEER_SERVICE);
resultRequest.setService(serviceName); resultRequest.setService(serviceName);
String method = metadataContext.getSystemMetadata(SystemMetadataKey.PEER_PATH); URI uri = URI.create(request.url());
resultRequest.setMethod(method); resultRequest.setMethod(uri.getPath());
resultRequest.setRetStatus(RetStatus.RetSuccess); resultRequest.setRetStatus(RetStatus.RetSuccess);
String sourceNamespace = MetadataContext.LOCAL_NAMESPACE; String sourceNamespace = MetadataContext.LOCAL_NAMESPACE;
String sourceService = MetadataContext.LOCAL_SERVICE; String sourceService = MetadataContext.LOCAL_SERVICE;
if (StringUtils.isNotBlank(sourceNamespace) if (StringUtils.isNotBlank(sourceNamespace) && StringUtils.isNotBlank(sourceService)) {
&& StringUtils.isNotBlank(sourceService)) { resultRequest.setCallerService(new ServiceKey(sourceNamespace, sourceService));
resultRequest
.setCallerService(new ServiceKey(sourceNamespace, sourceService));
} }
URI uri = URI.create(request.url());
resultRequest.setHost(uri.getHost()); resultRequest.setHost(uri.getHost());
resultRequest.setPort(uri.getPort()); resultRequest.setPort(uri.getPort());

@ -20,7 +20,6 @@ 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.metadata.MetadataContext; 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.PolarisDiscoveryProperties; import com.tencent.cloud.polaris.PolarisDiscoveryProperties;
@ -70,9 +69,6 @@ public class PolarisDiscoveryHandler {
GetInstancesRequest getInstancesRequest = new GetInstancesRequest(); GetInstancesRequest getInstancesRequest = new GetInstancesRequest();
getInstancesRequest.setNamespace(namespace); getInstancesRequest.setNamespace(namespace);
getInstancesRequest.setService(service); getInstancesRequest.setService(service);
String method = MetadataContextHolder.get()
.getSystemMetadata(SystemMetadataKey.PEER_PATH);
getInstancesRequest.setMethod(method);
String localNamespace = MetadataContext.LOCAL_NAMESPACE; String localNamespace = MetadataContext.LOCAL_NAMESPACE;
String localService = MetadataContext.LOCAL_SERVICE; String localService = MetadataContext.LOCAL_SERVICE;
Map<String, String> allTransitiveCustomMetadata = MetadataContextHolder.get() Map<String, String> allTransitiveCustomMetadata = MetadataContextHolder.get()

@ -19,9 +19,9 @@
package com.tencent.cloud.polaris.registry; package com.tencent.cloud.polaris.registry;
import java.net.URI; import java.net.URI;
import java.util.Collections;
import java.util.Map; import java.util.Map;
import com.tencent.cloud.common.metadata.MetadataContextHolder;
import com.tencent.cloud.polaris.DiscoveryPropertiesAutoConfiguration; import com.tencent.cloud.polaris.DiscoveryPropertiesAutoConfiguration;
import com.tencent.cloud.polaris.PolarisDiscoveryProperties; import com.tencent.cloud.polaris.PolarisDiscoveryProperties;
import com.tencent.polaris.client.api.SDKContext; import com.tencent.polaris.client.api.SDKContext;
@ -84,7 +84,7 @@ public class PolarisRegistration implements Registration, ServiceInstance {
@Override @Override
public Map<String, String> getMetadata() { public Map<String, String> getMetadata() {
return MetadataContextHolder.get().getAllSystemMetadata(); return Collections.emptyMap();
} }
public PolarisDiscoveryProperties getPolarisProperties() { public PolarisDiscoveryProperties getPolarisProperties() {

@ -206,7 +206,7 @@ public class PolarisServiceRegistry implements ServiceRegistry<Registration> {
// first. // first.
// If the health check passes, the heartbeat will be reported. // If the health check passes, the heartbeat will be reported.
// If it does not pass, the heartbeat will not be reported. // If it does not pass, the heartbeat will not be reported.
if (!StringUtils.isNotBlank(healthCheckEndpoint)) { if (StringUtils.isNotBlank(healthCheckEndpoint)) {
if (!healthCheckEndpoint.startsWith("/")) { if (!healthCheckEndpoint.startsWith("/")) {
healthCheckEndpoint = "/" + healthCheckEndpoint; healthCheckEndpoint = "/" + healthCheckEndpoint;
} }

@ -26,28 +26,6 @@ import org.springframework.core.Ordered;
*/ */
public final class MetadataConstant { public final class MetadataConstant {
/**
* System metadata key.
*/
public static class SystemMetadataKey {
/**
* Peer namespace.
*/
public static String PEER_NAMESPACE = "PEER_NAMESPACE";
/**
* Peer service.
*/
public static String PEER_SERVICE = "PEER_SERVICE";
/**
* Peer path.
*/
public static String PEER_PATH = "PEER_PATH";
}
/** /**
* Order of filter, interceptor, ... * Order of filter, interceptor, ...
*/ */

@ -44,16 +44,6 @@ public class MetadataContext {
*/ */
public static String LOCAL_SERVICE; public static String LOCAL_SERVICE;
/**
* Transitive custom metadata content.
*/
private final Map<String, String> transitiveCustomMetadata;
/**
* System metadata content.
*/
private final Map<String, String> systemMetadata;
static { static {
String namespace = ApplicationContextAwareUtils String namespace = ApplicationContextAwareUtils
.getProperties("spring.cloud.polaris.namespace"); .getProperties("spring.cloud.polaris.namespace");
@ -73,9 +63,13 @@ public class MetadataContext {
LOCAL_SERVICE = serviceName; LOCAL_SERVICE = serviceName;
} }
/**
* Transitive custom metadata content.
*/
private final Map<String, String> transitiveCustomMetadata;
public MetadataContext() { public MetadataContext() {
this.transitiveCustomMetadata = new ConcurrentHashMap<>(); this.transitiveCustomMetadata = new ConcurrentHashMap<>();
this.systemMetadata = new ConcurrentHashMap<>();
} }
public Map<String, String> getAllTransitiveCustomMetadata() { public Map<String, String> getAllTransitiveCustomMetadata() {
@ -94,27 +88,10 @@ public class MetadataContext {
this.transitiveCustomMetadata.putAll(customMetadata); this.transitiveCustomMetadata.putAll(customMetadata);
} }
public Map<String, String> getAllSystemMetadata() {
return Collections.unmodifiableMap(this.systemMetadata);
}
public String getSystemMetadata(String key) {
return this.systemMetadata.get(key);
}
public void putSystemMetadata(String key, String value) {
this.systemMetadata.put(key, value);
}
public void putAllSystemMetadata(Map<String, String> systemMetadata) {
this.systemMetadata.putAll(systemMetadata);
}
@Override @Override
public String toString() { public String toString() {
return "MetadataContext{" + "transitiveCustomMetadata=" return "MetadataContext{" + "transitiveCustomMetadata="
+ JacksonUtils.serialize2Json(transitiveCustomMetadata) + JacksonUtils.serialize2Json(transitiveCustomMetadata) + '}';
+ ", systemMetadata=" + JacksonUtils.serialize2Json(systemMetadata) + '}';
} }
} }

@ -92,10 +92,8 @@ public final class MetadataContextHolder {
/** /**
* Save metadata map to thread local. * Save metadata map to thread local.
* @param customMetadataMap custom metadata collection * @param customMetadataMap custom metadata collection
* @param systemMetadataMap system metadata collection
*/ */
public static void init(Map<String, String> customMetadataMap, public static void init(Map<String, String> customMetadataMap) {
Map<String, String> systemMetadataMap) {
// Init ThreadLocal. // Init ThreadLocal.
MetadataContextHolder.remove(); MetadataContextHolder.remove();
MetadataContext metadataContext = MetadataContextHolder.get(); MetadataContext metadataContext = MetadataContextHolder.get();
@ -104,9 +102,6 @@ public final class MetadataContextHolder {
if (!CollectionUtils.isEmpty(customMetadataMap)) { if (!CollectionUtils.isEmpty(customMetadataMap)) {
metadataContext.putAllTransitiveCustomMetadata(customMetadataMap); metadataContext.putAllTransitiveCustomMetadata(customMetadataMap);
} }
if (!CollectionUtils.isEmpty(systemMetadataMap)) {
metadataContext.putAllSystemMetadata(systemMetadataMap);
}
MetadataContextHolder.set(metadataContext); MetadataContextHolder.set(metadataContext);
} }

@ -18,10 +18,7 @@
package com.tencent.cloud.common.metadata.config; package com.tencent.cloud.common.metadata.config;
import com.netflix.zuul.ZuulFilter;
import com.tencent.cloud.common.metadata.filter.gateway.MetadataFirstScgFilter; import com.tencent.cloud.common.metadata.filter.gateway.MetadataFirstScgFilter;
import com.tencent.cloud.common.metadata.filter.gateway.MetadataFirstZuulFilter;
import com.tencent.cloud.common.metadata.interceptor.feign.MetadataFirstFeignInterceptor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.cloud.gateway.filter.GlobalFilter; import org.springframework.cloud.gateway.filter.GlobalFilter;
@ -45,34 +42,6 @@ public class MetadataAutoConfiguration {
return new MetadataLocalProperties(); return new MetadataLocalProperties();
} }
/**
* 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. * Create when gateway application is SCG.
*/ */

@ -25,12 +25,10 @@ import reactor.core.publisher.Mono;
import org.springframework.cloud.gateway.filter.GatewayFilterChain; import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter; import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.cloud.gateway.route.Route;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.ServerWebExchange;
import static org.springframework.cloud.gateway.filter.RouteToRequestUrlFilter.ROUTE_TO_URL_FILTER_ORDER; import static org.springframework.cloud.gateway.filter.RouteToRequestUrlFilter.ROUTE_TO_URL_FILTER_ORDER;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR;
/** /**
* Scg output first filter used for setting peer info in context. * Scg output first filter used for setting peer info in context.
@ -51,9 +49,6 @@ public class MetadataFirstScgFilter implements GlobalFilter, Ordered {
@Override @Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
// get request context
Route route = exchange.getAttribute(GATEWAY_ROUTE_ATTR);
// 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);
@ -61,23 +56,6 @@ public class MetadataFirstScgFilter implements GlobalFilter, Ordered {
metadataContext = MetadataContextHolder.get(); metadataContext = MetadataContextHolder.get();
} }
// TODO The peer namespace is temporarily the same as the local namespace
metadataContext.putSystemMetadata(
MetadataConstant.SystemMetadataKey.PEER_NAMESPACE,
MetadataContext.LOCAL_NAMESPACE);
if (route != null) {
metadataContext.putSystemMetadata(
MetadataConstant.SystemMetadataKey.PEER_SERVICE,
route.getUri().getAuthority());
}
else {
metadataContext.putSystemMetadata(
MetadataConstant.SystemMetadataKey.PEER_SERVICE,
exchange.getRequest().getURI().getAuthority());
}
metadataContext.putSystemMetadata(MetadataConstant.SystemMetadataKey.PEER_PATH,
exchange.getRequest().getURI().getPath());
exchange.getAttributes().put(MetadataConstant.HeaderName.METADATA_CONTEXT, exchange.getAttributes().put(MetadataConstant.HeaderName.METADATA_CONTEXT,
metadataContext); metadataContext);

@ -1,70 +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.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;
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.PRE_TYPE;
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.REQUEST_URI_KEY;
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.SERVICE_ID_KEY;
/**
* Zuul output first filter used for setting peer info in context.
*
* @author Haotian Zhang
*/
public class MetadataFirstZuulFilter extends ZuulFilter {
@Override
public String filterType() {
return PRE_TYPE;
}
@Override
public int filterOrder() {
return PRE_DECORATION_FILTER_ORDER + 1;
}
@Override
public boolean shouldFilter() {
return true;
}
@Override
public Object run() {
// get request context
RequestContext requestContext = RequestContext.getCurrentContext();
// TODO The peer namespace is temporarily the same as the local namespace
MetadataContextHolder.get().putSystemMetadata(
MetadataConstant.SystemMetadataKey.PEER_NAMESPACE,
MetadataContext.LOCAL_NAMESPACE);
MetadataContextHolder.get().putSystemMetadata(
MetadataConstant.SystemMetadataKey.PEER_SERVICE,
(String) requestContext.get(SERVICE_ID_KEY));
MetadataContextHolder.get().putSystemMetadata(
MetadataConstant.SystemMetadataKey.PEER_PATH,
(String) requestContext.get(REQUEST_URI_KEY));
return null;
}
}

@ -1,55 +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.interceptor.feign;
import com.tencent.cloud.common.constant.MetadataConstant;
import com.tencent.cloud.common.metadata.MetadataContext;
import com.tencent.cloud.common.metadata.MetadataContextHolder;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.core.Ordered;
/**
* Interceptor used for setting peer info in context.
*
* @author Haotian Zhang
*/
public class MetadataFirstFeignInterceptor implements RequestInterceptor, Ordered {
@Override
public int getOrder() {
return MetadataConstant.OrderConstant.METADATA_FIRST_FEIGN_INTERCEPTOR_ORDER;
}
@Override
public void apply(RequestTemplate requestTemplate) {
// get metadata of current thread
MetadataContext metadataContext = MetadataContextHolder.get();
// TODO The peer namespace is temporarily the same as the 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,
requestTemplate.path());
}
}

@ -58,8 +58,7 @@ public class MetadataContextHolderTest {
customMetadata.put("a", "1"); customMetadata.put("a", "1");
customMetadata.put("b", "22"); customMetadata.put("b", "22");
customMetadata.put("c", "3"); customMetadata.put("c", "3");
Map<String, String> systemMetadata = new HashMap<>(); MetadataContextHolder.init(customMetadata);
MetadataContextHolder.init(customMetadata, systemMetadata);
metadataContext = MetadataContextHolder.get(); metadataContext = MetadataContextHolder.get();
customMetadata = metadataContext.getAllTransitiveCustomMetadata(); customMetadata = metadataContext.getAllTransitiveCustomMetadata();
Assertions.assertThat(customMetadata.get("a")).isEqualTo("1"); Assertions.assertThat(customMetadata.get("a")).isEqualTo("1");

@ -19,8 +19,6 @@
package com.tencent.cloud.common.metadata.config; package com.tencent.cloud.common.metadata.config;
import com.tencent.cloud.common.metadata.filter.gateway.MetadataFirstScgFilter; import com.tencent.cloud.common.metadata.filter.gateway.MetadataFirstScgFilter;
import com.tencent.cloud.common.metadata.filter.gateway.MetadataFirstZuulFilter;
import com.tencent.cloud.common.metadata.interceptor.feign.MetadataFirstFeignInterceptor;
import org.assertj.core.api.Assertions; import org.assertj.core.api.Assertions;
import org.junit.Test; import org.junit.Test;
@ -50,20 +48,10 @@ public class MetadataAutoConfigurationTest {
this.applicationContextRunner this.applicationContextRunner
.withConfiguration(AutoConfigurations.of(MetadataAutoConfiguration.class)) .withConfiguration(AutoConfigurations.of(MetadataAutoConfiguration.class))
.run(context -> { .run(context -> {
Assertions.assertThat(context) Assertions.assertThat(context).hasSingleBean(MetadataLocalProperties.class);
.hasSingleBean(MetadataLocalProperties.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( Assertions.assertThat(context).hasSingleBean(
MetadataAutoConfiguration.MetadataScgFilterConfig.class); MetadataAutoConfiguration.MetadataScgFilterConfig.class);
Assertions.assertThat(context) Assertions.assertThat(context).hasSingleBean(MetadataFirstScgFilter.class);
.hasSingleBean(MetadataFirstScgFilter.class);
}); });
} }
@ -75,20 +63,10 @@ public class MetadataAutoConfigurationTest {
this.webApplicationContextRunner this.webApplicationContextRunner
.withConfiguration(AutoConfigurations.of(MetadataAutoConfiguration.class)) .withConfiguration(AutoConfigurations.of(MetadataAutoConfiguration.class))
.run(context -> { .run(context -> {
Assertions.assertThat(context) Assertions.assertThat(context).hasSingleBean(MetadataLocalProperties.class);
.hasSingleBean(MetadataLocalProperties.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( Assertions.assertThat(context).hasSingleBean(
MetadataAutoConfiguration.MetadataScgFilterConfig.class); MetadataAutoConfiguration.MetadataScgFilterConfig.class);
Assertions.assertThat(context) Assertions.assertThat(context).hasSingleBean(MetadataFirstScgFilter.class);
.hasSingleBean(MetadataFirstScgFilter.class);
}); });
} }
@ -100,20 +78,10 @@ public class MetadataAutoConfigurationTest {
this.reactiveWebApplicationContextRunner this.reactiveWebApplicationContextRunner
.withConfiguration(AutoConfigurations.of(MetadataAutoConfiguration.class)) .withConfiguration(AutoConfigurations.of(MetadataAutoConfiguration.class))
.run(context -> { .run(context -> {
Assertions.assertThat(context) Assertions.assertThat(context).hasSingleBean(MetadataLocalProperties.class);
.hasSingleBean(MetadataLocalProperties.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( Assertions.assertThat(context).hasSingleBean(
MetadataAutoConfiguration.MetadataScgFilterConfig.class); MetadataAutoConfiguration.MetadataScgFilterConfig.class);
Assertions.assertThat(context) Assertions.assertThat(context).hasSingleBean(MetadataFirstScgFilter.class);
.hasSingleBean(MetadataFirstScgFilter.class);
}); });
} }

@ -70,7 +70,7 @@
</developers> </developers>
<properties> <properties>
<revision>1.4.2-Hoxton.SR9</revision> <revision>1.4.3-Hoxton.SR9-SNAPSHOT</revision>
<polaris.version>1.5.2</polaris.version> <polaris.version>1.5.2</polaris.version>
<powermock.version>2.0.0</powermock.version> <powermock.version>2.0.0</powermock.version>

@ -30,7 +30,6 @@ 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.ContextConstant; import com.tencent.cloud.common.constant.ContextConstant;
import com.tencent.cloud.common.constant.MetadataConstant.SystemMetadataKey;
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.common.metadata.MetadataContextHolder;
import com.tencent.cloud.common.pojo.PolarisServer; import com.tencent.cloud.common.pojo.PolarisServer;
@ -89,9 +88,6 @@ public class PolarisLoadBalancer extends DynamicServerListLoadBalancer<Server> {
String srcService = MetadataContext.LOCAL_SERVICE; String srcService = MetadataContext.LOCAL_SERVICE;
Map<String, String> transitiveCustomMetadata = MetadataContextHolder.get() Map<String, String> transitiveCustomMetadata = MetadataContextHolder.get()
.getAllTransitiveCustomMetadata(); .getAllTransitiveCustomMetadata();
String method = MetadataContextHolder.get()
.getSystemMetadata(SystemMetadataKey.PEER_PATH);
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();
serviceInfo.setNamespace(srcNamespace); serviceInfo.setNamespace(srcNamespace);
@ -111,14 +107,11 @@ public class PolarisLoadBalancer extends DynamicServerListLoadBalancer<Server> {
} }
private ServiceInstances getPolarisDiscoveryServiceInstances() { private ServiceInstances getPolarisDiscoveryServiceInstances() {
String serviceName = MetadataContextHolder.get().getSystemMetadata(SystemMetadataKey.PEER_SERVICE);
if (StringUtils.isBlank(serviceName)) {
List<Server> allServers = super.getAllServers(); List<Server> allServers = super.getAllServers();
if (CollectionUtils.isEmpty(allServers)) { if (CollectionUtils.isEmpty(allServers)) {
return null; return null;
} }
serviceName = ((PolarisServer) super.getAllServers().get(0)).getServiceInstances().getService(); String serviceName = ((PolarisServer) super.getAllServers().get(0)).getServiceInstances().getService();
}
return getAllInstances(MetadataContext.LOCAL_NAMESPACE, serviceName).toServiceInstances(); return getAllInstances(MetadataContext.LOCAL_NAMESPACE, serviceName).toServiceInstances();
} }

Loading…
Cancel
Save