「学习笔记」SpringCloud(三)Config配置中心
1. Spring Cloud Config简介
Spring Cloud Config可以为微服务架构中的应用提供集中化的外部配置支持,它分为服务端和客户端两个部分。
服务端被称为分布式配置中心,它是个独立的应用,可以从配置仓库获取配置信息并提供给客户端使用。
客户端可以通过配置中心来获取配置信息,在启动时加载配置。
Spring Cloud Config默认采用Git来存储配置信息,所以天然就支持配置信息的版本管理,并且可以使用Git客户端来方便地管理和访问配置信息。
2. 准备工作
因为config server是需要到git上拉取配置文件的,所以还需要在远程的git上新建一个存放配置文件的仓库,
如下仓库中存放客户端配置文件:
application-beta.yml
application-dev.yml
application-prod.yml
3. 配置中心服务端搭建
- 搭建服务端配置中心(
config-server),在pom.xml文件中添加依赖:
<!--配置服务-->
<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>
- 在服务端编辑
application.yml配置文件内容如下:
server:
port: 8009
# 指定当前服务的名称,这个名称会注册到注册中心
spring:
application:
name: config-server
cloud:
config:
server:
git: #配置存储配置信息的Git仓库
uri: http://git.xxx.com/config-files.git # 远程git仓库的地址
username: username # git账户名
password: password # git密码
clone-on-start: true # 开启启动时直接从git获取配置
search-paths: /** # 指定搜索根路径下的目录,若有多个路径使用逗号隔开
# 指定服务注册中心的地址
eureka:
instance:
prefer-ip-address: true # 是否使用 ip 地址注册
instance-id: ${spring.cloud.client.ip-address}:${server.port} # ip:port
client:
service-url: # 设置服务注册中心地址
defaultZone: http://localhost:18000/eureka/
- 在启动类上,加上
@EnableConfigServer注解,声明这是一个config-server。代码如下:
/**
* `@EnableDiscoveryClient`: 声明一个可以被发现的客户端
* `@EnableConfigServer`: 声明一个Config配置服务
*/
@EnableConfigServer
@EnableDiscoveryClient
@SpringBootApplication
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
}
}
- 启动项目,访问
http://localhost:8009/master/application-dev.yml,可以看到能够访问到客户端配置文件的内容。
4. 客户端获取配置
- 在
pom.xml文件中添加依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
- 编辑
bootstrap.yml配置文件内容如下:
server:
port: 18001
spring:
cloud:
config: #Config客户端配置
profile: dev #启用配置后缀名称
label: master #分支名称
uri: http://localhost:8009 #配置中心地址
name: application #配置文件名称
启动服务,配置中心读取的配置生效。
5. 获取配置信息的基本规则
# 获取配置信息
/{label}/{name}‐{profile}
# 获取配置文件信息
/{label}/{name}‐{profile}.yml
name: 文件名,一般以服务名(spring.application.name)来命名,如果配置了spring.cloud.config.name,则为该名称.profiles: 一般作为环境标识,对应配置文件中的spring.cloud.config.profilelable: 分支(branch),指定访问某分支下的配置文件,对应配置文件中的spring.cloud.config.label,默认值是在服务器上设置的(对于基于git的服务器,通常是“master”)
5.1 Maven的Profile管理
Maven提供了Profile切换功能(多环境dev,beta,prod), 如下pom.xml:
<profiles>
<profile>
<id>dev</id>
<properties>
<!-- 环境标识,需要与配置文件的名称相对应 -->
<activatedProperties>dev</activatedProperties>
</properties>
<activation>
<!-- 默认环境 -->
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>beta</id>
<properties>
<activatedProperties>beta</activatedProperties>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<activatedProperties>prod</activatedProperties>
</properties>
</profile>
</profiles>
配置文件bootstrap.yml:
spring:
cloud:
config: # Config客户端配置
profile: @activatedProperties@ # 启用配置后缀名称
label: master # 分支名称
uri: http://localhost:8009 # 配置中心地址
name: application # 配置文件名称
SpringBoot一把情况下会遵从你选的环境将@activatedProperties@替换掉。
但SpringCloud比较特殊,使用配置中心后客户端不会再使用application.yml,而是使用bootstrap.yml。但是Maven不认bootstrap.yml里的@activatedProperties@。
解决:在pom.xml的build标签里添加如下代码,用于过滤yml文件:
<build>
<finalName>${project.artifactId}</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<!--可以在此配置过滤文件 -->
<includes>
<include>**/*.yml</include>
</includes>
<!--开启filtering功能 -->
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
5.2 将不同服务的配置文件放到以服务名命名的目录下
因为config server默认情况下只会搜索git仓库根路径下的配置文件,所以我们还需要加上一个配置项:search-paths,
该配置项用于指定config server搜索哪些路径下的配置文件,需要注意的是这个路径是相对于git仓库的,并非是项目的路径。
spring:
cloud:
config:
server:
git:
...
search-paths: /** # 指定搜索根路径下的所有目录,若有多个路径使用逗号隔开
END .
相关系列文章
- 「学习笔记」SpringCloud(五)OpenFeign整合Sentinel实现熔断降级
- 「学习笔记」SpringCloud(四)OpenFeign服务间调用
- 「学习笔记」SpringCloud(三)Config配置中心
- 「学习笔记」SpringCloud(二)Gateway网关
- 「学习笔记」SpringCloud(一)Eureka注册中心
- 「学习笔记」Spring Reactive Stack(六)响应式 HTTP 请求客户端 WebClient
- 「学习笔记」Spring Reactive Stack(五)服务端事件推送Server-Sent Events
- 「学习笔记」Spring Reactive Stack(四)响应式方式访问Redis
- 「学习笔记」Spring Reactive Stack(三)使用R2DBC访问MySQL
- 「学习笔记」Spring Reactive Stack(二)Reactor异常处理
- 「学习笔记」Spring Reactive Stack(一)Spring WebFlux响应式Web框架入门
- 「工程实践」Spring Boot - 使用RocketMQ实战样例
- 「工程实践」RocketMQ安装并整合SpringBoot
- 「工程实践」Spring Boot + Thymeleaf页面静态化实现
- 「工程实践」Spring Boot - Java接口幂等性设计
- 「工程实践」Spring Boot 整合 FastDFS
- 「学习笔记」Spring Boot MVC 应用
- 「学习笔记」Spring Boot 数据库访问
- 「学习笔记」Spring Boot 入门
- 「学习笔记」Spring--SSM框架整合(Spring+SpringMVC+MyBatis)
- 「学习笔记」Spring--持久层框架Mybatis
- 「学习笔记」Spring--MVC框架
- 「学习笔记」Spring--面向切面编程(AOP模块)
- 「学习笔记」Spring--事务管理
- 「学习笔记」Spring--JDBC详解
- 「学习笔记」Spring--IoC注解实现
- 「学习笔记」Spring--IoC控制反转