fix:nacos & polaris discovery (#820)

Co-authored-by: liaochuntao <liaochuntao@live.com>
pull/828/head
Haotian Zhang 2 years ago committed by GitHub
parent cdffc4858c
commit 354da746db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -22,3 +22,4 @@
- [fix:fix nacos enabled condition bugs.](https://github.com/Tencent/spring-cloud-tencent/pull/795)
- [feat:update getPort method of PolarisRegistration.](https://github.com/Tencent/spring-cloud-tencent/pull/805)
- [fix:fix some compile output issues.](https://github.com/Tencent/spring-cloud-tencent/pull/812)
- [fix:nacos & polaris discovery](https://github.com/Tencent/spring-cloud-tencent/pull/820)

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

@ -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;
@ -39,9 +42,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;
}
@ -52,6 +64,7 @@ public class PolarisServiceDiscovery {
* @throws PolarisException polarisException
*/
public List<ServiceInstance> getInstances(String serviceId) throws PolarisException {
serviceId = DiscoveryUtil.rewriteServiceId(serviceId);
List<ServiceInstance> instances = new ArrayList<>();
InstancesResponse filteredInstances = polarisDiscoveryHandler.getHealthyInstances(serviceId);
ServiceInstances serviceInstances = filteredInstances.toServiceInstances();
@ -73,4 +86,5 @@ public class PolarisServiceDiscovery {
return polarisDiscoveryHandler.getServices().getServices().stream()
.map(ServiceInfo::getService).collect(Collectors.toList());
}
}

@ -39,6 +39,7 @@ import org.springframework.util.CollectionUtils;
* @author lingxiao.wlx
*/
public class NacosConfigModifier implements PolarisConfigModifier {
private static final String ID = "nacos";
/**
* nacos username.

@ -83,10 +83,12 @@ public class PolarisServiceRegistry implements ServiceRegistry<PolarisRegistrati
LOGGER.warn("No service to register for polaris client...");
return;
}
String serviceId = registration.getServiceId();
// Register instance.
InstanceRegisterRequest instanceRegisterRequest = new InstanceRegisterRequest();
instanceRegisterRequest.setNamespace(polarisDiscoveryProperties.getNamespace());
instanceRegisterRequest.setService(registration.getServiceId());
instanceRegisterRequest.setService(serviceId);
instanceRegisterRequest.setHost(registration.getHost());
instanceRegisterRequest.setPort(registration.getPort());
instanceRegisterRequest.setWeight(polarisDiscoveryProperties.getWeight());

@ -19,12 +19,14 @@ package com.tencent.cloud.polaris.discovery;
import java.util.List;
import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
import com.tencent.polaris.api.exception.PolarisException;
import com.tencent.polaris.api.pojo.DefaultInstance;
import com.tencent.polaris.api.pojo.DefaultServiceInstances;
import com.tencent.polaris.api.pojo.ServiceInfo;
import com.tencent.polaris.api.rpc.InstancesResponse;
import com.tencent.polaris.api.rpc.ServicesResponse;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
@ -34,6 +36,7 @@ import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.StaticApplicationContext;
import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
import static java.util.Collections.singletonList;
@ -56,6 +59,11 @@ public class PolarisServiceDiscoveryTest {
@InjectMocks
private PolarisServiceDiscovery polarisServiceDiscovery;
@Before
public void before() {
new ApplicationContextAwareUtils().setApplicationContext(new StaticApplicationContext());
}
@Test
public void testGetInstances() {
DefaultServiceInstances mockDefaultServiceInstances = mock(DefaultServiceInstances.class);

@ -24,6 +24,7 @@ import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
import com.tencent.cloud.common.util.DiscoveryUtil;
import com.tencent.cloud.common.util.JacksonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -79,31 +80,36 @@ public class MetadataContext {
public static String LOCAL_SERVICE;
static {
String namespace = ApplicationContextAwareUtils.getProperties("spring.cloud.polaris.namespace");
if (StringUtils.isEmpty(namespace)) {
String namespace = ApplicationContextAwareUtils
.getProperties("spring.cloud.polaris.namespace");
if (!StringUtils.hasText(namespace)) {
namespace = ApplicationContextAwareUtils
.getProperties("spring.cloud.polaris.discovery.namespace", "default");
}
if (StringUtils.isEmpty(namespace)) {
if (!StringUtils.hasText(namespace)) {
LOG.error("namespace should not be blank. please configure spring.cloud.polaris.namespace or "
+ "spring.cloud.polaris.discovery.namespace");
throw new RuntimeException("namespace should not be blank. please configure spring.cloud.polaris.namespace or "
+ "spring.cloud.polaris.discovery.namespace");
}
LOCAL_NAMESPACE = namespace;
String serviceName = ApplicationContextAwareUtils.getProperties("spring.cloud.polaris.service");
if (StringUtils.isEmpty(serviceName)) {
String serviceName = ApplicationContextAwareUtils
.getProperties("spring.cloud.polaris.service");
if (!StringUtils.hasText(serviceName)) {
serviceName = ApplicationContextAwareUtils.getProperties(
"spring.cloud.polaris.discovery.service", ApplicationContextAwareUtils
.getProperties("spring.application.name", null));
}
if (StringUtils.isEmpty(serviceName)) {
if (!StringUtils.hasText(serviceName)) {
LOG.error("service name should not be blank. please configure spring.cloud.polaris.service or "
+ "spring.cloud.polaris.discovery.service or spring.application.name");
throw new RuntimeException("service name should not be blank. please configure spring.cloud.polaris.service or "
+ "spring.cloud.polaris.discovery.service or spring.application.name");
}
serviceName = DiscoveryUtil.rewriteServiceId(serviceName);
LOCAL_SERVICE = serviceName;
}
@ -113,7 +119,6 @@ public class MetadataContext {
this.fragmentContexts = new ConcurrentHashMap<>();
}
public Map<String, String> getDisposableMetadata() {
return this.getFragmentContext(MetadataContext.FRAGMENT_DISPOSABLE);
}

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

@ -71,14 +71,16 @@
<properties>
<revision>1.9.0-Hoxton.SR12-SNAPSHOT</revision>
<polaris.version>1.10.1</polaris.version>
<!-- Dependencies -->
<polaris.version>1.10.2</polaris.version>
<guava.version>31.0.1-jre</guava.version>
<logback.version>1.2.11</logback.version>
<mocktio.version>4.5.1</mocktio.version>
<byte-buddy.version>1.12.10</byte-buddy.version>
<jackson.version>2.12.7</jackson.version>
<protobuf-java.version>3.21.7</protobuf-java.version>
<bcprov-jdk15on.version>1.69</bcprov-jdk15on.version>
<guava.version>31.0.1-jre</guava.version>
<system-stubs-junit4.version>2.0.1</system-stubs-junit4.version>
<!-- Maven Plugin Versions -->

@ -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:

@ -40,14 +40,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:

Loading…
Cancel
Save