Search code examples
javaspringspring-bootspring-cloudspring-cloud-config

Configuration from Spring Config Server overrides server port vm argument


I have the following services:

  1. Spring Cloud Config Server
  2. Eureka Discovery Service
  3. Event Service (spring boot app)

I use "Config First" mode. It means I start Config Server first and after that I start Discovery Service.

Then I run event service. It takes configuration from Config Server. In configuration I specify server.port property equals to 8081.

I see that my event service is being registered in discovery service.

The issue comes when I'm trying to start one more instance of event service. To run it on a different port, I use -Dserver.port vm argument. So my command looks like:

java -jar event-service.jar -Dserver.port=8082

But application fails to start, saying that 8081 is already in use. It seems like event service uses configuration from config server and this configuration takes precedence over VM arguments. But I was thinking that it should be vice-verca.


Solution

  • The order of your command line arguments is wrong: the system variable must be before the jarfile:

    $ java -jar -Dserver.port=8082 event-service.jar
    

    3 ways to specify properties from the command line

    • Environment variable: $ server_port=8082 java -jar event-service.jar
    • System variable: $ java -jar -Dserver.port=8082 event-service.jar
    • Command line argument: $ java -jar event-service.jar --server.port=8082

    Notice that for environment variable, the dots are replaced with underscores.

    source: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html