From e17589fc1ec554dfd7b0b5bd34514cf898788351 Mon Sep 17 00:00:00 2001 From: SkyeBeFreeman <928016560@qq.com> Date: Wed, 18 May 2022 12:04:02 +0800 Subject: [PATCH 1/2] fix:fix wrong context data storage. --- CHANGELOG.md | 1 + pom.xml | 2 +- .../DecodeTransferMetadataReactiveFilter.java | 2 +- .../DecodeTransferMetadataServletFilter.java | 2 +- ...odeTransferMedataFeignInterceptorTest.java | 7 +- ...sferMedataRestTemplateInterceptorTest.java | 7 +- .../feign/PolarisFeignClient.java | 52 +++++++------- .../feign/PolarisLoadBalancerFeignClient.java | 18 +++++ .../discovery/PolarisDiscoveryHandler.java | 4 -- .../polaris/registry/PolarisRegistration.java | 4 +- .../registry/PolarisServiceRegistry.java | 5 +- .../common/constant/MetadataConstant.java | 26 +------ .../common/metadata/MetadataContext.java | 35 ++-------- .../metadata/MetadataContextHolder.java | 7 +- .../metadata/aop/MetadataFeignAspect.java | 54 -------------- .../config/MetadataAutoConfiguration.java | 37 ---------- .../gateway/MetadataFirstScgFilter.java | 22 ------ .../gateway/MetadataFirstZuulFilter.java | 70 ------------------- .../feign/MetadataFirstFeignInterceptor.java | 56 --------------- .../metadata/MetadataContextHolderTest.java | 3 +- .../config/MetadataAutoConfigurationTest.java | 44 ++---------- spring-cloud-tencent-dependencies/pom.xml | 2 +- .../loadbalancer/PolarisLoadBalancer.java | 30 ++------ 23 files changed, 77 insertions(+), 413 deletions(-) delete mode 100644 spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/aop/MetadataFeignAspect.java delete mode 100644 spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/filter/gateway/MetadataFirstZuulFilter.java delete mode 100644 spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/interceptor/feign/MetadataFirstFeignInterceptor.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 0864f1961..898ffab5d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,3 +8,4 @@ - [feat: override recover router config](https://github.com/Tencent/spring-cloud-tencent/pull/167) - [fix:fix routes of gateway doesn't refresh bug.](https://github.com/Tencent/spring-cloud-tencent/pull/168) - [fix:Turn off automatic injection of Polars rule.](https://github.com/Tencent/spring-cloud-tencent/pull/171) +- [fix:fix wrong context data storage.](https://github.com/Tencent/spring-cloud-tencent/pull/180) diff --git a/pom.xml b/pom.xml index 8e9c56d14..90cae6c3a 100644 --- a/pom.xml +++ b/pom.xml @@ -86,7 +86,7 @@ - 1.4.2-Greenwich.SR6-SNAPSHOT + 1.4.4-Greenwich.SR6-SNAPSHOT Greenwich.SR6 diff --git a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/DecodeTransferMetadataReactiveFilter.java b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/DecodeTransferMetadataReactiveFilter.java index aac449fac..2dea4fd66 100644 --- a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/DecodeTransferMetadataReactiveFilter.java +++ b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/DecodeTransferMetadataReactiveFilter.java @@ -75,7 +75,7 @@ public class DecodeTransferMetadataReactiveFilter implements WebFilter, Ordered Map upstreamCustomMetadataMap = JacksonUtils .deserialize2Map(customMetadataStr); - MetadataContextHolder.init(upstreamCustomMetadataMap, null); + MetadataContextHolder.init(upstreamCustomMetadataMap); // Save to ServerWebExchange. serverWebExchange.getAttributes().put( diff --git a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/DecodeTransferMetadataServletFilter.java b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/DecodeTransferMetadataServletFilter.java index da1261f65..23934edec 100644 --- a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/DecodeTransferMetadataServletFilter.java +++ b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/DecodeTransferMetadataServletFilter.java @@ -72,7 +72,7 @@ public class DecodeTransferMetadataServletFilter extends OncePerRequestFilter { .deserialize2Map(customMetadataStr); try { - MetadataContextHolder.init(upstreamCustomMetadataMap, null); + MetadataContextHolder.init(upstreamCustomMetadataMap); filterChain.doFilter(httpServletRequest, httpServletResponse); } diff --git a/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/core/intercepter/EncodeTransferMedataFeignInterceptorTest.java b/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/core/intercepter/EncodeTransferMedataFeignInterceptorTest.java index c7e636692..ddc69dd62 100644 --- a/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/core/intercepter/EncodeTransferMedataFeignInterceptorTest.java +++ b/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/core/intercepter/EncodeTransferMedataFeignInterceptorTest.java @@ -24,7 +24,6 @@ import java.net.URLDecoder; import com.tencent.cloud.common.constant.MetadataConstant; import com.tencent.cloud.common.metadata.MetadataContextHolder; import com.tencent.cloud.common.metadata.config.MetadataLocalProperties; -import com.tencent.cloud.common.util.JacksonUtils; import com.tencent.cloud.metadata.core.EncodeTransferMedataFeignInterceptor; import feign.RequestInterceptor; import feign.RequestTemplate; @@ -65,7 +64,7 @@ public class EncodeTransferMedataFeignInterceptorTest { public void test1() { String metadata = testFeign.test(); Assertions.assertThat(metadata) - .isEqualTo("{\"a\":\"11\",\"b\":\"22\",\"c\":\"33\"}{}"); + .isEqualTo("{\"a\":\"11\",\"b\":\"22\",\"c\":\"33\"}"); Assertions.assertThat(metadataLocalProperties.getContent().get("a")) .isEqualTo("1"); Assertions.assertThat(metadataLocalProperties.getContent().get("b")) @@ -90,9 +89,7 @@ public class EncodeTransferMedataFeignInterceptorTest { public String test( @RequestHeader(MetadataConstant.HeaderName.CUSTOM_METADATA) String customMetadataStr) throws UnsupportedEncodingException { - String systemMetadataStr = JacksonUtils - .serialize2Json(MetadataContextHolder.get().getAllSystemMetadata()); - return URLDecoder.decode(customMetadataStr, "UTF-8") + systemMetadataStr; + return URLDecoder.decode(customMetadataStr, "UTF-8"); } @FeignClient(name = "test-feign", url = "http://localhost:8081") diff --git a/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/core/intercepter/EncodeTransferMedataRestTemplateInterceptorTest.java b/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/core/intercepter/EncodeTransferMedataRestTemplateInterceptorTest.java index 432050de6..e30063db8 100644 --- a/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/core/intercepter/EncodeTransferMedataRestTemplateInterceptorTest.java +++ b/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/core/intercepter/EncodeTransferMedataRestTemplateInterceptorTest.java @@ -24,7 +24,6 @@ import java.net.URLDecoder; import com.tencent.cloud.common.constant.MetadataConstant; import com.tencent.cloud.common.metadata.MetadataContextHolder; import com.tencent.cloud.common.metadata.config.MetadataLocalProperties; -import com.tencent.cloud.common.util.JacksonUtils; import com.tencent.cloud.metadata.core.EncodeTransferMedataRestTemplateInterceptor; import org.assertj.core.api.Assertions; import org.junit.Test; @@ -76,7 +75,7 @@ public class EncodeTransferMedataRestTemplateInterceptorTest { httpEntity, String.class) .getBody(); Assertions.assertThat(metadata) - .isEqualTo("{\"a\":\"11\",\"b\":\"22\",\"c\":\"33\"}{}"); + .isEqualTo("{\"a\":\"11\",\"b\":\"22\",\"c\":\"33\"}"); Assertions.assertThat(metadataLocalProperties.getContent().get("a")) .isEqualTo("1"); Assertions.assertThat(metadataLocalProperties.getContent().get("b")) @@ -105,9 +104,7 @@ public class EncodeTransferMedataRestTemplateInterceptorTest { public String test( @RequestHeader(MetadataConstant.HeaderName.CUSTOM_METADATA) String customMetadataStr) throws UnsupportedEncodingException { - String systemMetadataStr = JacksonUtils - .serialize2Json(MetadataContextHolder.get().getAllSystemMetadata()); - return URLDecoder.decode(customMetadataStr, "UTF-8") + systemMetadataStr; + return URLDecoder.decode(customMetadataStr, "UTF-8"); } } diff --git a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisFeignClient.java b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisFeignClient.java index bb061b3b7..5ab645961 100644 --- a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisFeignClient.java +++ b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisFeignClient.java @@ -20,9 +20,7 @@ package com.tencent.cloud.polaris.circuitbreaker.feign; import java.io.IOException; 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.MetadataContextHolder; import com.tencent.polaris.api.core.ConsumerAPI; import com.tencent.polaris.api.pojo.RetStatus; import com.tencent.polaris.api.pojo.ServiceKey; @@ -33,6 +31,7 @@ import feign.Request.Options; import feign.Response; import org.apache.commons.lang.StringUtils; +import static com.tencent.cloud.common.constant.MetadataConstant.HeaderName.PEER_SERVICE; import static feign.Util.checkNotNull; /** @@ -57,45 +56,46 @@ public class PolarisFeignClient implements Client { try { Response response = delegate.execute(request, options); // HTTP code greater than 500 is an exception - if (response.status() >= 500) { + if (resultRequest != null && response.status() >= 500) { resultRequest.setRetStatus(RetStatus.RetFail); } return response; } catch (IOException origin) { - resultRequest.setRetStatus(RetStatus.RetFail); + if (resultRequest != null) { + resultRequest.setRetStatus(RetStatus.RetFail); + } throw origin; } finally { - consumerAPI.updateServiceCallResult(resultRequest); + if (resultRequest != null) { + consumerAPI.updateServiceCallResult(resultRequest); + } } } private ServiceCallResult createServiceCallResult(final Request request) { ServiceCallResult resultRequest = new ServiceCallResult(); + resultRequest.setNamespace(MetadataContext.LOCAL_NAMESPACE); + Object[] headers = request.headers().get(PEER_SERVICE).toArray(); + int headersLength = headers.length; + if (headersLength != 0) { + String serviceName = (String) headers[headersLength - 1]; + resultRequest.setService(serviceName); + URI uri = URI.create(request.url()); + resultRequest.setMethod(uri.getPath()); + resultRequest.setRetStatus(RetStatus.RetSuccess); + String sourceNamespace = MetadataContext.LOCAL_NAMESPACE; + String sourceService = MetadataContext.LOCAL_SERVICE; + if (StringUtils.isNotBlank(sourceNamespace) && StringUtils.isNotBlank(sourceService)) { + resultRequest.setCallerService(new ServiceKey(sourceNamespace, sourceService)); + } + resultRequest.setHost(uri.getHost()); + resultRequest.setPort(uri.getPort()); - MetadataContext metadataContext = MetadataContextHolder.get(); - String namespace = metadataContext - .getSystemMetadata(SystemMetadataKey.PEER_NAMESPACE); - resultRequest.setNamespace(namespace); - String serviceName = metadataContext - .getSystemMetadata(SystemMetadataKey.PEER_SERVICE); - resultRequest.setService(serviceName); - String method = metadataContext.getSystemMetadata(SystemMetadataKey.PEER_PATH); - resultRequest.setMethod(method); - resultRequest.setRetStatus(RetStatus.RetSuccess); - String sourceNamespace = MetadataContext.LOCAL_NAMESPACE; - String sourceService = MetadataContext.LOCAL_SERVICE; - if (StringUtils.isNotBlank(sourceNamespace) - && StringUtils.isNotBlank(sourceService)) { - resultRequest - .setCallerService(new ServiceKey(sourceNamespace, sourceService)); + return resultRequest; } - URI uri = URI.create(request.url()); - resultRequest.setHost(uri.getHost()); - resultRequest.setPort(uri.getPort()); - - return resultRequest; + return null; } } diff --git a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisLoadBalancerFeignClient.java b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisLoadBalancerFeignClient.java index 41e4a608f..b460e1d0a 100644 --- a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisLoadBalancerFeignClient.java +++ b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisLoadBalancerFeignClient.java @@ -17,12 +17,23 @@ package com.tencent.cloud.polaris.circuitbreaker.feign; +import java.io.IOException; +import java.net.URI; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + import feign.Client; +import feign.Request; +import feign.Response; import org.springframework.cloud.netflix.ribbon.SpringClientFactory; import org.springframework.cloud.openfeign.ribbon.CachingSpringLoadBalancerFactory; import org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient; +import static com.tencent.cloud.common.constant.MetadataConstant.HeaderName.PEER_SERVICE; + /** * Wrap for {@link LoadBalancerFeignClient}. * @@ -36,4 +47,11 @@ public class PolarisLoadBalancerFeignClient extends LoadBalancerFeignClient { super(delegate, lbClientFactory, clientFactory); } + @Override + public Response execute(Request request, Request.Options options) throws IOException { + Map> headers = new HashMap<>(request.headers()); + headers.put(PEER_SERVICE, Collections.singletonList(URI.create(request.url()).getAuthority())); + request = Request.create(request.httpMethod(), request.url(), headers, request.requestBody()); + return super.execute(request, options); + } } diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/PolarisDiscoveryHandler.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/PolarisDiscoveryHandler.java index 86e829080..e71eb1fcd 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/PolarisDiscoveryHandler.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/PolarisDiscoveryHandler.java @@ -20,7 +20,6 @@ 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.PolarisDiscoveryProperties; @@ -70,9 +69,6 @@ public class PolarisDiscoveryHandler { GetInstancesRequest getInstancesRequest = new GetInstancesRequest(); getInstancesRequest.setNamespace(namespace); getInstancesRequest.setService(service); - String method = MetadataContextHolder.get() - .getSystemMetadata(SystemMetadataKey.PEER_PATH); - getInstancesRequest.setMethod(method); String localNamespace = MetadataContext.LOCAL_NAMESPACE; String localService = MetadataContext.LOCAL_SERVICE; Map allTransitiveCustomMetadata = MetadataContextHolder.get() diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisRegistration.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisRegistration.java index 9ca6b9e08..ed8baacc9 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisRegistration.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisRegistration.java @@ -19,9 +19,9 @@ package com.tencent.cloud.polaris.registry; import java.net.URI; +import java.util.Collections; import java.util.Map; -import com.tencent.cloud.common.metadata.MetadataContextHolder; import com.tencent.cloud.polaris.DiscoveryPropertiesAutoConfiguration; import com.tencent.cloud.polaris.PolarisDiscoveryProperties; import com.tencent.polaris.client.api.SDKContext; @@ -84,7 +84,7 @@ public class PolarisRegistration implements Registration, ServiceInstance { @Override public Map getMetadata() { - return MetadataContextHolder.get().getAllSystemMetadata(); + return Collections.emptyMap(); } public PolarisDiscoveryProperties getPolarisProperties() { diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java index 987413ed0..6b171c1de 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java @@ -34,14 +34,13 @@ import com.tencent.polaris.api.rpc.InstanceHeartbeatRequest; import com.tencent.polaris.api.rpc.InstanceRegisterRequest; import com.tencent.polaris.api.rpc.InstancesResponse; import com.tencent.polaris.client.util.NamedThreadFactory; -import org.apache.logging.log4j.util.Strings; +import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.BeanUtils; import org.springframework.cloud.client.serviceregistry.Registration; import org.springframework.cloud.client.serviceregistry.ServiceRegistry; -import org.springframework.util.StringUtils; import static org.springframework.util.ReflectionUtils.rethrowRuntimeException; @@ -207,7 +206,7 @@ public class PolarisServiceRegistry implements ServiceRegistry { // first. // If the health check passes, the heartbeat will be reported. // If it does not pass, the heartbeat will not be reported. - if (Strings.isNotEmpty(healthCheckEndpoint)) { + if (StringUtils.isNotBlank(healthCheckEndpoint)) { if (!healthCheckEndpoint.startsWith("/")) { healthCheckEndpoint = "/" + healthCheckEndpoint; } diff --git a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/constant/MetadataConstant.java b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/constant/MetadataConstant.java index ce498126b..09b9b2afb 100644 --- a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/constant/MetadataConstant.java +++ b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/constant/MetadataConstant.java @@ -26,28 +26,6 @@ import org.springframework.core.Ordered; */ 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, ... */ @@ -82,9 +60,9 @@ public final class MetadataConstant { public static final String CUSTOM_METADATA = "SCT-CUSTOM-METADATA"; /** - * System Metadata. + * Peer service name. */ - public static final String SYSTEM_METADATA = "SCT-SYSTEM-METADATA"; + public static final String PEER_SERVICE = "SCT-PEER-SERVICE"; /** * Metadata context. diff --git a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/MetadataContext.java b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/MetadataContext.java index 6f06e811b..584465ff9 100644 --- a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/MetadataContext.java +++ b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/MetadataContext.java @@ -44,16 +44,6 @@ public class MetadataContext { */ public static String LOCAL_SERVICE; - /** - * Transitive custom metadata content. - */ - private final Map transitiveCustomMetadata; - - /** - * System metadata content. - */ - private final Map systemMetadata; - static { String namespace = ApplicationContextAwareUtils .getProperties("spring.cloud.polaris.namespace"); @@ -73,9 +63,13 @@ public class MetadataContext { LOCAL_SERVICE = serviceName; } + /** + * Transitive custom metadata content. + */ + private final Map transitiveCustomMetadata; + public MetadataContext() { this.transitiveCustomMetadata = new ConcurrentHashMap<>(); - this.systemMetadata = new ConcurrentHashMap<>(); } public Map getAllTransitiveCustomMetadata() { @@ -94,27 +88,10 @@ public class MetadataContext { this.transitiveCustomMetadata.putAll(customMetadata); } - public Map 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 systemMetadata) { - this.systemMetadata.putAll(systemMetadata); - } - @Override public String toString() { return "MetadataContext{" + "transitiveCustomMetadata=" - + JacksonUtils.serialize2Json(transitiveCustomMetadata) - + ", systemMetadata=" + JacksonUtils.serialize2Json(systemMetadata) + '}'; + + JacksonUtils.serialize2Json(transitiveCustomMetadata) + '}'; } } diff --git a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/MetadataContextHolder.java b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/MetadataContextHolder.java index b8811f8a5..3ec3eb7fc 100644 --- a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/MetadataContextHolder.java +++ b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/MetadataContextHolder.java @@ -92,10 +92,8 @@ public final class MetadataContextHolder { /** * Save metadata map to thread local. * @param customMetadataMap custom metadata collection - * @param systemMetadataMap system metadata collection */ - public static void init(Map customMetadataMap, - Map systemMetadataMap) { + public static void init(Map customMetadataMap) { // Init ThreadLocal. MetadataContextHolder.remove(); MetadataContext metadataContext = MetadataContextHolder.get(); @@ -104,9 +102,6 @@ public final class MetadataContextHolder { if (!CollectionUtils.isEmpty(customMetadataMap)) { metadataContext.putAllTransitiveCustomMetadata(customMetadataMap); } - if (!CollectionUtils.isEmpty(systemMetadataMap)) { - metadataContext.putAllSystemMetadata(systemMetadataMap); - } MetadataContextHolder.set(metadataContext); } diff --git a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/aop/MetadataFeignAspect.java b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/aop/MetadataFeignAspect.java deleted file mode 100644 index 7ee2b45ae..000000000 --- a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/aop/MetadataFeignAspect.java +++ /dev/null @@ -1,54 +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.aop; - -import java.net.URI; - -import com.tencent.cloud.common.constant.MetadataConstant; -import com.tencent.cloud.common.metadata.MetadataContextHolder; -import feign.Request; -import org.aspectj.lang.JoinPoint; -import org.aspectj.lang.annotation.Aspect; -import org.aspectj.lang.annotation.Before; -import org.aspectj.lang.annotation.Pointcut; - -/** - * Aspect for getting service name of peer-service in Feign of Greenwich. - * - * @author Haotian Zhang - */ -@Aspect -public class MetadataFeignAspect { - - @Pointcut("execution(* feign.Client.execute(..))") - public void execute() { - } - - /** - * Get service name before execution of Feign client. - * @param joinPoint join point - */ - @Before("execute()") - public void doBefore(JoinPoint joinPoint) { - Request request = (Request) joinPoint.getArgs()[0]; - MetadataContextHolder.get().putSystemMetadata( - MetadataConstant.SystemMetadataKey.PEER_SERVICE, - URI.create(request.url()).getAuthority()); - } - -} diff --git a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/config/MetadataAutoConfiguration.java b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/config/MetadataAutoConfiguration.java index 462f00a49..6188c61ae 100644 --- a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/config/MetadataAutoConfiguration.java +++ b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/config/MetadataAutoConfiguration.java @@ -18,11 +18,7 @@ package com.tencent.cloud.common.metadata.config; -import com.netflix.zuul.ZuulFilter; -import com.tencent.cloud.common.metadata.aop.MetadataFeignAspect; import com.tencent.cloud.common.metadata.filter.gateway.MetadataFirstScgFilter; -import com.tencent.cloud.common.metadata.filter.gateway.MetadataFirstZuulFilter; -import com.tencent.cloud.common.metadata.interceptor.feign.MetadataFirstFeignInterceptor; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.cloud.gateway.filter.GlobalFilter; @@ -46,39 +42,6 @@ public class MetadataAutoConfiguration { return new MetadataLocalProperties(); } - /** - * Create when Feign exists. - */ - @Configuration - @ConditionalOnClass(name = "feign.Feign") - static class MetadataFeignInterceptorConfig { - - @Bean - public MetadataFirstFeignInterceptor metadataFirstFeignInterceptor() { - return new MetadataFirstFeignInterceptor(); - } - - @Bean - public MetadataFeignAspect metadataFeignAspect() { - return new MetadataFeignAspect(); - } - - } - - /** - * 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. */ diff --git a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/filter/gateway/MetadataFirstScgFilter.java b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/filter/gateway/MetadataFirstScgFilter.java index 21c6c4d54..a79ba5323 100644 --- a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/filter/gateway/MetadataFirstScgFilter.java +++ b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/filter/gateway/MetadataFirstScgFilter.java @@ -25,12 +25,10 @@ import reactor.core.publisher.Mono; import org.springframework.cloud.gateway.filter.GatewayFilterChain; import org.springframework.cloud.gateway.filter.GlobalFilter; -import org.springframework.cloud.gateway.route.Route; import org.springframework.core.Ordered; 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.support.ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR; /** * Scg output first filter used for setting peer info in context. @@ -51,9 +49,6 @@ public class MetadataFirstScgFilter implements GlobalFilter, Ordered { @Override public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) { - // get request context - Route route = exchange.getAttribute(GATEWAY_ROUTE_ATTR); - // get metadata of current thread MetadataContext metadataContext = exchange .getAttribute(MetadataConstant.HeaderName.METADATA_CONTEXT); @@ -61,23 +56,6 @@ public class MetadataFirstScgFilter implements GlobalFilter, Ordered { 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, metadataContext); diff --git a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/filter/gateway/MetadataFirstZuulFilter.java b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/filter/gateway/MetadataFirstZuulFilter.java deleted file mode 100644 index acb2acc87..000000000 --- a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/filter/gateway/MetadataFirstZuulFilter.java +++ /dev/null @@ -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; - } - -} diff --git a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/interceptor/feign/MetadataFirstFeignInterceptor.java b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/interceptor/feign/MetadataFirstFeignInterceptor.java deleted file mode 100644 index 31e0388bd..000000000 --- a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/interceptor/feign/MetadataFirstFeignInterceptor.java +++ /dev/null @@ -1,56 +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); - // Cannot get service name of peer-service in Feign interceptor of Greenwich. - // metadataContext.putSystemMetadata(MetadataConstant.SystemMetadataKey.PEER_SERVICE, - // requestTemplate.feignTarget().name()); - metadataContext.putSystemMetadata(MetadataConstant.SystemMetadataKey.PEER_PATH, - requestTemplate.path()); - } - -} diff --git a/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/metadata/MetadataContextHolderTest.java b/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/metadata/MetadataContextHolderTest.java index b945ac9aa..3fe243a9d 100644 --- a/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/metadata/MetadataContextHolderTest.java +++ b/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/metadata/MetadataContextHolderTest.java @@ -57,8 +57,7 @@ public class MetadataContextHolderTest { customMetadata.put("a", "1"); customMetadata.put("b", "22"); customMetadata.put("c", "3"); - Map systemMetadata = new HashMap<>(); - MetadataContextHolder.init(customMetadata, systemMetadata); + MetadataContextHolder.init(customMetadata); metadataContext = MetadataContextHolder.get(); customMetadata = metadataContext.getAllTransitiveCustomMetadata(); Assertions.assertThat(customMetadata.get("a")).isEqualTo("1"); diff --git a/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/metadata/config/MetadataAutoConfigurationTest.java b/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/metadata/config/MetadataAutoConfigurationTest.java index bcf42ee14..e713eba66 100644 --- a/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/metadata/config/MetadataAutoConfigurationTest.java +++ b/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/metadata/config/MetadataAutoConfigurationTest.java @@ -19,8 +19,6 @@ 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.interceptor.feign.MetadataFirstFeignInterceptor; import org.assertj.core.api.Assertions; import org.junit.Test; @@ -50,20 +48,10 @@ public class MetadataAutoConfigurationTest { this.applicationContextRunner .withConfiguration(AutoConfigurations.of(MetadataAutoConfiguration.class)) .run(context -> { - Assertions.assertThat(context) - .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(MetadataLocalProperties.class); Assertions.assertThat(context).hasSingleBean( MetadataAutoConfiguration.MetadataScgFilterConfig.class); - Assertions.assertThat(context) - .hasSingleBean(MetadataFirstScgFilter.class); + Assertions.assertThat(context).hasSingleBean(MetadataFirstScgFilter.class); }); } @@ -75,20 +63,10 @@ public class MetadataAutoConfigurationTest { this.webApplicationContextRunner .withConfiguration(AutoConfigurations.of(MetadataAutoConfiguration.class)) .run(context -> { - Assertions.assertThat(context) - .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(MetadataLocalProperties.class); Assertions.assertThat(context).hasSingleBean( MetadataAutoConfiguration.MetadataScgFilterConfig.class); - Assertions.assertThat(context) - .hasSingleBean(MetadataFirstScgFilter.class); + Assertions.assertThat(context).hasSingleBean(MetadataFirstScgFilter.class); }); } @@ -100,20 +78,10 @@ public class MetadataAutoConfigurationTest { this.reactiveWebApplicationContextRunner .withConfiguration(AutoConfigurations.of(MetadataAutoConfiguration.class)) .run(context -> { - Assertions.assertThat(context) - .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(MetadataLocalProperties.class); Assertions.assertThat(context).hasSingleBean( MetadataAutoConfiguration.MetadataScgFilterConfig.class); - Assertions.assertThat(context) - .hasSingleBean(MetadataFirstScgFilter.class); + Assertions.assertThat(context).hasSingleBean(MetadataFirstScgFilter.class); }); } diff --git a/spring-cloud-tencent-dependencies/pom.xml b/spring-cloud-tencent-dependencies/pom.xml index 0b9bf2c9c..0c05f7048 100644 --- a/spring-cloud-tencent-dependencies/pom.xml +++ b/spring-cloud-tencent-dependencies/pom.xml @@ -70,7 +70,7 @@ - 1.4.2-Greenwich.SR6-SNAPSHOT + 1.4.4-Greenwich.SR6-SNAPSHOT 1.5.2 diff --git a/spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/PolarisLoadBalancer.java b/spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/PolarisLoadBalancer.java index 37ae60526..574a42610 100644 --- a/spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/PolarisLoadBalancer.java +++ b/spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/PolarisLoadBalancer.java @@ -30,7 +30,6 @@ import com.netflix.loadbalancer.PollingServerListUpdater; import com.netflix.loadbalancer.Server; import com.netflix.loadbalancer.ServerList; 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.MetadataContextHolder; import com.tencent.cloud.common.pojo.PolarisServer; @@ -89,9 +88,6 @@ public class PolarisLoadBalancer extends DynamicServerListLoadBalancer { String srcService = MetadataContext.LOCAL_SERVICE; Map 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(); serviceInfo.setNamespace(srcNamespace); @@ -111,15 +107,7 @@ public class PolarisLoadBalancer extends DynamicServerListLoadBalancer { } private ServiceInstances getPolarisDiscoveryServiceInstances() { - String serviceName = MetadataContextHolder.get().getSystemMetadata(SystemMetadataKey.PEER_SERVICE); - if (StringUtils.isBlank(serviceName)) { - List allServers = super.getAllServers(); - if (CollectionUtils.isEmpty(allServers)) { - return null; - } - serviceName = ((PolarisServer) super.getAllServers().get(0)).getServiceInstances().getService(); - } - return getAllInstances(MetadataContext.LOCAL_NAMESPACE, serviceName).toServiceInstances(); + return getAllInstances(MetadataContext.LOCAL_NAMESPACE, name).toServiceInstances(); } private ServiceInstances getExtendDiscoveryServiceInstances() { @@ -128,26 +116,16 @@ public class PolarisLoadBalancer extends DynamicServerListLoadBalancer { return null; } ServiceInstances serviceInstances; - String serviceName; - // notice the difference between different service registries - if (StringUtils.isNotBlank( - allServers.get(0).getMetaInfo().getServiceIdForDiscovery())) { - serviceName = allServers.get(0).getMetaInfo().getServiceIdForDiscovery(); - } - else { - serviceName = allServers.get(0).getMetaInfo().getAppName(); - } - if (StringUtils.isBlank(serviceName)) { + if (StringUtils.isBlank(name)) { throw new IllegalStateException( "PolarisLoadBalancer only Server with AppName or ServiceIdForDiscovery attribute"); } - ServiceKey serviceKey = new ServiceKey(MetadataContext.LOCAL_NAMESPACE, - serviceName); + ServiceKey serviceKey = new ServiceKey(MetadataContext.LOCAL_NAMESPACE, name); List instances = new ArrayList<>(8); for (Server server : allServers) { DefaultInstance instance = new DefaultInstance(); instance.setNamespace(MetadataContext.LOCAL_NAMESPACE); - instance.setService(serviceName); + instance.setService(name); instance.setHealthy(server.isAlive()); instance.setProtocol(server.getScheme()); instance.setId(server.getId()); From 66e8c210fcf39cacd889bf68db200a5bcdb3239d Mon Sep 17 00:00:00 2001 From: SkyeBeFreeman <928016560@qq.com> Date: Thu, 19 May 2022 15:52:18 +0800 Subject: [PATCH 2/2] fix:fix route not refreshing bug when first instance of one service up. --- CHANGELOG.md | 1 + .../PolarisDiscoveryAutoConfiguration.java | 4 +- ...sRefreshApplicationReadyEventListener.java | 87 +++++++++++++++++ .../refresh/PolarisRefreshConfiguration.java | 49 ++++++++++ .../PolarisServiceStatusChangeListener.java | 97 +++++++++++++++++++ .../PolarisServiceChangeListener.java | 62 ------------ .../registry/PolarisServiceRegistry.java | 10 +- ...larisServiceRegistryAutoConfiguration.java | 12 +-- .../cloud/common/pojo/PolarisServer.java | 1 + 9 files changed, 241 insertions(+), 82 deletions(-) create mode 100644 spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/refresh/PolarisRefreshApplicationReadyEventListener.java create mode 100644 spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/refresh/PolarisRefreshConfiguration.java create mode 100644 spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/refresh/PolarisServiceStatusChangeListener.java delete mode 100644 spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceChangeListener.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 898ffab5d..d4ad17975 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,3 +9,4 @@ - [fix:fix routes of gateway doesn't refresh bug.](https://github.com/Tencent/spring-cloud-tencent/pull/168) - [fix:Turn off automatic injection of Polars rule.](https://github.com/Tencent/spring-cloud-tencent/pull/171) - [fix:fix wrong context data storage.](https://github.com/Tencent/spring-cloud-tencent/pull/180) +- [fix:fix route not refreshing bug when first instance of one service up.](https://github.com/Tencent/spring-cloud-tencent/pull/186) diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/PolarisDiscoveryAutoConfiguration.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/PolarisDiscoveryAutoConfiguration.java index 3c28371e8..6e55a8c01 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/PolarisDiscoveryAutoConfiguration.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/PolarisDiscoveryAutoConfiguration.java @@ -18,6 +18,8 @@ package com.tencent.cloud.polaris.discovery; +import com.tencent.cloud.polaris.discovery.refresh.PolarisRefreshConfiguration; + import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -30,7 +32,7 @@ import org.springframework.context.annotation.Import; */ @Configuration @ConditionalOnPolarisDiscoveryEnabled -@Import({ PolarisDiscoveryClientConfiguration.class }) +@Import({PolarisDiscoveryClientConfiguration.class, PolarisRefreshConfiguration.class}) public class PolarisDiscoveryAutoConfiguration { @Bean diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/refresh/PolarisRefreshApplicationReadyEventListener.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/refresh/PolarisRefreshApplicationReadyEventListener.java new file mode 100644 index 000000000..71200a2ac --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/refresh/PolarisRefreshApplicationReadyEventListener.java @@ -0,0 +1,87 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + */ +package com.tencent.cloud.polaris.discovery.refresh; + +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + +import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler; +import com.tencent.polaris.client.util.NamedThreadFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.springframework.boot.context.event.ApplicationReadyEvent; +import org.springframework.cloud.client.discovery.event.HeartbeatEvent; +import org.springframework.context.ApplicationEventPublisher; +import org.springframework.context.ApplicationEventPublisherAware; +import org.springframework.context.ApplicationListener; + +import static com.tencent.cloud.polaris.discovery.refresh.PolarisServiceStatusChangeListener.INDEX; + +/** + * Begin refresh when application is ready. + * + * @author Haotian Zhang + */ +public class PolarisRefreshApplicationReadyEventListener implements ApplicationListener, ApplicationEventPublisherAware { + + private static final Logger LOG = LoggerFactory.getLogger(PolarisRefreshApplicationReadyEventListener.class); + private static final int DELAY = 60; + private final PolarisDiscoveryHandler polarisDiscoveryHandler; + private final PolarisServiceStatusChangeListener polarisServiceStatusChangeListener; + private final ScheduledExecutorService refreshExecutor; + private ApplicationEventPublisher publisher; + + public PolarisRefreshApplicationReadyEventListener(PolarisDiscoveryHandler polarisDiscoveryHandler, PolarisServiceStatusChangeListener polarisServiceStatusChangeListener) { + this.polarisDiscoveryHandler = polarisDiscoveryHandler; + this.polarisServiceStatusChangeListener = polarisServiceStatusChangeListener; + this.refreshExecutor = Executors.newSingleThreadScheduledExecutor( + new NamedThreadFactory("polaris-service-refresh")); + } + + @Override + public void onApplicationEvent(ApplicationReadyEvent event) { + // Register service change listener. + polarisDiscoveryHandler.getSdkContext().getExtensions().getLocalRegistry() + .registerResourceListener(polarisServiceStatusChangeListener); + + // Begin scheduled refresh thread. + refresh(); + } + + /** + * Start the refresh thread. + */ + public void refresh() { + refreshExecutor.scheduleWithFixedDelay(() -> { + try { + // Trigger reload of gateway route cache. + this.publisher.publishEvent(new HeartbeatEvent(this, INDEX.getAndIncrement())); + } + catch (Exception e) { + LOG.error("refresh polaris service error.", e); + } + }, DELAY, DELAY, TimeUnit.SECONDS); + } + + @Override + public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) { + this.publisher = applicationEventPublisher; + } +} diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/refresh/PolarisRefreshConfiguration.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/refresh/PolarisRefreshConfiguration.java new file mode 100644 index 000000000..a1b04fae7 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/refresh/PolarisRefreshConfiguration.java @@ -0,0 +1,49 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + */ +package com.tencent.cloud.polaris.discovery.refresh; + +import com.tencent.cloud.polaris.context.ConditionalOnPolarisEnabled; +import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * Configuration for listening the change of service status. + * + * @author Haotian Zhang + */ +@Configuration +@ConditionalOnPolarisEnabled +public class PolarisRefreshConfiguration { + + @Bean + @ConditionalOnMissingBean + public PolarisServiceStatusChangeListener polarisServiceChangeListener() { + return new PolarisServiceStatusChangeListener(); + } + + @Bean + @ConditionalOnMissingBean + public PolarisRefreshApplicationReadyEventListener polarisServiceStatusApplicationReadyEventListener( + PolarisDiscoveryHandler polarisDiscoveryHandler, + PolarisServiceStatusChangeListener polarisServiceStatusChangeListener) { + return new PolarisRefreshApplicationReadyEventListener(polarisDiscoveryHandler, polarisServiceStatusChangeListener); + } +} diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/refresh/PolarisServiceStatusChangeListener.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/refresh/PolarisServiceStatusChangeListener.java new file mode 100644 index 000000000..8fd254274 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/refresh/PolarisServiceStatusChangeListener.java @@ -0,0 +1,97 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + */ +package com.tencent.cloud.polaris.discovery.refresh; + +import java.util.Set; +import java.util.concurrent.atomic.AtomicLong; +import java.util.stream.Collectors; + +import com.google.common.collect.Sets; +import com.tencent.polaris.api.plugin.registry.AbstractResourceEventListener; +import com.tencent.polaris.api.pojo.RegistryCacheValue; +import com.tencent.polaris.api.pojo.ServiceEventKey; +import com.tencent.polaris.client.pojo.ServiceInstancesByProto; +import com.tencent.polaris.client.pojo.ServicesByProto; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.springframework.cloud.client.discovery.event.HeartbeatEvent; +import org.springframework.context.ApplicationEventPublisher; +import org.springframework.context.ApplicationEventPublisherAware; +import org.springframework.util.CollectionUtils; + +/** + * Change listener of Polaris service info. When service info is created or deleted, or, instance of service is from 0 to + * + * @author Haotian Zhang + */ +public class PolarisServiceStatusChangeListener extends AbstractResourceEventListener implements ApplicationEventPublisherAware { + + /** + * Index of service info status. + */ + public static final AtomicLong INDEX = new AtomicLong(0); + + private static final Logger LOG = LoggerFactory.getLogger(PolarisServiceStatusChangeListener.class); + + private ApplicationEventPublisher publisher; + + @Override + public void onResourceUpdated(ServiceEventKey svcEventKey, RegistryCacheValue oldValue, + RegistryCacheValue newValue) { + if (newValue.getEventType() == ServiceEventKey.EventType.SERVICE) { + if (oldValue instanceof ServicesByProto && newValue instanceof ServicesByProto) { + LOG.debug("receive service={} change event", svcEventKey); + Set oldServiceInfoSet = ((ServicesByProto) oldValue).getServices().stream() + .map(i -> i.getNamespace() + "::" + i.getService()).collect(Collectors.toSet()); + Set newServiceInfoSet = ((ServicesByProto) newValue).getServices().stream() + .map(i -> i.getNamespace() + "::" + i.getService()).collect(Collectors.toSet()); + + Sets.SetView addServiceInfoSetView = Sets.difference(newServiceInfoSet, oldServiceInfoSet); + Sets.SetView deleteServiceInfoSetView = Sets.difference(oldServiceInfoSet, newServiceInfoSet); + + if (addServiceInfoSetView.isEmpty() && deleteServiceInfoSetView.isEmpty()) { + return; + } + LOG.info("Service status is update. Add service of {}. Delete service of {}", addServiceInfoSetView, deleteServiceInfoSetView); + + // Trigger reload of gateway route cache. + this.publisher.publishEvent(new HeartbeatEvent(this, INDEX.getAndIncrement())); + } + } + else if (newValue.getEventType() == ServiceEventKey.EventType.INSTANCE) { + if (oldValue instanceof ServiceInstancesByProto && newValue instanceof ServiceInstancesByProto) { + LOG.debug("receive service instances={} change event", svcEventKey); + ServiceInstancesByProto oldIns = (ServiceInstancesByProto) oldValue; + ServiceInstancesByProto newIns = (ServiceInstancesByProto) newValue; + if ((CollectionUtils.isEmpty(oldIns.getInstances()) && !CollectionUtils.isEmpty(newIns.getInstances())) || + (!CollectionUtils.isEmpty(oldIns.getInstances()) && CollectionUtils.isEmpty(newIns.getInstances()))) { + LOG.info("Service status of {} is update.", newIns.getService()); + + // Trigger reload of gateway route cache. + this.publisher.publishEvent(new HeartbeatEvent(this, INDEX.getAndIncrement())); + } + } + } + } + + @Override + public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) { + this.publisher = applicationEventPublisher; + } +} diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceChangeListener.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceChangeListener.java deleted file mode 100644 index d52628f28..000000000 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceChangeListener.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.tencent.cloud.polaris.registry; - -import java.util.Set; -import java.util.concurrent.atomic.AtomicInteger; -import java.util.stream.Collectors; - -import com.google.common.collect.Sets; -import com.tencent.polaris.api.plugin.registry.AbstractResourceEventListener; -import com.tencent.polaris.api.pojo.RegistryCacheValue; -import com.tencent.polaris.api.pojo.ServiceEventKey; -import com.tencent.polaris.client.pojo.ServicesByProto; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import org.springframework.cloud.client.discovery.event.HeartbeatEvent; -import org.springframework.context.ApplicationEventPublisher; -import org.springframework.context.ApplicationEventPublisherAware; - -/** - * Change listener of Polaris service info. - * - * @author Haotian Zhang - */ -public class PolarisServiceChangeListener extends AbstractResourceEventListener implements ApplicationEventPublisherAware { - - private static final Logger LOG = LoggerFactory.getLogger(PolarisServiceChangeListener.class); - - private static final AtomicInteger INDEX = new AtomicInteger(0); - - private ApplicationEventPublisher publisher; - - @Override - public void onResourceUpdated(ServiceEventKey svcEventKey, RegistryCacheValue oldValue, - RegistryCacheValue newValue) { - if (newValue.getEventType() != ServiceEventKey.EventType.SERVICE) { - return; - } - if (oldValue instanceof ServicesByProto && newValue instanceof ServicesByProto) { - LOG.debug("receive service={} change event", svcEventKey); - Set oldServiceInfoSet = ((ServicesByProto) oldValue).getServices().stream() - .map(i -> i.getNamespace() + "::" + i.getService()).collect(Collectors.toSet()); - Set newServiceInfoSet = ((ServicesByProto) newValue).getServices().stream() - .map(i -> i.getNamespace() + "::" + i.getService()).collect(Collectors.toSet()); - - Sets.SetView addServiceInfoSetView = Sets.difference(newServiceInfoSet, oldServiceInfoSet); - Sets.SetView deleteServiceInfoSetView = Sets.difference(oldServiceInfoSet, newServiceInfoSet); - - if (addServiceInfoSetView.isEmpty() && deleteServiceInfoSetView.isEmpty()) { - return; - } - LOG.info("Service info is update. Add service of {}. Delete service of {}", addServiceInfoSetView, deleteServiceInfoSetView); - - // Trigger reload of gateway route cache. - this.publisher.publishEvent(new HeartbeatEvent(this, INDEX.getAndIncrement())); - } - } - - @Override - public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) { - this.publisher = applicationEventPublisher; - } -} diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java index 6b171c1de..0d39c7262 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java @@ -64,11 +64,9 @@ public class PolarisServiceRegistry implements ServiceRegistry { private final ScheduledExecutorService heartbeatExecutor; - private final PolarisServiceChangeListener polarisServiceChangeListener; - public PolarisServiceRegistry(PolarisDiscoveryProperties polarisDiscoveryProperties, PolarisDiscoveryHandler polarisDiscoveryHandler, - MetadataLocalProperties metadataLocalProperties, PolarisServiceChangeListener polarisServiceChangeListener) { + MetadataLocalProperties metadataLocalProperties) { this.polarisDiscoveryProperties = polarisDiscoveryProperties; this.polarisDiscoveryHandler = polarisDiscoveryHandler; this.metadataLocalProperties = metadataLocalProperties; @@ -80,8 +78,6 @@ public class PolarisServiceRegistry implements ServiceRegistry { else { this.heartbeatExecutor = null; } - - this.polarisServiceChangeListener = polarisServiceChangeListener; } @Override @@ -119,10 +115,6 @@ public class PolarisServiceRegistry implements ServiceRegistry { // Start the heartbeat thread after the registration is successful. heartbeat(heartbeatRequest); } - - // Register service change listener - polarisDiscoveryHandler.getSdkContext().getExtensions().getLocalRegistry() - .registerResourceListener(polarisServiceChangeListener); } catch (Exception e) { log.error("polaris registry, {} register failed...{},", diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistryAutoConfiguration.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistryAutoConfiguration.java index 72ac86162..14ce69c62 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistryAutoConfiguration.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistryAutoConfiguration.java @@ -27,7 +27,6 @@ import com.tencent.polaris.client.api.SDKContext; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; -import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationAutoConfiguration; @@ -53,9 +52,8 @@ public class PolarisServiceRegistryAutoConfiguration { @Bean public PolarisServiceRegistry polarisServiceRegistry( PolarisDiscoveryProperties polarisDiscoveryProperties, PolarisDiscoveryHandler polarisDiscoveryHandler, - MetadataLocalProperties metadataLocalProperties, PolarisServiceChangeListener polarisServiceChangeListener) { - return new PolarisServiceRegistry(polarisDiscoveryProperties, - polarisDiscoveryHandler, metadataLocalProperties, polarisServiceChangeListener); + MetadataLocalProperties metadataLocalProperties) { + return new PolarisServiceRegistry(polarisDiscoveryProperties, polarisDiscoveryHandler, metadataLocalProperties); } @Bean @@ -76,10 +74,4 @@ public class PolarisServiceRegistryAutoConfiguration { return new PolarisAutoServiceRegistration(registry, autoServiceRegistrationProperties, registration); } - - @Bean - @ConditionalOnMissingBean - public PolarisServiceChangeListener polarisServiceChangeListener() { - return new PolarisServiceChangeListener(); - } } diff --git a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/pojo/PolarisServer.java b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/pojo/PolarisServer.java index be55ed31e..fa458f5c3 100644 --- a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/pojo/PolarisServer.java +++ b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/pojo/PolarisServer.java @@ -48,6 +48,7 @@ public class PolarisServer extends Server { } this.serviceInstances = serviceInstances; this.instance = instance; + this.setAlive(true); this.metaInfo = new MetaInfo() { @Override public String getAppName() {