fix:nacos & polaris discovery (#816)

pull/826/head
liaochuntao 2 years ago committed by GitHub
parent 4a3d72a455
commit beebcecb50
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -29,3 +29,4 @@
- [feat:update getPort method of PolarisRegistration.](https://github.com/Tencent/spring-cloud-tencent/pull/803) - [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) - [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) - [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)

@ -18,13 +18,16 @@
package com.tencent.cloud.polaris.discovery; 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.reactive.PolarisReactiveDiscoveryClientConfiguration;
import com.tencent.cloud.polaris.discovery.refresh.PolarisRefreshConfiguration; 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.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.lang.Nullable;
/** /**
* Discovery Auto Configuration for Polaris. * Discovery Auto Configuration for Polaris.
@ -40,7 +43,12 @@ public class PolarisDiscoveryAutoConfiguration {
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public PolarisServiceDiscovery polarisServiceDiscovery( public PolarisServiceDiscovery polarisServiceDiscovery(
@Nullable NacosContextProperties nacosContextProperties,
PolarisDiscoveryProperties polarisDiscoveryProperties,
PolarisDiscoveryHandler polarisDiscoveryHandler) { 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 java.util.stream.Collectors;
import com.tencent.cloud.common.pojo.PolarisServiceInstance; 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.exception.PolarisException;
import com.tencent.polaris.api.pojo.Instance; import com.tencent.polaris.api.pojo.Instance;
import com.tencent.polaris.api.pojo.ServiceInfo; import com.tencent.polaris.api.pojo.ServiceInfo;
@ -37,9 +40,18 @@ import org.springframework.util.CollectionUtils;
*/ */
public class PolarisServiceDiscovery { public class PolarisServiceDiscovery {
private final NacosContextProperties nacosContextProperties;
private final PolarisDiscoveryProperties polarisDiscoveryProperties;
private final PolarisDiscoveryHandler polarisDiscoveryHandler; 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; this.polarisDiscoveryHandler = polarisDiscoveryHandler;
} }
@ -50,6 +62,7 @@ public class PolarisServiceDiscovery {
* @throws PolarisException polarisException * @throws PolarisException polarisException
*/ */
public List<ServiceInstance> getInstances(String serviceId) throws PolarisException { public List<ServiceInstance> getInstances(String serviceId) throws PolarisException {
serviceId = DiscoveryUtil.rewriteServiceId(serviceId);
List<ServiceInstance> instances = new ArrayList<>(); List<ServiceInstance> instances = new ArrayList<>();
InstancesResponse filteredInstances = polarisDiscoveryHandler.getHealthyInstances(serviceId); InstancesResponse filteredInstances = polarisDiscoveryHandler.getHealthyInstances(serviceId);
ServiceInstances serviceInstances = filteredInstances.toServiceInstances(); ServiceInstances serviceInstances = filteredInstances.toServiceInstances();
@ -71,4 +84,5 @@ public class PolarisServiceDiscovery {
return polarisDiscoveryHandler.getServices().getServices().stream() return polarisDiscoveryHandler.getServices().getServices().stream()
.map(ServiceInfo::getService).collect(Collectors.toList()); .map(ServiceInfo::getService).collect(Collectors.toList());
} }
} }

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

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

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

@ -24,6 +24,7 @@ import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import com.tencent.cloud.common.util.ApplicationContextAwareUtils; import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
import com.tencent.cloud.common.util.DiscoveryUtil;
import com.tencent.cloud.common.util.JacksonUtils; import com.tencent.cloud.common.util.JacksonUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -102,13 +103,13 @@ public class MetadataContext {
"spring.cloud.polaris.discovery.service", ApplicationContextAwareUtils "spring.cloud.polaris.discovery.service", ApplicationContextAwareUtils
.getProperties("spring.application.name", null)); .getProperties("spring.application.name", null));
} }
if (!StringUtils.hasText(serviceName)) { if (!StringUtils.hasText(serviceName)) {
LOG.error("service name should not be blank. please configure spring.cloud.polaris.service or " LOG.error("service name should not be blank. please configure spring.cloud.polaris.service or "
+ "spring.cloud.polaris.discovery.service or spring.application.name"); + "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 " 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"); + "spring.cloud.polaris.discovery.service or spring.application.name");
} }
serviceName = DiscoveryUtil.rewriteServiceId(serviceName);
LOCAL_SERVICE = serviceName; LOCAL_SERVICE = serviceName;
} }
@ -118,7 +119,6 @@ public class MetadataContext {
this.fragmentContexts = new ConcurrentHashMap<>(); this.fragmentContexts = new ConcurrentHashMap<>();
} }
public Map<String, String> getDisposableMetadata() { public Map<String, String> getDisposableMetadata() {
return this.getFragmentContext(MetadataContext.FRAGMENT_DISPOSABLE); 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;
}
}
}

@ -73,7 +73,7 @@
<revision>1.9.0-2021.0.5-SNAPSHOT</revision> <revision>1.9.0-2021.0.5-SNAPSHOT</revision>
<!-- Dependencies --> <!-- Dependencies -->
<polaris.version>1.10.1</polaris.version> <polaris.version>1.10.2</polaris.version>
<guava.version>31.0.1-jre</guava.version> <guava.version>31.0.1-jre</guava.version>
<logback.version>1.2.11</logback.version> <logback.version>1.2.11</logback.version>
<mocktio.version>4.5.1</mocktio.version> <mocktio.version>4.5.1</mocktio.version>

@ -35,6 +35,16 @@ spring:
# service-name: ${spring.application.name} # service-name: ${spring.application.name}
# ip-address: localhost # ip-address: localhost
# prefer-ip-address: true # 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: #eureka:
# client: # client:
# serviceUrl: # serviceUrl:

@ -43,14 +43,14 @@ spring:
# prefer-ip-address: true # prefer-ip-address: true
# nacos: # nacos:
# enabled: true # enabled: true
# password: nacos
# username: nacos
# context-path: /nacos # context-path: /nacos
# discovery: # discovery:
# enabled: true # enabled: true
# register-enabled: true # register-enabled: true
# group: polaris # group: polaris
# server-addr: 127.0.0.1:8848 # server-addr: 127.0.0.1:8848
# username: nacos
# password: nacos
# cluster-name: polaris # cluster-name: polaris
#eureka: #eureka:
# client: # client:

Loading…
Cancel
Save