簡介
Spring Cloud 提供了一個部署微服務的平臺,包括了微服務中常見的組件:配置中心服務, API網關,斷路器,服務註冊與發現,分佈式追溯,OAuth2,消費者驅動合約等。咱們沒必要先知道每一個組件有什麼做用,隨着教程的深刻,咱們會逐漸接觸到它們。一個分佈式服務大致結構見下圖(圖片來自於:spring.io):前端
使用Spring Cloud搭建分佈式的系統十分簡單,咱們只須要幾行簡單的配置就能啓動一系列的組件,而後能夠在代碼中控制、使用和管理這些組件。Spring Cloud使用Spring Boot做爲基礎框架,能夠參考個人上一篇博客介紹如何建立一個Spring Boot項目, Spring Boot 2.0.1 入門教程。本教程將教你們如何配置服務中心服務,並經過web客戶端讀取配置。java
基礎環境
JDK 1.8git
Maven 3.3.9github
IntelliJ 2018.1web
Gitspring
項目源碼
Gitee碼雲apache
建立 Web Client
首先用IntelliJ建立一個Maven項目,pom.xml文件內容以下:json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
<?xml version="1.0" encoding="UTF-8"?> <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 > <groupId > cn.zxuqian</groupId > <artifactId > web</artifactId > <version > 1.0-SNAPSHOT</version > <parent > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-parent</artifactId > <version > 2.0.1.RELEASE</version > </parent > <dependencies > <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-starter-config</artifactId > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-actuator</artifactId > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-web</artifactId > </dependency > </dependencies > <dependencyManagement > <dependencies > <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-dependencies</artifactId > <version > Finchley.M9</version > <type > pom</type > <scope > import</scope > </dependency > </dependencies > </dependencyManagement > <properties > <java.version > 1.8</java.version > </properties > <build > <plugins > <plugin > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-maven-plugin</artifactId > </plugin > </plugins > </build > <repositories > <repository > <id > spring-milestones</id > <name > Spring Milestones</name > <url > https://repo.spring.io/libs-milestone</url > <snapshots > <enabled > false</enabled > </snapshots > </repository > </repositories > </project >
添加控制器
新建一個控制器類 cn.zxuqian.controllers.HelloController
, 並添加以下代碼:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
package cn.zxuqian.controllers;import org.springframework.beans.factory.annotation.Value;import org.springframework.cloud.context.config.annotation.RefreshScope;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RefreshScope @RestController public class HelloController { @Value ("${message: 本地消息}" ) private String message; @RequestMapping ("/message" ) public String message () { return this .message; } }
一個簡單的控制器,匹配/message
路徑,並返回message
變量的值。這裏先不用管 @RefreshScope
這個註解,等下會用到時再講。@Value
會取來自配置中心服務的配置項,或本地環境變量等等,此處取了配置中心的message
的值,並給了它一個默認值」本地消息」,即若是遠程配置中心不可用時,此變量將會用默認值初始化。
添加配置文件
bootstrap.xml
咱們須要在web客戶端項目徹底啓動以前去加載配置中心的配置項,因此須要在src/main/resources
下建立bootstrap.yml
文件,而後指定此客戶端的名字和遠程配置中心的uri:
1 2 3 4 5 6
spring: application: name: web-client cloud: config: uri: http://localhost:8888
yml相比properties文件更加簡潔,不用寫不少重複的前綴,上邊的內容能夠轉換爲對應的properties:
1 2
spring.application.name=web-client spring.cloud.config.uri=http://localhost:8888
建立配置中心項目
新建一個Maven項目,使用以下pom配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
<?xml version="1.0" encoding="UTF-8"?> <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 > <groupId > cn.zxuqian</groupId > <artifactId > config-server</artifactId > <version > 1.0-SNAPSHOT</version > <packaging > jar</packaging > <parent > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-parent</artifactId > <version > 2.0.1.RELEASE</version > <relativePath /> </parent > <properties > <project.build.sourceEncoding > UTF-8</project.build.sourceEncoding > <java.version > 1.8</java.version > </properties > <dependencies > <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-config-server</artifactId > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-test</artifactId > <scope > test</scope > </dependency > </dependencies > <dependencyManagement > <dependencies > <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-dependencies</artifactId > <version > Finchley.M9</version > <type > pom</type > <scope > import</scope > </dependency > </dependencies > </dependencyManagement > <build > <plugins > <plugin > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-maven-plugin</artifactId > </plugin > </plugins > </build > <repositories > <repository > <id > spring-milestones</id > <name > Spring Milestones</name > <url > https://repo.spring.io/libs-milestone</url > <snapshots > <enabled > false</enabled > </snapshots > </repository > </repositories > </project >
spring-cloud-config-server
即爲配置中心服務的核心依賴。
配置Application
新建一個Java類cn.zxuqian.Application
,添加以下代碼:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
package cn.zxuqian;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); } }
新建Git倉庫
配置中心的文件都是基於版本控制的,因此須要在本地新建一個git倉庫來保存配置文件。或者也可用公共遠程git倉庫,github, 碼雲等。在任意位置(如項目的上級目錄)建立一空白文件夾,這裏叫作config
,可使用任何名字。而後進入此文件夾下,運行
來初始化git倉庫,而後新建一個文件,名爲web-client.yml
,並添加以下內容:
1
message: 此條消息來自於cofig server
注意此文件名須要和以前在web項目中配置的spring.application.name
保持一致。這個文件的內容就是web客戶端要獲取的message的值。建立完成以後,運行以下git命令提交到本地倉庫:
1 2
$ git add web-client.yml $ git commit -m "added web-client.yml"
配置git倉庫位置
咱們須要爲配置中心指定上述建立的git倉庫地址。在src/main/resources
下建立applicaiton.yml
文件,提供以下內容:
1 2 3 4 5 6 7 8
server: port: 8888 spring: cloud: config: server: git: uri: ${HOME}/development/codes/backend/gitee/config
此文件指定了配置中心服務的端口號,和保存配置文件的git倉庫目錄,若是是遠程倉庫,能夠直接指定url地址。到此,配置中心服務建立完成。
測試
首先啓動配置中心服務,使用spring-boot maven插件:spring-boot:run。啓動成功後再啓動web客戶端,訪問http://localhost:8080/message
,若是看到此條消息來自於cofig server
即配置成功。而後關閉配置中心服務,再重啓web客戶端,訪問http://localhost:8080/message
,咱們就會看到本地消息
。
動態更新配置
那麼每次配置更新後都要重啓是否是很麻煩?Spring boot提供了spring-boot-starter-actuator
組件,用來進行生產環境的維護,如檢查健康信息等。還記得上面HelloController
的@RefreshScope
註解嗎?使用它咱們能夠動態的加載配置中心修改後的配置。而後咱們還須要在配置中心的web-client.yml
添加以下內容用以暴露acurator的/refresh
終端api。
1 2 3 4 5 6 7
message: 此條消息來自於cofig server management: endpoints: web: exposure: include: "*"
更新完成後提交的git倉庫,而後重啓配置中心服務和web客戶端。修改message爲
1
message: 更新:此條消息來自於cofig server
而後發送一個空的post請求到/refresh
1
$ curl http://localhost:8080/actuator/refresh -d {} -H "Content-Type: application/json"
以後刷新頁面,便可看到更新後的消息。
總結
如今咱們搭建好了一個配置中心服務,它是根據每一個組件的spring.application.name
來決定讀取哪一個配置文件,而後咱們用了acurator的/refresh
api在運行時刷新配置項。另外配置項都是基於版本控制的,能夠方便的進行還原和更新。經過這個教程能夠看到Spring Cloud的各組件的配置至關簡單,基本就只用一條註解就能夠建立一個完整的服務組件。