简介
在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在Spring Cloud中,有分布式配置中心组件spring cloud config ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。
新建config-server
引入相关依赖
1 | <dependencies> |
新建bootstrap.yml
1 | server: |
- spring.cloud.config.server.git.uri:配置git仓库地址
- spring.cloud.config.server.git.search-paths:配置仓库路径
- spring.cloud.config.default-label:配置仓库的分支
- spring.cloud.config.server.git.username:访问git仓库的用户名
- spring.cloud.config.server.git.password:访问git仓库的用户密码
启动类
1 |
|
在github Create a new repository 取名SpringCloudConfig
创建userservice-dev.yml 文件并上传到github SpringCloudConfig项目中
1 |
|

可以看到已经从远程获取到配置信息
http请求地址和资源文件映射如下:
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties
新建config client
引入相关依赖
1 | <dependencies> |
bootstarp.yml
1 | spring: |
新建controller 测试
1 | import org.springframework.beans.factory.annotation.Value; |
访问 http://localhost:8789/
显示结果如下:
user: user1 port: 8789
说明成功通过配置中心获取到配置信息
刷新
config-client引入
1
2
3
4 <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
config-client application.yml 添加
1
2
3
4
5
6
7
8
9
10
11
12 management:
#refresh接入点显式暴露出来
endpoints:
web:
exposure:
include: refresh,health,info,bus-refresh #指定监控项。默认开启了health、info。我们这里加入了refresh
base-path: /actuator # 用于指定actuator的URL路径。1.X默认是/,2.X默认是/actuator
endpoint:
health:
show-details: always # 用于展示监控项详细信息,默认不展示
server:
port: 8988 #用于指定actuator端口。如果不指定则与系统服务端口一致,建议修改
修改配置文件
user:user4
访问 http://localhost:8789/
没有发生变化
使用
curl -v -X POST http://localhost:8983/actuator/refresh
或者使用postman

结果 user: user4 port: 8789
手动刷新成功
1078

被折叠的 条评论
为什么被折叠?



