十三 SpringCloud Config分布式配置中心

clipboard

一、概述

1、分布式服务过程中面临的一些问题?

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

SpringCloud提供了ConfigServer来解决这个问题,否则我们的每个微服务都带着自己的一个application.yml,上百个配置文件

的管理 o(╥﹏╥)o

2、SpringCloud-config是什么?

中心化的外部配置

clipboard

clipboard

3、能干嘛

1)集中管理配置文件

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

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

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

5)将配置信息以rest接口的形式暴露

4、与 github整合配置

SpringCloud Config默认使用Git来存储配置文件

二、config服务端的配置和测试

项目结构

clipboard

1) pom文件

<dependencies>
    <!--添加消息总线RabbitMQ支持-->
    <!--<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-bus-amqp</artifactId>
    </dependency>-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</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-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>
</dependencies>

2)配置文件

server:
  port: 3344

spring:
  application:
    name:  cloud-config-center #注册进Eureka服务器的微服务名
  cloud:
    config:
      server:
        git:
          uri: https://github.com/zzyybs/springcloud-config.git  #GitHub上面的git仓库名字
          ####搜索目录
          search-paths:
            - springcloud-config
           #设置跳过ssl校验
          skip-ssl-validation: true 
      ####读取分支
      label: master
#服务注册到eureka 上
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka

3)主启动类

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

4)启动成功后,访问路径 http://localhost:3344/master/config-dev.yml

clipboard

【config-server读取配置规则】

① /label/{application}-{profile}.yml

http://localhost:3344/master/config-dev.yml
http://localhost:3344/master/config-dev.yml
http://localhost:3344/master/config-dev.yml

http://localhost:3344/dev/config-dev.yml
http://localhost:3344/dev/config-dev.yml
http://localhost:3344/dev/config-dev.yml

②、/{application}-{profile}.yml

http://http://localhost:3344/config-dev.yml

为什么没有label,这是因为application.yml中设置了默认的label

clipboard

③ /{application}/{profile}/label

http://localhost:3344/config/test/master

三、config客户端的配置与测试

application.yml和 bootstrap.yml的区别?

① application.yml是用户级别的资源配置项,bootstrap是系统级别的,优先级更高

② SpringCloud会创建一个“BootStrap Context"。作为Spring应用的 ”Application Context“的父上下文。初始化的时候Bootstrap Context负责从外部源加载配置属性并解析配置。这两个上下文共享一个从外部获取的Environment

③ Bootstrap 属性有着高优先级,默认情况下,它们不会被本地配置覆盖。Bootstrap Context和 Application Context有着不同的约定,所以新增了一个bootstrap.yml文件,保证Bootstrap Context和 Application Context的分离

项目路径

clipboard

1)添加pom文件

<dependencies>
    <dependency> <!-- config的客户端依赖 -->
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</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-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>
</dependencies>

2) 编写配置文件 bootstrap.yml

server:
  port: 3355

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

#服务注册到eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka

3) 主启动类

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

4)controller类:

这个接口是为了直观的查看有没有获取到git上的配置,直接获取配置文件中的 config.info

@RestController
public class ConfigClientController {

    @Value("${config.info}") 
    private String configInfo;  //相当于将git上的配置加载到本地的bootstrap.yml中

    @Value("${server.port}")
    private String serverPort;

    @GetMapping("/configInfo")
    public String getConfigInfo()
    {
        return "server port:"+ serverPort + " ---> " +configInfo;
    }
}

5)测试,访问http://localhost:3355/configInfo,获取github中相应文件的内容

clipboard

由上述可见:成功实现了客户端3355访问服务端3344,从而拿到git上的配置

6)存在的问题

分布式配置的动态刷新问题

【问题详情】

Linux运维修改GitHub上的配置文件内容

clipboard

改为

clipboard

刷新上面的config-server http://localhost:3344/master/config-dev.yml

clipboard

刷新上面的config-client http://localhost:3355/configInfo

clipboard

可见config-client端没有任何变化,除非自己重启,难道每次运维修改配置文件,客户端都要自己重启

四、Config客户端 动态刷新之手动版

1、配置手动动态刷新步骤

修改config-client模块

1)pom文件引入 actuator监控依赖

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

2)yml文件暴露监控端口

# 暴露监控端点
management:
  endpoints:
    web:
      exposure:
        include: "*"

3)在业务类controller上添加 @refreshScope注解

@RestController
@RefreshScope
public class ConfigClientController {

    @Value("${config.info}")
    private String configInfo; //相当于将git上的配置加载到本地的bootstrap.yml中

    @Value("${server.port}")
    private String serverPort;

    @GetMapping("/configInfo")
    public String getConfigInfo()
    {
        return "server port:"+ serverPort + " ---> " +configInfo;
    }
}

4)此时修改github上的配置

clipboard

查看config-server 3344

clipboard

5)运维人员手动发送post请求刷新config-client 3355

clipboard

查看config-client 3355,发现成功刷新配置

clipboard

2、存在的问题

image

到此SpringCoud Config的基本用法就介绍完了,另外还可以配合 SpringCloud Bus实现自动刷新,下节再进行介绍

原文地址:https://www.cnblogs.com/houchen/p/14711653.html