zoukankan      html  css  js  c++  java
  • springcloud(十二)-springcloud-config统一管理微服务配置

    1.为什么要统一管理微服务配置

    对于传统的单体应用,常使用配置文件管理所有配置。例如一个SpringBoot开发的单体应用,可将配置内容放在application.yml文件中。如果需要切换环境,可设置多个Profile,并在启动应用时指定spring.profiles.active={profile}.

    然而,微服务架构中,微服务的配置管理一般有以下需求:

    1. 集中管理配置。一个使用微服务架构的应用系统可能会包含成百上千个微服务,因此集中管理配置是非常有必要的。
    2. 不同环境,不同配置。例如,数据源配置在不同的环境(开发、测试、预发布、生产等)中是不同的。
    3. 运行期间可动态调整。例如,可根据各个微服务的负载情况,动态调整数据源连接池大小或熔断阈值,并且在调整时不停止微服务。
    4. 配置修改后可自动更新。如配置内容发生变化,微服务能够自动更新配置。

    综上所述,对于微服务架构而言,一个通用的配置管理机制是必不可少的,常见的做法是使用配置服务器管理配置。

    2.Spring Cloud Config 简介

      spring cloud config为分布式系统外部化配置提供了服务器和客户端的支持,它包括Config Server和ConfigClient 两部分。由于Config Server和Config Client都实现了对Spring Environment和PropertySource抽象的映射,因此,Spring Cloud Config非常适合Spring应用程序,当然也可与任何其他语言编写的应用程序配合使用。

      Config Server是一个可横向扩展、集中式的配置服务器,它用于集中管理应用程序各个环境下的配置,默认使用Git存储配置文件(也可使用Subversion、本地文件系统或Vault存储配置),因此可以很方便地实现对配置的版本控制与内容审计。

      Config Client 是Config Server 的客户端,用于操作存储在Config Server中的配置属性。所有的微服务都指向Config Server。各个微服务在启动时,会请求Config Server 以获取所需要的配置属性,然后缓存这些属性以提高性能。

    3.编写Config Server

    1.在Git仓库https://gitee.com/fengyuduke/spring-cloud-config-repo中新建几个配置文件,

    例如:

       

    内容分别是:

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

    为了测试版本控制,为该Git创建config-label-v2.0分支,并为各个配置文件中的1.0改为2.0。

    2.创建一个maven工程,Artifacted是microservice-config-server,并为项目添加一下依赖。

    <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> <!-- 这个需要为 true 热部署才有效 -->
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-config-server</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>

    3.编写启动类,在启动类上添加注解@EnableConfigServer,声明这是一个Config Server。

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

    4.编写配置文件application.yml,并在其中添加一下内容。

    server:
      port: 8080
    spring:
      application:
        name: microservice-config-server
      cloud:
        config:
          server:
            git:
              # 配置Git仓库的地址
              uri: https://gitee.com/fengyuduke/spring-cloud-config-repo
              # Git仓库的账号
              username: 
              #Git仓库的密码
              password: 

    这样,一个Config Server就完成了。

    可以使用Config Server的端点获取配置文件的内容。端点与配置文件的映射规则如下:

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

    以上端点都可映射到{application}-{profile}.properties这个配置文件,{application}表示微服务名称,{label}对应Git仓库的分支,默认是master 。

    按照以上规则,对于本例,可使用以下URL访问到Git仓库master分支的microservice-foo-dev.properties,例如:

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

    测试一下:

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

    访问http://localhost:8080/microservice-foo-dev.properties,返回配置文件中的属性:

    访问http://localhost:8080/config-label-v2.0/microservice-foo-dev.properties,获得如下结果:

    说明获得了Git仓库config-label-v2.0分支中的配置信息。

    至此,已成功构建了Config Server,并通过构造URL的方式,获取了Git仓库中的配置信息。

    4.编写Config Client

    1.创建一个maven工程,ArtifactId是microservice-config-client,并为项目添加以下依赖。

    <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.创建一个基本的Springboot启动类。

    3.编写配置文件application.yml,并在其中添加以下内容。

    server:
      port: 8081

    4.创建配置文件bootstrap.yml,并在其中添加如下内容。

    spring:
      application:
        # 对应config server所获取的配置文件的{application}
        name: microservice-foo
      cloud:
        config:
          uri: http://localhost:8080/
          # profile对应config server所获取的配置文件中的{profile}
          profile: dev
          # 指定Git仓库的分支,对应config server所获取的配置文件的{label}
          label: master

    其中:

    • spring.application.name:对应Config Server 所获取的配置文件中的{application}。
    • spring.cloud.config.uri:指定Config Server 的地址,默认是http://localhost:8888.
    • spring.cloud.config.profile:profile对应Config Server所获取的配置文件中的{profile}。
    • spring.cloud.config.label:指定Git仓库的分支,对应Config Server所获取配置文件的{label}。

    值得注意的是,以上属性必须配置在bootstrap.yml文件中,而不是application.yml。原因参考:https://www.cnblogs.com/fengyuduke/p/11024078.html

     5.编写Controller

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

    在Controller中,通过注解@Value("${profile}"),绑定Git仓库配置文件中的profile属性。

    测试

    1.启动microservice-config-server。

    2.启动microservice-config-client.

    3.访问http://localhost:8081/profile,可获得如下结果。

     

    说明Config Client能够正常通过Config Server获得Git仓库中对应环境的配置。

  • 相关阅读:
    HDU 1269 迷宫城堡
    HDU 4771 Stealing Harry Potter's Precious
    HDU 4772 Zhuge Liang's Password
    HDU 1690 Bus System
    HDU 2112 HDU Today
    HDU 1385 Minimum Transport Cost
    HDU 1596 find the safest road
    HDU 2680 Choose the best route
    HDU 2066 一个人的旅行
    AssetBundle管理机制(下)
  • 原文地址:https://www.cnblogs.com/fengyuduke/p/11246332.html
Copyright © 2011-2022 走看看