diff --git a/CHANGELOG.md b/CHANGELOG.md index 5878f8a0..51bd7d9a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,3 +29,4 @@ - [feat:update getPort method of PolarisRegistration.](https://github.com/Tencent/spring-cloud-tencent/pull/803) - [fix:fix some compile output issues.](https://github.com/Tencent/spring-cloud-tencent/pull/814) - [Feature: upgrade framework version.](https://github.com/Tencent/spring-cloud-tencent/pull/815) +- [fix:nacos & polaris discovery](https://github.com/Tencent/spring-cloud-tencent/pull/816) 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 b92ca625..c35d0d72 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,13 +18,16 @@ package com.tencent.cloud.polaris.discovery; +import com.tencent.cloud.polaris.PolarisDiscoveryProperties; import com.tencent.cloud.polaris.discovery.reactive.PolarisReactiveDiscoveryClientConfiguration; import com.tencent.cloud.polaris.discovery.refresh.PolarisRefreshConfiguration; +import com.tencent.cloud.polaris.extend.nacos.NacosContextProperties; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; +import org.springframework.lang.Nullable; /** * Discovery Auto Configuration for Polaris. @@ -40,7 +43,12 @@ public class PolarisDiscoveryAutoConfiguration { @Bean @ConditionalOnMissingBean public PolarisServiceDiscovery polarisServiceDiscovery( + @Nullable NacosContextProperties nacosContextProperties, + PolarisDiscoveryProperties polarisDiscoveryProperties, PolarisDiscoveryHandler polarisDiscoveryHandler) { - return new PolarisServiceDiscovery(polarisDiscoveryHandler); + return new PolarisServiceDiscovery( + nacosContextProperties, + polarisDiscoveryProperties, + polarisDiscoveryHandler); } } diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/PolarisServiceDiscovery.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/PolarisServiceDiscovery.java index e46c34d8..022cab1d 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/PolarisServiceDiscovery.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/PolarisServiceDiscovery.java @@ -23,6 +23,9 @@ import java.util.List; import java.util.stream.Collectors; import com.tencent.cloud.common.pojo.PolarisServiceInstance; +import com.tencent.cloud.common.util.DiscoveryUtil; +import com.tencent.cloud.polaris.PolarisDiscoveryProperties; +import com.tencent.cloud.polaris.extend.nacos.NacosContextProperties; import com.tencent.polaris.api.exception.PolarisException; import com.tencent.polaris.api.pojo.Instance; import com.tencent.polaris.api.pojo.ServiceInfo; @@ -37,9 +40,18 @@ import org.springframework.util.CollectionUtils; */ public class PolarisServiceDiscovery { + private final NacosContextProperties nacosContextProperties; + + private final PolarisDiscoveryProperties polarisDiscoveryProperties; + private final PolarisDiscoveryHandler polarisDiscoveryHandler; - public PolarisServiceDiscovery(PolarisDiscoveryHandler polarisDiscoveryHandler) { + public PolarisServiceDiscovery( + NacosContextProperties nacosContextProperties, + PolarisDiscoveryProperties polarisDiscoveryProperties, + PolarisDiscoveryHandler polarisDiscoveryHandler) { + this.nacosContextProperties = nacosContextProperties; + this.polarisDiscoveryProperties = polarisDiscoveryProperties; this.polarisDiscoveryHandler = polarisDiscoveryHandler; } @@ -50,6 +62,7 @@ public class PolarisServiceDiscovery { * @throws PolarisException polarisException */ public List getInstances(String serviceId) throws PolarisException { + serviceId = DiscoveryUtil.rewriteServiceId(serviceId); List instances = new ArrayList<>(); InstancesResponse filteredInstances = polarisDiscoveryHandler.getHealthyInstances(serviceId); ServiceInstances serviceInstances = filteredInstances.toServiceInstances(); @@ -71,4 +84,5 @@ public class PolarisServiceDiscovery { return polarisDiscoveryHandler.getServices().getServices().stream() .map(ServiceInfo::getService).collect(Collectors.toList()); } + } diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/extend/nacos/NacosConfigModifier.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/extend/nacos/NacosConfigModifier.java index 6f8cbf78..01426a73 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/extend/nacos/NacosConfigModifier.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/extend/nacos/NacosConfigModifier.java @@ -40,6 +40,7 @@ import org.springframework.util.CollectionUtils; * @author lingxiao.wlx */ public class NacosConfigModifier implements PolarisConfigModifier { + private static final String ID = "nacos"; /** * nacos username. 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 4a185902..17ea5307 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 @@ -84,10 +84,12 @@ public class PolarisServiceRegistry implements ServiceRegistry(); } - public Map getDisposableMetadata() { return this.getFragmentContext(MetadataContext.FRAGMENT_DISPOSABLE); } diff --git a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/DiscoveryUtil.java b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/DiscoveryUtil.java new file mode 100644 index 00000000..e5e5237b --- /dev/null +++ b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/DiscoveryUtil.java @@ -0,0 +1,82 @@ +/* + * 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.util; + +/** + * Utils for Discovery. + */ +public final class DiscoveryUtil { + + private static String ENABLE_NACOS; + + private static String ENABLE_NACOS_DISCOVERY; + + private static String ENABLE_NACOS_REGISTRY; + + private static String ENABLE_POLARIS_DISCOVERY; + + private static String NACOS_GROUP; + + private static final Object MUTEX = new Object(); + + private static boolean INITIALIZE = false; + + private DiscoveryUtil() { + } + + /** + * rewrite serviceId when open double registry and discovery by nacos and polaris. + * + * @param serviceId service id + * @return new service id + */ + public static String rewriteServiceId(String serviceId) { + init(); + if (Boolean.parseBoolean(ENABLE_NACOS)) { + boolean rewrite = false; + if (Boolean.parseBoolean(ENABLE_NACOS_REGISTRY) && Boolean.parseBoolean(ENABLE_POLARIS_DISCOVERY)) { + rewrite = true; + } + if (Boolean.parseBoolean(ENABLE_NACOS_DISCOVERY) || Boolean.parseBoolean(ENABLE_POLARIS_DISCOVERY)) { + rewrite = true; + } + if (rewrite) { + serviceId = NACOS_GROUP + "__" + serviceId; + } + } + return serviceId; + } + + private static void init() { + if (INITIALIZE) { + return; + } + synchronized (MUTEX) { + if (INITIALIZE) { + return; + } + ENABLE_NACOS = ApplicationContextAwareUtils.getProperties("spring.cloud.nacos.enabled"); + ENABLE_NACOS_DISCOVERY = ApplicationContextAwareUtils.getProperties("spring.cloud.nacos.discovery.enabled"); + ENABLE_NACOS_REGISTRY = ApplicationContextAwareUtils.getProperties("spring.cloud.nacos.discovery.register-enabled"); + ENABLE_POLARIS_DISCOVERY = ApplicationContextAwareUtils.getProperties("spring.cloud.polaris.discovery.enabled"); + NACOS_GROUP = ApplicationContextAwareUtils.getProperties("spring.cloud.nacos.discovery.group", "DEFAULT_GROUP"); + INITIALIZE = true; + } + } + +} diff --git a/spring-cloud-tencent-dependencies/pom.xml b/spring-cloud-tencent-dependencies/pom.xml index 414d3850..dfba9cb2 100644 --- a/spring-cloud-tencent-dependencies/pom.xml +++ b/spring-cloud-tencent-dependencies/pom.xml @@ -73,7 +73,7 @@ 1.9.0-2021.0.5-SNAPSHOT - 1.10.1 + 1.10.2 31.0.1-jre 1.2.11 4.5.1 diff --git a/spring-cloud-tencent-examples/polaris-discovery-example/discovery-callee-service/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-callee-service/src/main/resources/bootstrap.yml index 7268c7fc..87b751a4 100644 --- a/spring-cloud-tencent-examples/polaris-discovery-example/discovery-callee-service/src/main/resources/bootstrap.yml +++ b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-callee-service/src/main/resources/bootstrap.yml @@ -35,6 +35,16 @@ spring: # service-name: ${spring.application.name} # ip-address: localhost # prefer-ip-address: true +# nacos: +# enabled: true +# password: nacos +# username: nacos +# discovery: +# enabled: true +# register-enabled: true +# group: polaris +# server-addr: 127.0.0.1:8848 + #eureka: # client: # serviceUrl: diff --git a/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/src/main/resources/bootstrap.yml index c36f4ac2..2f77154b 100644 --- a/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/src/main/resources/bootstrap.yml +++ b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/src/main/resources/bootstrap.yml @@ -43,14 +43,14 @@ spring: # prefer-ip-address: true # nacos: # enabled: true +# password: nacos +# username: nacos # context-path: /nacos # discovery: # enabled: true # register-enabled: true # group: polaris # server-addr: 127.0.0.1:8848 -# username: nacos -# password: nacos # cluster-name: polaris #eureka: # client: