关闭
Hit
enter
to search or
ESC
to close
May I Suggest ?
#leanote #leanote blog #code #hello world
Okeeper's Blog
Home
Archives
Tags
DevOps
软件笔记
Spring
学习
JVM系列
关于我
Spring Cloud 配置中心Config Server
无
995
0
0
zhangyue
Spring Config Server是Spring Cloud的子项目,它提供配置集中管理方案 #使用 ## 在pom.xml加入 ``` <parent> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-parent</artifactId> <version>Camden.SR6</version> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> </dependencies> ``` ## 在Spring Boot的配置文件application.properties或者application.yml中配置如下: ``` server: port: 8888 spring: cloud: config: server: git: uri: https://github.com/okeeper/spring-cloud-config-example.git ``` or application.properties ``` server.port=8888 spring.cloud.config.server.git.uri=https://github.com/okeeper/spring-cloud-config-example.git ``` ## 添加一个几乎为空的Application.java类 ``` @SpringBootApplication @EnableConfigServer public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 项目结构如下:  启动Spring boot 项目就可以了 ## 测试 可以在浏览器中通过以下地址访问到指定配置文件jdbc.properties ``` #指定文件名称/文件profile/git的分支名称label,label默认是default /{name}/{profiles:.*[^-].*} /{name}/{profiles}/{label:.*} /jdbc/default /jdbc/dev /jdbc/dev/master #以不同的文件格式返回json,properties,yml /{name}-{profiles}.properties /jdbc-default.properties /jdbc-dev.properties /jdbc-prod.json /jdbc-prod.yml #指定分支的某个文件返回 /{label}/{name} /{label}/{name}-{profiles}.json 示例: /master/jdbc /master/jdbc-dev.properties /master/jdbc-default.json ``` > 如果在git仓库中添加application.properties或者application.yml文件,在加载每一个name的配置文件都会默认包含application里面的配置属性,所以利用这个特性,可以在git中添加默认的一些spring配置,比如Eureka的配置 ``` eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ ``` 在EurekaClient使用时就可以不用指定Eureka的相关配置,只需要指定Config Server的地址即可
觉得不错,点个赞?
Please enable JavaScript to view the
comments powered by Disqus.
comments powered by
Disqus
文章目录