Spring Cloud实战(一)-Spring Cloud Config Server
2016-12-28 17:47
716 查看
概要
什么是Spring Cloud Config?创建并运行一个Spring Cloud Config Server
建立一个Repository
创建并运行一个Spring Cloud Config Client
Spring Cloud Config是什么?
什么是配置信息?一个Application中不只是代码,还需要连接资源和其它应用,经常有很多需要外部设置的项去调整Application行为,如切换不同的数据库,i18n国际化 等.应用中的会经常见到的xml,properties,yaml等就是配置信息.
常见的实现信息配置的方法:
硬编码(缺点:需要修改代码,风险大)
放在xml等配置文件中,和应用一起打包(缺点:需要重新打包和重启)
文件系统中(缺点:依赖操作系统等)
环境变量(缺点:有大量的配置需要人工设置到环境变量中,不便于管理,且依赖平台)
云端存储(缺点:与其他应用耦合)Spring Cloud Config 就是云端存储配置信息的,它具有中心化,版本控制,支持动态更新,平台独立,语言独立等特性.
Spring Cloud Config的原理如图所示,真正的数据存在Git等repository中,Config Server去获取相应的信息,然后开发给Client
Application,相互间的通信基于HTTP,TCP,UDP等协议.
创建并运行一个Spring Cloud Config Server
1.创建一个名为my-config-server的应用,并添加spring-cloud-starter-parent,spring-cloud-config-server依赖,pom信息具体如下<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-parent</artifactId> <version>Brixton.SR4</version> <relativePath/> </parent> <groupId>org.mmb.cloud</groupId> <artifactId>mmb-config-server</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> </dependencies> </project>
2.在Application主类上添加@EnableConfigServer注解,具体如下
package config; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; /** * Created by mmb on 2016/7/30. */ @EnableConfigServer @SpringBootApplication public class MMBConfigServerApplication { public static void main(String[] args) { SpringApplication.run(MMBConfigServerApplication.class, args); } }
3.去自己的GitHub上创建一个repository命名=MMBConfigData,并创建一个mmb-config-client.yml的配置文件,并添加一个key为luck-word,value
mmb,或者其它任何值.具体如下
--- lucky-word: mmb
4.回自己的工程,设置应用application.yml,配置spring.cloud.config.server.git.uri为"https://github.com/"YOUR-GITHUB-ID"/"YOUR-REPOSITORY-NAME"",并设置端口server.port为8001
server: port: 8001 spring: cloud: config: server: git: uri: https://github.com/mumubin/MMBConfigData searchPaths: data
5.启动应用并打开地址http://localhost:8001/mmb-con...将会看到配置好的信息,我的如下
{ "name": "mmb-config-client", "profiles": [ "default" ], "label": "master", "version": "4d9240f45fecd34136f81683d44c2e144792af86", "propertySources": [ { "name": "https://github.com/mumubin/MMBConfigData/data/mmb-config-client.yml", "source": { "lucky-word": "mmb" } } ] }
创建并运行一个Spring Cloud Config Client
1.创建一个名为my-config-client的应用,并添加spring-cloud-starter-parent,spring-cloud-starter-config,spring-boot-starter-web依赖,pom信息具体如下<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-parent</artifactId> <version>Brixton.SR4</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Allow for automatic restarts when classpath contents change. --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> </dependencies> <groupId>org.mmb.cloud</groupId> <artifactId>mmb-config-client</artifactId> <version>1.0-SNAPSHOT</version> </project>
2.创建bootstrap.yml在resource下,并设置spring.application.name,spring.cloud.config.uri,server.port信息,具体如下
spring: application: name: mmb-config-client cloud: config: uri: http://localhost:8001 --- server: port: 8002
注意这里是bootstrap.yml而不是appliction.yml,因为bootstrap.yml会在应用启动之前读取,而spring.cloud.config.uri会影响应用启动
3.创建一个Controller
@RestController public class LuckyWordController { @Value("${lucky-word}") String luckyWord; @RequestMapping("/lucky-word") public String showLuckyWord() { return "The lucky word is: " + luckyWord; } }
4.启动Application,打开http://localhost:8002/lucky-word
@SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class,args); } }
The lucky word is: mmb
特别感谢 kennyk65
Spring Cloud 中文用户组 31777218
Spring-Cloud-Config 官方文档-中文译本
Spring Cloud Netflix 官网文档-中文译本
本文实例github地址 Config Server Config
Client
原文地址:https://segmentfault.com/a/1190000006138698
相关文章推荐
- Spring Cloud 笔记实战之 Spring Cloud Config
- J360-cloud SpringCloud系列一:分布式配置服务器ConfigServer
- 干货实操:微服务Spring Cloud 系列(一) Spring cloud Config 用SVN做配置仓库
- 基于mysql的spring cloud config server
- spring-cloud-config-server——Environment Repository(Version Vontrol Backend Filesystem Use)
- Spring Cloud学习:05分布式配置中心(Spring Cloud Config)
- spring基础:快速入门spring cloud(6):统一配置管理之spring cloud config
- spring cloud config server使用svn来作为外部配置中心
- 【SpringBoot】SpringCloud Config Server实践
- Spring cloud config server 缓存问题
- spring-cloud-config-server——Environment Repository(Git Backend)
- 将SpringCloud ConfigServer持久化存储改为MySQL
- springcloud微服务实战:Eureka+Zuul+Feign/Ribbon+Hystrix Turbine+SpringConfig+sleuth+zipkin
- spring cloud学习(六)Spring Cloud Config
- spring cloud config server 使用本地配置文件报错
- spring-cloud中遇到的坑sidecar与config server client
- spring cloud config-server 高可用配置中心
- spring cloud config server 配置中心
- Spring Cloud入门之Spring Cloud Config
- spring-cloud-config-server