diff --git a/spring-cloud-huawei-nacos/spring-cloud-huawei-nacos-discovery/src/main/java/com/huaweicloud/nacos/discovery/registry/NacosMicroserviceHandler.java b/spring-cloud-huawei-nacos/spring-cloud-huawei-nacos-discovery/src/main/java/com/huaweicloud/nacos/discovery/registry/NacosMicroserviceHandler.java index 8ad73e594..96429ba75 100644 --- a/spring-cloud-huawei-nacos/spring-cloud-huawei-nacos-discovery/src/main/java/com/huaweicloud/nacos/discovery/registry/NacosMicroserviceHandler.java +++ b/spring-cloud-huawei-nacos/spring-cloud-huawei-nacos-discovery/src/main/java/com/huaweicloud/nacos/discovery/registry/NacosMicroserviceHandler.java @@ -27,6 +27,7 @@ import org.apache.commons.lang3.StringUtils; import org.springframework.core.env.Environment; +import com.alibaba.nacos.api.naming.PreservedMetadataKeys; import com.alibaba.nacos.api.naming.pojo.Instance; import com.huaweicloud.nacos.discovery.NacosDiscoveryProperties; @@ -47,6 +48,8 @@ public class NacosMicroserviceHandler { private static final String SERVICE_PROPS = "SERVICECOMB_SERVICE_PROPS"; + private static final String FRAMEWORK_TYPE = "springCloudHuawei"; + public static Instance createMicroserviceInstance(NacosDiscoveryProperties properties, Environment environment) { Instance instance = new Instance(); instance.setWeight(properties.getWeight()); @@ -56,6 +59,13 @@ public static Instance createMicroserviceInstance(NacosDiscoveryProperties prope instance.setPort(properties.getPort()); instance.setIp(properties.getIp()); Map metadata = properties.getMetadata(); + + // set framework + metadata.put(FRAMEWORK_TYPE, NacosMicroserviceHandler.class.getPackage().getImplementationVersion()); + if (properties.isSecure()) { + metadata.put("secure", "true"); + } + metadata.putAll(buildHeartBeatConfiguration(properties)); metadata.putAll(genCasProperties(environment)); instance.setMetadata(metadata); return instance; @@ -105,4 +115,22 @@ private static Map parseProps(String... value) { .filter(v -> v.length == 2) .collect(Collectors.toMap(v -> v[0], v -> v[1])); } + + + private static Map buildHeartBeatConfiguration(NacosDiscoveryProperties properties) { + Map heartBeatConfiguration = new HashMap<>(); + if (null != properties.getHeartBeatInterval()) { + heartBeatConfiguration.put(PreservedMetadataKeys.HEART_BEAT_INTERVAL, + properties.getHeartBeatInterval().toString()); + } + if (null != properties.getHeartBeatTimeout()) { + heartBeatConfiguration.put(PreservedMetadataKeys.HEART_BEAT_TIMEOUT, + properties.getHeartBeatTimeout().toString()); + } + if (null != properties.getIpDeleteTimeout()) { + heartBeatConfiguration.put(PreservedMetadataKeys.IP_DELETE_TIMEOUT, + properties.getIpDeleteTimeout().toString()); + } + return heartBeatConfiguration; + } } diff --git a/spring-cloud-huawei-nacos/spring-cloud-huawei-nacos-discovery/src/main/java/com/huaweicloud/nacos/discovery/registry/NacosRegistration.java b/spring-cloud-huawei-nacos/spring-cloud-huawei-nacos-discovery/src/main/java/com/huaweicloud/nacos/discovery/registry/NacosRegistration.java index cea38715e..6bc8bed8c 100644 --- a/spring-cloud-huawei-nacos/spring-cloud-huawei-nacos-discovery/src/main/java/com/huaweicloud/nacos/discovery/registry/NacosRegistration.java +++ b/spring-cloud-huawei-nacos/spring-cloud-huawei-nacos-discovery/src/main/java/com/huaweicloud/nacos/discovery/registry/NacosRegistration.java @@ -24,7 +24,6 @@ import org.springframework.cloud.client.DefaultServiceInstance; import org.springframework.cloud.client.serviceregistry.Registration; -import com.alibaba.nacos.api.naming.PreservedMetadataKeys; import com.huaweicloud.nacos.discovery.NacosDiscoveryProperties; public class NacosRegistration implements Registration { @@ -42,33 +41,11 @@ public NacosRegistration(List registrationC } public void init() { - buildPropertiesAttributes(); + customize(); this.instanceId = buildInstanceId(); } - private void buildPropertiesAttributes() { - Map metadata = nacosDiscoveryProperties.getMetadata(); - metadata.put(PreservedMetadataKeys.REGISTER_SOURCE, "SPRING_CLOUD"); - if (nacosDiscoveryProperties.isSecure()) { - metadata.put("secure", "true"); - } - if (null != nacosDiscoveryProperties.getHeartBeatInterval()) { - metadata.put(PreservedMetadataKeys.HEART_BEAT_INTERVAL, - nacosDiscoveryProperties.getHeartBeatInterval().toString()); - } - if (null != nacosDiscoveryProperties.getHeartBeatTimeout()) { - metadata.put(PreservedMetadataKeys.HEART_BEAT_TIMEOUT, - nacosDiscoveryProperties.getHeartBeatTimeout().toString()); - } - if (null != nacosDiscoveryProperties.getIpDeleteTimeout()) { - metadata.put(PreservedMetadataKeys.IP_DELETE_TIMEOUT, - nacosDiscoveryProperties.getIpDeleteTimeout().toString()); - } - customize(registrationCustomizers); - } - - protected void customize( - List registrationCustomizers) { + protected void customize() { if (registrationCustomizers != null) { for (NacosRegistrationMetadataCustomizer customizer : registrationCustomizers) { customizer.customize(this); diff --git a/spring-cloud-huawei-nacos/spring-cloud-huawei-nacos-discovery/src/main/java/com/huaweicloud/nacos/discovery/registry/NacosServiceRegistry.java b/spring-cloud-huawei-nacos/spring-cloud-huawei-nacos-discovery/src/main/java/com/huaweicloud/nacos/discovery/registry/NacosServiceRegistry.java index a06093ecd..e5a8e067c 100644 --- a/spring-cloud-huawei-nacos/spring-cloud-huawei-nacos-discovery/src/main/java/com/huaweicloud/nacos/discovery/registry/NacosServiceRegistry.java +++ b/spring-cloud-huawei-nacos/spring-cloud-huawei-nacos-discovery/src/main/java/com/huaweicloud/nacos/discovery/registry/NacosServiceRegistry.java @@ -56,8 +56,7 @@ public NacosServiceRegistry(List namingServiceManagers, this.nacosDiscoveryProperties = nacosDiscoveryProperties; this.namingServiceManagers = namingServiceManagers.stream().sorted(Comparator.comparingInt(Ordered::getOrder)) .collect(Collectors.toList()); - this.instance = - NacosMicroserviceHandler.createMicroserviceInstance(nacosDiscoveryProperties, environment); + this.instance = NacosMicroserviceHandler.createMicroserviceInstance(nacosDiscoveryProperties, environment); eventBus = EventManager.getEventBus(); }