一,构建配置中心

  1.在pom.xml文件中添加相关依赖 

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

  </dependencies>

  

  2.在SpringBoot程序主类上添加@EnableConfigServer注解, 开启SpringCloud Config的服务端功能

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

  

  3.在application.properties中添加相关配置信息

  spring.application.name=config-server
  server.port=7001
  #Git仓库位置   spring.cloud.config.server.git.uri=https://gitee.com/*****/config-properties.git   #指定Git仓库路径下的文件夹   spring.cloud.config.server.git.search-paths=myconfig   spring.cloud.config.server.git.username=username   spring.cloud.config.server.git.password=password   #指定Git分支   spring.cloud.config.label=dev

  

  4.URL调用查看配置是否生效

    (1) /{application}/{profile}[/{label}]

    (2) /{application}-{profile}.yml

  上面的url会映射{application}-{profile}.properties对应的配置文件,其中{label}对应Git不同的分支, 默认为master.

  查看Git项目中的application-myproject.properties文件中的内容, 调用http://localhost:7001/application/myproject或http://localhost:7001/application/myproject/master

{
    "name": "application",
    "profiles": ["myproject"],
    "label": null,
    "version": "",
    "state": null,
    "propertySources": [{
        "name": "https://gitee.com/****/config-properties.git/master/application-myproject.properties",
        "source": {
            "server.servlet.context-path": "/myproject",
            "server.port": "7002",
            "spring.application.name": "myproject",
            "spring.cloud.consul.host": "localhost",
            "spring.cloud.consul.port": "8500",
            "spring.cloud.consul.discovery.health-check-path": "${server.servlet.context-path}/healthcheck",
            "spring.cloud.consul.discovery.register": "true"
        }
    }]
}

  配置服务器在从Git中获取配置信息后, 会存储一份在 config-server 的文件系统中, 实质上 config-server 是通过 git clone 命令将配置内容复制了一份在本地存储, 然后读取这些内容并返回给微服务应用进行加载.

  config-server 通过 Git 在本地库暂存,可以有效防止 Git 仓库出现故障而引起无法加载配置信息的情况. 

 

二.客户端配置映射

  1.在pom.xml文件中添加相关依赖 

         <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>    
     <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-consul-discovery</artifactId>
     </dependency>

  2.创建SpringBoot的应用主类

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

  3.创建boostrap.properties配置,来指定获取配置文件的config-server位置

  spring.cloud.config.enabled=true

  #配置中心config-server的地址
  spring.cloud.config.uri=http://127.0.0.1:23001/

  #对应配置文件规则中的{application}部分
  spring.cloud.config.name=application

  #对应配置文件规则中的{profile}部分
  spring.cloud.config.profile=order

  #Git分支
  spring.cloud.config.label=dev
  spring.cloud.config.username=q741622318@163.com
  spring.cloud.config.password=bananas66424

  SpringBoot对配置文件的加载顺序, 本应用jar报之外的配置文件加载会优先于应用jar包内的配置内容, 而通过bootstrap.properties对config-server的配置, 使得该应用从config-server中获取一些外部配置信息, 这些信息的优先级比本地的内容要高, 从而实现外部化配置.

 

三.服务端详解

    (1)Git仓库的Hook功能可以帮助我们实时监控配置内容的修改.

    (2)占位符配置URI

      {application}, {profile}, {label}这些占位符除了用于标识配置文件的规则之外, 还可以用于 Config Server 中对 Git 仓库地址的URI配置.

      比如通过{application}占位符来实现同时匹配多个不同服务的配置仓库

     spring.cloud.config.server.git.uri=https://gitee.com/*****/{application}-properties.git

      又比如通过{application}占位符实现一个应用一个目录的效果

     spring.cloud.config.server.git.search-paths={application}

    (3)属性覆盖

      Config Server 有"属性覆盖"的特性, 它可以让开发人员为所有的应用提供配置属性, 只需要通过 spring.cloud.config.server.overrides 属性来设置键值对的参数, 这些参数会以 Map 的方式加载到客户端的配置中. 比如:

    spring.cloud.config.server.overrides.name=userstring

    (4)安全保护

      由于我们的微服务应用和配置中心都构建于 Spring Boot 基础上,所以与 Spring Security 结合使用会更加方便.

      在 pom.xml 中引入依赖后,不需要任何其他改动就能实现对配置中心访问的安全保护.

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

      在配置文件中指定用户和密码, 比如:

    security.user.name=user
    security.user.password=37CC5635-559b-4e6f-b633-7e932b813f73

      由于我们已经为 config-server 设置了安全保护, 需要在客户端中加入安全信息才可以正常访问服务端.

    

    spring.cloud.config.username=user
    spring.cloud.config.password=37CC5635-559b-4e6f-b633-7e932b813f73

    (5)加密解密

      使用加密解密功能时, 需要在配置中心的运行环境中安装不限长度的JCE版本. 我们可以从Oracle 的官方网站下载, 它是一个压缩包,解压后可以看到下面三个文件:

     README.TXT
     local_policy.jar
     US_export_policy.jar

      之后将 local_policy.jar 和 US_export_policy.jar 两个文件复制到$JAVA_HOME/jre/lib/security 目录下.

      再配置加密数据, 如下:

    spring.datasource.username=user
    spring.datasource.password={cipher}dba650baa81d78bd08799d8d4429de499bd4c2053c05f029e7cfbf143695f5b

      通过在属性值前使用{cipher}前缀来标注该内容为加密值, 当微服务客户端加载配置时, 配置中心会自动为带有{cipher}前缀的值进行解密.

    

 

 

 

      

内容来源于网络如有侵权请私信删除
你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!