[菜鳥SpringCloud實(shí)戰(zhàn)入門]第七章:配置中心客戶端主動(dòng)刷新機(jī)制 + 配置中心服務(wù)化和高可用改造

前言

歡迎來到菜鳥SpringCloud實(shí)戰(zhàn)入門系列(SpringCloudForNoob),該系列通過層層遞進(jìn)的實(shí)戰(zhàn)視角,來一步步學(xué)習(xí)和理解SpringCloud。

本系列適合有一定Java以及SpringBoot基礎(chǔ)的同學(xué)閱讀。

每篇文章末尾都附有本文對(duì)應(yīng)的Github源代碼,方便同學(xué)調(diào)試。

實(shí)戰(zhàn)版本

  • SpringBoot:2.0.3.RELEASE
  • SpringCloud:Finchley.RELEASE

-----正文開始-----

配置中心客戶端主動(dòng)刷新機(jī)制 + 配置中心服務(wù)化和高可用改造

客戶端Refresh:客戶端主動(dòng)獲取配置信息

經(jīng)過上一章節(jié)配置好Spring Cloud Config后,客戶端(config-client模塊)能夠獲得從服務(wù)端(config-server模塊)傳來的配置文件信息。

文末寫出了一個(gè)問題,客戶端并不能獲取更新后的配置信息,想要刷新信息,必須重啟config-client模塊,這顯然不切實(shí)際。

實(shí)驗(yàn):驗(yàn)證客戶端無法更新

下面做一個(gè)實(shí)驗(yàn),啟動(dòng)客戶端和服務(wù)端,隨后更新dev配置文件,新加了(new):

在這里插入圖片描述

隨后push到遠(yuǎn)程倉庫,我們?cè)俅沃苯釉L問服務(wù)端的 http://localhost:8769/spring-cloud-config-dev.properties

在這里插入圖片描述

發(fā)現(xiàn)更新成了新的配置文件。

之后訪問客戶端:

在這里插入圖片描述

發(fā)現(xiàn)依然是老的配置文件信息,客戶端只在啟動(dòng)時(shí)獲取了當(dāng)時(shí)的配置文件信息。

開啟更新機(jī)制

我們只需要在config-server模塊中進(jìn)行改動(dòng)。

實(shí)現(xiàn)Refresh機(jī)制需要添加依賴spring-boot-starter-actuator,這個(gè)依賴在我們的root模塊中就已經(jīng)添加,在config-server模塊就不需要重復(fù)添加了。如果你在root父模塊沒有添加,那么就需要加上。

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

大坑:

對(duì)需要刷新的類加上注解@RefreshScope

當(dāng)配置更改時(shí),標(biāo)有@RefreshScope的Bean將得到特殊處理來生效配置。

注意這里不是直接加載主類上,除非你的controller寫在了主類里。

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;

@RestController
@RefreshScope
public class HelloController {
    @Value("${config.hello}")
    private String hello;

    @RequestMapping("/hello")
    public String from() {
        return this.hello;
    }
}

對(duì)于springboot 1.5.X 以上版本,需要在配置文件中關(guān)閉安全認(rèn)證。

management.security.enabled=false

對(duì)于springboot 2,上述配置不起作用,需要修改server端配置文件,將端口暴露:

management:
  endpoints:
    web:
      exposure:
        include: "*"

還要將客戶端端口暴露:

management:
  endpoints:
    web:
      exposure:
        include: refresh

測試:

我們開啟服務(wù)端和客戶端,先測試下未更新前獲取的配置信息:

在這里插入圖片描述

隨后我們修改配置文件并push:

在這里插入圖片描述

然后以post請(qǐng)求訪問 curl -v -X POST "http://localhost:8002/actuator/refresh"

在這里插入圖片描述

得到了:

在這里插入圖片描述

如果在不變更的情況下,再次發(fā)送POST請(qǐng)求:

在這里插入圖片描述

使用Webhook監(jiān)聽配置更新

WebHook是當(dāng)某個(gè)事件發(fā)生時(shí),通過發(fā)送http post請(qǐng)求的方式來通知信息接收方。Webhook來監(jiān)測你在Github.com上的各種事件,最常見的莫過于push事件。如果你設(shè)置了一個(gè)監(jiān)測push事件的Webhook,那么每當(dāng)你的這個(gè)項(xiàng)目有了任何提交,這個(gè)Webhook都會(huì)被觸發(fā),這時(shí)Github就會(huì)發(fā)送一個(gè)HTTP POST請(qǐng)求到你配置好的地址。

如此一來,你就可以通過這種方式去自動(dòng)完成一些重復(fù)性工作,比如,你可以用Webhook來自動(dòng)觸發(fā)一些持續(xù)集成(CI)工具的運(yùn)作,比如Travis CI;又或者是通過 Webhook 去部署你的線上服務(wù)器。下圖就是github上面的webhook配置。

這種機(jī)制適用于只有少數(shù)微服務(wù)的情況,在大量未服務(wù)的情況下,這種機(jī)制就顯得捉襟見肘。

消息總線機(jī)制

如果項(xiàng)目少配置少的情況可以通過/refresh來手動(dòng)刷新配置,如果項(xiàng)目比較復(fù)雜的情況呢這種肯定是行不通的,Spring Cloud Bus消息總線可以解決配置修改的真正的動(dòng)態(tài)刷新。我們放在下一章進(jìn)行學(xué)習(xí)。

配置中心服務(wù)化和高可用改造

目前我們的兩個(gè)子模塊config-server和config-client是相互耦合的,client需要輸入server的地址來調(diào)用它,這樣的調(diào)用違反了低耦合原則(低耦合:就是A模塊與B模塊存在依賴關(guān)系,那么當(dāng)B發(fā)生改變時(shí),A模塊仍然可以正常工作,那么就認(rèn)為A與B是低耦合的。)

現(xiàn)在我們就是用之前學(xué)習(xí)的Eureka來對(duì)配置中心進(jìn)行改造。

服務(wù)端改造

改造集中在兩方面,一個(gè)是在注冊(cè)中心注冊(cè),一個(gè)是開啟多個(gè)服務(wù)端達(dá)到高可用的目的。

添加依賴(由于eureka的依賴在我們的父模塊已經(jīng)添加,所以對(duì)于config-server模塊我們不需要改動(dòng)):

<dependencies>
    <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>

配置文件新增注冊(cè)配置:

server:
  port: 8769
spring:
  application:
    name: spring-cloud-config-server
  cloud:
    config:
      server:
        git:
          uri: https://xxxxxxxxxxx.git # 配置git倉庫的地址
          search-paths: config-repo                              # git倉庫地址下的相對(duì)地址,可以配置多個(gè),用,分割。
          username: xxxxxx                                     # git倉庫的賬號(hào)
          password: xxxxx                                   # git倉庫的密碼
# 客戶端調(diào)用需要
management:
  endpoints:
    web:
      exposure:
        include: "*"
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

啟動(dòng)類添加@EnableDiscoveryClient:

@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class ConfigServerApplication {

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

客戶端改造

依賴修改:同服務(wù)端相同,我們不需要修改,父模塊將注冊(cè)中心等都已經(jīng)引入(見第一章)

啟動(dòng)類添加@EnableDiscoveryClient:同上。

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class ConfigClientApplication {

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

}

配置文件yml修改:

在前面我們給config-client子模塊配置了兩個(gè)yml文件,一個(gè)是傳統(tǒng)application.yml一個(gè)是bootstrap.yml,bootstrap.yml的啟動(dòng)優(yōu)先于application.yml

我們修改bootstrap.yml,添加注冊(cè)中心配置,并將config的配置加上:

  • spring.cloud.config.discovery.enabled :開啟Config服務(wù)發(fā)現(xiàn)支持
  • spring.cloud.config.discovery.serviceId :指定server端的name,也就是server端spring.application.name的值
  • 刪除spring.cloud.config.uri
spring:
  cloud:
    config:
      name: spring-cloud-config
      profile: dev
      label: master
      discovery:
        enabled: true
        service-id: spring-cloud-config-server
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

隨后我們啟動(dòng)三個(gè)模塊:

  • Eureka子模塊
  • config-server
  • config-client
在這里插入圖片描述

查看Eureka狀態(tài) http://localhost:8761/

在這里插入圖片描述

為了達(dá)成高可用,我們將config-server的端口號(hào)再修改為8770,啟動(dòng)一個(gè)新的config-server,這樣就有兩個(gè)config-server同時(shí)為我們服務(wù)。

調(diào)用客戶端接口:

在這里插入圖片描述

本章代碼

https://github.com/qqxx6661/springcloud_for_noob/tree/master/07-config-server-config-client-eureka

參考

http://www.ityouknow.com/springcloud/2017/05/23/springcloud-config-svn-refresh.html

https://blog.csdn.net/youanyyou/article/details/78993060

http://www.ityouknow.com/springcloud/2017/05/25/springcloud-config-eureka.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容