[SpringCloud] Spring Cloud Config Configuration Center (20)

Introduction to Spring Cloud Config

  Spring Cloud Config provides server and client support for external configuration in distributed systems. With Config Server, you can centrally manage the external properties of applications in all environments. The concepts on the client and server are abstracted from Spring Environment and PropertySource, so they are very suitable for Spring applications, but can be used with any application running in any language. As applications enter the production process from developers to testers, you can manage the configuration between these environments and ensure that the applications have everything they need to migrate. The default implementation of the server storage backend uses git, so it easily supports the tagged configuration environment version, and can access these contents through various tools to manage the content. It is easy to add an alternative implementation and insert it into the Spring configuration.

  advantage:

  1. Spring Cloud Config Server function:

  2. HTTP, resource-based API for external configuration (name-value pairs, or equivalent YAML content)

  3. Encrypt and decrypt attribute values ​​(symmetrical or asymmetrical)

  4. Easily embed @EnableConfigServer using Spring Boot application

  5. Config Client function (for Spring applications):

  6. Bind to Config Server and Environment to initialize Spring using remote property source

  7. Encrypt and decrypt attribute values ​​(symmetric or asymmetric)

  Spring Cloud Config is used to provide centralized external configuration support for infrastructure and microservice applications in distributed systems.

  • Server: A distributed configuration center, an independent microservice application, used to connect to a configuration warehouse (GIT) and provide clients with access interfaces to obtain configuration information, encryption / decryption, etc.
  • Client: Each microservice application and infrastructure in the microservices architecture, manages the application resources and business-related configuration content through the designated configuration center, and obtains and loads configuration information from the configuration center at startup

Spring Cloud Config architecture diagram

  

Spring Cloud Config server build

  ready:

  To prepare an Eureka registration center, build reference: [SpringCloud] Quick Start (1)

  Prepare a configuration center warehouse on Github as follows:

    

    The content of the config-dev.yml file is as follows:

    

  Build:

  1. Create a new SpringCloud module springcloud-config-center8888 as a configuration center

  2. In pom, introduce spring-cloud-config-server and spring-cloud-starter-netflix-eureka-client dependencies

 1 <!-- spring cloud config server-->
 2 <dependency>
 3     <groupId>org.springframework.cloud</groupId>
 4     <artifactId>spring-cloud-config-server</artifactId>
 5 </dependency>
 6 
 7 <!-- eureka client -->
 8 <dependency>
 9     <groupId>org.springframework.cloud</groupId>
10     <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
11 </dependency>

    The complete pom file is as follows:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <parent>
 6         <artifactId>test-springcloud</artifactId>
 7         <groupId>com.test</groupId>
<8         version>1.0-SNAPSHOT</version>
 9     </parent>
10     <modelVersion>4.0.0</modelVersion>
11 
12     <artifactId>springcloud-config-center8888</artifactId>
13 
14     <dependencies>
15 
16         <!-- spring cloud config server-->
17         <dependency>
18             <groupId>org.springframework.cloud</groupId>
19             <artifactId>spring-cloud-config-server</artifactId>
20         </dependency>
21 
22         <!-- eureka client -->
23         <dependency>
24             <groupId>org.springframework.cloud</groupId>
25             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
26         </dependency>
27 
28         <!-- spring boot -->
29         <dependency>
30             <groupId>org.springframework.boot</groupId>
31             <artifactId>spring-boot-starter-web</artifactId>
32         </dependency>
33         <dependency>
34             <groupId>org.springframework.boot</groupId>
35             <artifactId>spring-boot-starter-actuator</artifactId>
36         </dependency>
37         <dependency>
38             <groupId>org.springframework.boot</groupId>
39             <artifactId>spring-boot-devtools</artifactId>
40             <scope>runtime</scope>
41             <optional>true</optional>
42         </dependency>
43         <dependency>
<44             groupId>org.projectlombok</groupId>
45             <artifactId>lombok</artifactId>
46             <optional>true</optional>
47         </dependency>
48         <dependency>
49             <groupId>org.springframework.boot</groupId>
50             <artifactId>spring-boot-starter-test</artifactId>
51             <scope>test</scope>
52         </dependency>
53 
54     </dependencies>
55 </project>
pom.xml

  3. Edit the application.yml configuration file

1  # Port 
2  Server:
 . 3    Port: 8888
 . 4  
. 5  Spring:
 . 6    file application:
 . 7      name: Cloud-config- Conter
 . 8    Cloud:
 . 9      config:
 10        Server:
 . 11          Git:
 12 is            # Git repository address 
13 is            URI: HTTPS: // GitHub .com / xxxx / springcloud- config.git
 14            username: xxxx
 15            password: 123456
 16            # skip SSL verification 
17            skipSslValidation: true 
18           # Search path, by default the root path, the following wording represents a more find the file in the directory and folder directory 
19            SearchPaths: folder
 20  
21  Eureka:
 22    Client:
 23      Service- url:
 24-        defaultzone: HTTP: // localhost: 8761 / eureka

  4. Edit the main startup class and use @EnableConfigServer to activate the configuration center server

1 // 激活配置中心
2 @EnableConfigServer
3 @SpringBootApplication
4 public class ConfigCenterMain8888 {
5     public static void main(String[] args) {
6         SpringApplication.run(ConfigCenterMain8888.class, args);
7     }
8 }

   5. Test

    1) Start the test center module

    2) Access address: http: // localhost: 8888 / config-dev.yml, normally get the files in the git repository

      

    3) View the console, you can see the directory where the file is located

      

    4) Enter the directory where the file is located, use the command: git branch -a, you can find that the directory is the local git repository directory (for the local repository of the above Spring Cloud Config architecture diagram)

      

    5) Modify the remote git repository, the content of the config-dev.yml file on github, changed to:

config:
  info: this is master branch, config-dev.yml, version == 2

    6) Refresh the access address: http: // localhost: 8888 / config-dev.yml, view the display content

      Found that the displayed content has also been updated

      

Spring Cloud Config configuration and others

  1. Configure the reading rules:

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

    For example, you can access the address:

    a. View the config-dev.yml file information on the test branch: http: // localhost: 8888 / config / dev / test

    b. Get the config-dev.yml file on the default (master) branch: http: // localhost: 8888 / config-dev.yml

    c. Obtain the config-dev.yml file on the dev branch: http: // localhost: 8888 / dev / config-dev.yml

  2. The git attribute (searchPaths) configured in the project application.yml

    Search path, the default is in the root path, the following writing means to find files in the change directory and folder directory

    You can build a folder folder under the change directory, then build a config-test.yml file in the folder folder

    Access address: http: // localhost: 8888 / master / config-test.yml, check the file, and automatically search under the root path and folder directory

 

Guess you like

Origin www.cnblogs.com/h--d/p/12758117.html