fix: fix nacos service discovery. (#1752)

pull/1763/head
Fishtail 2 weeks ago committed by GitHub
parent f41fb6dd2f
commit b356355d18
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

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

@ -74,7 +74,7 @@
<revision>2.1.0.2-2021.0.9-SNAPSHOT</revision>
<!-- Polaris SDK version -->
<polaris.version>2.1.0.1</polaris.version>
<polaris.version>2.1.0.2-SNAPSHOT</polaris.version>
<!-- Dependencies -->
<bcpkix-jdk18on.version>1.78.1</bcpkix-jdk18on.version>

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

Loading…
Cancel
Save