apache / dubbo

The java implementation of Apache Dubbo. An RPC and microservice framework.
https://dubbo.apache.org/
Apache License 2.0
40.24k stars 26.35k forks source link

[Bug] When Service Discovery is enabled, the same registryId may create two ServiceDiscoveryRegistry #14347

Open huisman6 opened 3 weeks ago

huisman6 commented 3 weeks ago

Pre-check

Search before asking

Apache Dubbo Component

Java SDK (apache/dubbo)

Dubbo Version

dubbo3.2.12 , jdk17

Steps to reproduce this issue

We have a Dubbo 3.2 service provider that is also a Dubbo Consumer. With application-level Service Discovery enabled, the same registryId creates two ServiceDiscoveryRegistry after ServiceConfig #doExportUrl and RefrenceConfig#refer.

I created a reproducible Dubbo application, the configuration of application-level Service Discovery is as follows:

dubbo:
  application:
    name: dubbo-playground
    metadata-type: local
    register-mode: instance
    migration.step: FORCE_APPLICATION
  registry:
    id: registry-multiple
    address: default://localhost:8848
    use-as-config-center: false
    use-as-metadata-center: false
    register-mode: instance
    parameters:
      registry-type: service

Our application supports dynamically adjusting instance weights, warm-up duration, and other service discovery metadata through the Http Endpoint runtime.

When the instance metadata changes, it triggers instance re-registration or update. In the underlying implementation, all discovery registries are obtained through RegistryManager#getServiceDiscoveries and its API is called.

Now the same registryId creates two ServiceDiscoveryRegistry, which means that we need to update the metadata once and send two requests to the same service discovery registry.

ServiceDiscoveryRegistry created twice because ServiceDiscoveryRegistryFactory registry cache key contains the full url parameter.

public class ServiceDiscoveryRegistryFactory extends AbstractRegistryFactory {

    @Override
    protected String createRegistryCacheKey(URL url) {
        return url.toFullString();
    }
 }

When ServiceConfig exports the service, it adds a URL parameter register = false.

private void doExportUrl(URL url, boolean withMetaData, RegisterTypeEnum registerType) {
    if (!url.getParameter(REGISTER_KEY, true)) {
        registerType = RegisterTypeEnum.MANUAL_REGISTER;
    }
    if (registerType == RegisterTypeEnum.NEVER_REGISTER
            || registerType == RegisterTypeEnum.MANUAL_REGISTER
            || registerType == RegisterTypeEnum.AUTO_REGISTER_BY_DEPLOYER) {
        url = url.addParameter(REGISTER_KEY, false);
    }

However, when using ReferenceConfig#createInvoker, there is no parameter register = false in the Registry URL, resulting in two cache keys.

What you expected to happen

Is it possible to adjust the registry cache key to the following code to ensure that the same registryId always returns the same ServiceDiscoveryRegistry?

protected String createRegistryCacheKey(URL url) {
    String registryCluster=url.getParameter(RegistryConstants.REGISTRY_CLUSTER_KEY);
    if (StringUtils.hasText(registryCluster)){
        return registryCluster;
    }
    return url.toFullString();
}

Anything else

No response

Are you willing to submit a pull request to fix on your own?

Code of Conduct

vishv843 commented 3 weeks ago

Hey, can I work on this?

wcy666103 commented 3 weeks ago

Hey, can I work on this?

Of course,go for it