Git仓库配置详解——Environment Repository
-
目录
-
-
Git仓库配置详解 官方文档
doc:https://projects.spring.io/spring-cloud/spring-cloud.html#_environment_repository
- 1 创建名为simple/special的Git项目,并且Git clone下来
- 2 将三个项目都放到一个git-repos目录里面,将config-repo-51cto-video的application.yml复制到simple和special,里面
profile-default分别改成simple和special,并且提交到Git仓库!我是用IDEA提交的,如果不是再利用IDEA分别打开simple和special是没有Git提交的功
能的!!
-
改编microserver-config-server之application.yml
-
3.1 官方doc的demo
!!
spring:
cloud:
config:
server:
git:
uri: https://github.com/myorg/{application}
- 3.2 参考官方demo,microserver-config-server的配置文件如下:
https://gitee.com/xuhaiyancoco/special.git
https://gitee.com/xuhaiyancoco/simple.git
spring:
cloud:
config:
server:
git:
uri: https://gitee.com/xuhaiyancoco/{application}
server:
port: 8080
- 4 启动microserver-config-server服务,访问http://localhost:8080/master/special-default.properties .
总结:也就是说使用上述方式,一个Git对应一个微服务!!这样的话,只要我们的微服务名称和Git仓库有一定的对应关系,很容易在Git上面获取对应的东西,而且修改是隔离的!!一个仓库对应一个微服务的好处,在分配权限的时候,可以实现比较好的隔离,关注点在于自己的配置,十分清晰的知道无论怎么修改都不会影响其他的项目!!不建议分成两个配置文件,放在一个微服务对应的Git上面,一个环境一个文件,当然你可以把公告永远不会修改如应用名称可以放在上面。
管理上和使用上十分优雅。
问:一种环境一个仓库如何配置?
答:uri 修改的{profile}
uri: https://github.com/myorg/{application}-{profile}
问:为simple的开发环境做一个配置?
答:建立一个仓库simple-dev
-
模式匹配
-
官方demo
spring:
cloud:
config:
server:
git:
uri: https://github.com/spring-cloud-samples/config-repo
repos:
simple: https://github.com/simple/config-repo
special:
pattern: special*/dev*,*special*/dev*
uri: https://github.com/special/config-repo
local:
pattern: local*
uri: file:/home/configsvc/config-repo
-
模式匹配实战
- 1 application.yml
server:
port: 8080
spring:
cloud:
config:
server:
git:
uri: https://gitee.com/xuhaiyancoco/config-repo-51cto-video
repos:
simple: https://gitee.com/xuhaiyancoco/simple
special:
pattern: special*/dev*,*special*/test*
uri: https://gitee.com/xuhaiyancoco/special
- 2 ConfigServerApplication.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
- 3 special项目中创建special-dev.yml和special-test.yml,并且内容分别为profile: special-dev和profile: special-test
- 4 启动项目microserver-config-server,访问http://localhost:8080/master/special-default.properties结果如图,为什么呢?
答:首先按照图片所示,special项目中是不存在profile: profile-default,而这个访问的是什么项目呢?是config-repo-51cto-video,即命中了配置信息的uri: https://gitee.com/xuhaiyancoco/config-repo-51cto-video。
解析:
总结:建议使用最简单的通配符
问:如果访问的是http://localhost:8080/master/special-dev.properties,命中到什么地方了呢?
答:也就是说命中到了uri: https://gitee.com/xuhaiyancoco/special,进入了pattern模式匹配了
问:如果访问的是http://localhost:8080/master/simple-dev.properties或者http://localhost:8080/master/simple-default.properties,是否能访问成功呢?
答:是可以的。
-
pattern 路径匹配
doc:https://projects.spring.io/spring-cloud/spring-cloud.html#_environment_repository
yml
server:
port: 8080
spring:
cloud:
config:
server:
git:
uri: https://gitee.com/xuhaiyancoco/config-repo-51cto-video # 公用
search-paths:
- foo # 路径
- bar # 路径
在config-repo-51cto-video创建foo和bar文件夹,并且分别在对应文件下面创建foor-dev.yml和bar-dev.yml,内容为profile: foo-dev和profile: bar-dev,如下图所示:
启动microserver-config-server项目,访问http://localhost:8080/master/foor-dev.properties结果如下:
-
配置cloneOnStart
server: port: 8080 spring: cloud: config: server: git: uri: https://gitee.com/xuhaiyancoco/config-repo-51cto-video repos: simple: https://gitee.com/xuhaiyancoco/simple special: pattern: special*/dev*,*special*/test* uri: https://gitee.com/xuhaiyancoco/special cloneOnStart: false # 局部配置针对special仓库,cloneOnStart优先级比clone-on-start高,如果两者都不配置的情况是默认都是false clone-on-start: true # 全局配置所有的启动之后自动下载,原先是启动完首次请求时去拉取
-
设置用户名密码
如果使用share仓库,上述仓库全都是share仓库。
server:
port: 8080
spring:
cloud:
config:
server:
git:
uri: https://gitee.com/xuhaiyancoco/config-repo-51cto-video
username: xxx
password: xxx