How to start with Spring Cloud Centralized Configuration

Spring Cloud provides an excellent mechanics to refresh all configured properties of a Spring Bean by @RefreshScope. So in the tutorial, JavaSampleApproach will introduce way to config Spring Cloud Centralized Configuration and the benifits of @RefreshScope.

I. Technologies

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot: 1.4.3.RELEASE

II. Overview
1. Project Structure

spring cloud - project structure

2. Step to do

– Create SpringBoot projects
– Configure for Spring Cloud Server
– Configure for Spring Cloud Client
– Create a RestController with RefreshScope
– Run & Enjoys Result

III. Practices
1. Create SpringBoot projects

Create 2 Spring Boot projects for Spring Cloud Server & Spring Cloud Client

Open Spring Tool Suite, on main menu, choose New->File->Spring Starter Project, input project info. Press Next, then Finish. Spring Boot project will be created.

For Spring Cloud Server, add Cloud Config Server dependency:

For Spring Cloud Client, add dependencies: cloud-starter-config, spring-boot-starter-actuator & spring-boot-starter-web:

2. Configure Spring Cloud Server

– Create a sampleclient.properties file in: src/main/resources, with simple content:
message = Hello world!.
– In application.properties, add config:
spring.profiles.active=native
server.port=8888
spring.cloud.config.server.native.searchLocations=classpath:/

We have many choices to define a repository for Cloud Server: Git, Svn, Filesystem. For simple approach, in the tutorial, Repository is a FileSystem by configs:
spring.profiles.active=native
spring.cloud.config.server.native.searchLocations=classpath:/

3. Configure Spring Cloud Client

Client need pointers to the Cloud Server:
spring.application.name=sampleclient
spring.cloud.config.uri=http://localhost:8888

4. Create RestController with RefreshScope

Create a simple RestController for loading the config properties:

@RefreshScope is used to force a bean to refresh its configuration – to pull updated values from the Config Server.

5. Run & Enjoys Result

Run with Spring Boot App mode by order:
1. Run Cloud Server
2. Run Cloud Client

Results:

– Request 1
Get all configs on Server side
http://localhost:8888/sampleclient/default
Spring Cloud Centralized Configuration - server default request

– Request 2
http://localhost:8080/message
Spring Cloud Centralized Configuration - client get msg request

Modified message in sampleclient.properties file: message = Modified Message!

– Request 3
http://localhost:8888/sampleclient/default
Spring Cloud Centralized Configuration - server see modified request

– Request 4
http://localhost:8888/refresh
Spring Cloud Centralized Configuration - client post refresh request

– Request 5
http://localhost:8080/message
Spring Cloud Centralized Configuration - client get modified msg request

IV. Source Code

SpringCloudConfigClient
SpringCloudConfigServer



By grokonez | January 24, 2017.

Last updated on June 20, 2017.



Related Posts


2 thoughts on “How to start with Spring Cloud Centralized Configuration”

  1. If the application is not hosted in cloud, then can we make use of spring config server to have externalized configuration. We are having spring batch jobs. Is this approach recommended for batch jobs.?

    1. Hello Pooja,

      Remmeber that SpringCloud does NOT mean ‘hosted services on Cloud’. SpringCloud helps us to build the common patterns in distributed systems.

      For your system, you can use Spring Cloud Centralized Configuration to central all your system config for batch job.

      Or it may be useful for your system if designed with Client Load Balancing, refer more at Spring Cloud Ribbon.

      When you want control Routing and Filtering with RestAPI requests in your system, you can use SpringCloud Zuul.

      Regards,
      JSA

Got Something To Say:

Your email address will not be published. Required fields are marked *

*