How to Centralized Microservices configuration using Spring cloud?

banner

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.

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

Microservcies are now integral to most applications.

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.

Related article

Microservice architecture is a distributed system where multiple services collaborate with one another. They typically communicate with each other using REST API.

Microservices have become one of the enablers in the digital transformation journey as it is always focused and built on Business Need" and Single Responsibility. Microservices pattern enables scalability, speed, efficiency, and agility because of De-centralized API.

In this article, we will create a .net core microservice and create a code first database using entity framework 3.1.

DMCA Logo do not copy