天天看点

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

Config Server

一、基本搭建

1 .首先创建一个 Spring Boot 项目作为 module ,项目名为config-server

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

依赖需要添加 Web、Config Server 依赖

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

项目创建完成

Spring Cloud 学习笔记 ——Spring Cloud Config 的 Server 端搭建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
# 仓库的用户名密码
[email protected]
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端搭建,

[email protected] spring.cloud.config.server.git.password=

分别是仓库的用户名和密码,仓库是公有的,所以密码可以不写。

4 .启动项目

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

5.访问

http://localhost:8081/client1/dev/master

可以看见 git 仓库中 client1-dev.properties 内容:

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

也可以换成 prod,

http://localhost:8081/client1/prod/master

client1-prod.properties 内容:

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

此外查看控制台,发现把 git 上的文件下载到 C 盘的本地目录下:

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

这个内容是跟远程仓库一样的

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

二 、路径总结

实际上,只要满足如下规则,都可以访问 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 的方式:

    第二种:

    Spring Cloud 学习笔记 ——Spring Cloud Config 的 Server 端搭建Config Server
    第三种:
    Spring Cloud 学习笔记 ——Spring Cloud Config 的 Server 端搭建Config Server
    第四种:
    Spring Cloud 学习笔记 ——Spring Cloud Config 的 Server 端搭建Config Server
    第五种:
    Spring Cloud 学习笔记 ——Spring Cloud Config 的 Server 端搭建Config Server

三、实时更新

当我们修改 Git 中配置文件的内容,Config Server 是可以实时感知到,并更新的,下面验证一下:

现在修改本地 client1-dev.properties 文件,并提交到 Git 仓库

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

再次访问 client1-dev.properties,可以看到修改后的内容:

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

这就是 Config Server 的实时更新功能。

继续阅读