Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compatible nacos service discovery, export noting suffix servicename #14096

Merged
merged 1 commit into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions dubbo-common/src/main/java/org/apache/dubbo/common/URL.java
Original file line number Diff line number Diff line change
Expand Up @@ -1295,6 +1295,19 @@ public String getColonSeparatedKey() {
return serviceNameBuilder.toString();
}

/**
* The format is "{interface}:[version]"
*
* @return
*/
public String getCompatibleColonSeparatedKey() {
StringBuilder serviceNameBuilder = new StringBuilder();
serviceNameBuilder.append(this.getServiceInterface());
compatibleAppend(serviceNameBuilder, VERSION_KEY);
compatibleAppend(serviceNameBuilder, GROUP_KEY);
return serviceNameBuilder.toString();
}

private void append(StringBuilder target, String parameterName, boolean first) {
String parameterValue = this.getParameter(parameterName);
if (!isBlank(parameterValue)) {
Expand All @@ -1307,6 +1320,14 @@ private void append(StringBuilder target, String parameterName, boolean first) {
}
}

private void compatibleAppend(StringBuilder target, String parameterName) {
String parameterValue = this.getParameter(parameterName);
if (!isBlank(parameterValue)) {
target.append(':');
target.append(parameterValue);
}
}

/**
* The format of return value is '{group}/{interfaceName}:{version}'
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,11 @@ public interface RegistryConstants {
String ENABLE_EMPTY_PROTECTION_KEY = "enable-empty-protection";
boolean DEFAULT_ENABLE_EMPTY_PROTECTION = false;
String REGISTER_CONSUMER_URL_KEY = "register-consumer-url";

/**
* export noting suffix servicename
* by default, dubbo export servicename is "${interface}:${version}:", this servicename with ':' suffix
* for compatible, we should export noting suffix servicename, eg: ${interface}:${version}
*/
String NACOE_REGISTER_COMPATIBLE = "nacos.register-compatible";
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -71,6 +72,7 @@
import static org.apache.dubbo.common.constants.RegistryConstants.DEFAULT_ENABLE_EMPTY_PROTECTION;
import static org.apache.dubbo.common.constants.RegistryConstants.EMPTY_PROTOCOL;
import static org.apache.dubbo.common.constants.RegistryConstants.ENABLE_EMPTY_PROTECTION_KEY;
import static org.apache.dubbo.common.constants.RegistryConstants.NACOE_REGISTER_COMPATIBLE;
import static org.apache.dubbo.common.constants.RegistryConstants.PROVIDERS_CATEGORY;
import static org.apache.dubbo.common.constants.RegistryConstants.REGISTER_CONSUMER_URL_KEY;
import static org.apache.dubbo.common.constants.RegistryConstants.ROUTERS_CATEGORY;
Expand Down Expand Up @@ -107,9 +109,8 @@ public class NacosRegistry extends FailbackRegistry {
private static final String UP = "UP";

/**
* The separator for service name
* Change a constant to be configurable, it's designed for Windows file name that is compatible with old
* Nacos binary release(< 0.6.1)
* The separator for service name Change a constant to be configurable, it's designed for Windows file name that is
* compatible with old Nacos binary release(< 0.6.1)
*/
private static final String SERVICE_NAME_SEPARATOR = System.getProperty("nacos.service.name.separator", ":");

Expand Down Expand Up @@ -174,18 +175,33 @@ public List<URL> lookup(final URL url) {
public void doRegister(URL url) {
try {
if (PROVIDER_SIDE.equals(url.getSide()) || getUrl().getParameter(REGISTER_CONSUMER_URL_KEY, false)) {
String serviceName = getServiceName(url);
Instance instance = createInstance(url);

Set<String> serviceNames = new HashSet<>();
// by default servicename is "org.apache.dubbo.xxService:1.0.0:"
String serviceName = getServiceName(url, false);
serviceNames.add(serviceName);

// in https://github.com/apache/dubbo/issues/14075
if (getUrl().getParameter(NACOE_REGISTER_COMPATIBLE, false)) {
// servicename is "org.apache.dubbo.xxService:1.0.0"
String compatibleServiceName = getServiceName(url, true);
serviceNames.add(compatibleServiceName);
}

/**
* namingService.registerInstance with {@link org.apache.dubbo.registry.support.AbstractRegistry#registryUrl}
* namingService.registerInstance with
* {@link org.apache.dubbo.registry.support.AbstractRegistry#registryUrl}
* default {@link DEFAULT_GROUP}
*
* in https://github.com/apache/dubbo/issues/5978
*/
namingService.registerInstance(serviceName, getUrl().getGroup(Constants.DEFAULT_GROUP), instance);
for (String service : serviceNames) {
namingService.registerInstance(service, getUrl().getGroup(Constants.DEFAULT_GROUP), instance);
}
} else {
logger.info(
"Please set 'dubbo.registry.parameters.register-consumer-url=true' to turn on consumer url registration.");
logger.info("Please set 'dubbo.registry.parameters.register-consumer-url=true' to turn on consumer "
+ "url registration.");
}
} catch (SkipFailbackWrapperException exception) {
throw exception;
Expand All @@ -198,10 +214,24 @@ public void doRegister(URL url) {
@Override
public void doUnregister(final URL url) {
try {
String serviceName = getServiceName(url);
Instance instance = createInstance(url);
namingService.deregisterInstance(
serviceName, getUrl().getGroup(Constants.DEFAULT_GROUP), instance.getIp(), instance.getPort());

Set<String> serviceNames = new HashSet<>();
// by default servicename is "org.apache.dubbo.xxService:1.0.0:"
String serviceName = getServiceName(url, false);
serviceNames.add(serviceName);

// in https://github.com/apache/dubbo/issues/14075
if (getUrl().getParameter(NACOE_REGISTER_COMPATIBLE, false)) {
// servicename is "org.apache.dubbo.xxService:1.0.0"
String serviceName1 = getServiceName(url, true);
serviceNames.add(serviceName1);
}

for (String service : serviceNames) {
namingService.deregisterInstance(
service, getUrl().getGroup(Constants.DEFAULT_GROUP), instance.getIp(), instance.getPort());
}
} catch (SkipFailbackWrapperException exception) {
throw exception;
} catch (Exception cause) {
Expand Down Expand Up @@ -230,7 +260,8 @@ private void doSubscribe(final URL url, final NacosAggregateListener listener, f
* Get all instances with serviceNames to avoid instance overwrite and but with empty instance mentioned
* in https://github.com/apache/dubbo/issues/5885 and https://github.com/apache/dubbo/issues/5899
*
* namingService.getAllInstances with {@link org.apache.dubbo.registry.support.AbstractRegistry#registryUrl}
* namingService.getAllInstances with
* {@link org.apache.dubbo.registry.support.AbstractRegistry#registryUrl}
* default {@link DEFAULT_GROUP}
*
* in https://github.com/apache/dubbo/issues/5978
Expand Down Expand Up @@ -268,8 +299,8 @@ private void doSubscribe(final URL url, final NacosAggregateListener listener, f
}

/**
* Since 2.7.6 the legacy service name will be added to serviceNames
* to fix bug with https://github.com/apache/dubbo/issues/5442
* Since 2.7.6 the legacy service name will be added to serviceNames to fix bug with
* https://github.com/apache/dubbo/issues/5442
*
* @param url
* @return
Expand All @@ -290,7 +321,8 @@ public void doUnsubscribe(URL url, NotifyListener listener) {
"",
"",
String.format(
"No aggregate listener found for url %s, this service might have already been unsubscribed.",
"No aggregate listener found for url %s, "
+ "this service might have already been unsubscribed.",
url));
return;
}
Expand Down Expand Up @@ -581,7 +613,8 @@ private List<URL> toUrlWithEmpty(URL consumerURL, Collection<Instance> instances
REGISTRY_NACOS_EXCEPTION,
"",
"",
"Received empty url address list and empty protection is disabled, will clear current available addresses");
"Received empty url address list and empty protection is "
+ "disabled, will clear current available addresses");
URL empty = URLBuilder.from(consumerURL)
.setProtocol(EMPTY_PROTOCOL)
.addParameter(CATEGORY_KEY, DEFAULT_CATEGORY)
Expand Down Expand Up @@ -697,14 +730,21 @@ private NacosServiceName createServiceName(URL url) {
return valueOf(url);
}

private String getServiceName(URL url) {
private String getServiceName(URL url, boolean needCompatible) {
if (needCompatible) {
return getCompatibleServiceName(url, url.getCategory(DEFAULT_CATEGORY));
}
return getServiceName(url, url.getCategory(DEFAULT_CATEGORY));
}

private String getServiceName(URL url, String category) {
return category + SERVICE_NAME_SEPARATOR + url.getColonSeparatedKey();
}

private String getCompatibleServiceName(URL url, String category) {
return category + SERVICE_NAME_SEPARATOR + url.getCompatibleColonSeparatedKey();
}

private void filterEnabledInstances(Collection<Instance> instances) {
filterData(instances, Instance::isEnabled);
}
Expand Down
Loading