Skip to content

Commit 86b1a98

Browse files
moriadrybeiwei30
authored andcommitted
complete lookup method of consul registry and add integration test (apache#3906)
* add test case * fix bug * add something * complete lookup method of consul registry and add test * clean code * correct dependency of embedded-consul
1 parent efa8332 commit 86b1a98

File tree

4 files changed

+165
-0
lines changed

4 files changed

+165
-0
lines changed

dubbo-dependencies-bom/pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
<curator_test_version>2.12.0</curator_test_version>
104104
<jedis_version>2.9.0</jedis_version>
105105
<consul_version>1.4.2</consul_version>
106+
<consul_process_version>2.0.0</consul_process_version>
106107
<xmemcached_version>1.3.6</xmemcached_version>
107108
<cxf_version>3.1.15</cxf_version>
108109
<thrift_version>0.8.0</thrift_version>
@@ -233,6 +234,11 @@
233234
<artifactId>consul-api</artifactId>
234235
<version>${consul_version}</version>
235236
</dependency>
237+
<dependency>
238+
<groupId>com.pszymczyk.consul</groupId>
239+
<artifactId>embedded-consul</artifactId>
240+
<version>${consul_process_version}</version>
241+
</dependency>
236242
<dependency>
237243
<groupId>com.googlecode.xmemcached</groupId>
238244
<artifactId>xmemcached</artifactId>

dubbo-registry/dubbo-registry-consul/pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@
3636
<groupId>com.ecwid.consul</groupId>
3737
<artifactId>consul-api</artifactId>
3838
</dependency>
39+
<dependency>
40+
<groupId>com.pszymczyk.consul</groupId>
41+
<artifactId>embedded-consul</artifactId>
42+
<scope>test</scope>
43+
</dependency>
3944
</dependencies>
4045

4146
</project>

dubbo-registry/dubbo-registry-consul/src/main/java/org/apache/dubbo/registry/consul/ConsulRegistry.java

+20
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@
3232
import com.ecwid.consul.v1.catalog.CatalogServicesRequest;
3333
import com.ecwid.consul.v1.health.HealthServicesRequest;
3434
import com.ecwid.consul.v1.health.model.HealthService;
35+
import org.apache.dubbo.rpc.RpcException;
3536

3637
import java.util.Collection;
3738
import java.util.Collections;
3839
import java.util.List;
40+
import java.util.ArrayList;
3941
import java.util.Map;
4042
import java.util.Objects;
4143
import java.util.concurrent.ConcurrentHashMap;
@@ -155,6 +157,24 @@ public void doUnsubscribe(URL url, NotifyListener listener) {
155157
notifier.stop();
156158
}
157159

160+
@Override
161+
public List<URL> lookup(URL url) {
162+
if (url == null) {
163+
throw new IllegalArgumentException("lookup url == null");
164+
}
165+
try {
166+
String service = url.getServiceKey();
167+
Response<List<HealthService>> result = client.getHealthServices(service, HealthServicesRequest.newBuilder().setTag(SERVICE_TAG).build());
168+
if (result == null || result.getValue() == null || result.getValue().isEmpty()) {
169+
return new ArrayList<>();
170+
} else {
171+
return convert(result.getValue());
172+
}
173+
} catch (Throwable e) {
174+
throw new RpcException("Failed to lookup " + url + " from consul " + getUrl() + ", cause: " + e.getMessage(), e);
175+
}
176+
}
177+
158178
@Override
159179
public boolean isAvailable() {
160180
return client.getAgentSelf() != null;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.dubbo.registry.consul;
18+
19+
import com.pszymczyk.consul.ConsulProcess;
20+
import com.pszymczyk.consul.ConsulStarterBuilder;
21+
import org.apache.dubbo.common.URL;
22+
import org.apache.dubbo.common.status.Status;
23+
import org.apache.dubbo.registry.NotifyListener;
24+
import org.apache.dubbo.registry.Registry;
25+
import org.apache.dubbo.registry.status.RegistryStatusChecker;
26+
27+
import org.junit.jupiter.api.AfterEach;
28+
import org.junit.jupiter.api.BeforeEach;
29+
import org.junit.jupiter.api.Test;
30+
31+
import java.util.List;
32+
import java.util.Map;
33+
import java.util.Set;
34+
35+
import static org.hamcrest.CoreMatchers.is;
36+
import static org.hamcrest.CoreMatchers.nullValue;
37+
import static org.hamcrest.CoreMatchers.not;
38+
import static org.hamcrest.MatcherAssert.assertThat;
39+
import static org.mockito.Mockito.mock;
40+
41+
public class ConsulRegistryTest {
42+
43+
private static ConsulProcess consul;
44+
private ConsulRegistry consulRegistry;
45+
private String service = "org.apache.dubbo.test.injvmServie";
46+
private URL serviceUrl = URL.valueOf("consul://127.0.0.1:8012/" + service + "?notify=false&methods=test1,test2");
47+
private URL registryUrl;
48+
private ConsulRegistryFactory consulRegistryFactory;
49+
50+
@BeforeEach
51+
public void setUp() throws Exception {
52+
this.consul = ConsulStarterBuilder.consulStarter()
53+
.build()
54+
.start();
55+
this.registryUrl = URL.valueOf("consul://localhost:" + consul.getHttpPort());
56+
57+
consulRegistryFactory = new ConsulRegistryFactory();
58+
this.consulRegistry = (ConsulRegistry) consulRegistryFactory.createRegistry(registryUrl);
59+
}
60+
61+
@AfterEach
62+
public void tearDown() throws Exception {
63+
consul.close();
64+
this.consulRegistry.destroy();
65+
}
66+
67+
@Test
68+
public void testRegister() {
69+
Set<URL> registered;
70+
71+
for (int i = 0; i < 2; i++) {
72+
consulRegistry.register(serviceUrl);
73+
registered = consulRegistry.getRegistered();
74+
assertThat(registered.contains(serviceUrl), is(true));
75+
}
76+
77+
registered = consulRegistry.getRegistered();
78+
79+
assertThat(registered.size(), is(1));
80+
}
81+
82+
@Test
83+
public void testSubscribe() {
84+
NotifyListener listener = mock(NotifyListener.class);
85+
consulRegistry.subscribe(serviceUrl, listener);
86+
87+
Map<URL, Set<NotifyListener>> subscribed = consulRegistry.getSubscribed();
88+
assertThat(subscribed.size(), is(1));
89+
assertThat(subscribed.get(serviceUrl).size(), is(1));
90+
91+
consulRegistry.unsubscribe(serviceUrl, listener);
92+
subscribed = consulRegistry.getSubscribed();
93+
assertThat(subscribed.size(), is(1));
94+
assertThat(subscribed.get(serviceUrl).size(), is(0));
95+
}
96+
97+
@Test
98+
public void testAvailable() {
99+
consulRegistry.register(serviceUrl);
100+
assertThat(consulRegistry.isAvailable(), is(true));
101+
102+
// consulRegistry.destroy();
103+
// assertThat(consulRegistry.isAvailable(), is(false));
104+
}
105+
106+
@Test
107+
public void testLookup() throws InterruptedException {
108+
List<URL> lookup = consulRegistry.lookup(serviceUrl);
109+
assertThat(lookup.size(), is(0));
110+
111+
consulRegistry.register(serviceUrl);
112+
Thread.sleep(5000);
113+
lookup = consulRegistry.lookup(serviceUrl);
114+
assertThat(lookup.size(), is(1));
115+
}
116+
117+
@Test
118+
public void testStatusChecker() {
119+
RegistryStatusChecker registryStatusChecker = new RegistryStatusChecker();
120+
Status status = registryStatusChecker.check();
121+
assertThat(status.getLevel(), is(Status.Level.UNKNOWN));
122+
123+
Registry registry = consulRegistryFactory.getRegistry(registryUrl);
124+
assertThat(registry, not(nullValue()));
125+
126+
status = registryStatusChecker.check();
127+
assertThat(status.getLevel(), is(Status.Level.OK));
128+
129+
registry.register(serviceUrl);
130+
status = registryStatusChecker.check();
131+
assertThat(status.getLevel(), is(Status.Level.OK));
132+
}
133+
134+
}

0 commit comments

Comments
 (0)