diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/pom.xml b/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/pom.xml index b404252b1..925bb2735 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/pom.xml +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/pom.xml @@ -17,6 +17,11 @@ + + com.alibaba.cloud + spring-cloud-alibaba-commons + + org.springframework.boot diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/autoconfigure/DubboServiceRegistrationAutoConfiguration.java b/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/autoconfigure/DubboServiceRegistrationAutoConfiguration.java index b0897e6ca..8d8d8df12 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/autoconfigure/DubboServiceRegistrationAutoConfiguration.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/autoconfigure/DubboServiceRegistrationAutoConfiguration.java @@ -18,6 +18,7 @@ package com.alibaba.cloud.dubbo.autoconfigure; import java.util.Collection; import java.util.HashSet; +import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; @@ -50,6 +51,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.serviceregistry.Registration; import org.springframework.cloud.client.serviceregistry.ServiceRegistry; +import org.springframework.cloud.consul.discovery.ConsulDiscoveryProperties; import org.springframework.cloud.consul.serviceregistry.ConsulRegistration; import org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean; import org.springframework.cloud.netflix.eureka.serviceregistry.EurekaAutoServiceRegistration; @@ -61,6 +63,7 @@ import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.context.event.EventListener; +import org.springframework.util.StringUtils; import static com.alibaba.cloud.dubbo.autoconfigure.DubboServiceRegistrationAutoConfiguration.CONSUL_AUTO_SERVICE_AUTO_CONFIGURATION_CLASS_NAME; import static com.alibaba.cloud.dubbo.autoconfigure.DubboServiceRegistrationAutoConfiguration.EUREKA_CLIENT_AUTO_CONFIGURATION_CLASS_NAME; @@ -260,6 +263,9 @@ public class DubboServiceRegistrationAutoConfiguration { @AutoConfigureOrder class ConsulConfiguration { + @Autowired + private ConsulDiscoveryProperties consulDiscoveryProperties; + @EventListener(DubboBootstrapStartedEvent.class) public void attachURLsIntoMetadataBeforeReRegist( DubboBootstrapStartedEvent event) { @@ -284,16 +290,40 @@ public class DubboServiceRegistrationAutoConfiguration { })); } + @EventListener(ServiceInstancePreRegisteredEvent.class) + public void onServiceInstancePreRegistered( + ServiceInstancePreRegisteredEvent event) { + Registration registration = event.getSource(); + attachURLsIntoMetadata((ConsulRegistration) registration); + } + private void attachURLsIntoMetadata(ConsulRegistration consulRegistration) { - NewService newService = consulRegistration.getService(); Map serviceMetadata = dubboServiceMetadataRepository .getDubboMetadataServiceMetadata(); - if (!isEmpty(serviceMetadata)) { - List tags = newService.getTags(); + if (isEmpty(serviceMetadata)) { + return; + } + NewService newService = consulRegistration.getService(); + if (consulDiscoveryProperties.isTagsAsMetadata()) { for (Map.Entry entry : serviceMetadata.entrySet()) { - tags.add(entry.getKey() + "=" + entry.getValue()); + attAsTag(newService.getTags(), entry.getKey(), entry.getValue()); + } + } + else { + newService.getMeta().putAll(serviceMetadata); + } + } + + private void attAsTag(List tags, String key, String value) { + Iterator iter = tags.iterator(); + while (iter.hasNext()) { + String tag = iter.next(); + String[] tmp = tag.split("="); + if (StringUtils.pathEquals(tmp[0], key)) { + iter.remove(); } } + tags.add(key + "=" + value); } }