<强> spring-cloud-config配置中心实现
强>
春云配置用于为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,分为服务器端和客户端。
服务器端为分布式配置中心,是一个独立的微服务应用,客户端为分布式系统中的基础设置或微服务应用,通过指定配置中心来管理相关的配置。
春云配置构建的配置中心,除了适用于春天构建的应用外,也可以在任何其他语言构建的应用中使用。
春云配置默认采用Git存储配置信息,支持对配置信息的版本管理。
<强>本示例主要内容强>
-
<李>配置中心演示客户端和服务器端实现李>
<李>配置文件放在git(因github有时候不太稳定,我放到了国内服务器)李>
<李>版本切换(测试、职业dev) 李>
<强>春云配置特点强>
-
<李>提供服务器端和客户端支持(云配置服务器和弹簧云配置客户端),李>
<李>集中式管理分布式环境下的应用配置;李>
<李>基于弹簧环境,实现了与弹簧应用无缝集成;李>
<李>可用于任何语言开发的程序,李>
<李>默认实现基于Git仓库(也支持SVN),从而可以进行配置的版本管理;同时也支持配置从本地文件或数据库读取。李>
<强>代码构建强>
<强>服务器端实现强>
1.。xml添加maven依赖
& lt; dependencies> & lt; dependency> & lt; groupId> org.springframework.cloud & lt; artifactId> spring-cloud-config-server & lt;/dependency> & lt;/dependencies>
2.应用程序。yml配置
服务器: 端口:8001 春天: 应用程序: 名称:cloud-config-server 云: 配置: 服务器: git: uri: https://gitee.com/tqlin/spring-boot-demo.git因为github有时候不稳定,我这里改到了码云仓 searchpath:/cloud-config/config-repo/#配置文件目录 force-pull:真cloudconfigserverapplication
3.。java启动类
@EnableConfigServer @SpringBootApplication 公开课CloudConfigServerApplication { 公共静态void main (String [] args) { SpringApplication.run (CloudConfigServerApplication.class, args); } }
<强>客户端实现强>
1.。xml添加maven依赖
& lt; dependencies> & lt; dependency> & lt; groupId> org.springframework.cloud & lt; artifactId> spring-cloud-starter-config & lt;/dependency> & lt; dependency> & lt; groupId> org.springframework.boot & lt; artifactId> spring-boot-starter-web & lt;/dependency> & lt; dependency> & lt; groupId> org.springframework.boot & lt; artifactId> spring-boot-starter-test & lt; scope> test & lt;/dependency> & lt;/dependencies>
2.引导。属性配置文件
spring.cloud.config.name=easy-config spring.cloud.config.profile=测试 spring.cloud.config.uri=http://localhost: 8001/spring.cloud.config.label=主
-
<李> spring.application.name:对应{应用}部分李>
<李> spring.cloud.config.profile:对应{概要}部分李>
<李> spring.cloud.config.label:对应git的分支。如果配置中心使用的是本地存储,则该参数无用李>
<李> spring.cloud.config.uri:配置中心的具体地址(服务器端地址)李>
<李> spring.cloud.config.discovery.service-id:指定配置中心的服务id,便于扩展为高可用配置集群。李>
特别注意:春云构建于弹簧引导之上,春天在引导中有两种上下文,一种是引导,另外一种是应用程序,引导是应用程序的父上下文,也就是说引导加载优先于applicaton.bootstrap主要用于从额外的资源来加载配置信息,还可以在本地外部配置文件中解密属性。
这两个上下文共用一个环境,它是任何春天应用程序的外部属性的来源.bootstrap里面的属性会优先加载,它们默认也不能被本地相同配置覆盖。
3.应用程序。属性配置文件