feat: support service registry and discovery with Polaris and Nacos (#1724)
parent
fa5d3ac2b5
commit
7808ebb62e
@ -0,0 +1,37 @@
|
||||
/*
|
||||
* 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.registry.nacos;
|
||||
|
||||
import com.tencent.cloud.polaris.extend.nacos.NacosContextProperties;
|
||||
import com.tencent.cloud.polaris.registry.PolarisServiceRegistryAutoConfiguration;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@AutoConfigureBefore(PolarisServiceRegistryAutoConfiguration.class)
|
||||
public class NacosDiscoveryRegistryAutoConfiguration {
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public NacosPolarisRegistrationCustomizer nacosPolarisRegistrationCustomizer(
|
||||
NacosContextProperties nacosContextProperties) {
|
||||
return new NacosPolarisRegistrationCustomizer(nacosContextProperties);
|
||||
}
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
/*
|
||||
* 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.registry.nacos;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
import com.tencent.cloud.polaris.extend.nacos.NacosContextProperties;
|
||||
import com.tencent.cloud.polaris.registry.PolarisRegistration;
|
||||
import com.tencent.cloud.polaris.registry.PolarisRegistrationCustomizer;
|
||||
import com.tencent.polaris.api.utils.StringUtils;
|
||||
|
||||
|
||||
import static com.tencent.polaris.plugins.connector.common.constant.NacosConstant.MetadataMapKey.NACOS_CLUSTER_KEY;
|
||||
import static com.tencent.polaris.plugins.connector.common.constant.NacosConstant.MetadataMapKey.NACOS_GROUP_KEY;
|
||||
import static shade.polaris.com.alibaba.nacos.api.common.Constants.DEFAULT_CLUSTER_NAME;
|
||||
import static shade.polaris.com.alibaba.nacos.api.common.Constants.DEFAULT_GROUP;
|
||||
|
||||
public class NacosPolarisRegistrationCustomizer implements PolarisRegistrationCustomizer {
|
||||
NacosContextProperties nacosContextProperties;
|
||||
public NacosPolarisRegistrationCustomizer(NacosContextProperties nacosContextProperties) {
|
||||
this.nacosContextProperties = nacosContextProperties;
|
||||
}
|
||||
@Override
|
||||
public void customize(PolarisRegistration registration) {
|
||||
// put internal-nacos-cluster if necessary
|
||||
Map<String, String> instanceMetadata = registration.getMetadata();
|
||||
if (Objects.nonNull(nacosContextProperties)) {
|
||||
String clusterName = nacosContextProperties.getClusterName();
|
||||
if (StringUtils.isNotBlank(clusterName) && !DEFAULT_CLUSTER_NAME.equals(clusterName)) {
|
||||
instanceMetadata.put(NACOS_CLUSTER_KEY, clusterName);
|
||||
}
|
||||
String groupName = nacosContextProperties.getGroup();
|
||||
if (StringUtils.isNotBlank(groupName) && !DEFAULT_GROUP.equals(groupName)) {
|
||||
instanceMetadata.put(NACOS_GROUP_KEY, groupName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,81 @@
|
||||
/*
|
||||
* 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.registry;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import com.tencent.cloud.polaris.context.PolarisSDKContextManager;
|
||||
import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration;
|
||||
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryClientConfiguration;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import static com.tencent.polaris.test.common.Consts.PORT;
|
||||
import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Test for {@link NacosPolarisRegistrationCustomizerTest}.
|
||||
*
|
||||
* @author Yuwei Fu
|
||||
*/
|
||||
public class NacosPolarisRegistrationCustomizerTest {
|
||||
|
||||
private final WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
|
||||
.withConfiguration(AutoConfigurations.of(
|
||||
PolarisContextAutoConfiguration.class,
|
||||
PolarisServiceRegistryAutoConfiguration.class,
|
||||
PolarisDiscoveryClientConfiguration.class))
|
||||
.withPropertyValues("spring.application.name=" + SERVICE_PROVIDER)
|
||||
.withPropertyValues("server.port=" + PORT)
|
||||
.withPropertyValues("spring.cloud.nacos.discovery.cluster-name=nacos")
|
||||
.withPropertyValues("spring.cloud.nacos.discovery.group=nacos")
|
||||
.withPropertyValues("spring.cloud.polaris.address=grpc://127.0.0.1:10081");
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
PolarisSDKContextManager.innerDestroy();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomize() {
|
||||
this.contextRunner.run(context -> {
|
||||
PolarisRegistration polarisRegistration = context.getBean(PolarisRegistration.class);
|
||||
polarisRegistration.customize();
|
||||
Map<String, String> metadata = polarisRegistration.getMetadata();
|
||||
assertThat(metadata.get("nacos.cluster")).isEqualTo("nacos");
|
||||
assertThat(metadata.get("nacos.group")).isEqualTo("nacos");
|
||||
});
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
static class PolarisServiceRegistryAutoConfiguration {
|
||||
@Bean
|
||||
public PolarisRegistrationCustomizer polarisRegistrationCustomizer() {
|
||||
return mock(PolarisRegistrationCustomizer.class);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,108 +0,0 @@
|
||||
/*
|
||||
* 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.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 String NACOS_NAMESPACE;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* rewrite namespace when open double registry and discovery by nacos and polaris.
|
||||
*
|
||||
* @param namespace namespace
|
||||
* @return new namespace
|
||||
*/
|
||||
public static String rewriteNamespace(String namespace) {
|
||||
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) {
|
||||
namespace = NACOS_NAMESPACE;
|
||||
}
|
||||
}
|
||||
return namespace;
|
||||
}
|
||||
|
||||
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");
|
||||
NACOS_NAMESPACE = ApplicationContextAwareUtils.getProperties("spring.cloud.nacos.discovery.namespace", "public");
|
||||
INITIALIZE = true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue