CentralizedMicroservices configuration using Spring cloud config

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.

microservices-java

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 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.

<spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>

Add the below dependencyManagement in pom.xml

<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>

And add the below dependency:

<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </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.

/{application}/{profile}[/{label}] /{application}-{profile}.yml /{label}/{application}-{profile}.yml /{application}-{profile}.properties /{label}/{application}-{profile}.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:

spring.application.name: myapp

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:

@SpringBootApplication @EnableConfigServer publicclassConfigServerApplication { publicstaticvoid main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } }

Skipping Git Server SSL Certification Validation:

In case if we are using local git server, we can skip SSL validation by using below property,

spring.cloud.config.server.git.skipSslValidation=true

Setting HTTP Connection Timeout

We can configure the time in seconds the configuration server will acquire an HTTP Connection.

spring.cloud.config.server.git.timeout=5

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.

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 is used to provide feature to load and share with other microservice, and aegis soft tech best java programmer describethe different configuration properties file related config server configuration, and also we learned how to get application based, application profile based configuration properties using config server, and also we can load the configuration properties other than master branch.

Download the source code from here