Skip to content

[Feature] support configuration key contains hyphen from environment. #15320

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

Open
4 tasks done
btpka3 opened this issue Apr 14, 2025 · 0 comments
Open
4 tasks done

[Feature] support configuration key contains hyphen from environment. #15320

btpka3 opened this issue Apr 14, 2025 · 0 comments
Labels
component/need-triage Need maintainers to triage type/need-triage Need maintainers to triage

Comments

@btpka3
Copy link

btpka3 commented Apr 14, 2025

Pre-check

  • I am sure that all the content I provide is in English.

Search before asking

  • I had searched in the issues and found no similar feature requirement.

Apache Dubbo Component

Java SDK (apache/dubbo)

Descriptions

Dubbo java client version : 3.3.3
I'm using triple protocol to send http request to dubbo service.
I will provide multiple this service with different group or version, so it's required to specify group and version via http header:

curl -s -XPOST "http://localhost:50051/com.my.Service/request" \
-H "Content-Type: application/json" \
-H "rest-service-group: DUBBO" \
-H "rest-service-version: 1.0.0" \
-d '["firstStringParam",{"k1":"v1","k2":"v2"}]'

I found it required to specify environment  DUBBO_RPC_TRI_RESOLVE-FALLBACK-TO-DEFAULT to false to work, or else the http header "rest-service-group", "rest-service-version" has no effect.

While hyphen is not allowed as environment key:

$ export DUBBO_RPC_TRI_RESOLVE-FALLBACK-TO-DEFAULT=false
-bash: export: `DUBBO_RPC_TRI_RESOLVE-FALLBACK-TO-DEFAULT=flase': not a valid identifier

Some k8s service (such as Aliyun EDAS can set such env key), some others can't.

Request : please support allow specify such dubbo configuration keys from dubbo.properties and enviroment keys , add explain it in dubbo reference .

links :

Tip : It should to be fixed with a custom org.apache.dubbo.common.context.ApplicationExt implemention (NOT TESTED).

  • provide META-INF/dubbo/org.apache.dubbo.common.context.ApplicationExt with content
environment=org.my.MyEnvironment
  • provide a custom org.apache.dubbo.common.config.Environment with a custom org.apache.dubbo.common.config.EnvironmentConfiguration field.
package org.my;

import org.apache.dubbo.common.config.Environment;
import org.apache.dubbo.common.config.EnvironmentConfiguration;
import org.apache.dubbo.rpc.model.ScopeModel;

public class MyEnvironment extends Environment {
    private EnvironmentConfiguration environmentConfiguration;

    public MyEnvironment(ScopeModel scopeModel) {
        super(scopeModel);
    }

    @Override
    public void initialize() throws IllegalStateException {
        super.initialize();
        this.environmentConfiguration = new MyEnvironmentConfiguration();
    }

    public EnvironmentConfiguration getEnvironmentConfiguration() {
        return environmentConfiguration;
    }
    // TODO: other method which needed to be override
}
package org.my;
package com.alibaba.security.gong9.mw.pandora.hsf.impl.dubbo;

import org.apache.dubbo.common.config.EnvironmentConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nullable;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;

/**
 * Get configuration key from environment. 
 * environment key: (1) `.` to `_`, (2) `-` to `_`  (3)  to uppercase.
 * @see org.apache.dubbo.common.config.EnvironmentConfiguration
 */
public class MyEnvironmentConfiguration extends EnvironmentConfiguration {

    static Logger logger = LoggerFactory.getLogger(G9EnvironmentConfiguration.class);
    private Map<String, String> envMap;

    public G9EnvironmentConfiguration() {
        this(System.getenv());
    }


    public G9EnvironmentConfiguration(Map<String, String> envMap) {
        Objects.requireNonNull(envMap, "envMap is required");
        this.envMap = envMap;
    }

    @Override
    public Object getInternalProperty(String name) {
        String actualName = resolvePropertyName(name);
        if (logger.isDebugEnabled() && !name.equals(actualName)) {
            logger.debug("{} does not contain environment property '{}', but found equivalent '{}'",
                G9EnvironmentConfiguration.class.getSimpleName(), name, actualName
            );
        }
        return envMap.get(actualName);
    }

    protected final String resolvePropertyName(String name) {
        Objects.requireNonNull(name, "Property name must not be null");
        String resolvedName = checkPropertyName(name);
        if (resolvedName != null) {
            return resolvedName;
        }
        String uppercasedName = name.toUpperCase(Locale.ROOT);
        if (!name.equals(uppercasedName)) {
            resolvedName = checkPropertyName(uppercasedName);
            if (resolvedName != null) {
                return resolvedName;
            }
        }
        return name;
    }

    @Nullable
    private String checkPropertyName(String name) {
        // Check name as-is
        if (this.envMap.containsKey(name)) {
            return name;
        }
        // Check name with just dots replaced
        String noDotName = name.replace('.', '_');
        if (!name.equals(noDotName) && this.envMap.containsKey(noDotName)) {
            return noDotName;
        }
        // Check name with just hyphens replaced
        String noHyphenName = name.replace('-', '_');
        if (!name.equals(noHyphenName) && this.envMap.containsKey(noHyphenName)) {
            return noHyphenName;
        }
        // Check name with dots and hyphens replaced
        String noDotNoHyphenName = noDotName.replace('-', '_');
        if (!noDotName.equals(noDotNoHyphenName) && this.envMap.containsKey(noDotNoHyphenName)) {
            return noDotNoHyphenName;
        }
        // Give up
        return null;
    }
}

Related issues

No response

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

  • Yes I am willing to submit a pull request on my own!

Code of Conduct

@btpka3 btpka3 added component/need-triage Need maintainers to triage type/need-triage Need maintainers to triage labels Apr 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component/need-triage Need maintainers to triage type/need-triage Need maintainers to triage
Projects
Status: Todo
Development

No branches or pull requests

1 participant