I created an instance of cloud config server using the following dependencies and properties:
<properties>
<java.version>17</java.version>
<spring-cloud.version>2023.0.0</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>3.2.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.jayway.jsonpath/json-path -->
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.9.0</version>
</dependency>
</dependencies>
I enables the config server:
@SpringBootApplication
@EnableConfigServer
public class CloudConfigServiceApplication {
public static void main(String[] args) {
SpringApplication.run(CloudConfigServiceApplication.class, args);
}
}
So far so good. Now I also passed several configuration in application.properties:
server.port=8888
spring.application.name=cloud-config-service
# https://cloud.spring.io/spring-cloud-config/multi/multi__spring_cloud_config_server.html#_git_ssh_configuration_using_properties
spring.profiles.active=git
spring.cloud.config.server.git.username=my@email.com
spring.cloud.config.server.git.password=ghp_tokenGenerated
spring.cloud.config.server.git.uri=https://github.com/me/spring-cloud-remote-configs.git
spring.cloud.config.server.git.clone-on-start=true
spring.cloud.config.server.git.force-pull=true
spring.cloud.config.server.git.default-label=master
spring.cloud.config.server.git.basedir=applicationName
logging.pattern.file=%d{dd-MM-yyyy HH:mm:ss.SSS} [%thread] %-5level %logger.%M - %msg%n
logging.pattern.dateformat=yyyy-MM-dd HH:mm:ss
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} %green([%thread]) %highlight(%level) %logger - %msg%n
logging.file.name=webapps-logs/cloud-config-service.log
logging.logback.rollingpolicy.max-history=5
logging.logback.rollingpolicy.max-file-size=1250MB
logging.level.root=INFO
logging.level.fr.dsidiff=INFO
logging.level.org.springframework=INFO
management.endpoints.web.exposure.include=*
management.endpoint.health.enabled=true
management.endpoint.health.show-details=always
Actually, the files are cloned in the same folder in applicationName/applicationName When application is started I noticed a warning message:
WARN org.springframework.cloud.config.server.environment.EnvironmentController - Error getting the Environment with name=dsipilot profiles=discovery-service label=default includeOrigin=false
org.springframework.cloud.config.server.environment.NoSuchLabelException: No such label: default
The second step consists on using the configuration in other micro services. Here is my application.yml for a webservice using configuration:
server:
port: ${PORT:0}
forward-headers-strategy: framework
spring:
application:
name: users-service
cloud:
config:
enabled: true
name: ${spring.application.name}
rabbitmq:
host: localhost
username: guest
password: guest
port: 5672
config:
import: "optional:configserver:${CONFIG_SERVER:http://localhost:8888}"
datasource:
username: root
password: root
url: jdbc:mysql://localhost:3306/users?autoReconnect=true&useSSL=false&createDatabaseIfNotExist=true
jpa:
properties:
hibernate:
column_ordering_strategy: legacy
hibernate:
ddl-auto: update
show-sql: 'true'
database-platform: org.hibernate.dialect.MySQLDialect
devtools:
restart:
enabled: true
eureka:
client:
serviceUrl:
defaultZone: ${EUREKA_SERVER:http://localhost:8761/eureka}
instance:
preferIpAddress: true
instance-id: ${spring.application.name}:${spring.application.instance_id:${random.value}}
logging:
pattern:
level: "%5p [${spring.application.name}, %X{traceId:-}, %X{spanId:-}]"
dateformat: yyyy-MM-dd HH:mm:ss
file:
name: webapps-logs/users-service.log
logback:
rollingpolicy:
max-history: '5'
max-file-size: 250MB
level:
root: WARN
fr.webservices: INFO
org.springframework: INFO
org.hibernate: INFO
management:
endpoints:
web:
exposure:
#include: openapi, swagger-ui, busrefresh
include: '*'
endpoint:
health:
enabled: true
show-details: always
tracing:
sampling:
probability: 1
zipkin:
tracing:
endpoint: http://localhost:9411/api/v2/spans
users-ws:
message: "message from Localhost"
My webservice contains the following dependency:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
For test purpose I added the following controller method:
@GetMapping
public String status() {
String status = "Users service > Working on port " +
env.getProperty("local.server.port") +
"Current message: \r\n " + env.getProperty("users-ws.message");
log.info(status);
return status;
}
In the git repository I do have a users-service.yml file in the directory applicationName on github.
This file contains a different message:
users-ws:
message: "message from Github"
My issues are the followings:
I simply replace
spring.cloud.config.server.git.basedir=applicationName
With
spring.cloud.config.server.git.search-paths=applicationName
I simply didn't pay attention when I set my properties.