Application server port cannot be overridden by JVM parameter -Dserver.port if the property is present in Spring Cloud Config. I'm using Spring cloud Greenwich.SR3 version and Spring Boot 2.1.9.RELEASE
The sample application could look like this
@SpringBootApplication
public class RandomPortApplication {
public static void main(String[] args) {
SpringApplication.run(RandomPortApplication.class, args);
}
}
with bootstrap.yml like
spring:
application:
name: random-port
cloud:
config:
discovery:
enabled: true
and with the pom.xml
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
<build>
<resources>
<!-- make sure that configs are not packaged and read them from Cloud Config Server -->
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>application*</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
This requires Eureka server running with default config and Cloud Config server having the following application.yml for our service
server:
port: 0
If I run such service with
mvn spring-boot:run -Dserver.port=7777 -Deureka.client.serviceUrl.defaultZone="http://localhost:8761/eureka/"
The service uses random port instead of the provided one!
The things that I notice
- if I move server.port=0 to bootstrap.yml, the port can be overridden
- or if I add dependency org.springframework.cloud:spring-cloud-config-server (although does not make sense to have this dependency) it works fine, I can override the port
Comment From: spencergibb
Try setting spring.cloud.config.override-system-properties=true. See https://cloud.spring.io/spring-cloud-static/Hoxton.RELEASE/reference/html/spring-cloud-hoxton-configprops.html
Comment From: dome313
Hello @spencergibb tried with override-system-properties and also doesn't work.
I created a sample repo here: https://github.com/dome313/random-port with only Cloud Client and Cloud Config Server which shows that is not possible to override port with JVM property -Dserver.port
Comment From: spencergibb
spring.cloud.config.override-system-properties=true needs to be in a property source returned from config server.
Comment From: dome313
Adding
spring:
cloud:
config:
override-system-properties: false
to a property source returned from config server did the trick. I updated my git repo to reflect that.
Thanks @spencergibb. You can close the ticket.