You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by li...@apache.org on 2020/08/14 09:37:20 UTC
[dubbo] branch 3.0-to-master updated: 元数据上报优化&url模型bugfix (#6584)
This is an automated email from the ASF dual-hosted git repository.
liujun pushed a commit to branch 3.0-to-master
in repository https://gitbox.apache.org/repos/asf/dubbo.git
The following commit(s) were added to refs/heads/3.0-to-master by this push:
new 05be1fa 元数据上报优化&url模型bugfix (#6584)
05be1fa is described below
commit 05be1faa3993003e53dfe640fca5846aabfdf994
Author: 斩秋 <qu...@users.noreply.github.com>
AuthorDate: Fri Aug 14 17:36:50 2020 +0800
元数据上报优化&url模型bugfix (#6584)
---
.../src/main/java/org/apache/dubbo/common/URL.java | 3 +-
.../dubbo-demo-service-consumer/pom.xml | 4 ++
.../dubbo/demo/consumer/ApplicationConsumer.java | 6 ++-
.../resources/spring/dubbo-consumer-zk-apollo.xml | 44 +++++++++++++++++++
.../dubbo-demo-service-provider/pom.xml | 4 ++
.../dubbo/demo/provider/ApplicationProvider.java | 1 +
.../dubbo/demo/provider/GreetingServiceImpl.java | 2 +-
.../resources/spring/dubbo-provider-zk-apollo.xml | 49 ++++++++++++++++++++++
.../pom.xml | 6 ++-
.../dubbo/demo/provider/ApplicationProvider2.java} | 3 +-
.../dubbo/demo/provider/GreetingServiceImpl.java | 2 +-
.../src/main/resources/dubbo.properties | 1 +
.../src/main/resources/log4j.properties | 7 ++++
.../main/resources/spring/dubbo-provider-zk.xml | 37 ++++++++++++++++
dubbo-demo/dubbo-demo-service/pom.xml | 1 +
.../apache/dubbo/metadata/MappingChangedEvent.java | 8 ----
.../org/apache/dubbo/metadata/MetadataInfo.java | 2 +-
.../store/zookeeper/ZookeeperMetadataReport.java | 33 ++++++++++++---
.../registry/client/ServiceDiscoveryRegistry.java | 14 ++++++-
19 files changed, 204 insertions(+), 23 deletions(-)
diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java b/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java
index f227353..e245297 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java
@@ -1316,7 +1316,8 @@ class URL implements Serializable {
}
public Map<String, String> toMap() {
- Map<String, String> map = new HashMap<>(parameters);
+ Map<String, String> map = null == parameters ? new HashMap<>() : new HashMap<>(parameters);
+
if (protocol != null) {
map.put(PROTOCOL_KEY, protocol);
}
diff --git a/dubbo-demo/dubbo-demo-service/dubbo-demo-service-consumer/pom.xml b/dubbo-demo/dubbo-demo-service/dubbo-demo-service-consumer/pom.xml
index 2bf37d2..6411cfe 100644
--- a/dubbo-demo/dubbo-demo-service/dubbo-demo-service-consumer/pom.xml
+++ b/dubbo-demo/dubbo-demo-service/dubbo-demo-service-consumer/pom.xml
@@ -87,5 +87,9 @@
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-sofa</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.apache.dubbo</groupId>
+ <artifactId>dubbo-configcenter-apollo</artifactId>
+ </dependency>
</dependencies>
</project>
diff --git a/dubbo-demo/dubbo-demo-service/dubbo-demo-service-consumer/src/main/java/org/apache/dubbo/demo/consumer/ApplicationConsumer.java b/dubbo-demo/dubbo-demo-service/dubbo-demo-service-consumer/src/main/java/org/apache/dubbo/demo/consumer/ApplicationConsumer.java
index ca896d2..59cab3b 100644
--- a/dubbo-demo/dubbo-demo-service/dubbo-demo-service-consumer/src/main/java/org/apache/dubbo/demo/consumer/ApplicationConsumer.java
+++ b/dubbo-demo/dubbo-demo-service/dubbo-demo-service-consumer/src/main/java/org/apache/dubbo/demo/consumer/ApplicationConsumer.java
@@ -31,8 +31,9 @@ public class ApplicationConsumer {
public static void main(String[] args) throws Exception {
//ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-consumer-sofaregistry.xml");
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-consumer-zk.xml");
+ //ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-consumer-zk-apollo.xml");
context.start();
- DemoService demoService = context.getBean("demoService", DemoService.class);
+ //DemoService demoService = context.getBean("demoService", DemoService.class);
GreetingService greetingService = context.getBean("greetingService", GreetingService.class);
new Thread(() -> {
@@ -51,6 +52,7 @@ public class ApplicationConsumer {
}
}).start();
+/*
while (true) {
try {
CompletableFuture<String> hello = demoService.sayHelloAsync("world");
@@ -65,6 +67,6 @@ public class ApplicationConsumer {
}
Thread.sleep(1000);
- }
+ }*/
}
}
diff --git a/dubbo-demo/dubbo-demo-service/dubbo-demo-service-consumer/src/main/resources/spring/dubbo-consumer-zk-apollo.xml b/dubbo-demo/dubbo-demo-service/dubbo-demo-service-consumer/src/main/resources/spring/dubbo-consumer-zk-apollo.xml
new file mode 100644
index 0000000..af92047
--- /dev/null
+++ b/dubbo-demo/dubbo-demo-service/dubbo-demo-service-consumer/src/main/resources/spring/dubbo-consumer-zk-apollo.xml
@@ -0,0 +1,44 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ 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.
+ -->
+<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
+ xmlns="http://www.springframework.org/schema/beans"
+ xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
+ http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
+
+ <dubbo:application name="demo-service-consumer" metadata-type="remote">
+ <dubbo:parameter key="mapping-type" value="metadata"/>
+ </dubbo:application>
+
+ <dubbo:metadata-report address="zookeeper://127.0.0.1:2181"/>
+
+ <dubbo:config-center protocol="apollo" address="localhost:8080">
+ <dubbo:parameter key="env" value="dev"/>
+ <dubbo:parameter key="cluster" value="default"/>
+ <dubbo:parameter key="app.id" value="SampleApp"/>
+ </dubbo:config-center>
+
+ <dubbo:registry address="zookeeper://127.0.0.1:2181?registry-type=service">
+ </dubbo:registry>
+
+ <dubbo:reference id="demoService" check="false" interface="org.apache.dubbo.demo.DemoService"/>
+
+ <dubbo:reference version="1.0.0" group="greeting" id="greetingService" check="false"
+ interface="org.apache.dubbo.demo.GreetingService"/>
+
+</beans>
diff --git a/dubbo-demo/dubbo-demo-service/dubbo-demo-service-provider/pom.xml b/dubbo-demo/dubbo-demo-service/dubbo-demo-service-provider/pom.xml
index b22856e..6df68b8 100644
--- a/dubbo-demo/dubbo-demo-service/dubbo-demo-service-provider/pom.xml
+++ b/dubbo-demo/dubbo-demo-service/dubbo-demo-service-provider/pom.xml
@@ -107,5 +107,9 @@
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-sofa</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.apache.dubbo</groupId>
+ <artifactId>dubbo-configcenter-apollo</artifactId>
+ </dependency>
</dependencies>
</project>
diff --git a/dubbo-demo/dubbo-demo-service/dubbo-demo-service-provider/src/main/java/org/apache/dubbo/demo/provider/ApplicationProvider.java b/dubbo-demo/dubbo-demo-service/dubbo-demo-service-provider/src/main/java/org/apache/dubbo/demo/provider/ApplicationProvider.java
index 0769dc5..c6d3ab6 100644
--- a/dubbo-demo/dubbo-demo-service/dubbo-demo-service-provider/src/main/java/org/apache/dubbo/demo/provider/ApplicationProvider.java
+++ b/dubbo-demo/dubbo-demo-service/dubbo-demo-service-provider/src/main/java/org/apache/dubbo/demo/provider/ApplicationProvider.java
@@ -22,6 +22,7 @@ public class ApplicationProvider {
public static void main(String[] args) throws Exception {
//ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-provider-sofaregistry.xml");
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-provider-zk.xml");
+ //ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-provider-zk-apollo.xml");
context.start();
System.in.read();
}
diff --git a/dubbo-demo/dubbo-demo-service/dubbo-demo-service-provider/src/main/java/org/apache/dubbo/demo/provider/GreetingServiceImpl.java b/dubbo-demo/dubbo-demo-service/dubbo-demo-service-provider/src/main/java/org/apache/dubbo/demo/provider/GreetingServiceImpl.java
index cc1b5de..3e60992 100644
--- a/dubbo-demo/dubbo-demo-service/dubbo-demo-service-provider/src/main/java/org/apache/dubbo/demo/provider/GreetingServiceImpl.java
+++ b/dubbo-demo/dubbo-demo-service/dubbo-demo-service-provider/src/main/java/org/apache/dubbo/demo/provider/GreetingServiceImpl.java
@@ -24,6 +24,6 @@ import org.apache.dubbo.demo.GreetingService;
public class GreetingServiceImpl implements GreetingService {
@Override
public String hello() {
- return "Greetings!";
+ return "Greetings from provider1!";
}
}
diff --git a/dubbo-demo/dubbo-demo-service/dubbo-demo-service-provider/src/main/resources/spring/dubbo-provider-zk-apollo.xml b/dubbo-demo/dubbo-demo-service/dubbo-demo-service-provider/src/main/resources/spring/dubbo-provider-zk-apollo.xml
new file mode 100644
index 0000000..8dcbaaf
--- /dev/null
+++ b/dubbo-demo/dubbo-demo-service/dubbo-demo-service-provider/src/main/resources/spring/dubbo-provider-zk-apollo.xml
@@ -0,0 +1,49 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ 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.
+ -->
+<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
+ xmlns="http://www.springframework.org/schema/beans"
+ xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
+ http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
+
+ <dubbo:application name="demo-service-provider" metadata-type="remote">
+ <dubbo:parameter key="mapping-type" value="metadata"/>
+ <dubbo:parameter key="instance1" value="xxx"/>
+ </dubbo:application>
+
+ <dubbo:config-center protocol="apollo" address="localhost:8080">
+ <dubbo:parameter key="env" value="dev"/>
+ <dubbo:parameter key="cluster" value="default"/>
+ <dubbo:parameter key="app.id" value="SampleApp"/>
+ </dubbo:config-center>
+
+ <dubbo:metadata-report address="zookeeper://127.0.0.1:2181"/>
+
+ <dubbo:registry id="registry1" address="zookeeper://127.0.0.1:2181?registry-type=service"/>
+
+ <bean id="demoService" class="org.apache.dubbo.demo.provider.DemoServiceImpl"/>
+ <bean id="greetingService" class="org.apache.dubbo.demo.provider.GreetingServiceImpl"/>
+
+ <dubbo:service interface="org.apache.dubbo.demo.DemoService" timeout="3000" ref="demoService" registry="registry1">
+ <dubbo:parameter key="serviceKey1" value="serviceKey1Value"/>
+ </dubbo:service>
+
+ <dubbo:service version="1.0.0" group="greeting" timeout="5000" interface="org.apache.dubbo.demo.GreetingService"
+ ref="greetingService"/>
+
+</beans>
diff --git a/dubbo-demo/dubbo-demo-service/dubbo-demo-service-provider/pom.xml b/dubbo-demo/dubbo-demo-service/dubbo-demo-service-provider2/pom.xml
similarity index 95%
copy from dubbo-demo/dubbo-demo-service/dubbo-demo-service-provider/pom.xml
copy to dubbo-demo/dubbo-demo-service/dubbo-demo-service-provider2/pom.xml
index b22856e..03d5d1b 100644
--- a/dubbo-demo/dubbo-demo-service/dubbo-demo-service-provider/pom.xml
+++ b/dubbo-demo/dubbo-demo-service/dubbo-demo-service-provider2/pom.xml
@@ -23,7 +23,7 @@
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
- <artifactId>dubbo-demo-service-provider</artifactId>
+ <artifactId>dubbo-demo-service-provider2</artifactId>
<packaging>jar</packaging>
<name>${project.artifactId}</name>
<description>The demo provider module of dubbo project</description>
@@ -107,5 +107,9 @@
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-sofa</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.apache.dubbo</groupId>
+ <artifactId>dubbo-configcenter-apollo</artifactId>
+ </dependency>
</dependencies>
</project>
diff --git a/dubbo-demo/dubbo-demo-service/dubbo-demo-service-provider/src/main/java/org/apache/dubbo/demo/provider/ApplicationProvider.java b/dubbo-demo/dubbo-demo-service/dubbo-demo-service-provider2/src/main/java/org/apache/dubbo/demo/provider/ApplicationProvider2.java
similarity index 87%
copy from dubbo-demo/dubbo-demo-service/dubbo-demo-service-provider/src/main/java/org/apache/dubbo/demo/provider/ApplicationProvider.java
copy to dubbo-demo/dubbo-demo-service/dubbo-demo-service-provider2/src/main/java/org/apache/dubbo/demo/provider/ApplicationProvider2.java
index 0769dc5..3aaed48 100644
--- a/dubbo-demo/dubbo-demo-service/dubbo-demo-service-provider/src/main/java/org/apache/dubbo/demo/provider/ApplicationProvider.java
+++ b/dubbo-demo/dubbo-demo-service/dubbo-demo-service-provider2/src/main/java/org/apache/dubbo/demo/provider/ApplicationProvider2.java
@@ -18,9 +18,8 @@ package org.apache.dubbo.demo.provider;
import org.springframework.context.support.ClassPathXmlApplicationContext;
-public class ApplicationProvider {
+public class ApplicationProvider2 {
public static void main(String[] args) throws Exception {
- //ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-provider-sofaregistry.xml");
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-provider-zk.xml");
context.start();
System.in.read();
diff --git a/dubbo-demo/dubbo-demo-service/dubbo-demo-service-provider/src/main/java/org/apache/dubbo/demo/provider/GreetingServiceImpl.java b/dubbo-demo/dubbo-demo-service/dubbo-demo-service-provider2/src/main/java/org/apache/dubbo/demo/provider/GreetingServiceImpl.java
similarity index 95%
copy from dubbo-demo/dubbo-demo-service/dubbo-demo-service-provider/src/main/java/org/apache/dubbo/demo/provider/GreetingServiceImpl.java
copy to dubbo-demo/dubbo-demo-service/dubbo-demo-service-provider2/src/main/java/org/apache/dubbo/demo/provider/GreetingServiceImpl.java
index cc1b5de..1394552 100644
--- a/dubbo-demo/dubbo-demo-service/dubbo-demo-service-provider/src/main/java/org/apache/dubbo/demo/provider/GreetingServiceImpl.java
+++ b/dubbo-demo/dubbo-demo-service/dubbo-demo-service-provider2/src/main/java/org/apache/dubbo/demo/provider/GreetingServiceImpl.java
@@ -24,6 +24,6 @@ import org.apache.dubbo.demo.GreetingService;
public class GreetingServiceImpl implements GreetingService {
@Override
public String hello() {
- return "Greetings!";
+ return "Greetings from provider2!";
}
}
diff --git a/dubbo-demo/dubbo-demo-service/dubbo-demo-service-provider2/src/main/resources/dubbo.properties b/dubbo-demo/dubbo-demo-service/dubbo-demo-service-provider2/src/main/resources/dubbo.properties
new file mode 100644
index 0000000..38c5326
--- /dev/null
+++ b/dubbo-demo/dubbo-demo-service/dubbo-demo-service-provider2/src/main/resources/dubbo.properties
@@ -0,0 +1 @@
+dubbo.application.qos.port=22224
diff --git a/dubbo-demo/dubbo-demo-service/dubbo-demo-service-provider2/src/main/resources/log4j.properties b/dubbo-demo/dubbo-demo-service/dubbo-demo-service-provider2/src/main/resources/log4j.properties
new file mode 100644
index 0000000..15a0900
--- /dev/null
+++ b/dubbo-demo/dubbo-demo-service/dubbo-demo-service-provider2/src/main/resources/log4j.properties
@@ -0,0 +1,7 @@
+###set log levels###
+log4j.rootLogger=info, stdout
+###output to the console###
+log4j.appender.stdout=org.apache.log4j.ConsoleAppender
+log4j.appender.stdout.Target=System.out
+log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
+log4j.appender.stdout.layout.ConversionPattern=[%d{dd/MM/yy HH:mm:ss:SSS z}] %t %5p %c{2}: %m%n
\ No newline at end of file
diff --git a/dubbo-demo/dubbo-demo-service/dubbo-demo-service-provider2/src/main/resources/spring/dubbo-provider-zk.xml b/dubbo-demo/dubbo-demo-service/dubbo-demo-service-provider2/src/main/resources/spring/dubbo-provider-zk.xml
new file mode 100644
index 0000000..8cda578
--- /dev/null
+++ b/dubbo-demo/dubbo-demo-service/dubbo-demo-service-provider2/src/main/resources/spring/dubbo-provider-zk.xml
@@ -0,0 +1,37 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ 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.
+ -->
+<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
+ xmlns="http://www.springframework.org/schema/beans"
+ xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
+ http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
+
+ <dubbo:application name="demo-service-provider2" metadata-type="remote">
+ <dubbo:parameter key="mapping-type" value="metadata"/>
+ </dubbo:application>
+
+ <dubbo:config-center address="zookeeper://127.0.0.1:2181"/>
+ <dubbo:metadata-report address="zookeeper://127.0.0.1:2181"/>
+ <dubbo:registry id="registry1" address="zookeeper://127.0.0.1:2181?registry-type=service"/>
+
+ <bean id="greetingService" class="org.apache.dubbo.demo.provider.GreetingServiceImpl"/>
+
+ <dubbo:service version="1.0.0" group="greeting" timeout="5000" interface="org.apache.dubbo.demo.GreetingService"
+ ref="greetingService"/>
+
+</beans>
diff --git a/dubbo-demo/dubbo-demo-service/pom.xml b/dubbo-demo/dubbo-demo-service/pom.xml
index 19520b6..c947277 100644
--- a/dubbo-demo/dubbo-demo-service/pom.xml
+++ b/dubbo-demo/dubbo-demo-service/pom.xml
@@ -37,6 +37,7 @@
<modules>
<module>dubbo-demo-service-provider</module>
+ <module>dubbo-demo-service-provider2</module>
<module>dubbo-demo-service-consumer</module>
</modules>
diff --git a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/MappingChangedEvent.java b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/MappingChangedEvent.java
index 36b6275..91a25f4 100644
--- a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/MappingChangedEvent.java
+++ b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/MappingChangedEvent.java
@@ -21,7 +21,6 @@ import java.util.Set;
public class MappingChangedEvent {
private String serviceKey;
private Set<String> apps;
- private Set<String> oldApps;
public String getServiceKey() {
return serviceKey;
@@ -39,11 +38,4 @@ public class MappingChangedEvent {
this.apps = apps;
}
- public Set<String> getOldApps() {
- return oldApps;
- }
-
- public void setOldApps(Set<String> oldApps) {
- this.oldApps = oldApps;
- }
}
diff --git a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/MetadataInfo.java b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/MetadataInfo.java
index 7ce4cfb..6fc6239 100644
--- a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/MetadataInfo.java
+++ b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/MetadataInfo.java
@@ -191,7 +191,7 @@ public class MetadataInfo implements Serializable {
// service + group + version + protocol
private transient String matchKey;
- private URL url;
+ private transient URL url;
public ServiceInfo() {
}
diff --git a/dubbo-metadata/dubbo-metadata-report-zookeeper/src/main/java/org/apache/dubbo/metadata/store/zookeeper/ZookeeperMetadataReport.java b/dubbo-metadata/dubbo-metadata-report-zookeeper/src/main/java/org/apache/dubbo/metadata/store/zookeeper/ZookeeperMetadataReport.java
index 92fb909..b221a5b 100644
--- a/dubbo-metadata/dubbo-metadata-report-zookeeper/src/main/java/org/apache/dubbo/metadata/store/zookeeper/ZookeeperMetadataReport.java
+++ b/dubbo-metadata/dubbo-metadata-report-zookeeper/src/main/java/org/apache/dubbo/metadata/store/zookeeper/ZookeeperMetadataReport.java
@@ -21,6 +21,7 @@ import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.utils.StringUtils;
+import org.apache.dubbo.metadata.MappingChangedEvent;
import org.apache.dubbo.metadata.MappingListener;
import org.apache.dubbo.metadata.MetadataInfo;
import org.apache.dubbo.metadata.report.identifier.BaseMetadataIdentifier;
@@ -29,18 +30,19 @@ import org.apache.dubbo.metadata.report.identifier.MetadataIdentifier;
import org.apache.dubbo.metadata.report.identifier.ServiceMetadataIdentifier;
import org.apache.dubbo.metadata.report.identifier.SubscriberMetadataIdentifier;
import org.apache.dubbo.metadata.report.support.AbstractMetadataReport;
+import org.apache.dubbo.remoting.zookeeper.ChildListener;
import org.apache.dubbo.remoting.zookeeper.ZookeeperClient;
import org.apache.dubbo.remoting.zookeeper.ZookeeperTransporter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
-import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.PATH_SEPARATOR;
@@ -58,6 +60,8 @@ public class ZookeeperMetadataReport extends AbstractMetadataReport {
private Gson gson = new Gson();
+ private Map<String, ChildListener> listenerMap = new ConcurrentHashMap<>();
+
public ZookeeperMetadataReport(URL url, ZookeeperTransporter zookeeperTransporter) {
super(url);
if (url.isAnyHost()) {
@@ -140,9 +144,12 @@ public class ZookeeperMetadataReport extends AbstractMetadataReport {
@Override
public void registerServiceAppMapping(String serviceKey, String application, URL url) {
- Map<String, String> value = new HashMap<>();
- value.put("timestamp", String.valueOf(System.currentTimeMillis()));
- zkClient.create(toRootDir() + serviceKey + PATH_SEPARATOR + application, gson.toJson(value), false);
+ String path = toRootDir() + serviceKey + PATH_SEPARATOR + application;
+ if (StringUtils.isBlank(zkClient.getContent(path))) {
+ Map<String, String> value = new HashMap<>();
+ value.put("timestamp", String.valueOf(System.currentTimeMillis()));
+ zkClient.create(path, gson.toJson(value), false);
+ }
}
@Override
@@ -154,7 +161,23 @@ public class ZookeeperMetadataReport extends AbstractMetadataReport {
@Override
public Set<String> getServiceAppMapping(String serviceKey, MappingListener listener, URL url) {
Set<String> appNameSet = new HashSet<>();
- appNameSet.addAll(zkClient.getChildren(toRootDir() + serviceKey));
+ String path = toRootDir() + serviceKey;
+ appNameSet.addAll(zkClient.getChildren(path));
+
+ if (null == listenerMap.get(path)) {
+ ChildListener zkListener = new ChildListener() {
+ @Override
+ public void childChanged(String path, List<String> children) {
+ MappingChangedEvent event = new MappingChangedEvent();
+ event.setServiceKey(serviceKey);
+ event.setApps(null != children ? new HashSet<>(children): null);
+ listener.onEvent(event);
+ }
+ };
+ zkClient.addChildListener(path, zkListener);
+ listenerMap.put(path, zkListener);
+ }
+
return appNameSet;
}
}
diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscoveryRegistry.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscoveryRegistry.java
index 21972e2..945ad26 100644
--- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscoveryRegistry.java
+++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscoveryRegistry.java
@@ -456,11 +456,23 @@ public class ServiceDiscoveryRegistry implements Registry {
@Override
public void onEvent(MappingChangedEvent event) {
Set<String> newApps = event.getApps();
+ Set<String> tempOldApps = oldApps;
+ oldApps = newApps;
+
if (CollectionUtils.isEmpty(newApps)) {
return;
}
- if (!CollectionUtils.equals(oldApps, newApps) && newApps.size() >= oldApps.size()) {
+
+ if (CollectionUtils.isEmpty(tempOldApps) && newApps.size() > 0) {
subscribeURLs(url, listener, newApps);
+ return;
+ }
+
+ for (String newAppName : newApps) {
+ if (!tempOldApps.contains(newAppName)) {
+ subscribeURLs(url, listener, newApps);
+ return;
+ }
}
}
}