Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring boot app actually running on port 0, instead of random

my application.properties file contains

server.port=0

Which is supposed to be captured by spring boot and set it to a random port.

Instead it actually launches it on port 0, its even in the spring log as such:

01/Mar/2019 12:50:43,600- TomcatEmbeddedServletContainer: Tomcat initialized with port(s): 0 (http)

Eureka sees it as an 'up' service, and provides the link to the service (localhost:0/info), clicking on it gives my browser 'ERR_ADDRESS_INVALID', guessing because its not a valid port..

App is running to send heartbeats to Eureka, but why is spring not setting it a random port number?

Are there any settings that can prevent the random? if so how to unset them?

edit: any new boot apps the server.port=0 is random, its just not working for an existing spring boot application that lots of dependencies

like image 990
StevenWernerCS Avatar asked Mar 01 '19 18:03

StevenWernerCS


People also ask

How do I run a spring boot on a random port?

Generally we can set the Spring boot server port by using server. port property in application properties file. But if we want to set the server port as random port (Generally used when working with micro-services) the server. port should be assigned with '0' (Zero).

What is the default port of spring boot application?

By default, the embedded server starts on port 8080. Now the server will start on port 8081. Both files are loaded automatically by Spring Boot if placed in the src/main/resources directory of a Maven application.

How do I stop spring boot from porting?

1) On the top right corner of your console, there is a red button, to stop the spring boot application which is already running on this port just click on the red button to terminate. 2) If the red button is not activated you need to right click on the console and select terminate/disconnect all. Hope this helps.


2 Answers

It doesn't actually start it on port 0, it starts it on a random port. In your eureka server you will see that it is in port 0 but if you put yourself on top without clicking you will see in the browser bar that the port is different.

In the log it shows:

INFO  o.s.b.w.e.tomcat.TomcatWebServer - Tomcat initialized with port(s): 0 (http)

but later changes it:

INFO  o.s.b.w.e.tomcat.TomcatWebServer - Tomcat started on port(s): 64039 (http) with context path ''
INFO  o.s.c.n.e.s.EurekaAutoServiceRegistration - Updating port to 64039

So if you have problems communicating with each other, it is because in every microservice you start with random port would have to configure in your application.yml a preferIpAddress to find it by ip and not by hostname:

eureka:
  client:
    registerWithEureka: true
    fetchRegistry: true
    serviceUrl:
      defaultZone: http://localhost:portServer/eureka/
  instance:
    preferIpAddress: true
like image 56
Francesc Recio Avatar answered Oct 23 '22 01:10

Francesc Recio


Try to set port programmatically:

@Configuration
public class ServletConfig {

    @Bean
    public EmbeddedServletContainerCustomizer containerCustomizer() {
        return (container -> {
            container.setPort(new Random().nextInt(65_535) + 1_000);
        });
    }
}

Also, this might help: Eureka not able to find port when running microservices on random port

like image 4
Mikhail Kholodkov Avatar answered Oct 22 '22 23:10

Mikhail Kholodkov