1. Overview

Here are ways of Spring boot write current port to file. Persist current running port to file while starting spring boot application. This very useful while working with microservice architecture because of the random port may select by the container or multiple services running on the same machine with the different port at that time easy to identify the current running port of application using  application.port  file.  Here we have written to generate or select random port while starting the application.

Spring boot also provides a way to persist or write application PID to file here article to write PID to file.

To write port to file we need to register WebServerPortFileWriter to spring boot application. There are two ways to register WebServerPortFileWriter in spring boot application.

  1. Add WebServerPortFileWriter using SpringApplication.addListeners() method.
  2. WebServerPortFileWriter to spring.factories file as a listener. make sure that spring.factories must be inside resources/META-INF derectory.

After register WebServerPortFileWriter Listener, When application will start it will automatically create application.port file at root directory where .jar file is available.

WebServerPortFileWriter is only available after spring boot 2 (spring-boot-starter-parent-2.0.0.RELEASE)

2. Example

     1. Add WebServerPortFileWriter  Listener at the start of the application

package com.javadeveloperzone;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.context.WebServerPortFileWriter;
import org.springframework.context.annotation.ComponentScan;
/**
 * Created by JavaDeveloperZone on 04-05-2018.
 */
@SpringBootApplication
@ComponentScan // Using a root package also allows the @ComponentScan annotation to be used without needing to specify a basePackage attribute
public class SpringBootConfig {
    public static void main(String[] args) throws Exception {
        SpringApplication application = new SpringApplication(SpringBootConfig.class);
        application.addListeners(new WebServerPortFileWriter());  
        //application.addListeners(new WebServerPortFileWriter("YourFileName")); for custom file name, default file name is application.port 
        application.run(); 
    } 
}

or

     2. Add WebServerPortFileWriter Listener to spring.factories

make sure that spring.factories file must be available inside resources/META-INF/.

org.springframework.context.ApplicationListener=\
org.springframework.boot.web.context.WebServerPortFileWriter

     Output

Let run application : mvn spring-boot:run

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.0.RELEASE)
2018-05-04 21:14:45.600 INFO 13132 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-05-04 21:14:45.690 INFO 13132 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8181 (http) with context path ''
2018-05-04 21:14:45.702 INFO 13132 --- [ main] com.javadeveloperzone.SpringBootConfig : Started SpringBootConfig in 6.47 seconds (JVM running for 7.469)

It will generate an application.port

Spring boot write port to file - output

Spring boot write port to file – output

3. References

4. Source Code

spring-boot-write-port-to-file (213 KB)

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *