SpringCloud(3-3)Spring Cloud Config 雲端儲存配置資訊

來源:互聯網
上載者:User

標籤: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 雲端儲存配置資訊

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.