I am implementing Spring Cloud config, but the clients are not fetching the config automatically... When I start up my client gets the data from the server properly, but never retrieves it again.
This is my CloudConfig Server.
@EnableDiscoveryClient
@EnableConfigServer
@SpringBootApplication
public class CloudConfigApplication {
public static void main(String[] args) {
SpringApplication.run(CloudConfigApplication.class, args);
}
}
aplication.yml
eureka:
client:
serviceUrl:
defaultZone: http://localhost:9091/eureka
server:
port: 8888
spring:
application:
name: config-server
cloud:
config:
server:
bootstrap: true
health:
enabled: true
native:
searchLocations: file:${user.dir}/src/main/resources/configs/
profiles:
active: native
Pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>CloudConfig</groupId>
<artifactId>CloudConfig</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>CloudConfig</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR3</spring-cloud.version>
</properties>
<dependencies>
<!-- 3a) Dependency for spring-cloud-config-Server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!-- 3b) Dependency for testing boot projects -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
/src/main/resources/config/CloudConfigClient.yaml
test: aaaaa
test2: 1111
SpringCloud Client
@EnableScheduling
@EnableDiscoveryClient
@SpringBootApplication
@EnableAutoConfiguration
public class CloudConfigClient {
@Value("${test}")
private String myProperty;
public static void main(String[] args) {
SpringApplication.run(CloudConfigClient.class, args);
}
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}
@Scheduled(fixedDelay =1000)
public void printProperty() {
System.out.println("Value of property \"myProperty\": " + myProperty);
}
}
bootstrap.yaml
eureka:
client:
serviceUrl:
defaultZone: http://localhost:9091/eureka
server:
port: 7004
datasource:
driver-class-name: org.h2.Driver
application:
name: CloudConfigClient
cloud:
config:
uri: http://localhost:8888
fail-fast: true
max-attempts: 20
max-interval: 15000
initial-interval: 10000
Pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
This is the ouput log:
2020-11-20 23:33:12.747 INFO 16764 --- [ restartedMain] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://localhost:8888
2020-11-20 23:33:13.510 INFO 16764 --- [ restartedMain] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=CloudConfigClient, profiles=[default], label=null, version=null, state=null
2020-11-20 23:33:13.510 INFO 16764 --- [ restartedMain] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource {name='configService', propertySources=[MapPropertySource {name='file:D:/CloudConfig/src/main/resources/configs/CloudConfigClient.yaml'}]}
...
...
Value of property "myProperty": aaaaa
Value of property "myProperty": aaaaa
if you want your config to be refresh you have to use @RefreshScope spring doc the config won't get update by magic himself because you call @Scheduled.
But base on how you fetch the value in Configuration component they might be limitation and @RefreshScope will not work refreshscope
I suggest create a bean a @ConfigurationProperties and annotate with @RefreshScope when you hit from actuator endpoint /refresh everything should update or maybe combination of @Schedule you might achieved what you want