zoukankan      html  css  js  c++  java
  • 六、springcloud之配置中心Config

    一、配置中心提供的核心功能

      Spring Cloud Config为服务端和客户端提供了分布式系统的外部化配置支持。配置服务器为各应用的所有环境提供了一个中心化的外部配置。它实现了对服务端和客户端对Spring Environment和PropertySource抽象的映射

      Spring Cloud Config项目是一个解决分布式系统的配置管理方案。它包含了Client和Server两个部分,server提供配置文件的存储、以接口的形式将配置文件的内容提供出去,client通过接口获取数据、并依据此数据初始化自己的应用。

      Spring cloud使用git或svn存放配置文件,当然他也提供本地化文件系统的存储方式,默认情况下使用git。

    二、构建Config Server

      分为三步:

      1.pom.xml中引入spring-cloud-config-server依赖:

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

      2.启动类添加@EnableConfigServer注解,激活对配置中心的支持

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

      3.配置文件

        Git配置:(开发,测试,生产三份文件)

    server:
      port: 8001
    spring:
      application:
        name: spring-cloud-config-server
      cloud:
        config:
          server:
            git:
              uri: https或ssh     # 配置git仓库的地址
              search-paths:   # git仓库地址下的相对地址,可以配置多个,用,分割。
              username:       # git仓库的账号
              password:       # git仓库的密码

        svn配置: (和git版本稍有区别,需要显示声明subversion.

    server:
      port: 8001
    
    spring:
      cloud:
        config:
          server:
            svn:
              uri: 
              username:
              password:
            default-label: trunk
      profiles:
        active: subversion
      application:
        name: spring-cloud-config-server

         本地存储配置的方式:(只需设置属性)

          spring.profiles.active=native

      Config Server会默认从应用的src/main/resource目录下检索配置文件。也可以通过spring.cloud.config.server.native.searchLocations=file:F:/properties/属性来指定配置文件的位置。

      虽然Spring Cloud Config提供了这样的功能,但是为了支持更好的管理内容和版本控制的功能,还是推荐使用git的方式。

     4.测试:

        github创建了一个springcloudconfigtest目录作为配置仓库,并根据不同环境新建了下面四个配置文件:

    • config-test-dev.properties
    • config-test-test.properties
    • config-test-prod.properties

        为每个配置文件分别设置了testproperties属性赋予不同的值

          如: hello im dev/test/pro   

        测试server端是否可以读取到github上面的配置信息,直接访问:http://localhost:8001/config-test/dev

    {
        "name": "config-test",
        "profiles": ["dev"],
        "label": null,
        "version": "",
        "state": null,
        "propertySources": [{
            "name": "{git/svn/本地配置地址}dev.yml",
            "source": {
                "debug": true,
                "server.port": 8001,
           "testproperties": "hello im dev" 
           ...//数据源,redis等配置
            }
        }]
    }

    上述的返回的信息包含了配置文件的位置、版本、配置文件的名称以及配置文件中的具体内容,说明server端已经成功获取了git仓库的配置信息。

      直接查看配置文件中的配置信息可访问http://localhost:8001/config-test-dev.properties,返回:testproperties: hello im dev

       仓库中的配置文件会被转换成web接口,访问可以参照以下的规则:

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

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

      如config-test-dev.properties,它的application是config-test,profile是dev。client会根据填写的参数来选择读取对应的配置。

        

      修改配置文件中的配置,并提交,server会自动更新提交的配置

    三、微服务客户端(获取server的配置)

      1.添加依赖:spring-cloud-starter-config

    <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>

        引入spring-boot-starter-web包方便web测试

      2.创建启动类

      启动类只需要@SpringBootApplication注解就可以

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

      3.配置文件

      需要配置两个配置文件,application.yml和bootstrap.yml

      application.yml如下:

    spring:
      application:
        name: spring-cloud-config-client
    server:
      port: 8002

      bootstrap.properties如下:

    spring:
        cloud:
             config:
            name: "config-test" lab: master uri: http://localhost:8001/ profile: dev

      说明:

    • spring.cloud.config.name:对应{application}部分
    • spring.cloud.config.profile:对应{profile}部分
    • spring.cloud.config.label:对应git的分支。如果配置中心使用的是本地存储,则该参数无用
    • spring.cloud.config.uri:配置中心的具体地址
    • spring.cloud.config.discovery.service-id:指定配置中心的service-id,便于扩展为高可用配置集群

      特别注意:上面这些与spring-cloud相关的属性必须配置在bootstrap.properties中,config部分内容才能被正确加载。因为config的相关配置会先于application.properties,而bootstrap.properties的加载也是先于application.properties。

      测试:

      使用@Value注解来获取server端参数的值

     @Value("${server端的参数}")
      private String hello;

      遇到的问题:此时在修改server端的配置文件的参数值,server可以读取到修改后的配置,但是client端读取的值还是旧的

      解决方案:/refresh

        Spring Cloud Config分服务端和客户端,服务端负责将git(svn)中存储的配置文件发布成REST接口,客户端可以从服务端REST接口获取配置。但客户端并不能主动感知到配置的变化,从而主动去获取新的配置。客户端如何去主动获取新的配置信息呢,springcloud已经给我们提供了解决方案,每个客户端通过POST方法触发各自的/refresh

      1.添加依赖

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

      增加了spring-boot-starter-actuator包,spring-boot-starter-actuator是一套监控的功能,可以监控程序在运行时状态,其中就包括/refresh的功能。

      2.开启更新机制

      需要给加载变量的类上面加载@RefreshScope,在客户端执行/refresh的时候就会更新此类下面的变量值。

    @RestController
    @RefreshScope // 使用该注解的类,会在接到SpringCloud配置中心配置刷新的时候,自动将新的配置更新到该类对应的字段中。

      3.springboot 1.5.X 以上默认开通了安全认证,所以需要在配置文件application.properties添加以下配置

    management.security.enabled=false

      tip:可以使用cmd,进行post访问

        curl -X POST http://localhost:8002/refresh

      每次手动刷新客户端也很麻烦,有没有什么办法只要提交代码就自动调用客户端来更新呢,github的webhook是一个好的办法

      webhook

      参见:http://www.ityouknow.com/springcloud/2017/05/23/springcloud-config-svn-refresh.html

    参考:http://www.ityouknow.com/springcloud/2017/05/22/springcloud-config-git.html

       http://blog.didispace.com/springcloud4/

  • 相关阅读:
    【java】关于泛型修饰符
    【java】使用lambda和函数接口Comparator
    【js】简单模拟JQuery原理
    【js】事件捕获与冒泡 (这可能是这个知识点 字最少,且讲的最清楚的教程)
    【js】实现 鼠标按下并未松开 事件
    【js】多维排序
    【JS】 初识JS原型
    【MyBatis】逆向工程
    【Struts】struts的DevMode模式
    Linux下gcc相关
  • 原文地址:https://www.cnblogs.com/soul-wonder/p/9214944.html
Copyright © 2011-2022 走看看