Skip to content

Commit 54e14e9

Browse files
nzomkxiachickenlj
authored andcommitted
Merge pull request #3502, apply #3295 to 2.6 branch.
Fixes #3294, referenceconfig#destroy never invoke unregister.
1 parent 342e814 commit 54e14e9

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

dubbo-registry/dubbo-registry-api/src/main/java/com/alibaba/dubbo/registry/integration/RegistryDirectory.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ public class RegistryDirectory<T> extends AbstractDirectory<T> implements Notify
7878

7979
private volatile URL overrideDirectoryUrl; // Initialization at construction time, assertion not null, and always assign non null value
8080

81+
private volatile URL registeredConsumerUrl;
82+
8183
/**
8284
* override rules
8385
* Priority: override>-D>consumer>provider
@@ -164,6 +166,16 @@ public void destroy() {
164166
if (isDestroyed()) {
165167
return;
166168
}
169+
170+
// unregister.
171+
try {
172+
if (getRegisteredConsumerUrl() != null && registry != null && registry.isAvailable()) {
173+
registry.unregister(getRegisteredConsumerUrl());
174+
}
175+
} catch (Throwable t) {
176+
logger.warn("unexpected error when unregister service " + serviceKey + "from registry" + registry.getUrl(), t);
177+
}
178+
167179
// unsubscribe.
168180
try {
169181
if (getConsumerUrl() != null && registry != null && registry.isAvailable()) {
@@ -612,6 +624,14 @@ public URL getUrl() {
612624
return this.overrideDirectoryUrl;
613625
}
614626

627+
public URL getRegisteredConsumerUrl() {
628+
return registeredConsumerUrl;
629+
}
630+
631+
public void setRegisteredConsumerUrl(URL registeredConsumerUrl) {
632+
this.registeredConsumerUrl = registeredConsumerUrl;
633+
}
634+
615635
@Override
616636
public boolean isAvailable() {
617637
if (isDestroyed()) {

dubbo-registry/dubbo-registry-api/src/main/java/com/alibaba/dubbo/registry/integration/RegistryProtocol.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@
5151
import static com.alibaba.dubbo.common.Constants.QOS_ENABLE;
5252
import static com.alibaba.dubbo.common.Constants.QOS_PORT;
5353
import static com.alibaba.dubbo.common.Constants.VALIDATION_KEY;
54+
import static com.alibaba.dubbo.common.Constants.CATEGORY_KEY;
55+
import static com.alibaba.dubbo.common.Constants.CONSUMERS_CATEGORY;
56+
import static com.alibaba.dubbo.common.Constants.CHECK_KEY;
5457

5558
/**
5659
* RegistryProtocol
@@ -300,8 +303,9 @@ private <T> Invoker<T> doRefer(Cluster cluster, Registry registry, Class<T> type
300303
URL subscribeUrl = new URL(Constants.CONSUMER_PROTOCOL, parameters.remove(Constants.REGISTER_IP_KEY), 0, type.getName(), parameters);
301304
if (!Constants.ANY_VALUE.equals(url.getServiceInterface())
302305
&& url.getParameter(Constants.REGISTER_KEY, true)) {
303-
registry.register(subscribeUrl.addParameters(Constants.CATEGORY_KEY, Constants.CONSUMERS_CATEGORY,
304-
Constants.CHECK_KEY, String.valueOf(false)));
306+
URL registeredConsumerUrl = getRegisteredConsumerUrl(subscribeUrl, url);
307+
registry.register(registeredConsumerUrl);
308+
directory.setRegisteredConsumerUrl(registeredConsumerUrl);
305309
}
306310
directory.subscribe(subscribeUrl.addParameter(Constants.CATEGORY_KEY,
307311
Constants.PROVIDERS_CATEGORY
@@ -313,6 +317,11 @@ private <T> Invoker<T> doRefer(Cluster cluster, Registry registry, Class<T> type
313317
return invoker;
314318
}
315319

320+
public URL getRegisteredConsumerUrl(final URL consumerUrl, URL registryUrl) {
321+
return consumerUrl.addParameters(CATEGORY_KEY, CONSUMERS_CATEGORY,
322+
CHECK_KEY, String.valueOf(false));
323+
}
324+
316325
@Override
317326
public void destroy() {
318327
List<Exporter<?>> exporters = new ArrayList<Exporter<?>>(bounds.values());

0 commit comments

Comments
 (0)