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

[Bug] random port -1 is not effect #14569

Open
3 of 4 tasks
songxiaosheng opened this issue Aug 22, 2024 · 5 comments
Open
3 of 4 tasks

[Bug] random port -1 is not effect #14569

songxiaosheng opened this issue Aug 22, 2024 · 5 comments
Assignees
Labels
component/need-triage Need maintainers to triage type/need-triage Need maintainers to triage

Comments

@songxiaosheng
Copy link
Member

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 issues.

Apache Dubbo Component

Java SDK (apache/dubbo)

Dubbo Version

3.3.0-beta.4

Steps to reproduce this issue

when i use port -1 ,it use a defaultPort 20880
ProtocolConfig protocolConfig = new ProtocolConfig(CommonConstants.DUBBO, -1);

image

What you expected to happen

when i use port -1 ,i want it use a port random

Anything else

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

@songxiaosheng songxiaosheng added type/need-triage Need maintainers to triage component/need-triage Need maintainers to triage labels Aug 22, 2024
@heliang666s
Copy link
Contributor

I'll try to fix it

@oxsean
Copy link
Collaborator

oxsean commented Aug 23, 2024

I think this is by design, and this random means not picking a random one, but looking for an available port starting from the default port upwards, see:
https://github.com/apache/dubbo/blob/3.3/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java#L117

DUBBO-501 随机端口改为在缺省端口的基础上增长:
bf0eee9

@songxiaosheng
Copy link
Member Author

getAvailablePort is ok

In this scenario, when multiple Models are created for data isolation, the port data cannot be isolated and only takes effect once.

public class ModelDemo {
    private static final Logger logger = LoggerFactory.getLogger(ModelDemo.class);

    public static void main(String[] args) throws InterruptedException {
      
        start(new FrameworkModel(), -1);
        logger.info("frameworkModelOne start success");

    
        start(new FrameworkModel(), -1);
        new CountDownLatch(1).await();
    }

    private static void start(FrameworkModel frameworkModel, int port) {
        //创建订单子系统应用级ApplicationModel
        ApplicationModel orderApplicationModel = frameworkModel.newApplication();
        //创建模块级ModuleModel
        ModuleModel moduleModel = orderApplicationModel.newModule();
        //Model的配置管理通过对应的ConfigManager进行
        ConfigManager appConfigManager = orderApplicationModel.getApplicationConfigManager();
        //应用配置
        ApplicationConfig application = new ApplicationConfig("dubbo-provider-app");
        //关闭QOS避免端口冲突
        application.setQosEnable(false);
        appConfigManager.setApplication(application);

        //应用层级的配置中心、元数据中心、协议默认设置
        appConfigManager.addRegistry(new RegistryConfig("zookeeper://127.0.0.1:2181"));
        appConfigManager.addMetadataReport(new MetadataReportConfig("zookeeper://127.0.0.1:2181"));
        appConfigManager.addProtocol(new ProtocolConfig(CommonConstants.DUBBO, port));

        //配置模块
        ModuleConfigManager moduleConfigManager = moduleModel.getConfigManager();
        moduleConfigManager.setModule(new ModuleConfig("dubbo-provider-app-module"));

        ServiceConfig<OrderService> serviceConfig = new ServiceConfig<>();
        //设置该ServiceConfig对应的ModuleModel
        serviceConfig.setScopeModel(moduleModel);
        serviceConfig.setInterface(OrderService.class);
        serviceConfig.setRef(new OrderServiceImpl());
        //为ModuleModel添加ServiceConfig
        moduleConfigManager.addConfig(serviceConfig);
        //导出服务
        serviceConfig.export();
    }
}

@oxsean
Copy link
Collaborator

oxsean commented Aug 25, 2024

@songxiaosheng
It looks like -1 means use the default port not randomized, this scenario can be support by getting a port using org.apache.dubbo.common.utils.NetUtils#getAvailablePort()

@Chenjp
Copy link
Contributor

Chenjp commented Sep 2, 2024

@songxiaosheng It looks like -1 means use the default port not randomized, this scenario can be support by getting a port using org.apache.dubbo.common.utils.NetUtils#getAvailablePort()

agree, consider change method name from getRandomPort to getRegisteredProtocolPort(), set... as above.

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

4 participants