springcloud (twelve) -springcloud-config unified micro-management service configuration

1. Why should micro-manage unified service configuration

For conventional single application, often used to manage all configuration profiles. For example, a monomer SpringBoot application development, content may be configured in application.yml file. To switch environment, you may be provided a plurality of Profile <br>, and specify spring.profiles.active = {profile} for starting the application.

However, micro-service architecture, configuration management micro services generally have the following requirements:

  1. Centralized management configuration. A micro-service architecture using an application system may contain hundreds or thousands of micro-services, centralized configuration management is very necessary.
  2. Different environments, different configurations. For example, the data source arranged in different environments (development, testing, pre-release, production, etc.) are different.
  3. It can be dynamically adjusted during operation. For example, according to load conditions of each micro-services dynamically adjusts the data source connection pool size thresholds or fuse, and a micro adjustment without stopping the service.
  4. After the configuration changes can be automatically updated. Such as configuring content changes, micro-services can automatically update the configuration.

In summary, for the micro-architecture in terms of service, a common configuration management system is essential, it is common practice to use configuration management server configuration.

2.Spring Cloud Config Introduction

  It provides support for server and client spring cloud config configured to externalize distributed system, which includes Config Server and ConfigClient two parts. Since the Config Server and Config Client implements the abstract map of Spring Environment and PropertySource, therefore, Spring Cloud Config is perfect for Spring applications, of course, can also be used with any application written in other languages.

  Config Server is a laterally extended, centralized configuration server, which is used in each environment centralized configuration management application using Git stored default configuration files (also using Subversion, local file system or storage Vault configuration), can be easily achieve the configuration version control and audit content.

  Config Client Config Server client for operating configuration properties stored in the Config Server. All micro-services point Config Server. Each micro service when started, will request Config Server for configuration properties need, then cache these properties to improve performance.

3. Write Config Server

1. Create several configuration files in the Git repository https://gitee.com/fengyuduke/spring-cloud-config-repo,

E.g:

   

Contents are:

profile=dev-1.0
profile=production-1.0
profile=texst-1.0
profile=default-1.0

In order to control the test version, config-label-v2.0 created for the Git branches, and for each profile 1.0 to 2.0.

2. Create a maven project, Artifacted is microservice-config-server, and add it to the project dependencies.

<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.itmuch.cloud</groupId>
  <artifactId>microservice-config-server</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>microservice-config-server</name>
  <url>http://maven.apache.org</url>

 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
  </properties>

  <dependencies>
           <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
<!--             <scope>provided</scope> -->
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>  <! - this needs to be effective as a true hot deployment -> 
        </ dependency > 
        < dependency > 
            < groupId > org.springframework.cloud </ groupId > 
            < artifactId > the Spring-Cloud-config-Server </ artifactId > 
        < / dependency > 
  </ Dependencies > 
  
<-!    dependent introduction of spring cloud -> 
    < the dependencyManagement > 
        < Dependencies > 
            < dependency > 
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Edgware.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
  
<!--   添加spring-boot 的maven插件 -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
  
</project>

3. Start writing class, add annotations @EnableConfigServer on startup class, which is declared a Config Server.

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

4. Write the configuration file application.yml, and add about content.

Server: 
  Port: 8080 
the Spring: 
  the Application: 
    name: microService -config- Server 
  Cloud: 
    config: 
      Server: 
        git: 
          # Configure Git repository address 
          uri: HTTPS: // gitee.com/fengyuduke/spring-cloud-config-repo 
          # account Git repository 
          username: 
          #git warehouse password 
          password:

In this way, a Config Server is complete.

 

You can use Config Server endpoint to obtain the contents of the configuration file. 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

Can be mapped to the above endpoints {application} - {profile} .properties configuration file, {application} denotes micro service name, {label} Git repository corresponding to branch default master.

According to the above rules, with the present embodiment, the following may be used to access the URL of the master branch Git repository microservice-foo-dev.properties, for example:

  • http://localhost:8080/microservice-foo/dev
  • http://localhost:8080/microservice-foo-dev.properties
  • http://localhost:8080/microservice-foo-dev.yml

have a test:

访问http://localhost:8080/microservice-foo/dev

 

Visit http: // localhost: 8080 / microservice-foo-dev.properties, return to the configuration file attributes:

Visit http: // localhost: 8080 / config-label-v2.0 / microservice-foo-dev.properties, following results were obtained:

Description obtain configuration information Git repository config-label-v2.0 branch.

So far, we have successfully built Config Server, and by way of constructing a URL, obtain configuration information Git repository.

 

4. Write Config Client

1. Create a maven project, ArtifactId is microservice-config-client, and add the following dependence for the project.

<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.itmuch.cloud</groupId>
  <artifactId>microservice-config-client</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>microservice-config-client</name>
  <url>http://maven.apache.org</url>

  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
  </properties>

  <dependencies>
           <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
<!--             <scope>provided</scope> -->
        </dependency>
         <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional> <!-- 这个需要为 true 热部署才有效 -->
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
  </dependencies>
  
<!--   引入spring cloud 的依赖 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Edgware.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
  
<!--   添加spring-boot 的maven插件 -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
  
</project>

2. Create a basic Springboot startup class.

3. Write the profile application.yml, and add the following content.

server:
  port: 8081

4. Create a profile bootstrap.yml, and add the following content.

Spring: 
  file application: 
    {} # file application config server corresponding to the acquired profile 
    name: foo microService- 
  Cloud: 
    config: 
      URI: HTTP: // localhost: 8080 / 
      # Profile config server corresponding to the acquired profile {profile } 
      profile: dev 
      # specify branch Git repository, config server corresponding to the acquired label configuration file} { 
      label: Master

among them:

  • spring.application.name: Config Server corresponding to the acquired profile {application}.
  • spring.cloud.config.uri: Specifies the Config Server address, the default is http: // localhost: 8888.
  • spring.cloud.config.profile: profile Config {profile} Server corresponding to the acquired profile.
  • spring.cloud.config.label: Git repository specified branch, corresponding to the acquired Config Server {label} profile.

It is worth noting that the above properties must be configured in bootstrap.yml file, rather than application.yml. The reason Reference: https: //www.cnblogs.com/fengyuduke/p/11024078.html

 5. Write Controller

@RestController
public class ConfigClientController {
    @Value("${profile}")
    private String profile;
    
    @GetMapping("/profile")
    public String hello() {
        return this.profile;
    }
}

In the Controller, the annotation @Value ( "$ {profile}"), Git repository binding profile property profile.

test

1. Start microservice-config-server.

2. Start microservice-config-client.

3. Visit http: // localhost: 8081 / profile, obtained the following results.

 

DESCRIPTION Config Client configuration can normally be obtained Git repository corresponding to the ambient through the Config Server.

 

Guess you like

Origin www.cnblogs.com/fengyuduke/p/11246332.html