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

[3.0] Nacos, add switch and disable consumer side registration by default #9827

Merged
merged 7 commits into from
Mar 23, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,6 @@ public interface RegistryConstants {
float DEFAULT_HASHMAP_LOAD_FACTOR = 0.75f;

String ENABLE_EMPTY_PROTECTION_KEY = "enable-empty-protection";
String REGISTER_CONSUMER_URL_KEY = "register-consumer-url";

}
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ public class ApplicationConfig extends AbstractConfig {
*/
private String metadataType;

/**
* Used to control whether register instance to registry or not. Set to 'false' only when instance is pure consumer.
*/
private Boolean registerConsumer;

private String repository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ public class MetadataConstants {
public static final int DEFAULT_METADATA_PUBLISH_DELAY = 30000;
public static final String METADATA_PROXY_TIMEOUT_KEY = "dubbo.application.metadata.proxy.delay";
public static final int DEFAULT_METADATA_TIMEOUT_VALUE = 5000;
public static String REPORT_CONSUMER_URL_KEY = "report-consumer-definition";
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import static org.apache.dubbo.common.constants.RemotingConstants.BACKUP_KEY;
import static org.apache.dubbo.common.utils.StringConstantFieldValuePredicate.of;
import static org.apache.dubbo.common.utils.StringUtils.HYPHEN_CHAR;
import static org.apache.dubbo.metadata.MetadataConstants.REPORT_CONSUMER_URL_KEY;
import static org.apache.dubbo.metadata.ServiceNameMapping.DEFAULT_MAPPING_GROUP;
import static org.apache.dubbo.metadata.ServiceNameMapping.getAppNames;

Expand Down Expand Up @@ -179,7 +180,9 @@ protected void doStoreProviderMetadata(MetadataIdentifier providerMetadataIdenti

@Override
protected void doStoreConsumerMetadata(MetadataIdentifier consumerMetadataIdentifier, String value) {
this.storeMetadata(consumerMetadataIdentifier, value);
if (getUrl().getParameter(REPORT_CONSUMER_URL_KEY, false)) {
this.storeMetadata(consumerMetadataIdentifier, value);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import static org.apache.dubbo.common.constants.CommonConstants.INTERFACE_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.PATH_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.PROTOCOL_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.PROVIDER_SIDE;
import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY;
import static org.apache.dubbo.common.constants.RegistryConstants.CATEGORY_KEY;
import static org.apache.dubbo.common.constants.RegistryConstants.CONFIGURATORS_CATEGORY;
Expand All @@ -72,6 +73,7 @@
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.PROVIDERS_CATEGORY;
import static org.apache.dubbo.common.constants.RegistryConstants.REGISTER_CONSUMER_URL_KEY;
import static org.apache.dubbo.common.constants.RegistryConstants.ROUTERS_CATEGORY;
import static org.apache.dubbo.registry.Constants.ADMIN_PROTOCOL;
import static org.apache.dubbo.registry.nacos.NacosServiceName.NAME_SEPARATOR;
Expand Down Expand Up @@ -166,16 +168,20 @@ public List<URL> lookup(final URL url) {
@Override
public void doRegister(URL url) {
try {
String serviceName = getServiceName(url);
Instance instance = createInstance(url);
/**
* 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);
if (PROVIDER_SIDE.equals(url.getSide()) || getUrl().getParameter(REGISTER_CONSUMER_URL_KEY, false)) {
String serviceName = getServiceName(url);
Instance instance = createInstance(url);
/**
* 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);
} else {
logger.info("Please set 'dubbo.registry.parameters.register-consumer-url=true' to turn on consumer url registration.");
}
} catch (Throwable cause) {
throw new RpcException("Failed to register " + url + " to nacos " + getUrl() + ", cause: " + cause.getMessage(), cause);
}
Expand Down