I have gone through so many links and example but non of them are working. https://howtodoinjava.com/spring-cloud/spring-cloud-config-server-git/ https://www.baeldung.com/spring-cloud-configuration https://cloud.spring.io/spring-cloud-config/reference/html/#_spring_cloud_config_server
No a complete information has been shared here with versions related or any other dependencies.
CodePudding user response:
The most important information missing in all examples are the versions for a dependencies.
Spring Config Server
pom.xml file
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2020.0.4</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
application.properties file to have content
server.port=8888
spring.cloud.config.server.git.uri=file:///${user.home}/config-repo
spring.cloud.config.server.git.cloneOnStart=true
#Disable security of the Management endpoint
management.security.enabled=false
spring.security.user.name=root
spring.security.user.password=root
user.home = The windows logged in user path. example: "C:\Users\UserName"
Java class to have Enable Config annotation
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
Create a Local Repo
Create a folder "config-repo" at user folder and move to it.
git init
echo 'user.role=Developer' > config-client-development.properties
echo 'user.role=User' > config-client-production.properties
git add .
git commit -m 'Initial config-client properties'
Spring Config Client
pom.xml file
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2020.0.4</spring-cloud.version>
</properties>
<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.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
Create bootstrap.properties file instead of application.properties since we have to load information before application starts.
server.port=8800
spring.application.name=config-client
spring.profiles.active=development
spring.cloud.config.uri=http://localhost:8888
spring.cloud.config.username=root
spring.cloud.config.password=root
management.security.enabled=false
spring.cloud.config.fail-fast=true
Java class to load few sample information.
@SpringBootApplication
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
}
@RefreshScope
@RestController
class MessageRestController {
@Value("${user.role: Local value}")
private String role;
@Value("${msg:Config Server is not working. Please check...}")
private String msg;
@GetMapping("/msg")
public String getMsg() {
return this.msg;
}
@GetMapping(value = "/whoami/{username}", produces = MediaType.TEXT_PLAIN_VALUE)
public String whoami(@PathVariable("username") String username) {
return String.format("Hello! You're %s and you'll become a(n) %s...\n", username, role);
}
}
How to run the complete application now
1. Config Server:
Build the project 'mvn install'.
Run the application.
Test the endpoint : http://localhost:8888/config-client/development
You will see the JSON information, which will have your required data in source object.
2. Config Client
Build the project 'mvn install'.
Run the application. You will see the starting logs as :
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.5.1)
INFO 7796 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Fetching config
from server at : http://localhost:8888
INFO 7796 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Located
environment: name=config-client, profiles=[development], label=null,
version=0000000000000000000000000, state=null
INFO 7796 --- [ main] b.c.PropertySourceBootstrapConfiguration : Located property
source: [BootstrapPropertySource {name='bootstrapProperties-configClient'},
BootstrapPropertySource
Test the endpoint : http://localhost:8800/whoami/tester
Output will be : Hello! You're tester and you'll become a(n) Developer...
CodePudding user response:
I will introduce you Spring Cloud Config Server
with a code walkthrough of a sample application I have. In this I am using Spring Cloud Config Server
as the central repository for application configurations of my servers. The properties defined in config server is not available locally and it is stored in a separate GIT
repository. Please be aware for clarity purpose, I am not adding whole code chunk here and I will only list down the code needed for spring cloud config configuration purpose only.
Configuration file in GIT repository
This is the application.yml
in the root directory of my repository.
# Configuration file of the application configuration server (This file holds common properties for all the microservices)
# Necessary to inform the eureka server registry, which the client is going to be registered with...
eureka:
client:
service-url:
defaultZone: http://localhost:9090/eureka/
# Actuator configuration
management:
endpoints:
web:
exposure:
include: '*'
# H2 database configuration
spring:
h2:
console:
enabled: true
jpa:
hibernate:
ddl-auto: update
datasource:
platform: org.hibernate.dialect.H2Dialect
#url: jdbc:h2:mem:bookdb
driverClassName: org.h2.Driver
POM file of the config server (Part)
Added the spring cloud config server dependency. There are other dependencies which are not required for this operation but those are required for the application functions. Therefore only focus on the cloud config server dependency.
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</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>
Bootstrap class of the config server
Adding the annotation @EnableConfigServer
is an indication for spring.
/**
* Boostrap class of the library system configuration server.
* Server responsible for handling all the configurations of each service in the application.
* Eureka Discovery is included in the build.
* Therefore @EnableEurekaClient is redundant.
*/
@SpringBootApplication
@EnableConfigServer
public class LibraryServerConfigurationApplication {
/**
* Main method of the LibraryServerConfigurationApplication.
* @param theArguments String[]
*/
public static void main(String[] theArguments) {
SpringApplication.run(LibraryServerConfigurationApplication.class, theArguments);
}
}
Property (application.yml) file of the config server
Only focus on the configuration related to cloud -> config -> server -> git -> uri, where it is mentioned the path for the GIT
repository of configuration. Please modify the URI
and put the relevant one of yours.
# Server port
server:
port: 9092
# Application configuration files are located in Git repository
spring:
# Spring application name
spplicatioin:
name: config-server
cloud:
config:
server:
git:
uri: https://github.com/<URL_OF_YOUR_REPOSITORY> # Git repository path
That is all you need to do in the config server level. Now let's create a Spring Cloud Config Client
to be able to fetch configuration from the config server created.
Spring cloud config client POM file (Part)
Add the following dependency into your services which are looking forward to be a config client and consume configurations via config server.
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
Property file changes of config client
Please be aware that the below application properties are not in YML
and you are free to use YML
as you wish. The below configuration properties pinpoints the spring config server to the spring config client.
# Configuration client
spring.cloud.config.import=optional:configserver:http://localhost:9092/
spring.config.import=optional:configserver:http://localhost:9092/
management.endpoints.web.exposure.include=*
Well now we are done! This is enough and we are having a fully functioning spring config server and a spring config client. This is just a demonstration and please be advised this configuration can get very complicated and sophisticated to be fit for different needs. You can maintain multiple application property files in the GIT
repository as well. This is surely not giving you all but hope this helps to give you enough to start and discover. Happy coding!