码迷,mamicode.com
首页 > 编程语言 > 详细

SpringCloud(3-3)Spring Cloud Config 云端存储配置信息

时间:2017-05-03 17:29:15      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:factor   xsd   org   pack   pac   创建   注意   1.7   value   

Spring Cloud Config 云端存储配置信息

Spring Cloud Config 具有中心化,版本控制,支持动态更新,平台独立,语言独立等特性.
我们的例子:
1、真正的数据存在Git等repository中,
2、ScConfigServer从git获取相应信息,
3、ScConfigClient从ScConfigServer获取
相互之间的通信基于HTTP,TCP,UDP等协议.

一、创建并运行一个ScConfigServer应用

1.pom.xml

<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>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jdk.version>1.7</jdk.version>
<spring.version>4.3.0.RELEASE</spring.version>
</properties>

<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>Brixton.SR4</version>
<relativePath/>
</parent>

<groupId>org.wanma.example</groupId>
<artifactId>org.wanma.example.ScConfigServer</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>
<name>ScConfigServer</name>

<dependencies>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>

</dependencies>

</project>

2.在Application主类上添加@EnableConfigServer注解,具体如下

package org.wanma.example.sconfigserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@EnableConfigServer
@SpringBootApplication
public class Application
{

public static void main(String[] args)
{
SpringApplication.run(Application.class, args);
}

}

3.在GitHub上创建一个名ScConfigData为的repository,并在其中创建一个mmb-config-client.yml文件,内容如下

---
lucky-word: Lexiaofei-v99

4.开发一个application.yml,内容如下:

server:
port: 8001

spring:
cloud:
config:
server:
git:
uri: https://github.com/lexiaofei/ScConfigData
searchPaths: data

5.启动ScConfigServer应用,打开地址http://localhost:8001/ScConfigClient/default, 显示如下

{
"name":"ScConfigClient",
"profiles":["default"],
"label":"master",
"version":"091149689cdc758927fb1781e8f89445d921b15c",
"propertySources":
[
{
"name":"https://github.com/lexiaofei/ScConfigData/ScConfigClient.yml",
"source":{"lucky-word":"Lexiaofei-v99"}
}
]
}


二、创建并运行一个ScConfigClient应用

1.pom.xml

<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>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jdk.version>1.7</jdk.version>
<spring.version>4.3.0.RELEASE</spring.version>
</properties>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.6.RELEASE</version>
<relativePath/>
</parent>

<groupId>org.wanma.example</groupId>
<artifactId>org.wanma.example.ScConfigClient</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>
<name>ScConfigClient</name>

<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.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>

</dependencies>


</project>


2.创建bootstrap.yml在resource下,并设置spring.application.name,spring.cloud.config.uri,server.port信息,具体如下

spring:
application:
name: ScConfigClient
cloud:
config:
uri: http://localhost:8001

---
server:
port: 8002

注意这里是bootstrap.yml而不是appliction.yml, 因为bootstrap.yml会在应用启动之前读取,而spring.cloud.config.uri会影响应用启动

3.创建一个Controller

package org.wanma.example.scconfigclient.web;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
*
*/
@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

package org.wanma.example.scconfigclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application
{

public static void main(String[] args)
{
SpringApplication.run(Application.class, args);
}

}

最终结果:
The lucky word is: Lexiaofei-v99

SpringCloud(3-3)Spring Cloud Config 云端存储配置信息

标签:factor   xsd   org   pack   pac   创建   注意   1.7   value   

原文地址:http://www.cnblogs.com/lexiaofei/p/6802406.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!