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

2

11

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

Warmth answered 1/3, 2019 at 18:57 Comment(4)
What is your spring boot version where it doesn't work?Mcelhaney
@Karol Spring boot 2.1.6.RELEASE with Greenwich.SR1Kaiserism
The application starts on proper dynamic port but Eureka sees port as 0 onlyKaiserism
@GauravVarma check this out: #53492470Acapulco
S
12

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
Spade answered 3/12, 2019 at 13:53 Comment(2)
I have setup preferIpAddress as true, but the problem is still thereKaiserism
I guess you have the spring-boot-starter-actuator dependency to access the url host:port/actuator/info, Have you tried a firefox browser if you get the same error?Spade
R
3

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

Rudbeckia answered 1/3, 2019 at 19:26 Comment(2)
That did launch the app on a random port, although possibly too late, as eureka still is looking at port 0Warmth
Which version of Spring Boot and Spring Cloud are you using?Rudbeckia

© 2022 - 2024 — McMap. All rights reserved.