Skip to content
Permalink
Browse files
Change zk to docker env in multi-registry (#401)
  • Loading branch information
AlbumenJ committed Dec 5, 2021
1 parent 9e730a7 commit 2537dc6c416ee61582494b9d0db922b452f8b98e
Showing 9 changed files with 41 additions and 24 deletions.
@@ -15,28 +15,46 @@
# limitations under the License.

services:
zookeeper-1:
image: zookeeper:latest

zookeeper-2:
image: zookeeper:latest

dubbo-samples-multi-registry:
type: app
basedir: .
mainClass: org.apache.dubbo.samples.multi.registry.MultiRegistryProvider
systemProps:
- zookeeper.address.1=zookeeper-1
- zookeeper.address.2=zookeeper-2
- zookeeper.port=2181
checkPorts:
- 20880
checkLog: "dubbo service started"
waitPortsBeforeRun:
- zookeeper-1:2181
- zookeeper-2:2181
depends_on:
- zookeeper-1
- zookeeper-2

dubbo-samples-multi-registry-test:
type: test
basedir: .
tests:
- "**/*IT.class"
systemProps:
- zookeeper.address=dubbo-samples-multi-registry
- zookeeper.port.1=2181
- zookeeper.port.2=2182
- zookeeper.address.1=zookeeper-1
- zookeeper.address.2=zookeeper-2
- zookeeper.port=2181
- dubbo.address=dubbo-samples-multi-registry
- dubbo.port=20880
waitPortsBeforeRun:
- dubbo-samples-multi-registry:2181
- dubbo-samples-multi-registry:2182
- zookeeper-1:2181
- zookeeper-2:2181
- dubbo-samples-multi-registry:20880
depends_on:
- zookeeper-1
- zookeeper-2
- dubbo-samples-multi-registry
@@ -27,8 +27,8 @@
public class MultiRegistryProvider {

public static void main(String[] args) throws Exception {
new EmbeddedZooKeeper(2181, false).start();
new EmbeddedZooKeeper(2182, false).start();
// new EmbeddedZooKeeper(2181, false).start();
// new EmbeddedZooKeeper(2182, false).start();

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/multi-registry-provider.xml");
context.start();
@@ -25,8 +25,8 @@

<dubbo:application name="multi-registry-consumer"/>

<dubbo:registry id="beijingRegistry" address="zookeeper://${zookeeper.address:127.0.0.1}:${zookeeper.port.1:2181}" default="false" />
<dubbo:registry id="shanghaiRegistry" address="zookeeper://${zookeeper.address:127.0.0.1}:${zookeeper.port.2:2182}" />
<dubbo:registry id="beijingRegistry" address="zookeeper://${zookeeper.address.1:127.0.0.1}:${zookeeper.port:2181}" default="false" />
<dubbo:registry id="shanghaiRegistry" address="zookeeper://${zookeeper.address.2:127.0.0.1}:${zookeeper.port:2181}" />

<dubbo:reference id="demoServiceFormDefault" interface="org.apache.dubbo.samples.multi.registry.api.DemoService"/>
<dubbo:reference id="demoServiceFormBeijing" interface="org.apache.dubbo.samples.multi.registry.api.DemoService"
@@ -25,8 +25,8 @@

<dubbo:application name="multi-registry-consumer"/>

<dubbo:registry id="beijingRegistry" address="zookeeper://${zookeeper.address:127.0.0.1}:${zookeeper.port.1:2181}" />
<dubbo:registry id="shanghaiRegistry" address="zookeeper://${zookeeper.address:127.0.0.1}:${zookeeper.port.2:2182}" />
<dubbo:registry id="beijingRegistry" address="zookeeper://${zookeeper.address.1:127.0.0.1}:${zookeeper.port:2181}" />
<dubbo:registry id="shanghaiRegistry" address="zookeeper://${zookeeper.address.2:127.0.0.1}:${zookeeper.port:2181}" />

<dubbo:reference id="demoService" registry="beijingRegistry" interface="org.apache.dubbo.samples.multi.registry.api.DemoService"/>
<dubbo:reference id="helloService" registry="shanghaiRegistry" interface="org.apache.dubbo.samples.multi.registry.api.HelloService"/>
@@ -25,7 +25,7 @@

<dubbo:application name="multi-registry-consumer"/>

<dubbo:registry id="shanghaiRegistry" address="zookeeper://${zookeeper.address:127.0.0.1}:${zookeeper.port.2:2182}" />
<dubbo:registry id="shanghaiRegistry" address="zookeeper://${zookeeper.address.2:127.0.0.1}:${zookeeper.port:2181}" />

<dubbo:reference id="demoService" interface="org.apache.dubbo.samples.multi.registry.api.DemoService"/>
<dubbo:reference id="helloService" interface="org.apache.dubbo.samples.multi.registry.api.HelloService"/>
@@ -25,7 +25,7 @@

<dubbo:application name="multi-registry-consumer"/>

<dubbo:registry id="beijingRegistry" address="zookeeper://${zookeeper.address:127.0.0.1}:${zookeeper.port.1:2181}" />
<dubbo:registry id="beijingRegistry" address="zookeeper://${zookeeper.address.1:127.0.0.1}:${zookeeper.port:2181}" />

<dubbo:reference id="demoService" interface="org.apache.dubbo.samples.multi.registry.api.DemoService"/>
<dubbo:reference id="helloService" interface="org.apache.dubbo.samples.multi.registry.api.HelloService"/>
@@ -25,8 +25,8 @@

<dubbo:application name="multi-registry-provider"/>

<dubbo:registry id="beijingRegistry" address="zookeeper://${zookeeper.address:127.0.0.1}:${zookeeper.port.1:2181}" default="false"/>
<dubbo:registry id="shanghaiRegistry" address="zookeeper://${zookeeper.address:127.0.0.1}:${zookeeper.port.2:2182}" />
<dubbo:registry id="beijingRegistry" address="zookeeper://${zookeeper.address.1:127.0.0.1}:${zookeeper.port:2181}" default="false"/>
<dubbo:registry id="shanghaiRegistry" address="zookeeper://${zookeeper.address.2:127.0.0.1}:${zookeeper.port:2181}" />

<dubbo:provider token="true"/>

@@ -67,17 +67,17 @@ public void testConsumer3() throws Exception {

@Test
public void verifyProvidersFromBeijingRegistry() throws Exception {
List<String> demoServiceProviders = ZKTools.getProviders(DemoService.class, 2181);
List<String> demoServiceProviders = ZKTools.getProviders(DemoService.class, "zookeeper.address.1", 2181);
Assert.assertEquals(1, demoServiceProviders.size());
List<String> helloServiceProviders = ZKTools.getProviders(HelloService.class, 2181);
List<String> helloServiceProviders = ZKTools.getProviders(HelloService.class, "zookeeper.address.1", 2181);
Assert.assertEquals(1, helloServiceProviders.size());
}

@Test
public void verifyProvidersFromShanghaiRegistry() throws Exception {
List<String> demoServiceProviders = ZKTools.getProviders(DemoService.class, 2182);
List<String> demoServiceProviders = ZKTools.getProviders(DemoService.class, "zookeeper.address.2", 2181);
Assert.assertEquals(1, demoServiceProviders.size());
List<String> helloServiceProviders = ZKTools.getProviders(HelloService.class, 2182);
List<String> helloServiceProviders = ZKTools.getProviders(HelloService.class, "zookeeper.address.2", 2181);
Assert.assertEquals(1, helloServiceProviders.size());
}
}
@@ -28,21 +28,20 @@
import java.util.stream.Collectors;

public class ZKTools {
private static String zookeeperHost = System.getProperty("zookeeper.address", "127.0.0.1");
private static Map<Integer, CuratorFramework> clients = new HashMap<>();

private static CuratorFramework initClient(int port) {
private static CuratorFramework initClient(String key, int port) {
if (!clients.containsKey(port)) {
CuratorFramework client = CuratorFrameworkFactory.newClient(zookeeperHost + ":" + port, 60 * 1000, 60 * 1000,
CuratorFramework client = CuratorFrameworkFactory.newClient(System.getProperty(key, "127.0.0.1") + ":" + port, 60 * 1000, 60 * 1000,
new ExponentialBackoffRetry(1000, 3));
client.start();
clients.put(port, client);
}
return clients.get(port);
}

public static List<String> getProviders(Class clazz, int port) throws Exception {
CuratorFramework client = initClient(port);
public static List<String> getProviders(Class clazz, String key, int port) throws Exception {
CuratorFramework client = initClient(key, port);
String path = "/dubbo/" + clazz.getName() + "/providers";
List<String> result = client.getChildren().forPath(path);
return result.stream().map(URL::decode).collect(Collectors.toList());

0 comments on commit 2537dc6

Please sign in to comment.