Spring Cloud 学习笔记 ——Spring Cloud Config 的 Server 端搭建

Config Server

一、基本搭建

1 .首先创建一个 Spring Boot 项目作为 module ,项目名为config-server
在这里插入图片描述
依赖需要添加 Web、Config Server 依赖
在这里插入图片描述
项目创建完成
在这里插入图片描述
2.项目创建成功后,在启动类上加 @EnableConfigServer 注解,开启 config server 功能

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }

}

3.在配置文件 application.properties 中配置仓库的基本信息

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

spring.cloud.config.server.git.uri=https://github.com/xuanjinnan/configRepo.git是 git 仓库地址,spring.cloud.config.server.git.search-paths=client1是 仓库的那个目录,详情查看Spring Cloud 学习笔记 ——Spring Cloud Config 的 Server端搭建,spring.cloud.config.server.git.username=xuanjinnan@163.com spring.cloud.config.server.git.password=分别是仓库的用户名和密码,仓库是公有的,所以密码可以不写。

4 .启动项目
在这里插入图片描述
5.访问 http://localhost:8081/client1/dev/master可以看见 git 仓库中 client1-dev.properties 内容:
在这里插入图片描述
也可以换成 prod,http://localhost:8081/client1/prod/masterclient1-prod.properties 内容:
在这里插入图片描述
此外查看控制台,发现把 git 上的文件下载到 C 盘的本地目录下:
在这里插入图片描述
这个内容是跟远程仓库一样的
在这里插入图片描述

二 、路径总结

实际上,只要满足如下规则,都可以访问 Git 上的配置文件:

  • 1./{application}/{profile}/{label}
  • 2./{application}-{profile}.yml
  • 3./{application}-{profile}.properties
  • 4./{label}/{application}-{profile}.properties
  • 5./{label}/{application}-{profile}.yml
    application 表示配置文件名
    profile 表示配置文件的 profile,例如 test、dev、prod,与 SpringBoot 切换配置文件的 profile 是一样的
    label表示git 的分支,参数可选,默认就是 master
    第一种情况,上面已经展示,验证 2 - 5 的方式:
    第二种:
    在这里插入图片描述
    第三种:
    在这里插入图片描述
    第四种:
    在这里插入图片描述
    第五种:
    在这里插入图片描述

三、实时更新

当我们修改 Git 中配置文件的内容,Config Server 是可以实时感知到,并更新的,下面验证一下:
现在修改本地 client1-dev.properties 文件,并提交到 Git 仓库
在这里插入图片描述
在这里插入图片描述
再次访问 client1-dev.properties,可以看到修改后的内容:
在这里插入图片描述
这就是 Config Server 的实时更新功能。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值