Spring Cloud Config
Spring Cloud Config provides server and client-side support for externalized configuration in a distributed system.
Spring Cloud Config Quick Start Page
1. Preparation
Install Spring boot by following Spring boot getting started
Linux for example:
Install Groovy Environment Manager$ gvm install springboot$ spring --versionSpring Boot v1.2.5.RELEASE
A simple sample for Spring boot as below:
package hello;import org.springframework.boot.*;import org.springframework.boot.autoconfigure.*;import org.springframework.stereotype.*;import org.springframework.web.bind.annotation.*;@Controller@EnableAutoConfigurationpublic class SampleController {@RequestMapping("/")@ResponseBodyString home() {return "Hello World!";}public static void main(String[] args) throws Exception {SpringApplication.run(SampleController.class, args);}}
Git Clone the Sample Code of Srping Cloud Config
https://github.com/spring-cloud/spring-cloud-config/tree/1.0.2.RELEASE
.
├── docs
├── Guardfile
├── pom.xml
├── README.adoc
├── sample.groovy
├── spring-cloud-config-client
├── spring-cloud-config-sample
└── spring-cloud-config-server
2. The basic architecture of Spring Cloud Config

Setup Tips
- Start Config Server first
- Then start client app.
- After Config Server is down, Cient still works.
- Restarting Config Server will re-clone git properties
- use POST method instead of GET for curl command above
Setup Config Server
1. Start and visit config server
$ cd spring-cloud-config-server$ mvn spring-boot:run$ curl localhost:8888/foo/default$ curl localhost:8888/foo/development{"name":"development","label":"master","propertySources":[{"name":"https://github.com/scratches/config-repo/foo-development.properties","source":{"bar":"spam"}}, # The priority of foo-development.properties is higher than foo.properties{"name":"https://github.com/scratches/config-repo/foo.properties","source":{"foo":"bar"}}]}
localhost:8888/foo/development is following this convention:
/{application}/{profile}[/{label}]
application: foo
profile: development (environment like develop/qa/release/production)
label: "master" (master branch by default)

Explain more below.
2. Configurations in config server
/spring-cloud-config-server$ tree.├── pom.xml├── src│ ├── main│ │ ├── java│ │ └── resources│ │ ├── configserver.yml
The content of the configserver.yml
info:component: Config Serverspring:application:name: configserverjmx:default_domain: cloud.config.servercloud:config:server:git:uri: https://github.com/spring-cloud-samples/config-reporepos:- patterns: multi-repo-demo-*uri: https://github.com/spring-cloud-samples/config-reposerver:port: 8888management:context_path: /admin
The content of the git repository https://github.com/spring-cloud-samples/config-repo:
.├── application.yml├── bar.properties├── configserver.yml├── eureka.yml├── foo-development.properties├── foo.properties├── processor.yml├── samplebackendservice-development.properties├── samplebackendservice.properties├── samplefrontendservice.properties├── stores.yml└── zuul.properties
Will be cloned to /tmp/config-repo-{id} in Linux
localhost:8888/foo/development refer to foo-development.properties
localhost:8888/foo/default refer to foo.properties
Updating git repostiory will reflect to localhost:8888 like /tmp/config-repo-{id}
3. Client Side Usage
Simple structure for client side.
├── pom.xml├── src│ ├── main│ │ ├── java│ │ │ └── sample│ │ │ └── Application.java│ │ └── resources│ │ ├── application.yml│ │ └── bootstrap.yml
$ cd spring-cloud-config-sample$ mvn spring-boot:run
spring-cloud-config-sample/pom.xml
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.2.3.RELEASE</version><relativePath /> <!-- lookup parent from repository --></parent><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-parent</artifactId><version>1.0.1.RELEASE</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-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build><!-- repositories also needed for snapshots and milestones -->
Main Client class:
spring-cloud-config-sample/src/main/java/sample/Application.java
@Configuration@EnableAutoConfigurationpublic class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}}
spring-cloud-config-sample/src/main/resources/bootstrap.yml
spring:application:name: barcloud:config:env: default # optionallabel: master # optionaluri: http://localhost:${config.port:8888}
where it specifies application name bar and the uri of spring cloud config server.
$ curl localhost:8080/env{"profiles":[],"configService:https://github.com/scratches/config-repo/bar.properties":{"foo":"bar"},"servletContextInitParams":{},"systemProperties":{...},...}
Usage:
1. Get/Refresh properties (Fetch value on request API call)
$ curl localhost:8080/env/foobar$ vi /tmp/config-repo-{id}/bar.properties.. change value of "bars"$ curl -X POST localhost:8080/refresh["foo"]$ curl localhost:8080/env/foobars
2. Usage of ClientAppClass
package demo;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.context.annotation.ComponentScan;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@EnableAutoConfiguration@ComponentScan@RestControllerpublic class ClientApp {@Value("${bar:World!}")String bar;@RequestMapping("/")String hello() {return "Hello " + bar + "!";}public static void main(String[] args) {SpringApplication.run(ClientApp.class, args);}}
You can also see a single property.
$ curl http://localhost:8080/env/bar
123456
When you access to the controller,
$ curl http://localhost:8080
Hello 123456!
you can find the property on Config Server is injected.
See more usage samples here: http://qiita.com/making@github/items/704d8e254e03c5cce546
Spring Cloud Config的更多相关文章
- spring cloud config 入门
简介 Spring cloud config 分为两部分 server client config-server 配置服务端,服务管理配置信息 config-client 客户端,客户端调用serve ...
- Spring Cloud官方文档中文版-Spring Cloud Config(上)
官方文档地址为:http://cloud.spring.io/spring-cloud-static/Dalston.SR2/#spring-cloud-feign 文中例子我做了一些测试在:http ...
- Spring Cloud官方文档中文版-Spring Cloud Config(下)-客户端等
官方文档地址为:http://cloud.spring.io/spring-cloud-static/Dalston.SR2/#_serving_alternative_formats 文中例子我做了 ...
- SpringCloud的配置管理:Spring Cloud Config
演示如何使用ConfigServer提供统一的参数配置服务 ###################################################################一.概 ...
- 搭建spring cloud config
很久没更新了,因为不是专职研究spring cloud,因此更新速度得看工作强度大不大,每天能抽出的时间不多,如果更新太慢了,并且有小伙伴看的话,请见谅了. Spring Cloud简介 Spring ...
- Spring Cloud Config - RSA简介以及使用RSA加密配置文件
简介 RSA非对称加密有着非常强大的安全性,HTTPS的SSL加密就是使用这种方法进行HTTPS请求加密传输的.因为RSA算法会涉及Private Key和Public Key分别用来加密和解密,所以 ...
- Spring Cloud Config 分布式配置中心使用教程
一.简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件.在Spring Cloud中,有分布式配置中心组件spring cloud config ...
- 【spring实战第五版遇到的坑】第14章spring.cloud.config.uri和token配置项无效
本文使用的Spring Boot版本为:2.1.4.RELEASE Spring Cloud版本为:Greenwich.SR1 按照书上的做法,在application.yml中配置配置服务器的地址和 ...
- .NET Core微服务之基于Steeltoe使用Spring Cloud Config统一管理配置
Tip: 此篇已加入.NET Core微服务基础系列文章索引 => Steeltoe目录快速导航: 1. 基于Steeltoe使用Spring Cloud Eureka 2. 基于Steelt ...
随机推荐
- MySQL Cluster-备份恢复初步测试
参考文档 http://blog.chinaunix.net/uid-20639775-id-1617795.html http://xxtianxiaxing.iteye.com/blog/5 ...
- MiddleGenIDE工具的使用
1. MiddleGenIDE工具 1) 先在网上下载MiddleGenIDE工具.能够參考这里 http://blog.csdn.net/wangcunhuazi/articl ...
- CPU指令的流水线运行
指令集是CPU体系架构的重要组成部分.C语言的语法是对解决现实问题的运算和流程的方法的高度概况和抽象,其主要为算术.逻辑运算和分支控制,而指令集就是对这些抽象的详细支持,汇编仅仅只是是为了让开发者更好 ...
- JavaScript语言基础知识6
在前面的章节中,我们知道JavaScript代码,字符和数字值当添加,将计值转换成字符,即用户输入的数目值它们被转换为字符. 如今我们要做这种样例,我想将1和2相加: <HTML> < ...
- win8系统特别慢的基本判断方法
单位有6,7台同事的电脑是win8.1.不知道什么原因,两位领导反映电脑特别慢. 我查看了一下,确实很慢. 虽然知道win8系统不好,但也不至于这样. 打开任务管理器,看了一下cpu和内存的使用状态, ...
- aspx.cs上传文件
aspx.cs文件 using System; using System.Collections.Generic; using System.Linq; using System.Web; using ...
- POJ 3528 求三维凸包表面积
也是用模板直接套的题目诶 //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include < ...
- Ubuntu无法进入图形界面及VirtualBox扩容的解决方案
升级Ubuntu 12.04后出现“Ubuntu is running in low-graphics mode?”,无法进入图形界面,而且给了一些选项,发现其他几个都没有用,最终只能使用low-gr ...
- 基于Sql Server 2008的分布式数据库的实践(二)
原文 基于Sql Server 2008的分布式数据库的实践(二) 从Win7连接Win2003的Sql Server 2008 1.新建链接服务器链接到Win2003的Sql Server 2008 ...
- 设计模式(四)原型模式Prototype(创建型)
设计模式(四)原型模式Prototype(创建型) 1. 概述 我们都知道,创建型模式一般是用来创建一个新的对象,然后我们使用这个对象完成一些对象的操作,我们通过原型模式可以快速的创建一个对象 ...