10

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

StevenWernerCS
  • 839
  • 9
  • 15

2 Answers2

11

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
Francesc Recio
  • 2,187
  • 2
  • 13
  • 26
  • I have setup preferIpAddress as true, but the problem is still there – Crickcoder Dec 04 '19 at 09:42
  • 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? – Francesc Recio Dec 05 '19 at 09:12
4

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

Mikhail Kholodkov
  • 23,642
  • 17
  • 61
  • 78