fix:fix PolarisRegistration cannot get metadata bug. (#218)

pull/284/head
Haotian Zhang 3 years ago committed by GitHub
parent 58a82731fe
commit f0b499a1c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -19,9 +19,10 @@
package com.tencent.cloud.polaris.registry; package com.tencent.cloud.polaris.registry;
import java.net.URI; import java.net.URI;
import java.util.Collections; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import com.tencent.cloud.common.metadata.StaticMetadataManager;
import com.tencent.cloud.polaris.DiscoveryPropertiesAutoConfiguration; import com.tencent.cloud.polaris.DiscoveryPropertiesAutoConfiguration;
import com.tencent.cloud.polaris.PolarisDiscoveryProperties; import com.tencent.cloud.polaris.PolarisDiscoveryProperties;
import com.tencent.polaris.client.api.SDKContext; import com.tencent.polaris.client.api.SDKContext;
@ -30,6 +31,7 @@ import org.apache.commons.lang.StringUtils;
import org.springframework.cloud.client.DefaultServiceInstance; import org.springframework.cloud.client.DefaultServiceInstance;
import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.serviceregistry.Registration; import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.util.CollectionUtils;
/** /**
* Registration object of Polaris. * Registration object of Polaris.
@ -44,12 +46,17 @@ public class PolarisRegistration implements Registration, ServiceInstance {
private final SDKContext polarisContext; private final SDKContext polarisContext;
private final StaticMetadataManager staticMetadataManager;
private Map<String, String> metadata;
public PolarisRegistration( public PolarisRegistration(
DiscoveryPropertiesAutoConfiguration discoveryPropertiesAutoConfiguration, DiscoveryPropertiesAutoConfiguration discoveryPropertiesAutoConfiguration,
PolarisDiscoveryProperties polarisDiscoveryProperties, SDKContext context) { PolarisDiscoveryProperties polarisDiscoveryProperties, SDKContext context, StaticMetadataManager staticMetadataManager) {
this.discoveryPropertiesAutoConfiguration = discoveryPropertiesAutoConfiguration; this.discoveryPropertiesAutoConfiguration = discoveryPropertiesAutoConfiguration;
this.polarisDiscoveryProperties = polarisDiscoveryProperties; this.polarisDiscoveryProperties = polarisDiscoveryProperties;
this.polarisContext = context; this.polarisContext = context;
this.staticMetadataManager = staticMetadataManager;
} }
@Override @Override
@ -84,7 +91,13 @@ public class PolarisRegistration implements Registration, ServiceInstance {
@Override @Override
public Map<String, String> getMetadata() { public Map<String, String> getMetadata() {
return Collections.emptyMap(); if (CollectionUtils.isEmpty(metadata)) {
metadata = new HashMap<>();
metadata.putAll(staticMetadataManager.getMergedStaticMetadata());
// location info will be putted both in metadata and instance's field
metadata.putAll(staticMetadataManager.getLocationMetadata());
}
return metadata;
} }
public PolarisDiscoveryProperties getPolarisProperties() { public PolarisDiscoveryProperties getPolarisProperties() {
@ -97,8 +110,12 @@ public class PolarisRegistration implements Registration, ServiceInstance {
@Override @Override
public String toString() { public String toString() {
return "PolarisRegistration{" + "polarisDiscoveryProperties=" return "PolarisRegistration{" +
+ polarisDiscoveryProperties + ", polarisContext=" + polarisContext + '}'; "discoveryPropertiesAutoConfiguration=" + discoveryPropertiesAutoConfiguration +
", polarisDiscoveryProperties=" + polarisDiscoveryProperties +
", polarisContext=" + polarisContext +
", staticMetadataManager=" + staticMetadataManager +
", metadata=" + metadata +
'}';
} }
} }

@ -18,8 +18,6 @@
package com.tencent.cloud.polaris.registry; package com.tencent.cloud.polaris.registry;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -103,7 +101,7 @@ public class PolarisServiceRegistry implements ServiceRegistry<Registration> {
if (null != heartbeatExecutor) { if (null != heartbeatExecutor) {
instanceRegisterRequest.setTtl(ttl); instanceRegisterRequest.setTtl(ttl);
} }
instanceRegisterRequest.setMetadata(getInstanceMetadata()); instanceRegisterRequest.setMetadata(registration.getMetadata());
instanceRegisterRequest.setProtocol(polarisDiscoveryProperties.getProtocol()); instanceRegisterRequest.setProtocol(polarisDiscoveryProperties.getProtocol());
instanceRegisterRequest.setVersion(polarisDiscoveryProperties.getVersion()); instanceRegisterRequest.setVersion(polarisDiscoveryProperties.getVersion());
try { try {
@ -128,16 +126,6 @@ public class PolarisServiceRegistry implements ServiceRegistry<Registration> {
} }
} }
private Map<String, String> getInstanceMetadata() {
Map<String, String> metadata = new HashMap<>();
metadata.putAll(staticMetadataManager.getMergedStaticMetadata());
// location info will be putted both in metadata and instance's field
metadata.putAll(staticMetadataManager.getLocationMetadata());
return metadata;
}
@Override @Override
public void deregister(Registration registration) { public void deregister(Registration registration) {

@ -61,9 +61,10 @@ public class PolarisServiceRegistryAutoConfiguration {
@ConditionalOnBean(AutoServiceRegistrationProperties.class) @ConditionalOnBean(AutoServiceRegistrationProperties.class)
public PolarisRegistration polarisRegistration( public PolarisRegistration polarisRegistration(
DiscoveryPropertiesAutoConfiguration discoveryPropertiesAutoConfiguration, DiscoveryPropertiesAutoConfiguration discoveryPropertiesAutoConfiguration,
PolarisDiscoveryProperties polarisDiscoveryProperties, SDKContext context) { PolarisDiscoveryProperties polarisDiscoveryProperties, SDKContext context,
StaticMetadataManager staticMetadataManager) {
return new PolarisRegistration(discoveryPropertiesAutoConfiguration, return new PolarisRegistration(discoveryPropertiesAutoConfiguration,
polarisDiscoveryProperties, context); polarisDiscoveryProperties, context, staticMetadataManager);
} }
@Bean @Bean

@ -0,0 +1,144 @@
/*
* 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.PolarisDiscoveryProperties;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;
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.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
/**
* Test for {@link PolarisAutoServiceRegistration}.
*
* @author Haotian Zhang
*/
@RunWith(MockitoJUnitRunner.class)
public class PolarisAutoServiceRegistrationTest {
@Mock
private ServiceRegistry<Registration> serviceRegistry;
@Mock
private AutoServiceRegistrationProperties autoServiceRegistrationProperties;
@Mock
private PolarisDiscoveryProperties polarisDiscoveryProperties;
@Mock
private ApplicationContext applicationContext;
@Mock
private Environment environment;
@Mock
private PolarisRegistration registration;
private PolarisAutoServiceRegistration polarisAutoServiceRegistration;
@Before
public void setUp() {
doReturn(polarisDiscoveryProperties).when(registration).getPolarisProperties();
doNothing().when(serviceRegistry).register(nullable(Registration.class));
polarisAutoServiceRegistration =
new PolarisAutoServiceRegistration(serviceRegistry, autoServiceRegistrationProperties, registration);
doReturn(environment).when(applicationContext).getEnvironment();
polarisAutoServiceRegistration.setApplicationContext(applicationContext);
}
@Test
public void testRegister() {
doReturn(false).when(registration).isRegisterEnabled();
try {
polarisAutoServiceRegistration.register();
}
catch (Exception e) {
fail();
}
doReturn(true).when(registration).isRegisterEnabled();
doReturn(-1).when(registration).getPort();
try {
polarisAutoServiceRegistration.register();
}
catch (Exception e) {
fail();
}
doReturn(PORT).when(registration).getPort();
try {
polarisAutoServiceRegistration.register();
}
catch (Exception e) {
fail();
}
}
@Test
public void testGetManagementRegistration() {
assertThat(polarisAutoServiceRegistration.getManagementRegistration()).isNull();
}
@Test
public void testRegisterManagement() {
doReturn(false).when(registration).isRegisterEnabled();
try {
polarisAutoServiceRegistration.registerManagement();
}
catch (Exception e) {
fail();
}
doReturn(true).when(registration).isRegisterEnabled();
try {
polarisAutoServiceRegistration.registerManagement();
}
catch (Exception e) {
fail();
}
}
@Test
public void testGetAppName() {
doReturn("application").when(environment).getProperty(anyString(), anyString());
assertThat(polarisAutoServiceRegistration.getAppName()).isEqualTo("application");
doReturn(SERVICE_PROVIDER).when(polarisDiscoveryProperties).getService();
assertThat(polarisAutoServiceRegistration.getAppName()).isEqualTo(SERVICE_PROVIDER);
}
}

@ -0,0 +1,133 @@
/*
* 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 java.util.Collections;
import java.util.Map;
import com.tencent.cloud.common.metadata.StaticMetadataManager;
import com.tencent.cloud.polaris.DiscoveryPropertiesAutoConfiguration;
import com.tencent.cloud.polaris.PolarisDiscoveryProperties;
import com.tencent.polaris.api.config.Configuration;
import com.tencent.polaris.api.config.global.APIConfig;
import com.tencent.polaris.api.config.global.GlobalConfig;
import com.tencent.polaris.client.api.SDKContext;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
import static com.tencent.polaris.test.common.Consts.HOST;
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.doReturn;
import static org.mockito.Mockito.mock;
/**
* Test for {@link PolarisRegistration}.
*
* @author Haotian Zhang
*/
@RunWith(MockitoJUnitRunner.class)
public class PolarisRegistrationTest {
private PolarisRegistration polarisRegistration;
@Before
public void setUp() {
// mock DiscoveryPropertiesAutoConfiguration
DiscoveryPropertiesAutoConfiguration discoveryPropertiesAutoConfiguration =
mock(DiscoveryPropertiesAutoConfiguration.class);
doReturn(true).when(discoveryPropertiesAutoConfiguration).isRegisterEnabled();
// mock PolarisDiscoveryProperties
PolarisDiscoveryProperties polarisDiscoveryProperties = mock(PolarisDiscoveryProperties.class);
doReturn(SERVICE_PROVIDER).when(polarisDiscoveryProperties).getService();
doReturn(PORT).when(polarisDiscoveryProperties).getPort();
doReturn("http").when(polarisDiscoveryProperties).getProtocol();
// mock SDKContext
APIConfig apiConfig = mock(APIConfig.class);
doReturn(HOST).when(apiConfig).getBindIP();
GlobalConfig globalConfig = mock(GlobalConfig.class);
doReturn(apiConfig).when(globalConfig).getAPI();
Configuration configuration = mock(Configuration.class);
doReturn(globalConfig).when(configuration).getGlobal();
SDKContext polarisContext = mock(SDKContext.class);
doReturn(configuration).when(polarisContext).getConfig();
// mock StaticMetadataManager
StaticMetadataManager staticMetadataManager = mock(StaticMetadataManager.class);
doReturn(Collections.singletonMap("key1", "value1")).when(staticMetadataManager).getMergedStaticMetadata();
doReturn(Collections.singletonMap("key2", "value2")).when(staticMetadataManager).getLocationMetadata();
polarisRegistration = new PolarisRegistration(
discoveryPropertiesAutoConfiguration, polarisDiscoveryProperties, polarisContext, staticMetadataManager);
}
@Test
public void testGetServiceId() {
assertThat(polarisRegistration.getServiceId()).isEqualTo(SERVICE_PROVIDER);
}
@Test
public void testGetHost() {
assertThat(polarisRegistration.getHost()).isEqualTo(HOST);
}
@Test
public void testGetPort() {
assertThat(polarisRegistration.getPort()).isEqualTo(PORT);
}
@Test
public void testIsSecure() {
assertThat(polarisRegistration.isSecure()).isFalse();
}
@Test
public void testGetUri() {
assertThat(polarisRegistration.getUri().toString()).isEqualTo("http://" + HOST + ":" + PORT);
}
@Test
public void testGetMetadata() {
Map<String, String> metadata = polarisRegistration.getMetadata();
assertThat(metadata).isNotNull();
assertThat(metadata).isNotEmpty();
assertThat(metadata.size()).isEqualTo(2);
assertThat(metadata.get("key1")).isEqualTo("value1");
assertThat(metadata.get("key2")).isEqualTo("value2");
}
@Test
public void testGetPolarisProperties() {
assertThat(polarisRegistration.getPolarisProperties()).isNotNull();
}
@Test
public void testIsRegisterEnabled() {
assertThat(polarisRegistration.isRegisterEnabled()).isTrue();
}
@Test
public void testToString() {
System.out.println(polarisRegistration);
}
}

@ -30,7 +30,6 @@ import org.mockito.Mockito;
import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner; import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import static com.tencent.polaris.test.common.Consts.NAMESPACE_TEST; import static com.tencent.polaris.test.common.Consts.NAMESPACE_TEST;
@ -38,6 +37,7 @@ import static com.tencent.polaris.test.common.Consts.PORT;
import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER; import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
/** /**
@ -111,9 +111,24 @@ public class PolarisServiceRegistryTest {
}); });
} }
@Test
public void testDeRegister() {
this.contextRunner.run(context -> {
PolarisServiceRegistry registry = context
.getBean(PolarisServiceRegistry.class);
PolarisRegistration registration = Mockito.mock(PolarisRegistration.class);
doReturn(null).when(registration).getServiceId();
try {
registry.deregister(registration);
}
catch (Throwable throwable) {
fail();
}
});
}
@Configuration @Configuration
@EnableAutoConfiguration @EnableAutoConfiguration
@EnableDiscoveryClient
static class PolarisPropertiesConfiguration { static class PolarisPropertiesConfiguration {
} }

Loading…
Cancel
Save