Technology:
Spring Cloud Config provides server and client modules for supporting externalized Configuration in a distributed system. With Config server, spring bootmicroservices configuration centralized across all environments.
The concept is similar to Environment, PropertySource in client applications. If the application moves from one profile to another profile, spring boot applications need not restarted, without downtime microservices will switch from one profile to another.
The default storage for these externalizes properties are git version system, as it supports branching, different properties for each configuration environment.
Spring Cloud Config Features:
Spring cloud Config provides the Rest API we can easily access the shared properties.
Encrypt and Decrypt configuration properties
Spring boot developers provides starter application, enable using @EnableConfigServer Spring cloud Config provides 2 ways to centralize the property source, one using the local properties file, other using git repository.
By default, the git repository used by default in the spring cloud config framework.
Project setup:
Add the below property in properties section.
Add the below dependencyManagement in pom.xml
And add the below dependency:
@EnableConfigServer is the annotation is used to bootstrap the config server,add this annotation in any of the classes annotated with @Configuration or @SpringBootApplication.
spring.cloud.config.server.git.uri is the property used to configure the git repository.
JGitEnvironmentRepository is the class used to clone the provided git repository into the %TEMP% folder and start serving using these properties.
These are the endpoints provided by the spring config server, where the application injected as spring.config.name in the spring application. Normally application is the regular spring boot application the label is the git branch default to master.
When this HTTP server runs, it picks up the external configuration from the default local config server. By default, if no application name set, the application will be used. To modify name, the following property can add to bootstrap.properties file:
The bootstrap properties show up in the /env endpoint as a high-priority property source
Main class if spring cloud config java file look like below:
Skipping Git Server SSL Certification Validation:
In case if we are using local git server, we can skip SSL validation by using below property,
Setting HTTP Connection Timeout
We can configure the time in seconds the configuration server will acquire an HTTP Connection.
Authentication:
To use HTTP basic authentication on the remote repository, we can configure the username and password properties on the configuration file.
spring:
cloud:
config:
server:
git:
https://github.com/sravan4rmhyd/spring-config-server
username: admin
password: adm1n
Refresh Repository:
We can control how often the config server will pull updated configuration data from your Git backend by using spring.cloud.config.server.git.refreshRate. The value of this property specified in seconds. By default the value is 0, meaning the config server will fetch updated configuration from the Git repo every time it is requested.
Serving Configuration from GIT subpath:
Suppose based on requirement if need to load configuration properties from git repository subfolder then use spring.cloud.config.server.git.search-paths property as follows:
spring:
cloud:
config:
server:
git:
https://github.com/sravan4rmhyd/spring-config-server
search-paths: config
If we want to load from multiple subfolders, we can provide comma separated values.
Your organization needs this upgrade with spring cloud. As enterprise solution providers, we offer this solution to support your configurations. Get in touch with us for the upgrade.
Serving Configuration from A Branch or Label:
By default, spring config server will load properties from a master, but we can configure this using spring.cloud.config.server.git.default-label property.
Serving application and profile specific properties:
Spring Config server can also provide the application and application profile based properties to its client.
Create a file named bookservice.properties(yml) file, so that these properties file only available to bookservice application named microservice, these properties will override the default configuration file application.properties(yml) file.
We can also load profile specific properties using {application}-{profile}.properties(yml) file format. Suppose if a file named bookservice-default.properties file is present, then it will be loaded only if none of the profiles are configured, bookservice-dev.properties will load bookservice service only if he is running in dev as an active profile in bookservice application.
Spring config server also supports Vault storage, which is used to storage the secured properties, like passwords, and SSL certificates, etc.
Conclusion:
Spring Cloud Config server is the framework used to provide features to load and share with other microservice. Here, the best java programmer described different configuration properties file-related config server configuration. Also, we learned how to get application profile-based configuration properties using the config server and load the configuration properties other than the master branch.