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:

  1. Install Groovy Environment Manager
  2. $ gvm install springboot
  3. $ spring --version
  4. Spring Boot v1.2.5.RELEASE

A simple sample for Spring boot as below:

  1. package hello;
  2. import org.springframework.boot.*;
  3. import org.springframework.boot.autoconfigure.*;
  4. import org.springframework.stereotype.*;
  5. import org.springframework.web.bind.annotation.*;
  6. @Controller
  7. @EnableAutoConfiguration
  8. public class SampleController {
  9. @RequestMapping("/")
  10. @ResponseBody
  11. String home() {
  12. return "Hello World!";
  13. }
  14. public static void main(String[] args) throws Exception {
  15. SpringApplication.run(SampleController.class, args);
  16. }
  17. }

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

  1. Start Config Server first
  2. Then start client app.
  3. After Config Server is down, Cient still works.
  4. Restarting Config Server will re-clone git properties
  5. use POST method instead of GET for curl command above

Setup Config Server

1. Start and visit config server

  1. $ cd spring-cloud-config-server
  2. $ mvn spring-boot:run
  3. $ curl localhost:8888/foo/default
  4. $ curl localhost:8888/foo/development
  5. {"name":"development","label":"master","propertySources":[
  6. {"name":"https://github.com/scratches/config-repo/foo-development.properties","source":{"bar":"spam"}}, # The priority of foo-development.properties is higher than foo.properties
  7. {"name":"https://github.com/scratches/config-repo/foo.properties","source":{"foo":"bar"}}
  8. ]}

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

  1. /spring-cloud-config-server$ tree
  2. .
  3. ├── pom.xml
  4. ├── src
  5. │   ├── main
  6. │   │   ├── java
  7. │   │   └── resources
  8. │   │   ├── configserver.yml

The content of the configserver.yml

  1. info:
  2. component: Config Server
  3. spring:
  4. application:
  5. name: configserver
  6. jmx:
  7. default_domain: cloud.config.server
  8. cloud:
  9. config:
  10. server:
  11. git:
  12. uri: https://github.com/spring-cloud-samples/config-repo
  13. repos:
  14. - patterns: multi-repo-demo-*
  15. uri: https://github.com/spring-cloud-samples/config-repo
  16. server:
  17. port: 8888
  18. management:
  19. context_path: /admin

The content of the git repository https://github.com/spring-cloud-samples/config-repo:

  1. .
  2. ├── application.yml
  3. ├── bar.properties
  4. ├── configserver.yml
  5. ├── eureka.yml
  6. ├── foo-development.properties
  7. ├── foo.properties
  8. ├── processor.yml
  9. ├── samplebackendservice-development.properties
  10. ├── samplebackendservice.properties
  11. ├── samplefrontendservice.properties
  12. ├── stores.yml
  13. └── 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.

  1. ├── pom.xml
  2. ├── src
  3. │   ├── main
  4. │   │   ├── java
  5. │   │   │   └── sample
  6. │   │   │   └── Application.java
  7. │   │   └── resources
  8. │   │   ├── application.yml
  9. │   │   └── bootstrap.yml
  1. $ cd spring-cloud-config-sample
  2. $ mvn spring-boot:run

spring-cloud-config-sample/pom.xml

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>1.2.3.RELEASE</version>
  5. <relativePath /> <!-- lookup parent from repository -->
  6. </parent>
  7. <dependencyManagement>
  8. <dependencies>
  9. <dependency>
  10. <groupId>org.springframework.cloud</groupId>
  11. <artifactId>spring-cloud-starter-parent</artifactId>
  12. <version>1.0.1.RELEASE</version>
  13. <type>pom</type>
  14. <scope>import</scope>
  15. </dependency>
  16. </dependencies>
  17. </dependencyManagement>
  18. <dependencies>
  19. <dependency>
  20. <groupId>org.springframework.cloud</groupId>
  21. <artifactId>spring-cloud-starter-config</artifactId>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-starter-test</artifactId>
  26. <scope>test</scope>
  27. </dependency>
  28. </dependencies>
  29. <build>
  30. <plugins>
  31. <plugin>
  32. <groupId>org.springframework.boot</groupId>
  33. <artifactId>spring-boot-maven-plugin</artifactId>
  34. </plugin>
  35. </plugins>
  36. </build>
  37. <!-- repositories also needed for snapshots and milestones -->

Main Client class:

spring-cloud-config-sample/src/main/java/sample/Application.java

  1. @Configuration
  2. @EnableAutoConfiguration
  3. public class Application {
  4. public static void main(String[] args) {
  5. SpringApplication.run(Application.class, args);
  6. }
  7. }

spring-cloud-config-sample/src/main/resources/bootstrap.yml

  1. spring:
  2. application:
  3. name: bar
  4. cloud:
  5. config:
  6. env: default # optional
  7. label: master # optional
  8. uri: http://localhost:${config.port:8888}

where it specifies application name bar and the uri of spring cloud config server.

  1. $ curl localhost:8080/env
  2. {
  3. "profiles":[],
  4. "configService:https://github.com/scratches/config-repo/bar.properties":{"foo":"bar"},
  5. "servletContextInitParams":{},
  6. "systemProperties":{...},
  7. ...
  8. }

Usage:

1. Get/Refresh properties (Fetch value on request API call)

  1. $ curl localhost:8080/env/foo
  2. bar
  3. $ vi /tmp/config-repo-{id}/bar.properties
  4. .. change value of "bars"
  5. $ curl -X POST localhost:8080/refresh
  6. ["foo"]
  7. $ curl localhost:8080/env/foo
  8. bars

2. Usage of ClientAppClass

  1. package demo;
  2. import org.springframework.beans.factory.annotation.Value;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  5. import org.springframework.context.annotation.ComponentScan;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RestController;
  8. @EnableAutoConfiguration
  9. @ComponentScan
  10. @RestController
  11. public class ClientApp {
  12. @Value("${bar:World!}")
  13. String bar;
  14. @RequestMapping("/")
  15. String hello() {
  16. return "Hello " + bar + "!";
  17. }
  18. public static void main(String[] args) {
  19. SpringApplication.run(ClientApp.class, args);
  20. }
  21. }

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的更多相关文章

  1. spring cloud config 入门

    简介 Spring cloud config 分为两部分 server client config-server 配置服务端,服务管理配置信息 config-client 客户端,客户端调用serve ...

  2. Spring Cloud官方文档中文版-Spring Cloud Config(上)

    官方文档地址为:http://cloud.spring.io/spring-cloud-static/Dalston.SR2/#spring-cloud-feign 文中例子我做了一些测试在:http ...

  3. Spring Cloud官方文档中文版-Spring Cloud Config(下)-客户端等

    官方文档地址为:http://cloud.spring.io/spring-cloud-static/Dalston.SR2/#_serving_alternative_formats 文中例子我做了 ...

  4. SpringCloud的配置管理:Spring Cloud Config

    演示如何使用ConfigServer提供统一的参数配置服务 ###################################################################一.概 ...

  5. 搭建spring cloud config

    很久没更新了,因为不是专职研究spring cloud,因此更新速度得看工作强度大不大,每天能抽出的时间不多,如果更新太慢了,并且有小伙伴看的话,请见谅了. Spring Cloud简介 Spring ...

  6. Spring Cloud Config - RSA简介以及使用RSA加密配置文件

    简介 RSA非对称加密有着非常强大的安全性,HTTPS的SSL加密就是使用这种方法进行HTTPS请求加密传输的.因为RSA算法会涉及Private Key和Public Key分别用来加密和解密,所以 ...

  7. Spring Cloud Config 分布式配置中心使用教程

    一.简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件.在Spring Cloud中,有分布式配置中心组件spring cloud config ...

  8. 【spring实战第五版遇到的坑】第14章spring.cloud.config.uri和token配置项无效

    本文使用的Spring Boot版本为:2.1.4.RELEASE Spring Cloud版本为:Greenwich.SR1 按照书上的做法,在application.yml中配置配置服务器的地址和 ...

  9. .NET Core微服务之基于Steeltoe使用Spring Cloud Config统一管理配置

    Tip: 此篇已加入.NET Core微服务基础系列文章索引 =>  Steeltoe目录快速导航: 1. 基于Steeltoe使用Spring Cloud Eureka 2. 基于Steelt ...

随机推荐

  1. 多线程下载工具-Axel

    1.安装: apt-get install axel 2.用法: axel 参数 文件下载地址 3.常用参数: -n 指定线程数 -o 指定文件存储位置(如不指定,默认存在当前位置(pwd)) -q ...

  2. open和fopen的区别:

    1.缓冲文件系统缓冲文件系统的特点是:在内存开辟一个“缓冲区”,为程序中的每一个文件使用,当执行读文件的操作时,从磁盘文件将数据先读入内存“缓冲区”, 装满后再从内存“缓冲区”依此读入接收的变量.执行 ...

  3. VCC、VDD、VEE、VSS的区别

    电路设计以及PCB制作中,经常碰见电源符号:VCC. VDD.VEE.VSS,他们具有什么样的关系那? 一.解释 VCC:C=circuit 表示电路的意思, 即接入电路的电压 VDD:D=devic ...

  4. 梳排序(Comb sort)

    Comb Sort,梳排序或者梳子排序,就像梳子那样有间隔地比较两个数,很形象,O(n*logn)时间复杂度,O(1)空间复杂度,属于不稳定的排序算法.算法的思想是使逆序的元素尽可能快地移动到最终的位 ...

  5. 服务器端开发(Python/C++)-今日头条-拉勾网-最专业的互联网招聘平台

    服务器端开发(Python/C++)-今日头条-拉勾网-最专业的互联网招聘平台 服务器端开发(Python/C++)

  6. [Swust OJ 217]--Factor(数论,类素数表)

    题目链接:http://acm.swust.edu.cn/problem/0217/ Time limit(ms): 2000 Memory limit(kb): 65535    Descripti ...

  7. django中上传图片的写法

    view参数 @csrf_exemptdef before_upload_avatar(request):    before = True    return render_to_response( ...

  8. 深入JDK源码之Arrays类中的排序查找算法(转)

    原文出处: 陶邦仁 binarySearch()方法 二分法查找算法,算法思想:当数据量很大适宜采用该方法.采用二分法查找时,数据需是排好序的. 基本思想:假设数据是按升序排序的,对于给定值x,从序列 ...

  9. BZOJ 1499 NOI2005 瑰丽华尔兹 单调队列

    题目大意:给定一个m*n的地图,一些点有障碍物,钢琴初始在一个点,每一个时间段能够选择向给定的方向移动一段距离,求最长路径长 朴素DP的话,我们有T个时间段,每一个时间段有m*n个点,n个时间,一定会 ...

  10. vpn技术原理详解

    转载于:http://blog.csdn.net/quqi99/article/details/7334617 假设有这样一个需求,需要从家中访问公司内网机器,可以用ssh遂道技术来作转发,遂道分正向 ...