SpringCloudConfig is a configuration hub in our usual sense. It extracts the configuration of the application from local files and puts it on a central server to provide better management and distribution capabilities. SpringCloudConfig consists of a server and a client. The server releases the configuration file stored in Git (SVN) to the REST interface, and the client obtains the configuration from the REST interface on the server. However, clients are not actively aware of configuration changes to obtain new configurations, requiring each client to trigger its own /refresh via POST.

SpringCloudBus connects nodes of a distributed system through a lightweight message broker. This can be used to broadcast state changes (such as configuration changes) or other administrative instructions. SpringCloudBus provides an endpoint/bus/refresh accessible through the POST method. This interface is usually invoked by git’s hook function to notify individual SpringCloudConfig clients to update configurations on the server. In the Spring Cloud Config component, there are two roles: Config Server and Config client.

Create a spring-boot project named config-server with pom.xml:

<? xml version="1.0" encoding="UTF-8"? > <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> < modelVersion > 4.0.0 < / modelVersion > < groupId > cn. Easted < / groupId > < artifactId > com < / artifactId > <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>com</name> <description>Demo projectforSpring Boot</description> <parent> <groupId>org.springframework.boot</groupId> The < artifactId > spring - the boot - starter - parent < / artifactId > < version > 1.5.6. RELEASE < / version > < relativePath / > <! -- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> < project. Reporting. OutputEncoding > utf-8 < / project. Reporting. OutputEncoding > < Java version > 1.8 < / Java version > <spring-cloud.version>Dalston.SR3</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies>  <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
     <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
</project>
Copy the code

Add @enableconFigServer to the Application class to enable the configuration of the server.

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}
Copy the code

You need to configure the following in the application. Yml file in the application configuration file:

spring.application.name=config-server
server.port=8888
 
spring.cloud.config.server.git.uri=https://github.com/forezp/SpringcloudConfig/
spring.cloud.config.server.git.searchPaths=respo
spring.cloud.config.label=master
spring.cloud.config.server.git.username=
spring.cloud.config.server.git.password=
Copy the code

Spring. Cloud. Config. Server. Git. Uri: configure git warehouse address

Spring. Cloud. Config. Server. Git. SearchPaths: configuration path to the warehouse

Spring. Cloud. Config. Label: configuration branch warehouse

Spring. Cloud. Config. Server. Git. Username: access to the git repository user name

Spring. Cloud. Config. Server. Git. Password: access to the git repository user password

Remote warehouse have a config file in https://github.com/shaweiwei/myspringcloudconfig/ – the client – dev. The properties file has an attribute:

Fooo = version to start the program: http://localhost:8888/fooo/dev

HTTP request addresses and resource files are mapped as follows:

/{application}/{profile}[/{label}]

/{application}-{profile}.yml

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

/{application}-{profile}.properties

/{label}/{application}-{profile}.properties
Copy the code

Create a springBoot project named config-client with its POM file:

<? xml version="1.0" encoding="UTF-8"? > <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> < modelVersion > 4.0.0 < / modelVersion > < groupId > com. Forezp < / groupId > < artifactId > config - client < / artifactId > <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>config-client</name> <description>Demo projectfor Spring Boot</description>
 
    <parent>
        <groupId>com.forezp</groupId>
        <artifactId>sc-f-chapter6</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
 
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
    </dependencies>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
 
 
</project>
Copy the code

Its configuration file bootstrap.properties:

spring.application.name=config-client
spring.cloud.config.label=master
spring.cloud.config.profile=dev
spring.cloud.config.uri= http://localhost:8888/
server.port=8881
Copy the code

Spring. Cloud. Config. Indicate the branch of remote warehouse label

spring.cloud.config.profile

Dev development environment configuration file

Test Test environment

Pro Formal environment

Spring.cloud.config. uri= http://localhost:8888/ Specifies the web address of the configuration service center.

/ hi (foo); / hi (foo); / hi (foo);

@SpringBootApplication
@RestController
public class ConfigClientApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication.class, args);
    }
 
    @Value("${foo}")
    String foo;
    @RequestMapping(value = "/hi")
    public String hi() {returnfoo; }}Copy the code

Open the website to visit: http://localhost:8881/hi, the web page shows: version welcome everyone to study the relevant technology is willing to understand the source code of friends directly beg exchange and share technology: 2147775633

This means that config-client gets foo’s properties from config-server, which reads them from the Git repository. As shown in figure: