dubbo icon indicating copy to clipboard operation
dubbo copied to clipboard

[Bug] random port -1 is not effect

Open songxiaosheng opened this issue 1 year ago • 5 comments

Pre-check

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

Search before asking

  • [X] 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 avatar Aug 22 '24 23:08 songxiaosheng

I'll try to fix it

heliang666s avatar Aug 23 '24 03:08 heliang666s

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 随机端口改为在缺省端口的基础上增长: https://github.com/apache/dubbo/commit/bf0eee922ba995e78533ae7e8983518c4be247e8

oxsean avatar Aug 23 '24 05:08 oxsean

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();
    }
}

songxiaosheng avatar Aug 24 '24 23:08 songxiaosheng

@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()

oxsean avatar Aug 25 '24 15:08 oxsean

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

Chenjp avatar Sep 02 '24 01:09 Chenjp