Spring Cloud Config achieve the cluster configuration center

Spring Cloud Config provides configuration server and configure the client as a distributed system that can manage the configuration file cluster.
Use Git, SVN and other version management system to store configuration files, configure the server to obtain configuration management system version, cluster and then configure the client to configure the server to obtain configuration.

Development Tools: IntelliJ IDEA 2019.2.2

First, create a configuration server

1, SVN server configuration file and add items

 client-config-dev.yml 内容:

server:
  port: 8092
test:
  user:
    name: aa

client-config-test.yml

server:
  port: 8093
test:
  user:
    name: bb

2. Create a project

IDEA to create a new SpringBoot project, named "spring-config-server", SpringBoot version 2.1.10 choice in the selection Dependencies (dependent) interface check Spring Cloud Config -> Config Server.
pom.xml introduces spring-cloud-config-server dependencies, dependency org.tmatesoft.svnkit added in pom.xml, complete pom.xml follows:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.10.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>spring-config-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-config-server</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR4</spring-cloud.version>
    </properties>

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

        <dependency>
            <groupId>org.tmatesoft.svnkit</groupId>
            <artifactId>svnkit</artifactId>
            <version>1.10.1</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </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>

</project>
View Code

3, modify the configuration application.yml

spring-cloud-config-server configuration provides 4, can be activated by different names:
(. 1) Git : default value, reads the configuration file indicates to Git repository;
(2) Subversion : indicates to read the configuration file repository SVN ;
(. 3) native : indicates to the local file system reads the configuration file;
(. 4) Vault : Vault indicates to (a resource control means) reads the configuration file;

server:
  port: 8091
spring:
  application:
    name: config-server
  profiles:
    active: subversion
  cloud:
    config:
      server:
        svn:
          uri: https://localhost/svn/test-project
          username: abc
          password: 123456
        default-label: default-config

4, to modify the startup class code

Notes @EnableConfigServer increase

package com.example.springconfigserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableConfigServer
public class SpringConfigServerApplication {

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

}
View Code

You can use Config Server endpoint obtain the configuration file content, mapping rules and the endpoint configuration file is as follows:

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

{application} is the application name, the name of the corresponding part of the configuration file, the present embodiment is config-client.
{profile} is the version of the configuration file, the present embodiment is dev and test.
{label} represents a branch, if the default is git master branch.

Start the service, browser access (replaced with the following test dev, results similar to) the following addresses are output as follows:
HTTP: // localhost: 8091 / config-Client / test

{"name":"config-client","profiles":["test"],"label":null,"version":"6","state":null,"propertySources":[{"name":"https://localhost/svn/test-project/default-config/config-client-test.yml","source":{"server.port":8093,"test.user.name":"bb"}}]}

http://localhost:8091/config-client/test/default-config

{"name":"config-client","profiles":["test"],"label":"default-config","version":"6","state":null,"propertySources":[{"name":"https://localhost/svn/test-project/default-config/config-client-test.yml","source":{"server.port":8093,"test.user.name":"bb"}}]}

http://localhost:8091/config-client-test.yml

server:
  port: 8093
test:
  user:
    name: bb

http://localhost:8091/default-config/config-client-test.yml

server:
  port: 8093
test:
  user:
    name: bb

Second, the configuration client reads configuration SVN

1. Create a project

IDEA to create a new SpringBoot project, named "spring-config-client", SpringBoot version 2.1.10 choose, check the Web in select Dependencies (dependent) Interface -> Spring Web, Spring Cloud Config -> Config Client.
pom.xml introduces spring-boot-starter-web and the spring-cloud-starter-config dependency, complete pom.xml follows:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.10.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>spring-config-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-config-client</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR4</spring-cloud.version>
    </properties>

    <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>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </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>

</project>
View Code

2, modify the startup class code

Increase the test method

package com.example.springconfigclient;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class SpringConfigClientApplication {

    @Autowired
    private Environment env;

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

    @RequestMapping("/")
    public String home(){
        return env.getProperty("test.user.name");
    }
}

3, add the configuration bootstrap.yml

spring:
  application:
    name: config-client
  cloud:
    config:
      uri: http://localhost:8091
      profile: dev

Set application name config-client, using spring.cloud.config.uri to set the configuration server address used to read spring.cloud.config.profile specified configuration. Finally, the client configuration to the next test-project / default-config SVN server directory read config-client-dev.yml (.properties).

Start the service, access the browser: http: // localhost: 8092 / (where port 8092 is specified in the config-client-dev.yml in), page output: aa

You can also use spring.cloud.config.name instead of spring.application.name, the same result.

spring:
  cloud:
    config:
      uri: http://localhost:8091
      profile: dev
      name: config-client

If spring.cloud.config.name and spring.application.name not provided, the read application-dev.yml default.
New file application-dev.yml in the SVN test-project / default-config directory, content 

server:
  port: 8092
test:
  user:
    name: cc

Start the service, access the browser: http: // localhost: 8092 /, page output: cc

May be provided to override the default-lable spring.client.config.label properties of the server, the above additional profile can be changed to the following wording.

spring:
  application:
    name: config-client
  cloud:
    config:
      uri: http://localhost:8091
      lable: default-config
      name: config-client
  profiles:
    active: dev

Third, the use / refresh manually refresh the endpoint configuration

1, adding a dependency on the client configuration above the pom.xml

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

2, application.yml add configuration

management:
  endpoints:
    web:
      exposure:
        include: "*"

3, add annotations on the Controller @RefreshScope

package com.example.springconfigclient;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
@RefreshScope
public class SpringConfigClientApplication {

   @Autowired
    private Environment env;

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

    @RequestMapping("/")
    public String home(){
        return env.getProperty("test.user.name");
    }
}
View Code

4, modify the config-client-dev.yml content server SVN

The name value is modified by aa aa11, submit SVN changes.

5, / refresh only supports POST request sends a POST request to http: // localhost: 8092 / actuator / refresh

Postman using a POST request, if SVN unmodified, [], if any changes and return the following results:

Refresh your browser address: http: // localhost: 8092 /, the result has been aa, became aa11.

 

Guess you like

Origin www.cnblogs.com/gdjlc/p/11968129.html