Spring Cloud Config Distributed Configuration Management 5.3

Spring Cloud Config Introduction

  In traditional monolithic application system, we usually use the configuration file and code together, but as more and more systems, you need to realize more and more functions for a long time, we had to upgrade to a distributed system the system, which will also feature a more refined system of split. After the split, all service applications will have their own profile, when you need to modify a configuration services, we may need to modify a lot of the, and in order to modify a particular setup, you may need to restart the services related to all services this is obviously very troublesome.
  In traditional monolithic application system, we usually use the configuration file and code together, but as more and more systems, you need to realize more and more functions for a long time, we had to upgrade to a distributed system the system, which will also feature a more refined system of split. After the split, all service applications will have their own profile, when you need to modify a configuration services, we may need to modify a lot of the, and in order to modify a particular setup, you may need to restart the services related to all services this is obviously very troublesome.
  Spring Cloud Config is a brand new project Spring Cloud team was created, the project is mainly used for the external configuration of a distributed system provides server (Config Server) and client (Config Client) support.
  Server-side (Config Server): also known as a distributed configuration center, it is an independent micro-service applications, mainly used to configure the environment under the centralized management of various applications, using Git to store the contents of the default configuration file, you can also use SVN storage, or local file storage.
  The client (Config Client): Config Server is a client, that each micro-micro-service application service architecture. They manage application resources and related content through the service configuration specified configuration center (Config Server), and obtaining and loading configuration information from a configuration center at startup.
  Spring Cloud Config workflow shown in Figure 5-17.

 



  In Figure 5-17, the user will first push profile or Git SVN, and then, obtains the configuration information from the configuration center (Config Server) at the time of the micro-service application (Config Client) promoter, are arranged based on the central obtaining configuration information from a configuration corresponding to the SVN or Git.

Using local storage means to achieve configuration management

  By learning the previous section, we already know that Spring Cloud Config supports local storage, Git and SVN are. This section will be the next local storage, for example, to explain the use of Spring Cloud Config.
  1. structures Config Server
  (. 1) create a configuration center project microservice-config-server, and introduced in its dependence Config Server pom.xml, as indicated in document 5-12.
  5-12 pom.xml 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>
    <parent>
        <groupId>com.xc</groupId>
        <artifactId>xcservice-springcloud</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <groupId>com.xc</groupId>
    <artifactId>xcservice-config-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>xcservice-config-server</name>
    <description>配置管理</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency><!--Config Server的依赖-->
            <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>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

 

  (2) prepare profile application.yml, add the port number, and the service attribute information is stored as files 5-13.
  Files 5-13 application.yml
Server: 
  Port: 8888 

the Spring: 
  the Application: 
    name: xcservice-config-Server # Specify the name of 
  Profiles: 
    the Active: Native # use local storage file system to store configuration information

 

  (3) create three configuration files are used to represent resource development, and pre-release testing under src / main / resources directory and write the following contents in the file.
  · Preparation of application-dev.yml content: clientParam: Native-dev-1.0
  -written content in the Application-prod.yml: clientParam: Native-Prod-1.0
  -written content in the Application-test.yml: clientParam: native-test- 1.0
  above resource file is in accordance with the standard "application context name + name + format" named, naming its common file is as follows:
   
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

                  

  Where the application represents the application name, profile represents a file changes, and label is optional, represents the Git branch, the default is master.

  (4) create a startup class, and increase @EnableConfigServer annotation on the class to open the server function, as shown in documents 5-14.
  Files 5-14 Application.java
package com.xc.xcserviceconfigserver;

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

/**
 * http://localhost:8888/microservice-config-server/dev
 * http://localhost:8888/application-dev.yml
 */
@EnableConfigServer
@SpringBootApplication
public class XcserviceConfigServerApplication {

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

}

 

  (5) to start the project, test applications. After the application is started successfully, initiating a request URL according to the following format:
  HTTP: // localhost: 8888 / ApplicationName {} / {the env} / {} label present application access address http: // localhost: 8888 / microservice -config -server / dev, JSON information in the browser to display the information on the application name microservice-config-server, the name of the environment dev, and resource file path and file content.
  In addition, we can also directly access the resource files, to view the configuration information in the resource file. Through a browser to access the address http: // localhost: 8888 / the AP-plication-dev.yml

  2. Client build Config
  (1) Create a client project microservice-config-client, and Web Config and add in its pom.xml dependent, as indicated in document 5-15.
  5-15 pom.xml 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>
    <parent>
        <groupId>com.xc</groupId>
        <artifactId>xcservice-springcloud</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <groupId>com.xc</groupId>
    <artifactId>xcservice-config-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>xcservice-config-client</name>
    <description>Config Client</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

 

  (2) to write the configuration file bootstrap.yml, where you can configure the application name, information service center address shown, need to access files and port number, such as files 5-16.
  Files 5-16 bootstrap.yml
Server: 
  Port: 8801 

Spring: 
  file application: 
    name: xcservice-config-Client 
  Cloud: 
    config: 
      Profile: Profile} {# Test configuration service 
      address 8888 / # center configuration: uri: http: // localhost

  Note that the name of the above configuration file must be bootstrap.yml or bootstrap.properties, the only way to be able to properly configure the load center (although application.yml Spring Boot can also be loaded, but bootstrap.yml priority load) .

  (3) create a startup class, and add @RestController annotation on the class, as shown in the file after editing 5-17.
  Files 5-17 Application.java
package com.xc.xcserviceconfigclient;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * http://localhost:8801/client-Param
 */
@SpringBootApplication
@RestController
public class XcserviceConfigClientApplication {

    @Value("${clientParam}")
    private String clientParam;

    @RequestMapping("/clientParam")
    public String getParam() {
        return this.clientParam;
    }

    /**
     * http://localhost:8801/hello
     *
     * @return
     */
    @RequestMapping("/hello")
    public String hello() {
        return "hello world";
    }

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

}

 

  (4) to start the project, test applications. After the application is started successfully, you can address http: // localhost: 8801 / hello test whether the application can be accessed
  at this time through a browser to access the address http: // localhost: 8801 / clientParam , you can get information in the configuration file
  when we when the need to access information on other configuration files, you can simply modify the property values in the profile of bootstrap.yml.

Using Git storage mode configuration management

  Spring Cloud Config the default server configuration uses a Git repository, you can easily use a variety of third-party tools by Git to update their content management and version control, and Git Webhooks warehouse functions can also be configured to monitor content in real-time changes, so use Git storage approach is very convenient.
  Mastered the use of local storage after use Git storage it is easy to grasp. We are also following the case of more than one measure, for example, to explain how to get Git repository different versions of the configuration information from the server via the client's Spring Cloud Config.
  (1) Configuration Git. Created on Git xcservice-study-config directory, and increase development in the catalog, pre-release testing and configuration files, edit the contents of the three documents are as follows:
  · dev.yml in the Application-written content: clientParam: git- dev-1.0
  written content · application-prod.yml in: clientParam: git-Prod-1.0
  · test.yml in the Application-written content: clientParam: git-the Test-1.0
  (2) modify the server configuration file. The configuration xcservice-config-server configuration file in the project local file storage to delete (or comment), and add the git configuration information, as shown in documents 5-18.
  Files 5-18 application.yml
Server: 
  Port: 8888 

the Spring: 
  the Application: 
    name: xcservice-config-Server # Specify the name of 
  #profiles: 
  # the Active: Native # use local storage file system to store configuration information 
  Cloud: 
    config: 
      Server: 
        git: git use # way 
          uri: https://gitee.com/secret8/microservice-study-config.git

 

  In the above configuration, spring.cloud.config.server.git.uri Git repository attribute is used to specify the network address. This is because the configuration of a public warehouse, it is not necessary to fill in the username and password information. If a private warehouse, you need to fill out the account information, then you can increase the username and password properties in git property.
  (3) modify the client configuration file. Add xcservice-config-client project attributes label configuration file, and the attribute value is set to master (label attribute indicates branch in Git, the attribute value default Master), edited documents as the 5-19 shows.
Files 5-19 bootstrap.yml
Server: 
  Port: 8801 

Spring: 
  file application: 
    name: xcservice-config-Client 
  Cloud: 
    config: 
      Profile: Profile} {# Test configuration service 
      label: master # git of the corresponding branch, default Master 
      URI: HTTP: // address address 8888 / # configuration center: localhost

 

  (4) to start the project, test applications. Respectively start Spring Cloud Config service and client projects, by accessing the address http: // localhost: 8801 / clientParam, you can obtain configuration information has been found in the Git

Manually update the running configuration file

  In the actual project application, we may need to make some changes to the contents of the configuration file, but in order for the changes to take effect profile, the usual practice is to restart the application. For small applications this way, as well as the use of the small number of applications, more suitable, but for large enterprises and Internet applications, restart the application does not work. It also requires operation and maintenance personnel after modifying the application configuration to ensure timely entry into force configuration. Spring Cloud Config just provide this functionality, we can use the POST method in a client request to refresh to refresh the configuration content.
Cases more than one measure, for example, to achieve real-time update the configuration file, perform the following steps.
  (1) add a dependency spring-boot-starter-actuator at the client's pom.xml. This dependence can be monitored at runtime state, including / refresh function. pom.xml dependent information added as follows:
< Dependency > <-! Status monitor program at run time -> 
    < the groupId > org.springframework.boot </ the groupId > 
    < the artifactId > Spring-Boot-Starter-Actuator </ the artifactId > 
</ dependency >

 

  (2) Add @RefreshScope notes on startup class, open refresh mechanism. After this add annotations, when executed / refresh will update all of the values of the variables at the class label annotation, including the configuration Config Client acquired from Git repository.
  (3) authentication security attribute enabled is set to false information in the configuration file, code is as follows:
Management: 
  Security: 
    Enabled: false # whether to open the actuator safety certification

 

  After performing the above three steps, the following applications may be implemented to detect whether the profile update operation. Start the application and access http: // localhost: 8801 / clientParam
  this time to modify the configuration file in Git application-prod.yml, its contents clientParam: git-prod-1.0, revised as clientParam: After git-prod-2.0, again browser to access the above address, you will find the contents of the browser has not changed, but through the address http: // localhost: when 8888 / application / prod visit, you will find the server has been acquired to update the configuration information in Git
  use the POST request access address http: // localhost: after 8801 / refresh, this time again through the browser to http: // localhost: 8801 / when clientParam, the browser shows the contents of the configuration has been successfully updated files, which means that we We have successfully achieved the profile manually update operation. 
 

Guess you like

Origin www.cnblogs.com/ooo0/p/11203606.html