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


Related Posts


Got Something To Say:

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

*