skip to Main Content

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>

UPDATE: @Vinit – my main class is exactly like yours, except a std println I have to let me know it’s running:

System.out.println("Running...");

As for my application.properties, I cannot paste them as I’m behind a firewall, but they are basically below, and there are more than one of each:

logging.level
server.port=xxxx  // as described above, i've tried declaring here or cli
server.ssl
# custom auth properties
customauth.url
spring.profiles.active
spring.application.name
spring.task.scheduling
spring.jmx.enabled
swagger
management.endpoints
sanitization
spring.jackson

On another note, I run

sudo netstat -nlp | grep "<port>"

…before I run the app (where is the port I have either in my config or passed CLI), and no results. Then I run the app, repeat the netstat call, and that port is listening sure enough. But same thing: if 8767, all is well; but if 8768, time out.

2

Answers


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

    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search