Git配置管理----配置中心
<p>详细了解SpringCloud中Config-Server相关可参见---(<a href="https://www.cnblogs.com/wdzhz/p/10431038.html">https://www.cnblogs.com/wdzhz/p/10431038.html</a>)
一、概述
在一个相对成熟,高可用的Spring cloud项目中,都会配置一个配置中心去管理各个服务的配置文件,而往往配置文件不会放到本地配置中心,一般都会放到Git,GitHub,SVN上,</p>
<p>这里以GitHub远程仓库为例,这里首先介绍一下Config-Server服务如何配置搭建,以及其他微服务的配置,以及如何从配置中心读取文件和服务如何实现动态加载配置</p>
<p>首先:
我们要单独创建一个Config-Server的服务
<img src="https://www.showdoc.cc/server/api/common/visitfile/sign/827cb6c95ca52355c70f54450f9c2378?showdoc=.jpg" alt="" />
该服务的pom.xml中依赖的包,内容如下:其中spring-cloud-config-server依赖最为重要
1 <dependencies>
2 <dependency>
3 <groupId>org.springframework.cloud</groupId>
4 <artifactId>spring-cloud-config-server</artifactId>
5 </dependency>
6 <!-- bus总线 -->
7 <dependency>
8 <groupId>org.springframework.cloud</groupId>
9 <artifactId>spring-cloud-starter-bus-amqp</artifactId>
10 </dependency>
11 <!-- 动态刷新的支持 -->
12 <dependency>
13 <groupId>org.springframework.boot</groupId>
14 <artifactId>spring-boot-starter-actuator</artifactId>
15 </dependency>
16 <!-- 为了方便,将服务注册到注册中,引入依赖包 -->
17 <dependency>
18 <groupId>org.springframework.cloud</groupId>
19 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
20 </dependency>
21 </dependencies>
为了项目后期的灵活,我这里关于config-server的配置文件是如下结构:···-dev开发环境;···-prod正式环境;···-uat发布环境,这里以开发环境为例
<img src="https://www.showdoc.cc/server/api/common/visitfile/sign/a8f587766b6a998b229b8a970a8c3849?showdoc=.jpg" alt="" /></p>
<ol>
<li>application.properties文件配置如下:
1 spring.application.name=Config-Service
2 spring.profiles.active=dev
spring.application.name :服务名
spring.profiles.active :服务运行的环境</li>
<li>application-dev.properties文件配置如下:
1 #config-server
2 server.port=10003
3
4 #eureka
5 eureka.client.service-url.defaultZone=<a href="http://localhost:10001/eureka/,http://localhost:10011/eureka/,http://localhost:10111/eureka/">http://localhost:10001/eureka/,http://localhost:10011/eureka/,http://localhost:10111/eureka/</a>
6 eureka.instance.prefer-ip-address=false
7 eureka.instance.lease-renewal-interval-in-seconds=10
8 eureka.instance.lease-expiration-duration-in-seconds=20
9
10 #config-server
11 #配置文件所在的分支
12 spring.cloud.config.label=master
13 #远程配置文件所在的Github仓库的地址,用户名,密码
14 spring.cloud.config.server.git.uri=<a href="https://github.com/zhz12311/QiWuIOT.git">https://github.com/zhz12311/QiWuIOT.git</a>
15 spring.cloud.config.server.git.username=XXXX
16 spring.cloud.config.server.git.password=xxxxxxx
17 spring.cloud.config.server.git.clone-on-start=true
18 #配置文件所在的目录
19 spring.cloud.config.server.git.search-paths=Consumers,Providers,Zuul
20 # 对于使用git,svn做为后端配置,从远程库获取配置文件,需要存储到本地文件
21 spring.cloud.config.server.git.basedir=/application-dev
22 # 配置中心通过git从远程git库,有时本地的拷贝被污染,这时配置中心无法从远程库更新本地配置,设置force-pull=true,则强制从远程库中更新本地库
23 spring.cloud.config.server.git.force-pull=true
24 # 自定义配置文件路径
25 spring.cloud.config.server.native.search-locations=
26 # native:启动从本地读取配置文件,必须指定active的值,才可以使用本地文件配置模式,如果使用本地系统配置,则此值必须是native
27 spring.profiles.active=
28 #RabbitMQ
29 spring.rabbitmq.host=localhost
30 spring.rabbitmq.port=5672
31 spring.rabbitmq.username=guest
32 spring.rabbitmq.password=guest
33 #动态刷新配置 ---需要忽略权限拦截
34 #关闭刷新安全认证
35 management.endpoints.web.exposure.include=*
36 # 开启消息跟踪
37 spring.cloud.bus.trace.enabled=true
GitHub远程仓库中的目录如下:
<img src="https://www.showdoc.cc/server/api/common/visitfile/sign/572fcb4de04fa3c1e6386f1012c64e50?showdoc=.jpg" alt="" />
<img src="https://www.showdoc.cc/server/api/common/visitfile/sign/e3d8c39b059dc5d3144c1a5c218b8cd7?showdoc=.jpg" alt="" />
进行拉去配置测试:
操作如下:(以application-pro-role-uat.properties为例)找到该配置文件
<img src="https://www.showdoc.cc/server/api/common/visitfile/sign/b4baf8fa86ff69ae877cb96d67da1be9?showdoc=.jpg" alt="" />
内容如下:
<img src="https://www.showdoc.cc/server/api/common/visitfile/sign/160aff84254dc4e6684403e4c7f272a3?showdoc=.jpg" alt="" />
启动Config-Server,然后在浏览器中访问:<a href="http://192.168.1.35:8886/application-pro-role-uat.properties">http://192.168.1.35:8886/application-pro-role-uat.properties</a> 会看到如下页面,说明config-server配置成功,可以拉去配置
<img src="https://www.showdoc.cc/server/api/common/visitfile/sign/bbd50b21273aac6cacf96052bdd6a229?showdoc=.jpg" alt="" /></li>
</ol>
<p>PS:http请求地址和资源文件映射如下: </p>
<ul>
<li>/{application}/{profile}[/{label}] </li>
<li>/{application}-{profile}.yml </li>
<li>/{label}/{application}-{profile}.yml </li>
<li>/{application}-{profile}.properties </li>
<li>/{label}/{application}-{profile}.properties</li>
</ul>