In this tutorials, I am going to show you how to set or use Spring Boot Random Port in application.
Spring Boot 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).
So that spring boot will automatically pick the available ports from OS and assigns to the application.
application.properties
server.port=0
application.yaml
server:
port:0
Check it :
mvn clean install
mvn spring-boot:run
2018-02-23 06:39:33.449 INFO 10820 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet
.resource.ResourceHttpRequestHandler]
2018-02-23 06:39:33.541 INFO 10820 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework
.web.servlet.resource.ResourceHttpRequestHandler]
2018-02-23 06:39:33.952 INFO 10820 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-02-23 06:39:34.236 INFO 10820 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 58535 (http)
2018-02-23 06:39:34.244 INFO 10820 --- [ main] com.onlinetutorialspoint.Application : Started Application in 8.103 seconds (JVM running for 15.911)
While running the application, we can see (highlighted lines on above) the generated random port in logs.
Reference :
Happy Learning 🙂