Skip to content

Commit

Permalink
[huaweicloud#1419] Optimized nacos metadata build and framework type-…
Browse files Browse the repository at this point in the history
…version reporting info (huaweicloud#1420)
  • Loading branch information
chengyouling committed Jan 14, 2025
1 parent 1e0d6b1 commit 6cb0afd
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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());
Expand All @@ -56,6 +59,13 @@ public static Instance createMicroserviceInstance(NacosDiscoveryProperties prope
instance.setPort(properties.getPort());
instance.setIp(properties.getIp());
Map<String, String> 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;
Expand Down Expand Up @@ -105,4 +115,22 @@ private static Map<String, String> parseProps(String... value) {
.filter(v -> v.length == 2)
.collect(Collectors.toMap(v -> v[0], v -> v[1]));
}


private static Map<String, String> buildHeartBeatConfiguration(NacosDiscoveryProperties properties) {
Map<String, String> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -42,33 +41,11 @@ public NacosRegistration(List<NacosRegistrationMetadataCustomizer> registrationC
}

public void init() {
buildPropertiesAttributes();
customize();
this.instanceId = buildInstanceId();
}

private void buildPropertiesAttributes() {
Map<String, String> 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<NacosRegistrationMetadataCustomizer> registrationCustomizers) {
protected void customize() {
if (registrationCustomizers != null) {
for (NacosRegistrationMetadataCustomizer customizer : registrationCustomizers) {
customizer.customize(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ public NacosServiceRegistry(List<NamingServiceManager> 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();
}

Expand Down

0 comments on commit 6cb0afd

Please sign in to comment.