Spring Cloud 学习笔记 ——Spring Cloud Config 的 Server 的简单配置

本文详细介绍了SpringCloudConfig如何从Git、classpath及本地文件系统动态获取配置。通过设置search-paths、profile和label占位符,实现了配置的灵活性。当切换到classpath或本地路径时,config-server能正确读取并返回配置信息,验证了配置的正确性。
摘要由CSDN通过智能技术生成

Server 的配置

1. 使用占位符灵活控制查询目录

先来看配置好的 config-server 和 config-client 的配置文件

config-sever 的 application.properties 文件

spring.application.name=config-server
server.port=8081
# 配置文件仓库地址
spring.cloud.config.server.git.search-paths=client1
spring.cloud.config.server.git.uri=https://github.com/xuanjinnan/configRepository.git
# 仓库中配置文件的目录
# 仓库的用户名密码
spring.cloud.config.server.git.username=xuanjinnan@163.com
spring.cloud.config.server.git.password=

config-client 的 bootstrap.properties 文件

# 下面三行配置分别对应 config-server 中的{application}、{profile}、{label}占位符
spring.application.name=client1
spring.cloud.config.label=master
spring.cloud.config.profile=dev
spring.cloud.config.uri=http://localhost:8081
server.port=8082

原来是spring.cloud.config.server.git.search-paths=client1,写死的,可以通过 {application}占位符,表示连接上来的 client1 的 spring.application.name 属性的值,它是动态的。

spring.application.name=config-server
server.port=8081
# 配置文件仓库地址
spring.cloud.config.server.git.search-paths={application}
spring.cloud.config.server.git.uri=https://github.com/xuanjinnan/configRepository.git
# 仓库中配置文件的目录
# 仓库的用户名密码
spring.cloud.config.server.git.username=xuanjinnan@163.com
spring.cloud.config.server.git.password=

r然后重启 config-server 和 config-client 服务,访问http://localhost:8082/hello依然正常
在这里插入图片描述
同样,可以用 {profile} 表示 client1 的 spring.cloud.config.profile 的属性,用 {label} 表示spring.cloud.config.label 的属性:

spring.cloud.config.profile={profile}
spring.cloud.config.label={label}
spring.cloud.config.server.git.search-paths={application}

再次重启 config-client 和 config-server ,访问http://localhost:8082/hello接口也是正常的:
在这里插入图片描述

2.Config-server 配置文件本地获取

①classpath

通常情况下配置文件是从 Git 仓库获取的,但是也可以直接 classpath 获取

  • 首先复制配置文件:
    在这里插入图片描述
  • 直接拷贝到 config-server 的 resources 目录下
    在这里插入图片描述
  • 然后配置 config-server 的 application.properties 文件,增加spring.profiles.active=native
spring.application.name=config-server
server.port=8081
# 配置文件仓库地址
spring.cloud.config.profile={profile}
spring.cloud.config.label={label}
spring.cloud.config.server.git.search-paths={application}
spring.cloud.config.server.git.uri=https://github.com/xuanjinnan/configRepository.git
# 表示让 config-sever 从 classpath 下查找配置,而不是 Git 仓库中
spring.profiles.active=native
# 仓库中配置文件的目录
# 仓库的用户名密码
spring.cloud.config.server.git.username=xuanjinnan@163.com
spring.cloud.config.server.git.password=
  • 简单修改下 client1 中 接口访问的配置文件,来做验证,值修改为 dev123456
    在这里插入图片描述
  • 重启 config-server 和 config-client 两个服务,访问 http://localhost:8082/hello接口
    在这里插入图片描述
    接口的返回值就是刚才修改的内容,说明 config-server 从 classpath 获取配置文件成功。
②本地获取

也可以通过配置本地路径来获取配置文件,比如我电脑的F:\springcloudStudy\configRepository\client1
在这里插入图片描述

  • 在刚刚的spring.profiles.active=native基础上,再添加spring.cloud.config.server.native.search-locations=file:/F:/springcloudStudy/configRepository/client1的路径
spring.application.name=config-server
server.port=8081
# 配置文件仓库地址
spring.cloud.config.profile={profile}
spring.cloud.config.label={label}
spring.cloud.config.server.git.search-paths={application}
spring.cloud.config.server.git.uri=https://github.com/xuanjinnan/configRepository.git
# 表示让 config-sever 从 classpath 下查找配置,而不是 Git 仓库中
spring.profiles.active=native
spring.cloud.config.server.native.search-locations=file:/F:/springcloudStudy/configRepository/client1
# 仓库中配置文件的目录
# 仓库的用户名密码
spring.cloud.config.server.git.username=xuanjinnan@163.com
spring.cloud.config.server.git.password=
  • 简单修改下 client1 中 接口访问的配置文件,来做验证,值修改为 dev123456789
    在这里插入图片描述

  • 重启 config-server 和 config-client 两个服务,访问 http://localhost:8082/hello接口
    在这里插入图片描述
    接口的返回值就是刚才修改的内容,说明 config-server 从本地 F:\springcloudStudy\configRepository\client1 获取配置文件成功。

总结

Spring Cloud Config 的配置文件可以从 Git 或者 classpath 或 本地获取,一般都是用 Git 配置,但是作为技术方案其他两种方法也可以

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值