refactor:refactor Polaris registration. (#1057)

pull/1065/head
Haotian Zhang 1 year ago committed by GitHub
parent 5750e510c2
commit 360b29248d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -17,3 +17,4 @@
- [fix:fix reporting bug when port is -1.](https://github.com/Tencent/spring-cloud-tencent/pull/1040)
- [fix:update guava version.](https://github.com/Tencent/spring-cloud-tencent/pull/1043)
- [fix:fix circuit breaker bean load order bug when using Nacos discovery.](https://github.com/Tencent/spring-cloud-tencent/pull/1050)
- [refactor:refactor Polaris registration.](https://github.com/Tencent/spring-cloud-tencent/pull/1057)

@ -18,6 +18,7 @@
package com.tencent.cloud.polaris.registry;
import com.tencent.cloud.common.metadata.MetadataContext;
import com.tencent.cloud.polaris.PolarisDiscoveryProperties;
import com.tencent.polaris.api.pojo.ServiceKey;
import com.tencent.polaris.assembly.api.AssemblyAPI;
import org.slf4j.Logger;
@ -39,16 +40,20 @@ public class PolarisAutoServiceRegistration extends AbstractAutoServiceRegistrat
private final PolarisRegistration registration;
private final PolarisDiscoveryProperties polarisDiscoveryProperties;
private final AssemblyAPI assemblyAPI;
public PolarisAutoServiceRegistration(
ServiceRegistry<PolarisRegistration> serviceRegistry,
AutoServiceRegistrationProperties autoServiceRegistrationProperties,
PolarisRegistration registration,
PolarisDiscoveryProperties polarisDiscoveryProperties,
AssemblyAPI assemblyAPI
) {
super(serviceRegistry, autoServiceRegistrationProperties);
this.registration = registration;
this.polarisDiscoveryProperties = polarisDiscoveryProperties;
this.assemblyAPI = assemblyAPI;
}
@ -84,7 +89,7 @@ public class PolarisAutoServiceRegistration extends AbstractAutoServiceRegistrat
@Override
protected Object getConfiguration() {
return this.registration.getPolarisProperties();
return this.polarisDiscoveryProperties;
}
@Override
@ -95,7 +100,7 @@ public class PolarisAutoServiceRegistration extends AbstractAutoServiceRegistrat
@Override
@SuppressWarnings("deprecation")
protected String getAppName() {
String appName = registration.getPolarisProperties().getService();
String appName = registration.getServiceId();
return StringUtils.isEmpty(appName) ? super.getAppName() : appName;
}
}

@ -13,13 +13,13 @@
* 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.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@ -55,24 +55,18 @@ public class PolarisRegistration implements Registration {
private final PolarisDiscoveryProperties polarisDiscoveryProperties;
private final ConsulContextProperties consulContextProperties;
private final SDKContext polarisContext;
private final StaticMetadataManager staticMetadataManager;
private final NacosContextProperties nacosContextProperties;
private final String serviceId;
private final String host;
private final boolean isSecure;
private final ServletWebServerApplicationContext servletWebServerApplicationContext;
private final ReactiveWebServerApplicationContext reactiveWebServerApplicationContext;
private boolean registerEnabled = false;
private Map<String, String> metadata;
private final String host;
private int port;
private String instanceId;
public PolarisRegistration(
@ -84,34 +78,97 @@ public class PolarisRegistration implements Registration {
@Nullable ServletWebServerApplicationContext servletWebServerApplicationContext,
@Nullable ReactiveWebServerApplicationContext reactiveWebServerApplicationContext) {
this.polarisDiscoveryProperties = polarisDiscoveryProperties;
this.consulContextProperties = consulContextProperties;
this.polarisContext = context;
this.staticMetadataManager = staticMetadataManager;
this.nacosContextProperties = nacosContextProperties;
this.servletWebServerApplicationContext = servletWebServerApplicationContext;
this.reactiveWebServerApplicationContext = reactiveWebServerApplicationContext;
host = polarisContext.getConfig().getGlobal().getAPI().getBindIP();
if (polarisContextProperties != null) {
port = polarisContextProperties.getLocalPort();
}
}
@Override
public String getServiceId() {
// generate serviceId
if (Objects.isNull(nacosContextProperties)) {
return polarisDiscoveryProperties.getService();
serviceId = polarisDiscoveryProperties.getService();
}
else {
String group = nacosContextProperties.getGroup();
if (StringUtils.isNotBlank(group) && !DEFAULT_GROUP.equals(group)) {
return String.format(GROUP_SERVER_ID_FORMAT, group, polarisDiscoveryProperties.getService());
serviceId = String.format(GROUP_SERVER_ID_FORMAT, group, polarisDiscoveryProperties.getService());
}
else {
return polarisDiscoveryProperties.getService();
serviceId = polarisDiscoveryProperties.getService();
}
}
// generate host
host = polarisContext.getConfig().getGlobal().getAPI().getBindIP();
// generate port
if (polarisContextProperties != null) {
port = polarisContextProperties.getLocalPort();
}
// generate isSecure
isSecure = StringUtils.equalsIgnoreCase(polarisDiscoveryProperties.getProtocol(), "https");
// generate metadata
if (CollectionUtils.isEmpty(metadata)) {
Map<String, String> instanceMetadata = new HashMap<>();
// put internal metadata
instanceMetadata.put(METADATA_KEY_IP, host);
instanceMetadata.put(METADATA_KEY_ADDRESS, host + ":" + port);
// put internal-nacos-cluster if necessary
if (Objects.nonNull(nacosContextProperties)) {
String clusterName = nacosContextProperties.getClusterName();
if (StringUtils.isNotBlank(clusterName) && !DEFAULT_CLUSTER.equals(clusterName)) {
instanceMetadata.put(NACOS_CLUSTER, clusterName);
}
}
instanceMetadata.putAll(staticMetadataManager.getMergedStaticMetadata());
this.metadata = instanceMetadata;
}
// generate registerEnabled
if (null != polarisDiscoveryProperties) {
registerEnabled = polarisDiscoveryProperties.isRegisterEnabled();
}
if (null != consulContextProperties && consulContextProperties.isEnabled()) {
registerEnabled |= consulContextProperties.isRegister();
}
if (null != nacosContextProperties && nacosContextProperties.isEnabled()) {
registerEnabled |= nacosContextProperties.isRegisterEnabled();
}
}
public static PolarisRegistration registration(PolarisDiscoveryProperties polarisDiscoveryProperties,
@Nullable PolarisContextProperties polarisContextProperties,
@Nullable ConsulContextProperties consulContextProperties,
SDKContext context, StaticMetadataManager staticMetadataManager,
@Nullable NacosContextProperties nacosContextProperties,
@Nullable ServletWebServerApplicationContext servletWebServerApplicationContext,
@Nullable ReactiveWebServerApplicationContext reactiveWebServerApplicationContext,
@Nullable List<PolarisRegistrationCustomizer> registrationCustomizers) {
PolarisRegistration polarisRegistration = new PolarisRegistration(polarisDiscoveryProperties,
polarisContextProperties, consulContextProperties, context, staticMetadataManager,
nacosContextProperties, servletWebServerApplicationContext, reactiveWebServerApplicationContext);
customize(registrationCustomizers, polarisRegistration);
return polarisRegistration;
}
public static void customize(List<PolarisRegistrationCustomizer> registrationCustomizers, PolarisRegistration registration) {
if (registrationCustomizers != null) {
for (PolarisRegistrationCustomizer customizer : registrationCustomizers) {
customizer.customize(registration);
}
}
}
@Override
public String getServiceId() {
return serviceId;
}
@Override
public String getHost() {
return host;
@ -135,7 +192,7 @@ public class PolarisRegistration implements Registration {
@Override
public boolean isSecure() {
return StringUtils.equalsIgnoreCase(polarisDiscoveryProperties.getProtocol(), "https");
return isSecure;
}
@Override
@ -145,30 +202,9 @@ public class PolarisRegistration implements Registration {
@Override
public Map<String, String> getMetadata() {
if (CollectionUtils.isEmpty(metadata)) {
Map<String, String> instanceMetadata = new HashMap<>();
// put internal metadata
instanceMetadata.put(METADATA_KEY_IP, host);
instanceMetadata.put(METADATA_KEY_ADDRESS, host + ":" + port);
// put internal-nacos-cluster if necessary
String clusterName = nacosContextProperties.getClusterName();
if (StringUtils.isNotBlank(clusterName) && !DEFAULT_CLUSTER.equals(clusterName)) {
instanceMetadata.put(NACOS_CLUSTER, clusterName);
}
instanceMetadata.putAll(staticMetadataManager.getMergedStaticMetadata());
this.metadata = instanceMetadata;
}
return metadata;
}
public PolarisDiscoveryProperties getPolarisProperties() {
return polarisDiscoveryProperties;
}
@Override
public String getInstanceId() {
return instanceId;
@ -179,18 +215,6 @@ public class PolarisRegistration implements Registration {
}
public boolean isRegisterEnabled() {
boolean registerEnabled = false;
if (null != polarisDiscoveryProperties) {
registerEnabled = polarisDiscoveryProperties.isRegisterEnabled();
}
if (null != consulContextProperties && consulContextProperties.isEnabled()) {
registerEnabled |= consulContextProperties.isRegister();
}
if (null != nacosContextProperties && nacosContextProperties.isEnabled()) {
registerEnabled |= nacosContextProperties.isRegisterEnabled();
}
return registerEnabled;
}

@ -0,0 +1,27 @@
/*
* 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.polaris.registry;
/**
* Customizer for {@link PolarisRegistration}.
*
* @author Haotian Zhang
*/
public interface PolarisRegistrationCustomizer {
void customize(PolarisRegistration registration);
}

@ -13,11 +13,12 @@
* 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.List;
import com.tencent.cloud.common.metadata.StaticMetadataManager;
import com.tencent.cloud.polaris.PolarisDiscoveryProperties;
import com.tencent.cloud.polaris.context.PolarisSDKContextManager;
@ -72,10 +73,11 @@ public class PolarisServiceRegistryAutoConfiguration {
PolarisSDKContextManager polarisSDKContextManager, StaticMetadataManager staticMetadataManager,
NacosContextProperties nacosContextProperties,
@Autowired(required = false) ServletWebServerApplicationContext servletWebServerApplicationContext,
@Autowired(required = false) ReactiveWebServerApplicationContext reactiveWebServerApplicationContext) {
return new PolarisRegistration(polarisDiscoveryProperties, polarisContextProperties, consulContextProperties,
@Autowired(required = false) ReactiveWebServerApplicationContext reactiveWebServerApplicationContext,
@Autowired(required = false) List<PolarisRegistrationCustomizer> registrationCustomizers) {
return PolarisRegistration.registration(polarisDiscoveryProperties, polarisContextProperties, consulContextProperties,
polarisSDKContextManager.getSDKContext(), staticMetadataManager, nacosContextProperties,
servletWebServerApplicationContext, reactiveWebServerApplicationContext);
servletWebServerApplicationContext, reactiveWebServerApplicationContext, registrationCustomizers);
}
@Bean
@ -84,9 +86,11 @@ public class PolarisServiceRegistryAutoConfiguration {
PolarisServiceRegistry registry,
AutoServiceRegistrationProperties autoServiceRegistrationProperties,
PolarisRegistration registration,
PolarisDiscoveryProperties polarisDiscoveryProperties,
PolarisSDKContextManager polarisSDKContextManager
) {
return new PolarisAutoServiceRegistration(registry, autoServiceRegistrationProperties, registration, polarisSDKContextManager.getAssemblyAPI());
return new PolarisAutoServiceRegistration(registry, autoServiceRegistrationProperties, registration,
polarisDiscoveryProperties, polarisSDKContextManager.getAssemblyAPI());
}
@Bean

@ -70,12 +70,11 @@ public class PolarisAutoServiceRegistrationTest {
@BeforeEach
void setUp() {
doReturn(polarisDiscoveryProperties).when(registration).getPolarisProperties();
doNothing().when(serviceRegistry).register(nullable(PolarisRegistration.class));
polarisAutoServiceRegistration =
new PolarisAutoServiceRegistration(serviceRegistry, autoServiceRegistrationProperties, registration, null);
new PolarisAutoServiceRegistration(serviceRegistry, autoServiceRegistrationProperties, registration,
polarisDiscoveryProperties, null);
doReturn(environment).when(applicationContext).getEnvironment();
polarisAutoServiceRegistration.setApplicationContext(applicationContext);
@ -117,7 +116,7 @@ public class PolarisAutoServiceRegistrationTest {
doReturn("application").when(environment).getProperty(anyString(), anyString());
assertThat(polarisAutoServiceRegistration.getAppName()).isEqualTo("application");
doReturn(SERVICE_PROVIDER).when(polarisDiscoveryProperties).getService();
doReturn(SERVICE_PROVIDER).when(registration).getServiceId();
assertThat(polarisAutoServiceRegistration.getAppName()).isEqualTo(SERVICE_PROVIDER);
}
}

@ -0,0 +1,76 @@
/*
* 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.polaris.registry;
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.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
/**
* Test for {@link PolarisRegistrationCustomizer}.
*
* @author Haotian Zhang
*/
public class PolarisRegistrationCustomizerTest {
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.polaris.address=grpc://127.0.0.1:10081");
@BeforeEach
public void setUp() {
PolarisSDKContextManager.innerDestroy();
}
@Test
public void testCustomize() {
this.contextRunner.run(context -> {
PolarisRegistrationCustomizer customizer = context.getBean(PolarisRegistrationCustomizer.class);
verify(customizer, times(1)).customize(any(PolarisRegistration.class));
});
}
@Configuration
@EnableAutoConfiguration
static class PolarisServiceRegistryAutoConfiguration {
@Bean
public PolarisRegistrationCustomizer polarisRegistrationCustomizer() {
return mock(PolarisRegistrationCustomizer.class);
}
}
}

@ -57,16 +57,13 @@ import static org.mockito.Mockito.when;
@MockitoSettings(strictness = Strictness.LENIENT)
public class PolarisRegistrationTest {
private static final int testLocalPort = 10086;
private NacosContextProperties nacosContextProperties;
private PolarisRegistration polarisRegistration1;
private PolarisRegistration polarisRegistration2;
private PolarisRegistration polarisRegistration3;
private PolarisRegistration polarisRegistration4;
private static int testLocalPort = 10086;
@BeforeEach
void setUp() {
// mock PolarisDiscoveryProperties
@ -122,21 +119,21 @@ public class PolarisRegistrationTest {
ReactiveWebServerApplicationContext reactiveWebServerApplicationContext = mock(ReactiveWebServerApplicationContext.class);
doReturn(reactiveWebServer).when(reactiveWebServerApplicationContext).getWebServer();
polarisRegistration1 = new PolarisRegistration(polarisDiscoveryProperties, null, consulContextProperties,
polarisRegistration1 = PolarisRegistration.registration(polarisDiscoveryProperties, null, consulContextProperties,
polarisContext, staticMetadataManager, nacosContextProperties,
servletWebServerApplicationContext, null);
servletWebServerApplicationContext, null, null);
polarisRegistration2 = new PolarisRegistration(polarisDiscoveryProperties, null, consulContextProperties,
polarisRegistration2 = PolarisRegistration.registration(polarisDiscoveryProperties, null, consulContextProperties,
polarisContext, staticMetadataManager, nacosContextProperties,
null, reactiveWebServerApplicationContext);
null, reactiveWebServerApplicationContext, null);
polarisRegistration3 = new PolarisRegistration(polarisDiscoveryProperties, null, consulContextProperties,
polarisRegistration3 = PolarisRegistration.registration(polarisDiscoveryProperties, null, consulContextProperties,
polarisContext, staticMetadataManager, nacosContextProperties,
null, null);
null, null, null);
polarisRegistration4 = new PolarisRegistration(polarisDiscoveryProperties, polarisContextProperties, consulContextProperties,
polarisRegistration4 = PolarisRegistration.registration(polarisDiscoveryProperties, polarisContextProperties, consulContextProperties,
polarisContext, staticMetadataManager, nacosContextProperties,
null, null);
null, null, null);
}
@Test
@ -187,11 +184,6 @@ public class PolarisRegistrationTest {
assertThat(metadata.get("key1")).isEqualTo("value1");
}
@Test
public void testGetPolarisProperties() {
assertThat(polarisRegistration1.getPolarisProperties()).isNotNull();
}
@Test
public void testIsRegisterEnabled() {
assertThat(polarisRegistration1.isRegisterEnabled()).isTrue();

Loading…
Cancel
Save