Spring Config Server
Spring Config Server
Spring Config Server
development (mainly service API). Microservices approach now has become an industry standard for any new API
development and almost all the organization is promoting this. Today we will discuss and do some demo on a
specific Microservice feature called Config Server. It is like externalizing properties/resource file out of project
code base to an external service altogether, so that any changes to that property does not have any impact in the
actual service which is using that – no code change and no deployment – so the approach is very much justifiable.
The idea has come from the original 12 factor app[https://12factor.net/config] manifesto related to modern cloud
native approach of application development. As per the 12-factor app manifesto, it is suggesting to keep
properties/resources in the environment where the values of those resources vary during run time.
As an example, let’s say one service is dependent on another service [invoked for certain business scenario] and if
that dependent service URL got changed to some other service, and then usually we need to build and deploy our
service with the updated location is required. Now if we go by 12 factor app suggestion and if we read those config
from external service deployed as different process, then we just need to refresh that config server.
So, the idea is very clear and effective. Let’s now see how we can achieve this.
We will do this using spring-boot based Spring-cloud api that is available and very popular. Also, we will use both
local and git configuration to place the properties file.
1. Java 1.8
2. Spring cloud
3. Spring boot
4. Spring rest
5. GitHub as resource repository.
6. Maven
7. Eclipse IDE
8. Unrestricted internet connectivity for maven download.
package com.howtodoinjava.example.springconfigserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@EnableConfigServer
@SpringBootApplication
public class SpringConfigServerApplication {
Create the Git repository – Next very important step is to create a local git repository. We can easily be
converted to a remote repository later by configuring it’s URL in the properties file. We will place the
external property file [configuration], that will be used by the Config server microservice to provide the
external configuration of properties. We need to follow the below steps to create a local git repository and
check in a sample properties file.
1. Make sure you have git shell installed in your machine and you can run git bash from
command prompt. To verify it open command prompt and type git, if it recognize then you
probably have the git prompt installed, if not please follow git website, download and install
as per the instruction.
2. Now Create a directory ‘config-server-repo’ in your Desktop.
3. Then create a file config-server-client.properties file in the config-server-repo directory and
add the message there msg = Hello world - this is from config server.
4. Then create another file config-server-client-development.properties file in the config-server-
repo directory and add the message there msg = Hello world - this is from config server –
Development environment.
5. Then create another file config-server-client-production.properties file in the config-server-
repo directory and add the message there msg = Hello world - this is from config server –
Production environment.
6. Here we are maintaining same properties for different environment, as we generally maintain
properties for different environments like url/credentials, Database details etc. Here the most
important point is that we need to append hyphen (-) with the environment name in each
property so that config server understands it. Also, we need to name the properties file with
the config client service name that we will create after this.
7. Now open command prompt from ‘config-server-repo’ directory and run command git init to
make that directory as git repository
8. Now run git add . to add everything to this repo.
9. Then finally we need to commit the properties file by running command git commit –m “initial
checkin”. This should check in all the files in the git repository. Here is the command prompt
screen shot for the same.
Point the git repo from Config Server – Create one file called bootstrap.properties in the
src\main\resources directory of spring-config-sever project and add below lines.
#Server port
server.port = 8888
#Git repo location.
spring.cloud.config.server.git.uri=${USERPROFILE}\\Desktop\\config-server-repo
#Disable security of the Management endpoint
management.security.enabled=false
management.security.enabled=false will disable the spring security on the management enpoints like
/env, /refresh etc. This is for development settings, in production security should be enabled.
So, this step will point to a git location and server port.
This is very much we need to do in the config server side, not do a final clean install command on this
project so that everything gets compiled properly and packaged also in the target folder as well as in local
maven repository. We will start the config server service once we have the client part ready and we will
finally test the feature.
b. Now open browser and check below Urls, it will return the JSON output and in propertySources
section we can see all the properties we have added in the properties. This ensures that config-
server is running successfully, it has recognized the git location and it is serving configuration for
different environments. Now we will proceed to the client side implementation where we will use
those properties from a separate microservice which is our final goal – to externalize the
configuration to different service.
http://localhost:8888/config-server-client/development
http://localhost:8888/config-server-client/production
c. Check if any runtime change in the property file is reflected by the server without restart – Do
any change in the value of any environment’s property and check-in that file and then run that
specific environment’s endpoint, and verify that changed value should be reflected immediately
without restarting the server – that is the magic of Spring Config Server. To do the git check in,
after doing the change and save the file by any text editor, run the command git add. And git
commit -m “test” from config-server-repo directory in the desktop. This step ensures that we
don’t need to restart the server for any configuration change for the server side.
Maven project generation – go to https://start.spring.io/ web portal and generate client project with the
below selected artifacts
1. Actuator
2. Config Client
3. Web
4. Rest Repositories
The screen will look like below before generation, once we click on generate, we will get the artefactid.zip
file download option. Like Spring-Config-Server, unzip the file in some directory and import in eclipse.
Add one RestController to view the Server side property values in the response. To do that open the
@SpringBootApplication class file that has been generated, and add the below small class in the end of
that file. This is very simple and straight forward, we are just exposing one method at /message URL where
we will just return the property value of msg that will be supplied by the config server microservice, which
is configured to a local git repository [which will be migrated to a remote git repository in production!]
package com.howtodoinjava.example.springconfigclient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
public class SpringConfigClientApplication {
@RefreshScope
@RestController
class MessageRestController {
Bind with the config server – Create one file called bootstrap.properties in the src\main\resources
directory and add the below properties to connect with the config server along with some required
configuration.
spring.application.name=config-server-client
#Active Profile - will relate to development properties file in the server.
#If this property is absent then,default profile will be activated which is
#theh property file without any environment name at the end.
spring.profiles.active=development
# N.B. this is the default:
spring.cloud.config.uri=http://localhost:8888
management.security.enabled=false
management.security.enabled=false will disable the spring security on the management enpoints like
/env, /refresh etc. This is for development settings, in production security should be enabled.
This is very much we need to do in the config client side, not do a final clean install command on this
project so that everything gets compiled properly and packaged also in the target folder as well as in local
maven repository. We will start the config client service along with the server side and we will finally test
the feature.
Testing
5. Now we will do a property change and test if this can be reflected in the config client service without
restarting any of the microservices.
Do some change, in the value of the msg property in the config-server-client-development.properties and
check-in in the local git, then hit the http://localhost:8080/msg again in the browser, the VALUE SHOULD
BE THE OLD VALUE ONLY!!!
To reflect the new value, we need to refresh the configuration by hitting http://localhost:8080/refresh
endpoint in POST method from any of the REST client.
Once you have successfully refreshed the config client service, the new value should be reflected in the
service response. This is because, @RefreshScope annotation the RestController that we have
exposed.
1. Property files name and the Client Module service name [ spring.application.name=config-server-
client] should be exactly same, otherwise, properties will not be detected. Actually, Config Server exposes
the properties in an end point of property file name, if you browse URL http://localhost:8888/config-server-
client/development it will return all the environment values.
2. Make sure you have checked-in the properties files in the git reop by using git init/add/commit commands as
described above.
3. Make sure you have refreshed the client service environment by invoking POST method of
http://localhost:8080/refresh by any REST client. Otherwise changed values will not be reflected in the client
service.
4. Make sure at the time of starting the client service, your server service is running, otherwise it might take
some time to register.
That’s all about this topic. Please add comments if you have able to configure all the points mentioned in this
article, we will be happy to look into the problem.
Happy Learning!!!