fix: fix nacos service discovery. (#1752)

Signed-off-by: Haotian Zhang <928016560@qq.com>
pull/1757/head
Fishtail 3 weeks ago committed by Haotian Zhang
parent 3c3a7668c8
commit 158fd910fb

@ -30,3 +30,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/1752)

@ -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);
}
}

@ -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<String> 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;
}
}

@ -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",

@ -25,7 +25,9 @@ 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.common.util.PolarisCompletableFutureUtils;
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;
@ -62,13 +64,12 @@ import org.springframework.web.reactive.function.client.WebClient;
public class QuickstartCallerController {
private static final Logger LOG = LoggerFactory.getLogger(QuickstartCallerController.class);
@Autowired(required = false)
PolarisServiceDiscovery polarisServiceDiscovery;
@Autowired
private RestTemplate restTemplate;
@Autowired
private QuickstartCalleeService quickstartCalleeService;
@Autowired
private WebClient.Builder webClientBuilder;
@ -313,4 +314,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";
}
}
}

Loading…
Cancel
Save