Skip to content

Commit

Permalink
Merge pull request #3502, apply #3295 to 2.6 branch.
Browse files Browse the repository at this point in the history
Fixes #3294, referenceconfig#destroy never invoke unregister.
  • Loading branch information
nzomkxia authored and chickenlj committed Feb 19, 2019
1 parent 342e814 commit 54e14e9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
Expand Up @@ -78,6 +78,8 @@ public class RegistryDirectory<T> extends AbstractDirectory<T> implements Notify

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

private volatile URL registeredConsumerUrl;

/**
* override rules
* Priority: override>-D>consumer>provider
Expand Down Expand Up @@ -164,6 +166,16 @@ public void destroy() {
if (isDestroyed()) {
return;
}

// unregister.
try {
if (getRegisteredConsumerUrl() != null && registry != null && registry.isAvailable()) {
registry.unregister(getRegisteredConsumerUrl());
}
} catch (Throwable t) {
logger.warn("unexpected error when unregister service " + serviceKey + "from registry" + registry.getUrl(), t);
}

// unsubscribe.
try {
if (getConsumerUrl() != null && registry != null && registry.isAvailable()) {
Expand Down Expand Up @@ -612,6 +624,14 @@ public URL getUrl() {
return this.overrideDirectoryUrl;
}

public URL getRegisteredConsumerUrl() {
return registeredConsumerUrl;
}

public void setRegisteredConsumerUrl(URL registeredConsumerUrl) {
this.registeredConsumerUrl = registeredConsumerUrl;
}

@Override
public boolean isAvailable() {
if (isDestroyed()) {
Expand Down
Expand Up @@ -51,6 +51,9 @@
import static com.alibaba.dubbo.common.Constants.QOS_ENABLE;
import static com.alibaba.dubbo.common.Constants.QOS_PORT;
import static com.alibaba.dubbo.common.Constants.VALIDATION_KEY;
import static com.alibaba.dubbo.common.Constants.CATEGORY_KEY;
import static com.alibaba.dubbo.common.Constants.CONSUMERS_CATEGORY;
import static com.alibaba.dubbo.common.Constants.CHECK_KEY;

/**
* RegistryProtocol
Expand Down Expand Up @@ -300,8 +303,9 @@ private <T> Invoker<T> doRefer(Cluster cluster, Registry registry, Class<T> type
URL subscribeUrl = new URL(Constants.CONSUMER_PROTOCOL, parameters.remove(Constants.REGISTER_IP_KEY), 0, type.getName(), parameters);
if (!Constants.ANY_VALUE.equals(url.getServiceInterface())
&& url.getParameter(Constants.REGISTER_KEY, true)) {
registry.register(subscribeUrl.addParameters(Constants.CATEGORY_KEY, Constants.CONSUMERS_CATEGORY,
Constants.CHECK_KEY, String.valueOf(false)));
URL registeredConsumerUrl = getRegisteredConsumerUrl(subscribeUrl, url);
registry.register(registeredConsumerUrl);
directory.setRegisteredConsumerUrl(registeredConsumerUrl);
}
directory.subscribe(subscribeUrl.addParameter(Constants.CATEGORY_KEY,
Constants.PROVIDERS_CATEGORY
Expand All @@ -313,6 +317,11 @@ private <T> Invoker<T> doRefer(Cluster cluster, Registry registry, Class<T> type
return invoker;
}

public URL getRegisteredConsumerUrl(final URL consumerUrl, URL registryUrl) {
return consumerUrl.addParameters(CATEGORY_KEY, CONSUMERS_CATEGORY,
CHECK_KEY, String.valueOf(false));
}

@Override
public void destroy() {
List<Exporter<?>> exporters = new ArrayList<Exporter<?>>(bounds.values());
Expand Down

0 comments on commit 54e14e9

Please sign in to comment.