Home > database >  How to make spring boot app run on alternate port?
How to make spring boot app run on alternate port?

Time:08-01

I have a spring boot (2.5.3) app running on a centOS VM behind a firewall. I normally build a fat jar, then run it with a config passed via CLI:

  1. mvn clean package spring-boot:repackage
  2. java -jar target/service.jar --spring.config.location=/path/to/config.properties
  3. run curl GET commands: curl --key /a/b --cert /x/y "https://server-name:8767/path?arg=..."

It works using port 8767 set in the config, and I chose this port a while back randomly.

Since then, I've tried to see if I could make it work with a different port. I opened more ports on the linux public firewall-cmd zone, including 8768 & 9000. Problem is that no matter what I try, the only port I can get the app to run on is 8767. Seems like I've somehow hard-wired it to that port!

Normally server.port is set in the config, but even if I pass another port --server.port=xxxx via cli, the app runs, and logs show it is exposed to xxxx; however, curl can consistently only access 8767, and other ports time out. Or if I set server.port=xxxx in the config, same outcome.

What do I need to do to use a different port? (I saw this...would it help me?)

Dependencies (nothing special) Dependencies (nothing special)

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
</parent>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
</dependency>
<dependency>
  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpclient</artifactId>
</dependency>

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
</dependency>

CodePudding user response:

Spring boot takes into account cli arguments when you pass the arguments to SpringApplication.run method in the main method. Main class should look like this -

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
           SpringApplication.run(Application.class, args);
    }
}

Pass args as argument to run method and it should take cli arguments into account. With this class, if --server.port=8080 is used as cli argument, then spring application should run on 8080 port.

CodePudding user response:

this is the order of how spring boots evaluates the different approaches of setting the server port are:

  1. embedded server configuration
  2. command-line arguments
  3. property files
  4. main @SpringBootApplication configuration

so two typical issues if the server.port parameter does not work are overridden behavior in a WebServerFactoryCustomizer or in your main method of the SpringBootApplication

  • Related