10.Config

128 阅读4分钟

10.1   概述

  • 分布式系统面临的---配置问题

微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要的配置信息才能运行,所以一套集中式的、动态的配置管理设施是必不可少的。SpringCloud提供了ConfigServer来解决这个问题,我们每一个微服务自己带着一个application.yml,上百个配置文件的管理....../(ㄒoㄒ)/~~

  • 是什么image.png

SpringCloud Config为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置。

SpringCloud Config分为服务端和客户端两部分。

服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,加密/解密信息等访问接口

客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息配置服务器默认采用git来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过git客户端工具来方便的管理和访问配置内容。

  • 能干嘛
  • image.png
  • dev:    开发环境
  • test:    测试环境
  • prod:    产品环境
  • beta:    预发布环境
  • release:    灰度发布

10.2  SpringCloud Config服务端配置

image.png 1  用自己的GitHub账号在GitHub上新建一个名为microservicecloud-config的新Repository

2  由上一步获得协议的git地址

3  本地硬盘目录上新建git仓库并clone  microservicecloud-config仓库到本地

4  在本地microservicecloud-config仓库里面新建一个application.yml(保存格式必须为UTF-8

 spring:
   profiles:
     active:
     - dev
 ---
 spring:
   profiles: dev     #开发环境
   application: 
     name: microservicecloud-config-atguigu-dev
 ---
 spring:
   profiles: test   #测试环境
   application: 
     name: microservicecloud-config-atguigu-test
 #  请保存为UTF-8格式

5  将上一步的YML文件推送到github仓库上

6  新建Module模块microservicecloud-config-3344它即为Cloud的配置中心模块

7  修改pom和yml文件

 <dependencies>
    <!-- springCloud Config -->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
    <!-- 避免Config的Git插件报错:org/eclipse/jgit/api/TransportConfigCallback  -->
    <dependency>
        <groupId>org.eclipse.jgit</groupId>
        <artifactId>org.eclipse.jgit</artifactId>
        <version>4.10.0.201712302008-r</version>
    </dependency>
    <!-- 图形化监控 -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <!-- 熔断 -->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-hystrix</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-eureka</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-jetty</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>
    </dependency>
    <!-- 热部署插件 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>springloaded</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-devtools</artifactId>
    </dependency>
   </dependencies> 
 ​
 ​
 ​
 ​
 server:
   port: 3344
 ​
 spring:
   application:
     name:  cloud-config-center #注册进Eureka服务器的微服务名
   cloud:
     config:
       # 配置中心服务器端配置
       server:
         # git地址,建议填写uri为http,而不是git,不然认证失败
         #当然使用git,也可以不过必须要加username,password
         git:
           uri: https://gitee.com/iamyaojian/springcloud-config.git #GitHub上面的git仓库名字
           username: iamyaojian
           password: asdfghyj2580
           # 搜索路径
           search-paths:
             - springcloud-config
       # 读取分支
       label: springcloud2
 ​
 #服务注册到eureka地址
 eureka:
   client:
     service-url:
       defaultZone: http://localhost:7001/eureka

8  主启动类Config_3344_StartSpringCloudApp添加注解 @EnableConfigServer ** **

 @SpringBootApplication
 @EnableConfigServer

9  测试通过Config微服务是否可以从GitHub上获取配置内容

 启动微服务3344
 http://config-3344.com:3344/application-dev.yml
 http://config-3344.com:3344/application-test.yml
 http://config-3344.com:3344/application-xxx.yml(不存在的配置)

配置读取规则

image.png

image.png

10.3  SpringCloud Config客户端配置与测试

image.png

客户端3355调用SpringCloud  Config服务端3344获取仓库中的配置文件

10.4  SpringCloud Config配置实战

image.png 目前情况:

1  Config服务端配置配置OK且测试通过,我们可以和config+GitHub进行配置修改并获得内容

2 此时我们做一个eureka服务+一个Dept访问的微服务,将两个微服务的配置统一由于github获得实现统一配置分布式管理,完成多环境的变更

Config版的eureka服务端

1  新建工程microservicecloud-config-eureka-client-7001

2  修改pom文件,添加config依赖

 <!-- SpringCloudConfig配置 -->
     <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-config</artifactId>
     </dependency> 
     <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-eureka-server</artifactId>
     </dependency>

3  添加bootstrap.yml和application.yml文件

 bootstrap.yml:
 spring: 
   cloud: 
     config: 
       name: microservicecloud-config-eureka-client     #需要从github上读取的资源名称,注意没有yml后缀名
       profile: dev 
       label: master 
       uri: http://config-3344.com:3344      #SpringCloudConfig获取的服务地址
       
 
 application.yml:
 spring:
   application:
     name: microservicecloud-config-eureka-client

4  主启动类Config_Git_EurekaServerApplication添加注解

5  测试 image.png

Config版的dept微服务

1  参考之前的8001拷贝后新建工程microservicecloud-config-dept-client-8001

2  修改pom文件,添加config依赖

3  添加bootstrap.yml和application.yml文件

 bootstrap.yml:
 spring:
   cloud:
     config:
       name: microservicecloud-config-dept-client #需要从github上读取的资源名称,注意没有yml后缀名
       #profile配置是什么就取什么配置dev or test
       #profile: dev
       profile: test
       label: master
       uri: http://config-3344.com:3344  #SpringCloudConfig获取的服务地址
       
 
 application.yml:
 spring:
   application:
     name: microservicecloud-config-dept-client

4  主启动类及其它一套业务逻辑代码

5  测试 image.png