diff --git a/CHANGELOG.md b/CHANGELOG.md index 046f5df0d..f8d15b7d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,5 +2,6 @@ --- - [Feature: Add config change listener feature support](https://github.com/Tencent/spring-cloud-tencent/pull/254) +- [Feature: Add instance metadata spi for registration](https://github.com/Tencent/spring-cloud-tencent/pull/256) - [Feature: Add config module unit test](https://github.com/Tencent/spring-cloud-tencent/pull/255) - [Upgrade: fix third-party lib CVEs & upgrade core spring libs version](https://github.com/Tencent/spring-cloud-tencent/pull/258) diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisRegistration.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisRegistration.java index 240d04bf0..ba018a658 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisRegistration.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisRegistration.java @@ -19,12 +19,14 @@ package com.tencent.cloud.polaris.registry; import java.net.URI; +import java.util.Collections; import java.util.HashMap; 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.cloud.polaris.context.spi.InstanceMetadataProvider; import com.tencent.polaris.client.api.SDKContext; import org.apache.commons.lang.StringUtils; @@ -40,6 +42,9 @@ import org.springframework.util.CollectionUtils; */ public class PolarisRegistration implements Registration, ServiceInstance { + private final static String METADATA_KEY_IP = "internal-ip"; + private final static String METADATA_KEY_ADDRESS = "internal-address"; + private final DiscoveryPropertiesAutoConfiguration discoveryPropertiesAutoConfiguration; private final PolarisDiscoveryProperties polarisDiscoveryProperties; @@ -48,14 +53,24 @@ public class PolarisRegistration implements Registration, ServiceInstance { private final StaticMetadataManager staticMetadataManager; + private final InstanceMetadataProvider instanceMetadataProvider; + private Map metadata; - public PolarisRegistration(DiscoveryPropertiesAutoConfiguration discoveryPropertiesAutoConfiguration, - PolarisDiscoveryProperties polarisDiscoveryProperties, SDKContext context, StaticMetadataManager staticMetadataManager) { + private final String host; + + public PolarisRegistration( + DiscoveryPropertiesAutoConfiguration discoveryPropertiesAutoConfiguration, + PolarisDiscoveryProperties polarisDiscoveryProperties, SDKContext context, + StaticMetadataManager staticMetadataManager, + InstanceMetadataProvider instanceMetadataProvider) { this.discoveryPropertiesAutoConfiguration = discoveryPropertiesAutoConfiguration; this.polarisDiscoveryProperties = polarisDiscoveryProperties; this.polarisContext = context; this.staticMetadataManager = staticMetadataManager; + this.instanceMetadataProvider = instanceMetadataProvider; + + host = polarisContext.getConfig().getGlobal().getAPI().getBindIP(); } @Override @@ -65,7 +80,7 @@ public class PolarisRegistration implements Registration, ServiceInstance { @Override public String getHost() { - return polarisContext.getConfig().getGlobal().getAPI().getBindIP(); + return host; } @Override @@ -90,10 +105,35 @@ public class PolarisRegistration implements Registration, ServiceInstance { @Override public Map getMetadata() { if (CollectionUtils.isEmpty(metadata)) { - metadata = new HashMap<>(); - metadata.putAll(staticMetadataManager.getMergedStaticMetadata()); + Map instanceMetadata = new HashMap<>(); + + // put internal metadata + instanceMetadata.put(METADATA_KEY_IP, host); + instanceMetadata.put(METADATA_KEY_ADDRESS, host + ":" + polarisDiscoveryProperties.getPort()); + + instanceMetadata.putAll(staticMetadataManager.getMergedStaticMetadata()); + // location info will be putted both in metadata and instance's field - metadata.putAll(staticMetadataManager.getLocationMetadata()); + instanceMetadata.putAll(staticMetadataManager.getLocationMetadata()); + + // custom metadata from spi + if (instanceMetadataProvider != null) { + if (StringUtils.isNotBlank(instanceMetadataProvider.getRegion())) { + instanceMetadata.put(StaticMetadataManager.LOCATION_KEY_ZONE, instanceMetadataProvider.getRegion()); + } + if (StringUtils.isNotBlank(instanceMetadataProvider.getZone())) { + instanceMetadata.put(StaticMetadataManager.LOCATION_KEY_ZONE, instanceMetadataProvider.getZone()); + } + if (StringUtils.isNotBlank(instanceMetadataProvider.getCampus())) { + instanceMetadata.put(StaticMetadataManager.LOCATION_KEY_ZONE, instanceMetadataProvider.getCampus()); + } + + if (!CollectionUtils.isEmpty(instanceMetadataProvider.getMetadata())) { + instanceMetadata.putAll(instanceMetadataProvider.getMetadata()); + } + } + + this.metadata = Collections.unmodifiableMap(instanceMetadata); } return metadata; } diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistryAutoConfiguration.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistryAutoConfiguration.java index f6a689a57..01e4f2910 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistryAutoConfiguration.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistryAutoConfiguration.java @@ -21,6 +21,7 @@ package com.tencent.cloud.polaris.registry; import com.tencent.cloud.common.metadata.StaticMetadataManager; import com.tencent.cloud.polaris.DiscoveryPropertiesAutoConfiguration; import com.tencent.cloud.polaris.PolarisDiscoveryProperties; +import com.tencent.cloud.polaris.context.spi.InstanceMetadataProvider; import com.tencent.cloud.polaris.discovery.PolarisDiscoveryAutoConfiguration; import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler; import com.tencent.polaris.client.api.SDKContext; @@ -34,6 +35,7 @@ import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationC import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.lang.Nullable; /** * Autoconfiguration of service registry of Polaris. @@ -60,9 +62,9 @@ public class PolarisServiceRegistryAutoConfiguration { public PolarisRegistration polarisRegistration( DiscoveryPropertiesAutoConfiguration discoveryPropertiesAutoConfiguration, PolarisDiscoveryProperties polarisDiscoveryProperties, SDKContext context, - StaticMetadataManager staticMetadataManager) { + StaticMetadataManager staticMetadataManager, @Nullable InstanceMetadataProvider instanceMetadataProvider) { return new PolarisRegistration(discoveryPropertiesAutoConfiguration, - polarisDiscoveryProperties, context, staticMetadataManager); + polarisDiscoveryProperties, context, staticMetadataManager, instanceMetadataProvider); } @Bean diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/registry/PolarisRegistrationTest.java b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/registry/PolarisRegistrationTest.java index a81d69d2a..11d380a75 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/registry/PolarisRegistrationTest.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/registry/PolarisRegistrationTest.java @@ -78,7 +78,7 @@ public class PolarisRegistrationTest { doReturn(Collections.singletonMap("key2", "value2")).when(staticMetadataManager).getLocationMetadata(); polarisRegistration = new PolarisRegistration( - discoveryPropertiesAutoConfiguration, polarisDiscoveryProperties, polarisContext, staticMetadataManager); + discoveryPropertiesAutoConfiguration, polarisDiscoveryProperties, polarisContext, staticMetadataManager, null); } @Test @@ -111,7 +111,7 @@ public class PolarisRegistrationTest { Map metadata = polarisRegistration.getMetadata(); assertThat(metadata).isNotNull(); assertThat(metadata).isNotEmpty(); - assertThat(metadata.size()).isEqualTo(2); + assertThat(metadata.size()).isEqualTo(4); assertThat(metadata.get("key1")).isEqualTo("value1"); assertThat(metadata.get("key2")).isEqualTo("value2"); } diff --git a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/StaticMetadataManager.java b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/StaticMetadataManager.java index 95557598c..575775bf7 100644 --- a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/StaticMetadataManager.java +++ b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/StaticMetadataManager.java @@ -43,9 +43,18 @@ public class StaticMetadataManager { private static final String ENV_METADATA_REGION = "SCT_METADATA_REGION"; private static final String ENV_METADATA_CAMPUS = "SCT_METADATA_CAMPUS"; - private static final String LOCATION_KEY_REGION = "region"; - private static final String LOCATION_KEY_ZONE = "zone"; - private static final String LOCATION_KEY_CAMPUS = "campus"; + /** + * the metadata key of region. + */ + public static final String LOCATION_KEY_REGION = "region"; + /** + * the metadata key of zone. + */ + public static final String LOCATION_KEY_ZONE = "zone"; + /** + * the metadata key of campus/datacenter. + */ + public static final String LOCATION_KEY_CAMPUS = "campus"; private Map envMetadata; private Map envTransitiveMetadata; diff --git a/spring-cloud-tencent-examples/polaris-discovery-example/discovery-callee-service/src/main/java/com/tencent/cloud/polaris/discovery/service/callee/CustomMetadata.java b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-callee-service/src/main/java/com/tencent/cloud/polaris/discovery/service/callee/CustomMetadata.java new file mode 100644 index 000000000..efe3f8167 --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-callee-service/src/main/java/com/tencent/cloud/polaris/discovery/service/callee/CustomMetadata.java @@ -0,0 +1,56 @@ +/* + * 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.discovery.service.callee; + +import java.util.HashMap; +import java.util.Map; + +import com.tencent.cloud.polaris.context.spi.InstanceMetadataProvider; + +import org.springframework.stereotype.Component; + +/** + * custom metadata for instance. + *@author lepdou 2022-06-16 + */ +@Component +public class CustomMetadata implements InstanceMetadataProvider { + + @Override + public Map getMetadata() { + Map metadata = new HashMap<>(); + metadata.put("k1", "v1"); + return metadata; + } + + @Override + public String getRegion() { + return "shanghai"; + } + + @Override + public String getZone() { + return null; + } + + @Override + public String getCampus() { + return null; + } +} diff --git a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/spi/InstanceMetadataProvider.java b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/spi/InstanceMetadataProvider.java new file mode 100644 index 000000000..6b2b73a6b --- /dev/null +++ b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/spi/InstanceMetadataProvider.java @@ -0,0 +1,55 @@ +/* + * 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.context.spi; + +import java.util.Map; + +/** + * + * Instance's custom metadata, metadata will be register to polaris server. + * @author lepdou 2022-06-16 + */ +public interface InstanceMetadataProvider { + + /** + * @return the metadata of instance. + */ + Map getMetadata(); + + /** + * The region of current instance. + * + * @return the region info. + */ + String getRegion(); + + /** + * The zone of current instance. + * + * @return the zone info. + */ + String getZone(); + + /** + * The campus/datacenter of current instance. + * + * @return the campus or datacenter info. + */ + String getCampus(); +}