springcloud之config分布式配置中心

springcloud学习笔记,第8章,Config微服务配置中心

简介:

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

微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的颗粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要的配置信息才能运行,所以一套集中式的、动态的配置管理设施是必不可少的。

springcloud提供了configServer来解决这个问题,我们每一个微服务自己带一个application.yml,那么上百个配置文件的管理就是一个大问题了。

就像我们之前学的:可以看到,每个微服务都需要一个配置文件,并且,如果有几个微服务都需要连接数据库。那么就需要配4次数据库相关配置,并且当数据库发生改动,那么需要同时修改4个微服务的配置文件才可以。

是什么?

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

能干嘛?

  1. 集中管理配置文件

  2. 不同环境不同配置,动态化的配置更新,分环境部署比如dev/test/prod/beta/release

  3. 运行期间动态调整配置,不在需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息

  4. 当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置

  5. 将配置信息以REST接口的形式暴露,post/curl访问刷新即可

Config服务端配置与测试:

  1. 使用github作为配置中心的仓库:

    • 在github上创建一个配置中心仓库,注意得是公共仓库。不然会有密码问题。

    • 从github上将仓库代码拷贝至本地

  2. 新建config模块:

    cloud-config-center-3344,它即为cloud的配置中心模块cloudConfig Center。

  3. pom:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    <!--config-->
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
    <!--eureka client-->
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <!--web-->
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </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>
    <scope>runtime</scope>
    <optional>true</optional>
    </dependency>
    <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    </dependency>
  4. 配置文件YML:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    server:
    port: 3344

    spring:
    application:
    name: cloud-config-center
    cloud:
    config:
    server:
    git:
    uri: https://github.com/Aaron-boom/springcloud-config.git # git@github.com:Aaron-boom/springcloud-config.git,测试不通git的,使用https的。https://github.com/Aaron-boom/springcloud-config.git
    search-paths:
    - springcloud-config
    label: master

    eureka:
    client:
    service-url:
    defaultZone: http://localhost:7001/eureka
  1. 主启动类注解:

    1
    2
    @EnableEurekaClient
    @EnableConfigServer
  2. 修改hosts(可选):

    windows下修改host文件的映射,可以使用主机名进行访问。

  3. 配置完成:

    启动eureka7001和config3344

  4. 读取配置文件的3种规则:

    • 第一种

    • 第二种

    • 第三种

    • 总结:

Config客户端配置与测试:

使用3355客户端去访问3344服务端的配置信息。

  1. 创建Config客户端项目:cloud-config-client-3355

  2. pom:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    <!--config的客户端-->
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <!--eureka client-->
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
  3. 配置文件:

    • bootstrap.yml是什么:

    • 内容:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      server:
      port: 3355

      spring:
      application:
      name: config-client
      cloud:
      config:
      label: master # 分支名称
      name: config # 配置文件名称
      profile: dev # 读取后缀名称 上述三个综合:master分支上config-dev.yml的配置文件被读取为http://config-3344.com/master/conf
      uri: http://localhost:3344 # 配置中心地址

      eureka:
      client:
      service-url:
      defaultZone: http://localhost:7001/eureka
  4. 主启动类:

    1
    2
    3
    4
    5
    6
    7
    @SpringBootApplication
    @EnableEurekaClient
    public class ConfigClientMain3355 {
    public static void main(String[] args) {
    SpringApplication.run(ConfigClientMain3355.class, args);
    }
    }
  5. controller类:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    @RestController
    public class ConfigClientController {
    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/configInfo")
    public String getConfigInfo(){
    return configInfo;
    }
    }
  6. 测试:

  • 注意,读取的是config的info信息。

Config客户端之动态刷新:

上面3355确实获取到了配置文件,但是如果此时配置文件修改了,3355是获取不到的。3344可以实时获取到最新配置文件,但是3355却获取不到,除非重启服务。

springcloud bus配合springcloud config使用可以实现配置的动态刷新~

  1. 修改3355模块

  2. POM引入actuator监控

  3. 修改Yml,暴露监控端口

    1
    2
    3
    4
    5
    management:
    endpoints:
    web:
    exposure:
    include: "*"
  4. @RefreshScope业务类Controller修改

  5. 此时再修改github–》3344–》3355,再次测试:

    1. http://localhost:3344/master/config-dev.yml
    2. http://localhost:3355/configInfo
    3. 这里是没有生效的,3344访问到变化了,3355没有访问到
  6. 补救

    • 需要运维人员发送post请求(必须是post)刷新3355。

    • 此时再访问:http://localhost:3355/configInfo成功修改。

此时新的问题又出现了,就是:

  1. 假如有多个微服务客户端。。。。怎么可能去手动刷新?每个微服务都post请求一次?
  2. 可否广播。一次通知,处处生效?
  3. 我们想大范围的刷新,怎么办?

其实这就是下一章笔记要讲的,使用springcloud bus配合springcloud config使用可以实现配置的动态刷新~

  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!

请我喝杯咖啡吧~

支付宝
微信