From fbbac2ee7d6a040a27387f6703aa3bda64c63b6f Mon Sep 17 00:00:00 2001 From: Fishtail <49390359+fuyuwei01@users.noreply.github.com> Date: Thu, 20 Nov 2025 15:00:34 +0800 Subject: [PATCH] fix: fix nacos service discovery. (#1751) --- CHANGELOG.md | 1 + .../PolarisEagerLoadAutoConfiguration.java | 8 ++ .../ServicesEagerLoadSmartLifecycle.java | 84 +++++++++++++++++++ ...itional-spring-configuration-metadata.json | 6 ++ spring-cloud-tencent-dependencies/pom.xml | 2 +- .../caller/QuickstartCallerController.java | 14 ++++ 6 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/eager/instrument/services/ServicesEagerLoadSmartLifecycle.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 56f94f55c..521b7ff9c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,3 +31,4 @@ - [fix: tsf gateway config support tsf-data-access.](https://github.com/Tencent/spring-cloud-tencent/pull/1745) - [fix: fix multiple bugs in tsf.](https://github.com/Tencent/spring-cloud-tencent/pull/1746) - [fix: fix get gateway config in tsf ipv6.](https://github.com/Tencent/spring-cloud-tencent/pull/1747) +- [fix: fix nacos service discovery. ](https://github.com/Tencent/spring-cloud-tencent/pull/1751) \ No newline at end of file diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/eager/config/PolarisEagerLoadAutoConfiguration.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/eager/config/PolarisEagerLoadAutoConfiguration.java index 33560c29d..2b2949dd2 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/eager/config/PolarisEagerLoadAutoConfiguration.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/eager/config/PolarisEagerLoadAutoConfiguration.java @@ -20,6 +20,7 @@ package com.tencent.cloud.polaris.eager.config; import com.tencent.cloud.polaris.discovery.PolarisDiscoveryClient; import com.tencent.cloud.polaris.discovery.reactive.PolarisReactiveDiscoveryClient; import com.tencent.cloud.polaris.eager.instrument.feign.FeignEagerLoadSmartLifecycle; +import com.tencent.cloud.polaris.eager.instrument.services.ServicesEagerLoadSmartLifecycle; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; @@ -40,5 +41,12 @@ public class PolarisEagerLoadAutoConfiguration { @Autowired(required = false) PolarisReactiveDiscoveryClient polarisReactiveDiscoveryClient) { return new FeignEagerLoadSmartLifecycle(applicationContext, polarisDiscoveryClient, polarisReactiveDiscoveryClient); } + @Bean + @ConditionalOnProperty(name = "spring.cloud.polaris.discovery.eager-load.services.enabled", havingValue = "true", matchIfMissing = true) + public ServicesEagerLoadSmartLifecycle serviceEagerLoadSmartLifecycle( + ApplicationContext applicationContext, @Autowired(required = false) PolarisDiscoveryClient polarisDiscoveryClient, + @Autowired(required = false) PolarisReactiveDiscoveryClient polarisReactiveDiscoveryClient) { + return new ServicesEagerLoadSmartLifecycle(applicationContext, polarisDiscoveryClient, polarisReactiveDiscoveryClient); + } } diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/eager/instrument/services/ServicesEagerLoadSmartLifecycle.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/eager/instrument/services/ServicesEagerLoadSmartLifecycle.java new file mode 100644 index 000000000..175bb76c3 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/eager/instrument/services/ServicesEagerLoadSmartLifecycle.java @@ -0,0 +1,84 @@ +/* + * Tencent is pleased to support the open source community by making spring-cloud-tencent available. + * + * Copyright (C) 2021 Tencent. 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.eager.instrument.services; + +import java.util.List; + +import com.tencent.cloud.polaris.discovery.PolarisDiscoveryClient; +import com.tencent.cloud.polaris.discovery.reactive.PolarisReactiveDiscoveryClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.SmartLifecycle; + +public class ServicesEagerLoadSmartLifecycle implements SmartLifecycle { + + private static final Logger LOG = LoggerFactory.getLogger(ServicesEagerLoadSmartLifecycle.class); + + private final ApplicationContext applicationContext; + + private final PolarisDiscoveryClient polarisDiscoveryClient; + + private final PolarisReactiveDiscoveryClient polarisReactiveDiscoveryClient; + + public ServicesEagerLoadSmartLifecycle(ApplicationContext applicationContext, PolarisDiscoveryClient polarisDiscoveryClient, + PolarisReactiveDiscoveryClient polarisReactiveDiscoveryClient) { + this.applicationContext = applicationContext; + this.polarisDiscoveryClient = polarisDiscoveryClient; + this.polarisReactiveDiscoveryClient = polarisReactiveDiscoveryClient; + } + + @Override + public void start() { + LOG.info("services eager-load start"); + try { + if (polarisDiscoveryClient != null) { + List servicesList = polarisDiscoveryClient.getServices(); + LOG.info("eager-load got services: {}", servicesList); + } + else if (polarisReactiveDiscoveryClient != null) { + polarisReactiveDiscoveryClient.getServices().subscribe(services -> { + LOG.info("eager-load got services: {}", services); + }); + } + else { + LOG.warn("no discovery client found."); + } + } + catch (Exception e) { + LOG.debug("services eager-load failed.", e); + } + LOG.info("services eager-load end"); + } + + @Override + public void stop() { + + } + + @Override + public boolean isRunning() { + return false; + } + + @Override + public int getPhase() { + return 10; + } +} diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-cloud-starter-tencent-polaris-discovery/src/main/resources/META-INF/additional-spring-configuration-metadata.json index d28d2ce73..a401ce7af 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -108,6 +108,12 @@ "defaultValue": true, "description": "Feign eager load switch. Default: true." }, + { + "name": "spring.cloud.polaris.discovery.eager-load.services.enabled", + "type": "java.lang.Boolean", + "defaultValue": true, + "description": "Services eager load switch. Default: true." + }, { "name": "spring.cloud.nacos.discovery.enabled", "type": "java.lang.Boolean", diff --git a/spring-cloud-tencent-dependencies/pom.xml b/spring-cloud-tencent-dependencies/pom.xml index 85ed7074f..43382bf20 100644 --- a/spring-cloud-tencent-dependencies/pom.xml +++ b/spring-cloud-tencent-dependencies/pom.xml @@ -74,7 +74,7 @@ 2.1.0.2-2024.0.2-SNAPSHOT - 2.1.0.1 + 2.1.0.2-SNAPSHOT 2.8.13 diff --git a/spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/java/com/tencent/cloud/quickstart/caller/QuickstartCallerController.java b/spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/java/com/tencent/cloud/quickstart/caller/QuickstartCallerController.java index 97bf1abaf..f859fbf26 100644 --- a/spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/java/com/tencent/cloud/quickstart/caller/QuickstartCallerController.java +++ b/spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/java/com/tencent/cloud/quickstart/caller/QuickstartCallerController.java @@ -24,6 +24,8 @@ import java.util.concurrent.atomic.AtomicInteger; import com.tencent.cloud.common.metadata.MetadataContext; import com.tencent.cloud.common.metadata.MetadataContextHolder; +import com.tencent.cloud.common.util.JacksonUtils; +import com.tencent.cloud.polaris.discovery.PolarisServiceDiscovery; import com.tencent.cloud.quickstart.caller.pojo.User; import com.tencent.polaris.api.utils.StringUtils; import org.slf4j.Logger; @@ -69,6 +71,8 @@ public class QuickstartCallerController { @Autowired private WebClient.Builder webClientBuilder; + @Autowired(required = false) + PolarisServiceDiscovery polarisServiceDiscovery; /** * Get sum of two value. * @param value1 value 1 @@ -264,4 +268,14 @@ public class QuickstartCallerController { public String user(@RequestBody User user) { return restTemplate.postForObject("http://QuickstartCalleeService/quickstart/callee/user", user, String.class); } + + @GetMapping(value = "/getServices", produces = "application/json") + public String services() { + try { + return JacksonUtils.serialize2Json(polarisServiceDiscovery.getServices()); + } + catch (NullPointerException e) { + return "Polaris Discovery is not enabled. Please set spring.cloud.polaris.discovery.enabled=true"; + } + } }