apache / dubbo

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

Dubbo Random port conflict #6711

Closed lingcoder closed 3 years ago

lingcoder commented 4 years ago

Environment

Steps to reproduce this issue

  1. Generate a new dubbo project ,and 2+ dubbo modules at least to start
  2. The dubbo port configuration for both modules is as follows:
    dubbo:
    protocol:
    name: dubbo
    port: -1
  3. Start both modules at the same time on the same host

Expected Result

Two dubbo modules start normally and use two different random ports

Actual Result

One dubbo modules start normally and another not. Because they use the same dubbo protocol port.

If there is an exception, please attach the exception trace:

2020-09-09 10:42:23.414 WARN 17632 [main] o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Failed to start bean 'webServerStartStop'; nested exception is org.apache.dubbo.rpc.RpcException: Fail to start server(url: dubbo://192.168.137.1:20880/com.alibaba.cloud.dubbo.service.DubboMetadataService?anyhost=true&application=dubbo-sample-consumer&bind.ip=192.168.137.1&bind.port=20880&channel.readonly.sent=true&codec=dubbo&deprecated=false&dubbo=2.0.2&dynamic=true&generic=false&group=dubbo-sample-consumer&heartbeat=60000&interface=com.alibaba.cloud.dubbo.service.DubboMetadataService&methods=getAllServiceKeys,getServiceRestMetadata,getExportedURLs,getAllExportedURLs&pid=17632&qos.enable=false&release=2.7.8&retries=-1&revision=2.2.1.RELEASE&side=provider&timeout=40000&timestamp=1599619340513&version=1.0.0) Failed to bind NettyServer on /192.168.137.1:20880, cause: Address already in use: bind

... ... 

2020-09-09 10:42:48.024 ERROR 17632 [Nacso-Watch-Task-Scheduler-1] o.s.s.support.TaskUtils$LoggingErrorHandler Unexpected error occurred in scheduled task

java.lang.IllegalStateException: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@11841b15 has not been refreshed yet
    at org.springframework.context.support.AbstractApplicationContext.assertBeanFactoryActive(AbstractApplicationContext.java:1096) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1108) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.context.event.ApplicationListenerMethodAdapter.getTargetBean(ApplicationListenerMethodAdapter.java:332) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.context.event.ApplicationListenerMethodAdapter.doInvoke(ApplicationListenerMethodAdapter.java:297) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.context.event.ApplicationListenerMethodAdapter.processEvent(ApplicationListenerMethodAdapter.java:190) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.context.event.ApplicationListenerMethodAdapter.onApplicationEvent(ApplicationListenerMethodAdapter.java:153) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:404) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:361) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at com.alibaba.cloud.nacos.discovery.NacosWatch.nacosServicesWatch(NacosWatch.java:130) ~[spring-cloud-starter-alibaba-nacos-discovery-2.2.1.RELEASE.jar:2.2.1.RELEASE]
    at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [na:1.8.0_241]
    at java.util.concurrent.FutureTask.runAndReset$$$capture(FutureTask.java:308) [na:1.8.0_241]
    at java.util.concurrent.FutureTask.runAndReset(FutureTask.java) [na:1.8.0_241]
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180) [na:1.8.0_241]
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294) [na:1.8.0_241]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_241]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_241]
    at java.lang.Thread.run(Thread.java:748) [na:1.8.0_241]
AlbumenJ commented 4 years ago

Before port being exposed, there are many steps ,like Configuration validation, Serialization initialization, Protocol initialization and so on, after random port fetch. This will cause more than one provider fetch the same port because the port is not occupied. If you want to start more than one provider at one time, it is more recommend to specify specific port for each provider.

githubzuoyi commented 4 years ago

In the production environment, is there a scenario where ports are randomly assigned?

lingcoder commented 4 years ago

In the production environment, is there a scenario where ports are randomly assigned?

Thanks! Even if you configure a fixed port, it is possible to deploy multiple instances on one machine,then "Address already in use: bind" is s bound to occur.

I do configure dubbo protocol port to be random because I wonder if it is possible to completely ignore the concerns about port configuration.

lingcoder commented 4 years ago

Before port being exposed, there are many steps ,like Configuration validation, Serialization initialization, Protocol initialization and so on, after random port fetch. This will cause more than one provider fetch the same port because the port is not occupied. If you want to start more than one provider at one time, it is more recommend to specify specific port for each provider.

Thanks! Is it possible that we completely ignore the concerns about dubbo protocol port configuration when we use dubbo

ppzzyy11 commented 4 years ago

Be flexible, start one by one or specify the ports.

qixiaobo commented 4 years ago

Large port may be used some connections But in your code , I think you may be restart at least 2 app. The code cannot promise you . Image this, app 1 finds port 20880 not use and then app2 find sport 20880 not use too. So app 1 starts fast and then app2 must failed to start. I can say dubbo may improve code to find a random port. and not release this port . So docker or k8s may be the port solution

CrazyHZM commented 3 years ago

The above answer should solve your problem, temporarily close the issue, if there is still a problem, you can reopen it