Spring Cloud Config : POC

I have done a simple implementation of spring cloud config. All my implementation is done on  Spring Boot. So if you are not familiar with spring boot I recommend please get the basic knowledge of it first.

Here we start. Just one more thing Pivot guys made a portal by which you can download ready made application which is at Start Spring .

I have downloaded all my ready made project from here only.

Let’s get some theoretical knowledge of Cloud config.

It is centralized based configuration management. The configuration we normally store in the properties file in the project to load at runtime.

Here are some Key Point of Cloud Config.

  • Spring Cloud Config provides server and client-side support for externalized configuration in a distributed system
  • The concepts on both client and server map identically to the Spring Environment and PropertySource abstractions, so they fit very well with Spring applications, but can be used with any application running in any language
  • As an application moves through the deployment pipeline from dev to test and into production you can manage the configuration between those environments
  • The default implementation of the server storage backend uses git so it easily supports labeled versions of configuration environments, as well as being accessible to a wide range of tooling for managing the content. It is easy to add alternative implementations and plug them in with Spring configuration.

Config Server :

  • Create a web, config server project from start.spring.io .
  • Go to your main application class
  • Put @EnableConfigServer at class.
  • By default, if you run this class as java application it will start tomcat on 8080 port so do following conventional changes like port 8888

application.properties (under resources folder)

server.port: 8888

spring.cloud.config.server.git.uri: file://${user.home}/config-repo

where ${user.home}/config-repo is a git repository containing YAML and properties files.Example : Creating the git repository

$ cd $HOME
$ mkdir config-repo
$ cd config-repo
$ git init .
$ echo info.foo: bar > application.properties
$ git add -A .
$ git commit -m "Add application.properties"

Above code will create a local git repo named as config-repo and add a file appilcation.properties to it with following properties in it

info.foo=bar

by spring.cloud.config.server.git.uri we map this repo to fetch configurations.

Server Application class will look like this :

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}

application.properties

server.port=8888
spring.config.name=configserver
spring.cloud.config.server.git.uri: file://${user.home}/config-repo

Start your server.

Client :

  • Create a web, config server project from start.spring.io .
  • Go to your main application class
  • Put @RestController on application class

Add following in your application class

@RequestMapping("/")
    public String home() {
        return "Hello World!";
    }

Property mapping :

@Value("${info.foo}")
private String value;

Client Application will look like this

@SpringBootApplication
@RestController
public class ConfigClientApplication {

    @Value("${info.foo}")
    private String value;
    
    @RequestMapping("/")
    public String home() {
        return "Hello World! "+value;
    }
    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication.class, args);
    }
}

application.properties

spring.cloud.config.uri: http://localhost:8888

Start your client

As our client is running at 8080 port

http://localhost:8080/

output :

Hello World! bar

And out server is running at 8888 port

http://localhost:8888/

Thank you!!

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: