Basic use of SpringCloud's Config

Config overview

In a distributed environment, many services need to be deployed in clusters, which means that these services must provide the same service, so their configuration files must be the same; so extract a configuration file center, so that it can be pulled from a center Take the configuration file, so as to ensure that all configurations are the same; when modifying, only one place needs to be modified.

​ SpringCloud **Config provides centralized external configuration support for microservices in the microservice architecture; **Configuration server provides a centralized external configuration for all environments of different microservice applications; Config is divided into server and The client has two parts.

Config server:
Also known as the configuration center, it is an independent microservice application that is used to connect to the configuration server and provide clients with access interfaces such as obtaining configuration residences, encrypting, and decrypting residences.

Config client:
It manages application resources and business-related configuration content through a designated configuration center, and obtains and loads configuration information from the configuration center at startup.

Configure the server:
By default, git is used to store the configuration residence, which is helpful for version management of the environment configuration, and the configuration content can be easily managed and accessed through the git client tool.

Server creation:

Step 1: pom file

        <!--config server-side dependencies-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

Step 2: Add the annotation **@EnableConfigServer** to the startup class

package com.mmy;

//marked as config server
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {

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

Step 3: Configuration File

#The service name of the profile center
spring.application.name=config-server
#The port of the configuration file center -- generally 8888 8889
server.port=8888

#--------------------Associated configuration with git ------------------------- ----------------------

#The url address of the remote repository of the configuration file on gitee
spring.cloud.config.server.git.uri=***
#Configuration file on gitee The directory path where the configuration file is stored in the remote repository
spring.cloud.config.server.git.search-paths=***
#Username for logging in to gitee
spring.cloud.config.server.git.username=***
#Password to log in to gitee
spring.cloud.config.server.git.password=***
#Cache the configuration file in the remote repository of the configuration file on gitee to a local location
spring.cloud.config.server.git.basedir=***

Client build:

Step 1: pom file

		<!--config client-side dependencies-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

Step 2: Configuration File

#config client service name
spring.application.name=config-client
#config client port
server.port=8080

#---The config client pulls the configuration of the configuration file from the configuration center (config server)---

#The url address of the configuration center (config server)
spring.cloud.config.uri=http://localhost:8888

#The rules for reading configuration files are:
#/{label}/{name}-{profile}.properties or /{label}/{name}-{profile}.yml
#label is the name of the branch where the configuration file is located, e.g. master branch
#name is the prefix of the configuration file name, e.g. config-dev.properties
#profile is the environment for the profile name, e.g. config-dev.properties
spring.cloud.config.label=master
spring.cloud.config.name=config
spring.cloud.config.profile=dev

Solve dirty reads:

​ When the configuration file in git is modified, the data read by the client is still the data before the modification, and there is dirty reading;

Solution: use the Bus message bus to solve

Tags: Java Spring Spring Cloud

Posted by maliskoleather on Sun, 11 Sep 2022 23:25:24 +0530