spring-cloud-config使用、基本配置及拓展

    技术2022-07-11  125

    SpringCloudConfig 使用文档

    一、简介

    1.1. 配置中心

    配置中心是一种为方便运维人员统一管理多应用多环境多配置文件的分布式服务。没有配置中心的时候需要人工手动逐个修改每个服务的配置,而有配置中心后,在需要修改某配置时,只要一处修改,全局生效。

    1.2. SpringCloudConfig

    SpringCloudConfig是Spring对分布式配置中心的一种实现方式,具有高可用性,并支持配置文件的多种存储方式(如git/svn/本地磁盘/vault等)。

    二、SpringCloudConfig依赖引入及其配置

    2.1. 服务器端config-server

    2.1.1. 引入相关jar包到工程

    <!-- config-server依赖包 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <!-- config-server需要注册到服务注册与发现中心 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!-- 如依赖的eureka-client版本较新,则需要引入hystrix否则抛出无法找到类HystrixCommandAspect的异常,具体可观察spring-cloud-starter-netflix-eureka-client的依赖 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>

    2.1.2. 启动类上添加@EnableConfigServer

    @SpringBootApplication @EnableDiscoveryClient @EnableConfigServer public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } } 由于SpringCloud基于SpringBoot,@SpringBootApplication注解是必须的,具体作用不赘述,亦可用@SpringCloudApplication代替。@EnableDiscoveryClient注解是为了能够让注册中心发现,扫描到本服务,从Spring Cloud Edgware开始,可省略。@EnableConfigServer用来启动配置中心服务器端的相关功能

    2.1.3. 配置文件添加配置

    spring-cloud-config-server提供了4种配置,可以通过不同名字来激活:

    git:默认值,表示去Git仓库读取配置文件;subversion:表示去SVN仓库读取配置文件;native:表示去本地文件系统读取配置文件;vault:表示去Vault(一种资源控制工具)中读取配置文件;

    在基本SpringBoot的配置基础上添加配置中心的相关配置:

    #本服务端口 默认8080 一般都需要重新定义 server: port: 8080 #eureka服务注册 服务发现于注册端口一般需要用户自定义 eureka: client: service-url: defaultZone: http://localhost:8888/eureka/ spring: application: name: config-server cloud: config: server: bootstrap: true #本地存储文件路径 native: search-locations: E:\workspace\personal\system-stage\config #git存储配置文件,本地存储时也需要配置 git: basedir: E:\workspace\personal\system-stage\config uri: E:\workspace\personal\system-stage\config #也可以为git服务器地址,如uri: http://localhost:18080/root/temp.git #不在根目录时则需要配置目录 search-paths: /temp #git用户信息 username: root password: 12345678

    2.1.4. 启动程序

    启动程序后可以使用Config Server的端点获取配置文件的内容,端点与配置文件的映射规则如下:

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

    {application} 是应用名称,对应配置文件的名称部分。

    {profile} 是配置文件的版本。

    {label} 表示分支,如果是git则默认是master分支。

    2.2. 客户端config-client

    2.2.1. 引入相关jar包到工程

    <!-- config-client依赖包 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!-- config-client需要注册到服务注册与发现中心 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>

    2.2.2. 配置文件相关配置

    在bootstrap配置文件中配置,其他所有配置均写在application文件中,且由配置中心管理。

    spring: application: name: project-app cloud: config: name: project uri: http://localhost:21000/ username: user password: admin label: master stream: kafka: binder: brokers: localhost:9092 auto-add-partitions: true bindings: springCloudBusInput: destination: springCloudBus

    2.2.3. @RefreshScope注解

    修改配置文件后需要运行时刷新配置的bean需要用@RefreshScope注解进行修饰,内部实际上是注解@Scope("refresh"),主要作用是 将修饰的Spring管理的Bean配置为refresh的作用域,可在运行时刷新配置,实际刷新逻辑为销毁修饰对的bean后重新创建。

    三、拓展使用

    3.1. 文件commit后自动热更新应用配置

    文件变更且提交后,Jenkins Hook触发,用Bus事件通过Stream协调消息中间件广播到每个应用,从而运行时全局修改配置 (目前暂未验证成功,一直没有成功发送消息,后续成功后更新本部分)

    <!--接收config-server相关的刷新请求--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-monitor</artifactId> </dependency> <!-- bus 通过kafka消息 实时刷新所有应用 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-kafka</artifactId> </dependency> <!-- bus 通过rabbitMQ消息 以amqp协议形式 实时刷新所有应用 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency>

    配置文件需要暴露bus-refresh端点以及配置bus和stream相关的值。

    服务器端需要绑定生产消息到哪个destination,具体配置如下::

    #暴露所有端点(主要是bus-refresh端点)用于bus的刷新 management: endpoints: web: exposure: include: "*" spring: cloud: #配置bus的destination bus: trace: enabled: true destination: springCloudBus #配置stream的绑定关系 stream: kafka: binder: brokers: localhost:9092 auto-create-topics: true replication-factor: 2 required-acks: -1 bindings: springCloudBusOutput: destination: springCloudBus content-type: text/plain

    客户端需要绑定消息监听的destination,具体配置如下:

    spring: cloud: stream: kafka: binder: brokers: localhost:9092 auto-add-partitions: true bindings: springCloudBusInput: destination: springCloudBus

    3.2. 安全性及相关加密

    首先需要引入spring-boot-starter-security的依赖:

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

    添加如下配置可以校验用户身份验证:

    spring: #访问配置中心的身份验证 配置后客户端也需要对应的用户密码来访问 security: user: name: user password: admin

    添加如下配置可以加密需要加密的密钥,属性值前使用{cipher}前缀来标注该内容是一个加密值,通过encrypt.key属性在配置文件中直接指定密钥信息:

    spring: cloud: config: server: #加密功能 encrypt: enabled: true encrypt: key: secret

    四、注意事项

    配置中心仅支持properties和yml文件的热加载,不支持xml格式...

    五、总结

    5.1. 服务器配置文件

    #本服务端口 默认8080 一般都需要重新定义 server: port: 21000 #eureka服务注册 服务发现于注册端口一般需要用户自定义 eureka: client: service-url: defaultZone: http://localhost:8888/eureka/ spring: application: name: config-server cloud: config: server: bootstrap: true #本地存储文件路径 native: search-locations: /config #git存储配置文件,本地存储时也需要配置 git: basedir: /config uri: /config #也可以为git服务器地址,如uri: http://localhost:18080/config-files.git #不在根目录时则需要配置目录 search-paths: /config #git用户验证 username: root password: 12345678 #加密功能 encrypt: enabled: true #覆盖指定参数 #overrides: bus: trace: enabled: true destination: springCloudBus stream: kafka: binder: brokers: kafkaHost:9092 auto-create-topics: true replication-factor: 2 required-acks: -1 bindings: springCloudBusOutput: destination: springCloudBus content-type: text/plain #访问配置中心的身份验证 配置后客户端也需要对应的用户密码来访问 security: user: name: user password: admin #暴露所有端点(主要是bus-refresh端点)用于bus的刷新 management: endpoints: web: exposure: include: "*"

    5.2. 客户端配置文件

    spring: application: name: project-app cloud: config: name: project uri: http://localhost:21000/ username: user password: admin label: master #开启所有参数覆盖 #override-none: true bus: trace: enabled: true stream: kafka: binder: brokers: kafkaHost:9092 auto-add-partitions: true bindings: springCloudBusInput: destination: springCloudBus
    Processed: 0.009, SQL: 9