DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Distributed Tracing System (Spring Cloud Sleuth + OpenZipkin)
  • Component Tests for Spring Cloud Microservices
  • A Robust Distributed Payment Network With Enchanted Audit Functionality - Part 2: Spring Boot, Axon, and Implementation
  • 7 Microservices Best Practices for Developers

Trending

  • A Deep Dive into Tracing Agentic Workflows (Part 1)
  • From APIs to Actions: Rethinking Back-End Design for Agents
  • Why AI-Generated Code Breaks Your Testing Assumptions
  • Retesting Best Practices for Agile Teams: A Quick Guide to Bug Fix Verification
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Setting Up a Spring Cloud Config Client

Setting Up a Spring Cloud Config Client

Learn how to create a configuration client which can communicate with a configuration server to access a git repository and fetch configuration files.

By 
Manish Gupta user avatar
Manish Gupta
·
Dec. 31, 20 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
20.4K Views

Join the DZone community and get the full member experience.

Join For Free

This post is a continuation of another post of mine, "How to Setup the Spring Cloud Config Server With Git," i.e., setting up of Spring cloud config server with a git repository. In my earlier post, successful communication was established between a cloud config server and a git repository. In this post, the focus will be on the development of a cloud config client and its interaction with a cloud config server.

Prerequisites

  • Java 1.8+  
  • Maven
  • Eclipse with Spring Tools Suite (STS)
  • Git
  • Spring Cloud Config Server

Cloud Config Client Setup

  1. Create a new Spring Starter Project with the name "HelloServiceClient". 
  2. On the "Next" screen, select Spring version 2.3.7 with dependencies like Spring Web (for REST Controller development), Config Client (to make it a cloud config client), Actuator (for easy monitoring), and DevTools (for easy redeployment). Click "Next" to create a project. If you choose to work in Spring v.2.4+, then "Cloud Bootstrap" will be added as an additional dependency.  
  3. Once the project is created, go to the resource folder and delete the application.properties file. The application specific properties will be read from the git repository, so this file is no longer required. You will to add a new file, bootstrap.properties, in place of application.properties inside the resource folder. This file will be loaded during the service start process and will be used to access the cloud config server. 
  4. The bootstrap.properties file will have following properties:
    Properties files
     




    x


     
    1
    spring.application.name=hello-service
    2
    server.port=8080
    3
    spring.profiles.active=development
    4
    spring.cloud.config.uri=http://localhost:8888


    • spring.application.name - This is the identifier for the microservice application and there will be a property file with the same name in the repository. This will identify which property file to fetch from the repository for a particular microservice. For example, for hello-service.properties, we would use hello-service-development.properties (the -development suffix tells the system to fetch code for development profile).
    • spring.profile.active - Identifier used to for environment, i.e. development/production, etc.
    • spring.cloud.config.uri - Specifies the URL for the Spring cloud config server.
  5. Now create a REST Controller to access the attribute available in the git property file.
    Java
     




    xxxxxxxxxx
    1
    23


     
    1
    package com.hello.example.controller;
    2
    
                
    3
    import org.springframework.beans.factory.annotation.Autowired;
    4
    import org.springframework.cloud.context.config.annotation.RefreshScope;
    5
    import org.springframework.core.env.Environment;
    6
    import org.springframework.http.HttpStatus;
    7
    import org.springframework.http.ResponseEntity;
    8
    import org.springframework.ui.Model;
    9
    import org.springframework.web.bind.annotation.GetMapping;
    10
    import org.springframework.web.bind.annotation.RestController;
    11
    
                
    12
    @RefreshScope
    13
    @RestController
    14
    public class HelloController {
    15
    
                
    16
        @Autowired
    17
        private Environment env;
    18
    
                
    19
        @GetMapping("/hello")
    20
        public ResponseEntity<String> getHello(Model model) {   
    21
            return new ResponseEntity<String>( env.getProperty("message"), HttpStatus.OK);
    22
        }
    23
    }


    Here, the "message" attribute is available inside the hello-service-development.properties file which is available in the git repository.

    Properties files
     




    xxxxxxxxxx
    1


     
    1
    message=Hello world - this message is from development


  6. The basic setup is now complete. Let's run the application as "Spring Boot App." The application will run at default port, 8080 (make sure, the HelloConfigServer application is running at port 8888. If it's not running, then first start the HelloConfigServer application. Later. start the HelloConfigClient application, as the client application load properties during the start up process). 

  7. To test the config client, go to the URL http://localhost:8080/hello. The config client will look for the configuration file (hello-service-development.properties) in our git repo – the expected response is below:

  8. Congratulations on setting up the cloud config client successfully and creating communication with the config server!

Configuration Update

Now that communication is set up between the cloud config client, the cloud config server, and the git repository, w will happen if any property file is updated after the application's deployment? Can the config client fetch the latest configuration? No, Service/Application needs to be restarted to get the latest update. This is a problem.

To solve this problem, Spring Boot has provided an approach to fetch the latest configuration information from repository at runtime with the help of the @RefreshScope annotation and and the Actuator dependency.

@RefreshScope - By default, the configuration values are read at the client’s server startup. This annotation forces the bean to refresh its configuration, i.e. to pull updated configurations from the repository via a config server without the application restarting in real-time. 

Actuator - Once the /actuator/refresh event for the application is triggered, all the beans that are annotated with @RefreshScope get refreshed. This means updating values retrieved from the config server and updating the client's microservice bean.

We can trigger the refresh event with the help of the URL http://localhost:8080/actuator/refresh. This will result in refreshing all the beans annotated with @RefreshScope. Since the HelloController is annotated with @RefreshScope, its properties will get refreshed.

Conclusion

We are able to create a configuration client which can communicate with a configuration server to access a git repository and fetch configuration files.

The source code related to this tutorial is available on GitHub. 

Spring Framework Spring Cloud

Opinions expressed by DZone contributors are their own.

Related

  • Distributed Tracing System (Spring Cloud Sleuth + OpenZipkin)
  • Component Tests for Spring Cloud Microservices
  • A Robust Distributed Payment Network With Enchanted Audit Functionality - Part 2: Spring Boot, Axon, and Implementation
  • 7 Microservices Best Practices for Developers

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook