Spring Cloud Config为分布式系统中的外部配置提供服务器和客户端支持。使用Config Server,您可以在所有环境中管理应用程序的外部属性 ,通常情况下我们可以把需要管理的配置文件放置在svn或者git上进行做统一的配置仓库。
二 创建git远程仓库远程仓库中创建所需的配置文件
本例子中是demo-local.yml , 配置文件内容如下:
student:
name: test
age: 28
三 创建config-server端
1) 创建gradle模块config-server并添加gradle依赖
dependencies { compile('org.springframework.cloud:spring-cloud-config-server') }
2)编辑application.yml配置
server: port: 8000 spring: cloud: config: server: git: uri: git@gitlab.com:xxxx/config.git enabled: true profiles: active: local
其中spring.cloud.config.server.git是配置远程仓库的地址,如果不使用SSH协议请配置username和password属性
3)编写启动类
package com.bdqn.lyrk.config; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication @EnableConfigServer public class ConfigServer { public static void main(String[] args) { SpringApplication.run(ConfigServer.class, args); } }
注意在启动类上添加@EnableConfigServer注解
四 创建config-client端
1)创建gradle模块demo-server并添加gradle依赖
dependencies{ compile('org.springframework.cloud:spring-cloud-starter-config') }