Detailed explanation of spring cloud Config service configuration

Spring Cloud Config

  • summary
  • Config server configuration and test
  • Config client configuration and testing
  • Dynamic refresh of Config client


Configuration problems faced by distributed systems

What is it?

What can I do

  • Centrally manage profiles

  • Different environments have different configurations, dynamic configuration updates, and deployment by environment, such as dev/test/prod/beta/release

  • The configuration is dynamically adjusted during operation. It is no longer necessary to write configuration files on each service deployed machine. The service will agree to pull its own configuration information from the configuration center

  • When the configuration changes, the service can sense the configuration changes and apply new fields without restarting

  • Expose the configuration information in the form of REST interface

Integrated configuration with GitHub

Spring Config uses git to store configuration files by default (there are other ways, such as supporting svn and local files), but Git is recommended and is accessed in the form of http/https

Config server configuration and test

  • Create a new Repository named springcloud config on GitHub

  • Get the newly created git address from the previous step

  • Create a new git warehouse and clone in the local hard disk directory

  • Create a new Module cloud-config-center-3344, that is, the configuration center Module CloudConfig Center of microservices

  • pom

    newly added:

  • configuration file

      port: 3344
        name: cloud-config-center   #Register to eureka microservice name
              uri: #git warehouse name
                - springcloud-config     #search for directory
          label: master                  #Read branch
    #Service registration to eureka
  • Main startup class

    Add annotation @ enableconfig server

  • Modify the hosts file under windows and add mapping: config-3344 com

  • Test whether the configuration content can be obtained from GitHub through Config microservice

    • Start microservice
    • visit
  • Configure read rules:

    • /{label}/{application}-{profile}.yml

      Instance: Master branch:

      Dev branch:

    • /{application}-{profile}.yml


  • Successfully achieved the use of SpringCloud Config to obtain configuration information through GitHub

Config client configuration and testing

  1. Create cloud-config-client-3355

  2. pom

    <!--config client-->
  3. bootstrap.yml

    The configuration is as follows:

      port: 3355
        name: config-client
        config:        #config client configuration
          label: main     #Branch name
          name: config    #Profile name
          profile: dev    #Read the suffix name and integrate the above three into the config-dev.yml configuration file on the main branch
          uri: http://localhost:3344 # profile address
  4. Main startup class

    Register to enter the service center: @ EnableEurekaClient

  5. Business class:

    public class ConfigClientController {
        private String configInfo;
        public String getConfigInfo(){
            return configInfo;
  6. test

    Start the config configuration center ConfigServer3344 and self test:


    Start 3355 as a Client to prepare for access:

    • http://localhost:3355/configInfo
  7. The client 3355 accesses the config configuration center config-3344 and obtains the configuration information through github

  8. Problem: dynamic refresh of distributed configuration:

    1. Linux operation and maintenance modifies the content of the configuration file on Github to make adjustments
    2. Refresh 3344 and find that the ConfigServer configuration center responds immediately
    3. Refresh 3355 and find no response from ConfigClient client
    4. Unless you restart or reload 3355

Dynamic refresh of Config client

Avoid restarting the client micro service 3355 every time the configuration is updated

Dynamic refresh

  1. pom introduces actor service monitoring

    <!--actuator Service monitoring-->
  2. Modify yml and expose the monitoring port

    #Exposure monitoring endpoint
            include: "*"
  3. Business class Controller modification

    Add @ RefreshScope annotation

  4. At this time, modify the configuration file on github and test 3344 and 3355

  5. At this time, the client 3355 does not refresh dynamically. The operation and maintenance personnel need to send a Post request to refresh the client 3355

    Must be a post request: curl -X POST“ http://localhost:3355/actuator/refresh ”

  6. Visit the client 3355 again and successfully refresh the client 3355 to the latest configuration content to avoid service restart

Remaining problems:

Suppose there are multiple microservice clients, and each microservice must execute a post request to refresh manually? (script can be used)

Can you broadcast? One notice will take effect everywhere

Can you accurately notify and select a specific micro service to make its configuration effective

The above problems cannot be realized, so the message bus needs to be introduced to cooperate with spring cloud Bus

Tags: git github Spring Cloud Microservices

Posted by Mr_jmm on Wed, 25 May 2022 03:22:01 +0300