Home > OS >  Finding Port from java Spring Boot on localhost
Finding Port from java Spring Boot on localhost

Time:03-10

I created standalone version Spring Boot App which on started in localhost random port. Also I created desktop app (Java FX) that should connect to my app in localhost, but I ran into a problem finding the port on which the application started. How can I find a port using Java?

CodePudding user response:

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

CodePudding user response:

See how to set the server port, then connect from your client to the port you set on your server.

You have to either:

  1. 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

  2. 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:

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.

  • Related