Spring Cloud Config Server

Posted By : Amit Mishra | 25-Nov-2020

If you are wondering how you can centralize your distributed applications' configuration in one place, this is the right post for you. Spring Cloud's config server provides you the ability to externalize your configuration in distributed systems. You can have an environment-specific configuration which makes it very flexible while moving your applications from test to the development phase. With Config server, you have a central place to manage external properties across all the applications. Config server internally uses rabbit MQ to inject the properties in running applications with the help of an endpoint called bus-refresh.

Config Server Configuration:


To get started creating a new Config Server create a new Spring Boot application either from your IDE or you can import it from SpringIO. Once you have your project in your IDE, add the following dependency in your application. 

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

Once you have a dependency in your POM, open your default SpringBootApplication's main file and add an annotation to enable support for the config server.

@EnableConfigServer
@SpringBootApplication
public class ConfigServer{
   public static void main(String...args){
      SpringBootApplication.run(ConfigServer.class, args);
   }

By default Spring's Config server uses Git backend's configuration which means you can place your properties files on a GitHub repo and this config server will use that files to load configuration properties into your services. You can specify the profiles by the following property.

spring.application.name = config-server
spring.server.port = 8088
//Here you can set your active profiles
spring.profiles.active = git(default), Vault Backend, local, bit-bucket, Bitbucket

Now, when you have understood about the profiles and learned how you can enable config server, let's enable the support to GitHub repo so that this config server can communicate with GitHub repository or any other version control services to fetch your application's configurations. Let's add the following properties to get it working. 

spring.cloud.config.server.git.uri = https://github.com/{gitUserName}/ConfigServerRepo
spring.cloud.config.server.git.username =foo
spring.cloud.config.server.git.password =bar
spring.cloud.config.server.git.clone-on-start =true/false //if true -> all the services will clone the props on startup if false -> You will have to explicitly hit endpoint.

management.endpoints.web.exposure.include = bus-refresh //Enable actualtor's bus-referesh endpoint this will tell config server to reload the properties

//You will also need to have rabbitmq running and provide the credentials for that
spring.rabbitmq.host = foo
spring.rabbitmq.port = port
spring.rabbitmq.username = foo
spring.rabbitmq.password = bar
spring.rabbitmq.connection-timeout = 1000 //To prevent timeouts.

 

Also Read: Connect Angular with Spring Boot

Config Server Client Configuration: 

To configure the client which we can call consumers as well, who will be taking property values from the version source control repository. To configure a client for your config server you just need to create a new file in src/main/resources folder which will be called bootstrap.properties that get loaded automatically when BeanFactory initializes which makes the properties in your application available during the startup.  

 

Bootstrap.properties

#Config server properties so that your application knows where it's running.
spring.cloud.config.uri = #Your Config server's URL
spring.cloud.config.name = config-server application name.

That's it, now your application will prioritize the property values in the application.properties file which is available on Github or it will pick from your local properties file. In case, you have multiple services running and you want to create specific property files for specific applications, in that case in your repo you just need to create property files with your application name for ex: user-management-application.properties where user-management-application is your application for which you're specifying properties for. 

Also Read: Building and Deploying Java Microservices On The Cloud
 

 

There are a lot more things to learn about the config server that you can find on the official Site.

 

We are an experienced Java Development Company that specializes in building microservices-based applications. Our Java developers use advanced tools and agile methodologies to build high-quality applications for popular software platforms. We have successfully built several full-fledged microservice applications for our clients using Java-based programming. Our Java application development services provide increased flexibility and agility to implement new features and performance updates. We also have vast experience in cloud computing services and we let you seamlessly deploy your Java applications on platforms like AWS, Azure, and Google Cloud.

About Author

Author Image
Amit Mishra

Amit is a spring web developer. He has good knowledge of Spring Cloud, Spring Boot, Spring MVC, Hibernate, and some template engines like jsp and thymleaf.

Request for Proposal

Name is required

Comment is required

Sending message..