Is it also possible to access the management port in a similar way, e.g.:
@SpringBootTest(classes = {Application.class}, webEnvironment = WebEnvironment.RANDOM_PORT)
public class MyTest {
@LocalServerPort
int randomServerPort;
@LocalManagementPort
int randomManagementPort;
Answer from LaughingLemon on Stack OverflowIs it also possible to access the management port in a similar way, e.g.:
@SpringBootTest(classes = {Application.class}, webEnvironment = WebEnvironment.RANDOM_PORT)
public class MyTest {
@LocalServerPort
int randomServerPort;
@LocalManagementPort
int randomManagementPort;
Spring's Environment holds this information for you.
@Autowired
Environment environment;
String port = environment.getProperty("local.server.port");
On the surface this looks identical to injecting a field annotated @Value("${local.server.port}") (or @LocalServerPort, which is identical), whereby an autowiring failure is thrown at startup as the value isn't available until the context is fully initialised. The difference here is that this call is implicitly being made in runtime business logic rather than invoked at application startup, and hence the 'lazy-fetch' of the port resolves ok.
localhost:<your port> may not respond at all. Make sure you're hitting an endpoint you know is availible like localhost:8080/info. Make sure your app has completly started, it could take serveral minutes.
As @BrianC mentioned make sure your app is actually a web server.
in your springboot main method use below code to detect port of your application
Environment environment =app.run(args).getEnvironment();
System.out.println(environment.getProprty("server.port");
this will tell you actual port where your aplication running .
You can get this information via Environment for the port and the host you can obtain by using InternetAddress.
@Autowired
Environment environment;
// Port via annotation
@Value("${server.port}")
int aPort;
......
public void somePlaceInTheCode() {
// Port
environment.getProperty("server.port");
// Local address
InetAddress.getLocalHost().getHostAddress();
InetAddress.getLocalHost().getHostName();
// Remote address
InetAddress.getLoopbackAddress().getHostAddress();
InetAddress.getLoopbackAddress().getHostName();
}
If you use random port like server.port=${random.int[10000,20000]} method. and in Java code read the port in Environment use @Value or getProperty("server.port"). You will get a Unpredictable Port Because it is Random.
ApplicationListener, you can override onApplicationEvent to get the port number once it's set.
In Spring boot version Implements Spring interface ApplicationListener<EmbeddedServletContainerInitializedEvent>(Spring boot version 1) or ApplicationListener<WebServerInitializedEvent>(Spring 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;
}
See how to set the server port, then connect from your client to the port you set on your server.
You have to either:
Follow a convention to know the port you are connecting to.
- The convention followed may be either the standard port assignment convention, or a custom assignment convention that you establish.
OR
Publish the port (also usually an ip or dns) to a separate lookup service. Then use the lookup service to locate the target service location.
I recommend you just follow convention unless you have large scale complicated interconnected microservices (which you don’t).
The convention would be:
- public http (including rest services or soap services) unecrypted on port 80 and encrypted on 443.
- Internal http services use 8080 unencrypted and 8443 encrypted.
For an example of dynamic service discovery, see:
- hasicorp consul.
In particular the “Introduction to HashiCorp Consul” video section from minutes two to eight on service discovery. Systems like Kubernetes (adopted by Google Cloud), adopt a similar architecture.
There are other Java based network service discovery systems such as the (defunct) jini.
But, using rest services on the common ports is the standard way that common rest and web services are generally exposed.
One reason for that is that public access to those ports, 80 and 443, are usually allowed by public facing firewalls, but access to most other ports is disallowed.
The three most difficult things in computing are naming things and off by one errors. Finding things is the fourth.
For your question, you can reference this question: spring-boot-how-to-get-the-running-port, then print server port when startup.
And I think set a fixed port could be beeter like server.port=8080 in application.properties
You can autowire port number in any component class in following way
// Inject which port we were assigned
@Value("${local.server.port}")
int port;
Or with the annotation @LocalServerPort
@LocalServerPort
private int port;
And host address with following
String ip = InetAddress.getLocalHost().getHostAddress()
If you want to get it after application running try with this:
@Component
public class ApplicationLoader implements ApplicationRunner {
@Autowired
private Environment environment;
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println(environment.getProperty("java.rmi.server.hostname"));
System.out.println(environment.getProperty("local.server.port"));
System.out.println(InetAddress.getLocalHost().getHostAddress());
}
}
You can get port many ways:
@Value("${local.server.port}")
private int serverPort;
or
@LocalServerPort
private int serverPort;