1. 程式人生 > >6--SpringCloud搭建分散式配置中心(續-高可用性)

6--SpringCloud搭建分散式配置中心(續-高可用性)

  本文接之前的《5--SpringCloud搭建分散式配置中心》,繼續來說說Spring Cloud Config的使用。

  先來回顧一下,在前文中我們完成了什麼:

  • 構建了config-server,連線到Git倉庫
  • 在Git上建立了一個5--SpringCloud--Config目錄,用來儲存配置資訊
  • 構建了config-client,來獲取Git中的配置資訊

  在本文中,我們繼續來看看Spring Cloud Config的一些其他能力。

 

高可用問題

  Config Server與服務註冊中心一樣,我們也需要將其擴充套件為高可用的叢集。

  所以,簡單的方法就是把config-server也註冊為服務,這樣所有客戶端就能以服務的方式進行訪問。通過這種方法,只需要啟動多個指向同一Git倉庫位置的config-server就能實現高可用了。

  首先搭建一個服務註冊中心:如果不知道怎麼搭建服務註冊中心請閱讀:《1--SpringCloud的服務註冊與發現Eureka

config-server配置

  pom.xml的dependencies節點中引入如下依賴,相比之前的config-server就,加入了spring-cloud-starter-eureka,用來註冊服務

<dependencies>

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

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

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Brixton.RELEASE</version>
            <type>pom</type>
            <scope>import
</scope> </dependency> </dependencies> </dependencyManagement>

 

   

  配置application.properties新增# 配置服務註冊中心 eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

spring.application.name=config-server
server.port=7001
# 配置服務註冊中心
eureka.client.serviceUrl.defaultZone
=http://localhost:1111/eureka/ # git配置 #所在專案根目錄 spring.cloud.config.server.git.uri=https://gitee.com/cengjiang/springcloud_learning/ #所在地址目錄 spring.cloud.config.server.git.searchPaths=5--SpringCloud--Config #如果是公開專案則不用寫使用者名稱密碼 spring.cloud.config.server.git.username=username spring.cloud.config.server.git.password=password

 

 

  在應用主類中,新增@EnableDiscoveryClient註解,用來將config-server註冊到上面配置的服務註冊中心上去。

@EnableConfigServer
@EnableDiscoveryClient
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class).web(true).run(args);
    }

}

 

  

  啟動該應用,並訪問http://localhost:1111/,可以在Eureka Server的資訊面板中看到config-server已經被註冊了。

  

 

config-client配置

   同config-server一樣,在pom.xml的dependencies節點中新增spring-cloud-starter-eureka依賴,用來註冊服務:

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

 

  

  bootstrap.properties中,按如下配置:

server.port=7002
#對應前配置檔案中的{application}部分
spring.application.name=ghghspace
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
#引數設定為true,開啟通過服務來訪問Config Server的功能
spring.cloud.config.discovery.enabled=true
#對應前配置檔案中的{profile}部分
spring.cloud.config.profile=dev
#對應前配置檔案的git分支
spring.cloud.config.label=master
#配置中心的訪問地址
spring.cloud.config.uri=http://localhost:7001/

 

  通過eureka.client.serviceUrl.defaultZone引數指定服務註冊中心,用於服務的註冊與發現,再將spring.cloud.config.discovery.enabled引數設定為true,開啟通過服務來訪問Config Server的功能

 

  在應用主類中,增加@EnableDiscoveryClient註解,用來發現config-server服務,利用其來載入應用配置

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class).web(true).run(args);
    }

}

 

 

  沿用之前我們建立的Controller來載入Git中的配置資訊

@RefreshScope
@RestController
public class TestController {

    @Value("${from}")
    private String from;

    @RequestMapping("/from")
    public String from() {

        return this.from;
    }

}

 

 

  完成了上述配置之後,我們啟動該客戶端應用。若啟動成功,訪問http://localhost:1111/,可以在Eureka Server的資訊面板中看到該應用已經被註冊成功了。

  

 

   訪問客戶端應用提供的服務:http://localhost:7002/from 會返回資訊

   

配置重新整理

   有時候,我們需要對配置內容做一些實時更新的場景,那麼Spring Cloud Config是否可以實現呢?答案顯然是可以的。下面,我們看看如何進行改造來實現配置內容的實時更新。

   在改造程式之前,我們先將config-server和config-client都啟動起來,並訪問客戶端提供的REST APIhttp://localhost:7002/from來獲取配置資訊,可以獲得返回內容為:git-dev-1.0

   接著,我們可以嘗試使用Git工具修改當前配置的內容,比如,將5--SpringCloud--Config/didispace-dev.properties中的from的值從from=git-dev-1.0修改為from=git-dev-2.0,再訪問http://localhost:7002/from,可以看到其返回內容還是git-dev-1.0

   下面,我們將在config-client端增加一些內容和操作以實現配置的重新整理:

  在config-clinet的pom.xml中新增spring-boot-starter-actuator監控模組,其中包含了/refresh重新整理API。

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

 

  重新啟動config-clinet,訪問一次http://localhost:7002/from,可以看到當前的配置值

  • 修改Git倉庫5--SpringCloud--Config/didispace-dev.properties檔案中from的值
  • 再次訪問一次http://localhost:7002/from,可以看到配置值沒有改變
  • 通過POST請求傳送到http://localhost:7002/refresh,我們可以看到返回內容如下,代表from引數的配置內容被更新了(請求需要為POST請求,get請求為405)
[
  "from"
]

 

  再次訪問一次http://localhost:7002/from,可以看到配置值已經是更新後的值了通過上面的介紹,大家不難想到,當有Git提交變化時,就給對應的配置主機發送/refresh請求來實現配置資訊的實時更新。

  原始碼下載: 6--SpringCloud搭建分散式配置中心(續-高可用性)