Spring Cloud Config
2015-12-20 01:07
453 查看
Spring Cloud Config provides server and client-side support for externalized configuration in a distributed system.
Spring Cloud Config Quick Start Page
Linux for example:
[/code]
A simple sample for Spring boot as below:
[/code]
https://github.com/spring-cloud/spring-cloud-config/tree/1.0.2.RELEASE
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
[/code]
localhost:8888/foo/development is following this convention:
Explain more below.
[/code]
The content of the configserver.yml
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 --version
Spring Boot v1.2.5.RELEASE
[/code]
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
@EnableAutoConfiguration
public class SampleController {
@RequestMapping("/")
@ResponseBody
String home() {
return "Hello World!";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleController.class, args);
}
}
[/code]
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 firstThen 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"}}
]}
[/code]
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
[/code]
The content of the configserver.yml
info:
component: Config Server
spring:
application:
name: configserver
jmx:
default_domain: cloud.config.server
cloud:
config:
server:
git:
uri: https://github.com/spring-cloud-samples/config-repo[/code]repos:- patterns: multi-repo-demo-*uri: https://github.com/spring-cloud-samples/config-repo[/code]server:port: 8888management:context_path: /admin
[/code]
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
[/code]
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}Simple structure for client side.
3. Client Side Usage├── pom.xml├── src│ ├── main│ │ ├── java│ │ │ └── sample│ │ │ └── Application.java│ │ └── resources│ │ ├── application.yml│ │ └── bootstrap.yml
[/code]$ cd spring-cloud-config-sample$ mvn spring-boot:run
[/code]
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 -->
[/code]
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);}}
[/code]
spring-cloud-config-sample/src/main/resources/bootstrap.ymlspring:application:name: barcloud:config:env: default # optionallabel: master # optionaluri: http://localhost:${config.port:8888}[/code] [/code]
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":{...},...}
[/code]
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
[/code]
2. Usage of ClientAppClasspackage 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);}}
[/code]
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
- java操作Json工具类
- Java Web开发4___HTML基本标签: 超链接标签之页面内部链接
- java递归实例(一)---递归构造Tree结构
- javaday24-多线程上
- spring-jdbc
- SpringMVC中使用Interceptor拦截器
- 看了thinking in java 的initialize and cleanup 的一点总结
- 23种设计模式之Java实现
- JAVA并发编程笔记四
- java递归方法
- Hibernate三种状态和Session常用的方法
- Java基础--集合框架(Map集合)
- 关于Eclipse无法识别手机或者模拟器的解决方案
- 【Java基础】Java入门程序&基础数据类型(转换)&逻辑判断&运算等
- spring源码阅读笔记(一)
- springMVC面试题二
- ThreadLocal的使用
- scala-springmvc-hibernate观项目有感
- spring声明式事务