spring cloud的统一配置中心介绍和Server端代码实
为什么需要统一配置中心
在实际开发中,经常需要修改自己的配置文件,不小心提交到Git,对于别人来说也是很麻烦,不方便维护。而在公司里面,线上生产环境的配置是不会给开发知道的,涉及到安全和权限的问题。特别是我们可能经常需要更改一些配置,如果在配置文件里面写死,就得重新启动项目,在集群环境下,重启那么多项目,太麻烦了。所以就需要有一个统一配置中心。
配置中心的实现思路:·有一个项目单独负责管理这些配置文件,然后product、order从服务中心读取配置。运维人员修改配置提交到Git服务器上,推送到配置中心服务。配置中心服务从服务端Git获取配置文件,如果服务器git不可以就会从本地Git获取。

代码实现
创建一个统一配置中心config项目

同时它也是一个eureka客户端

它是config Server

配置eureka地址和应用名称

加eureka客户端注解

启动项目,发现可以注册到eureka

再加上EnableConfigServer注解表明这是配置中心,重启发现出错,提示需要配置一个仓库,就在你的Git新建一个config-repo仓库

把order服务的配置放到Git上面

在config项目配置git地址和账号,启动

访问:http://localhost:8088/order-a.yml、http://localhost:8088/order-b.yml都能获取到这个配置文件

但是访问http://localhost:8088/order.yml是获取不到的,查看启动日志,发现是以/{name}-{profiles}.yml、/{name}-{profiles}.json或者/{name}-{profiles}.properties格式的

发现以http://localhost:8088/order-a.properties获取到的是properties格式

http://localhost:8088/order-a.json获取到json格式的

此外,它还有{label}/{name}-{profiles}.yml这个格式
label:分支的意思,没有就默认master分支
name:服务名,如order、product
profiles:环境,比如prod、dev、test
自己新建几个配置文件

http://localhost:8088/order-dev.yml就能访问order的dev配置

创建一个分支configdev

通过http://localhost:8088/分支名/order-dev.yml就可以访问了

用默认master还是其他分支,这个根据你公司的实际情况来即可,这里没有强制规定。怎么方便怎么来。
开始时说过这些文件会有存放在本地,看日志就知道它默认存放在哪了

由于在公司在这种文件的路径是有规范的,可以通过下面这种方式指定存放路径

就这样指定本地仓库路径。
这样配置中心的Server端就实现了。
统一配置中心的高可用
统一配置中心的本质就是一个微服务,想要实现高可用就是要部署多个实例嘛,这个的实现和其他的业务的高可用是一样的,集群然后多个实例实现高可用。