How to get SPRING boot HOST and PORT address at runtime?

How can I get the host and port where my application is deployed at runtime so that I can use it in my java method?

+16
source share
6 answers

You can get this information through the Environment for port and host , you can get using InternetAddress .

 @Autowired Environment environment; ...... public void somePlaceInTheCode() { // Port environment.getProperty("server.port"); // Local address InetAddress.getLocalHost().getHostAddress(); InetAddress.getLocalHost().getHostName(); // Remote address InetAddress.getLoopbackAddress().getHostAddress(); InetAddress.getLoopbackAddress().getHostName(); } 
+18
source

For the host: As mentioned by Anton

 // Local address InetAddress.getLocalHost().getHostAddress(); InetAddress.getLocalHost().getHostName(); // Remote address InetAddress.getLoopbackAddress().getHostAddress(); InetAddress.getLoopbackAddress().getHostName(); 

Port: As mentioned by Nikolay, you can get this information on the environment property only if it is explicitly configured and not set to 0.

Spring related documentation: https://docs.spring.io/spring-boot/docs/current/reference/html/howto-embedded-servlet-containers.html#howto-discover-the-http-port-at-runtime

For the actual way to do this, the answer was given here: Spring Download - How to get the working port

And here is a github example on how to implement it: https://github.com/hosuaby/example-restful-project/blob/master/src/main/java/io/hosuaby/restful/PortHolder.java

0
source

Just a complete example of the answers above

 package bj; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationListener; import org.springframework.core.env.Environment; import java.net.InetAddress; import java.net.UnknownHostException; @SuppressWarnings("SpringJavaAutowiredFieldsWarningInspection") @SpringBootApplication class App implements ApplicationListener<ApplicationReadyEvent> { @Autowired private ApplicationContext applicationContext; public static void main(String[] args) { SpringApplication.run(App.class, args); } @Override public void onApplicationEvent(ApplicationReadyEvent event) { try { String ip = InetAddress.getLocalHost().getHostAddress(); int port = applicationContext.getBean(Environment.class).getProperty("server.port", Integer.class, 8080); System.out.printf("%s:%d", ip, port); } catch (UnknownHostException e) { e.printStackTrace(); } } } 
0
source

The port was bound at runtime and can be entered as:

 @Value('${local.server.port}') private int port; 
0
source

Here is the utilities component:

EnvUtil.java
(Put it in the appropriate packaging so that it becomes a component.)

 import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.env.Environment; import org.springframework.stereotype.Component; import java.net.InetAddress; import java.net.UnknownHostException; /** * Environment util. */ @Component public class EnvUtil { @Autowired Environment environment; private String port; private String hostname; /** * Get port. * * @return */ public String getPort() { if (port == null) port = environment.getProperty("local.server.port"); return port; } /** * Get port, as Integer. * * @return */ public Integer getPortAsInt() { return Integer.valueOf(getPort()); } /** * Get hostname. * * @return */ public String getHostname() throws UnknownHostException { // TODO ... would this cache cause issue, when network env change ??? if (hostname == null) hostname = InetAddress.getLocalHost().getHostAddress(); return hostname; } public String getServerUrlPrefi() throws UnknownHostException { return "http://" + getHostname() + ":" + getPort(); } } 

Example - use the utility:

Then it can enter the utility and call its method.
Here is an example in the controller:

 // inject it, @Autowired private EnvUtil envUtil; /** * env * * @return */ @GetMapping(path = "/env") @ResponseBody public Object env() throws UnknownHostException { Map<String, Object> map = new HashMap<>(); map.put("port", envUtil.getPort()); map.put("host", envUtil.getHostname()); return map; } 
0
source

If you use a random port, like the server.port=${random.int[10000,20000]} method. and in Java code read the port in Environment , use @Value or getProperty("server.port") . You will receive an unpredictable port because it is random.

ApplicationListener, you can override onApplicationEvent to get the port number after installing it.

In the boot version of Spring Implements the Spring interface ApplicationListener<EmbeddedServletContainerInitializedEvent> (boot version of Spring 1) or ApplicationListener<WebServerInitializedEvent> (boot version 2) override onApplicationEvent to get the Fact Port.

Spring boot 1

 @Override public void onApplicationEvent(EmbeddedServletContainerInitializedEvent event) { int port = event.getEmbeddedServletContainer().getPort(); } 

Spring boot 2

 @Override public void onApplicationEvent(WebServerInitializedEvent event) { Integer port = event.getWebServer().getPort(); this.port = port; } 
0
source

All Articles