Create client projects and read the service-oriented configuration center (consult + spring cloud config)


Create client project and read the service configuration center

Register the configuration center with the service center (consul)

POM file add dependency:


Profile add content:
#Service name registered to consumer

Start consul service discovery:

//Start configuration center
//Start service discovery
public class SpringCloudConfigServerApplication {

    public static void main(String[] args) {, args);


Start the server of the configuration center and view the consul background:
Alt text

Register and deploy two configuration centers

Start two configuration centers with the command and configuration file parameters and register the same service name to form high availability. Execute in the spring cloud config server project root directory:

mvn install

Copy the file in the spring cloud config server project to the directory / spring cloud config server / target /, rename it to and, and then modify the port number and application name in the configuration file:


Start two configuration centers by executing the following command in the / target / Directory:

java -jar spring-cloud-config-server-0.0.1-SNAPSHOT.jar
java -jar spring-cloud-config-server-0.0.1-SNAPSHOT.jar

Start the server of the configuration center and view the consul background:
Alt text

Create client project to read configuration center

Alt text
Alt text
Alt text
Alt text

Modify the configuration file
#Settings do not need to be registered in consumer

Create a new configuration file

#Configuration item name defined in profile name
#Environment name defined in profile name
#Turn on service discovery in configuration center
#Name of configuration center registered in Service Center

The content of the configuration center needs to be loaded before, so the configuration information about the configuration center should be written in the configuration file, because the configuration file will be loaded before

Add interface class for reading configuration:

public class ConfigTestController {

    //Configuration information is read by @ value annotation, and configuration items are read by ${configuration items}
    private String configBluersw;

    public String ConfigTest(){
        return this.configBluersw;

Start client test

Visit / configtest to get the configuration information Test-3 (I changed it several times):
Alt text

At this time, modify the content configured in Git warehouse and refresh the page, the content of the page will not change, because the configuration content is loaded when the program starts, and the content change in configuration center will not be automatically reflected in the client program, but the configuration content can be reloaded by calling the client’s refresh interface, which we will show in the bus part.

Source code

GitHub warehouse: