Consul作為SpringCloud配置中心

2022-06-11 09:51:08 字數 3728 閱讀 8300

在分布式系統中動態配置中,可以避免重複重啟服務,動態更改服務引數等。一句話非常重要。 另外一篇文章也是這樣說的,哈哈。 consul 作為spring 推薦的分布式排程系統其也具備配置中心的功能, 我們也可以利用其作為配置中心,其client端主動定時發起與配置中心同步機制,實現動態配置的的更新。

環境依賴:名稱值

備註jdk

1.8consul

1.5.2

註冊中心,consul安裝及介紹

springcloud

greenwich.sr1

1) pom依賴(主要)

1.8

greenwich.sr1

org.springframework.cloud

spring-cloud-dependencies

$pom

import

org.springframework.boot

spring-boot-starter-web

org.springframework.cloud

spring-cloud-starter-consul-discovery

org.springframework.cloud

spring-cloud-starter-consul-config

2)配置檔案

#0表示伺服器隨機埠

server.port=8090

company.pay.money=0

bootstrap.properties

#服務名稱

#consul 位址

spring.cloud.consul.host=localhost

#consul 埠

spring.cloud.consul.port=8500

spring.cloud.consul.discovery.prefer-ip-address=true

#consul配置中心功能,預設true

spring.cloud.consul.config.enabled=true

#consul配置中心值的格式

spring.cloud.consul.config.format=yaml

3)動態引數接收類

@configurationproperties("company.pay")

@refreshscope

@data

@component

public class paymoneyproperties

備註:

configurationproperties 表示這個類關聯動態配置,「company.pay」表示key的字首部分。

@refreshscope 表示動態重新整理config server 值

@component 表示將該類載入到ioc容器中

在實戰中嘗試用@value的方式獲取動態,只能實現服務重啟後獲取動態的config server 的值,最終找到解決方案在相應的取值類上加@refreshscope註解,完美解決。

4)對外介面(便於直觀驗證)

方式一:

@restcontroller

public class consulconfigcontroller

方式二:

5)啟動專案

上圖可以通過日誌看出config server 的連線資訊

6)consul config server 還沒設定對應節點值時演示(獲取的是本地配置檔案值)7) consul 中建立資料節點

如下圖,表示已經通知到專案更新的值

在驗證介面中請求一下對應介面,發現值已經和consul config server 中動態設定的值相同了

1) 如果在你們的微服務中已經使用consul 作為註冊中心, 那麼推薦使用上文的方案, 畢竟可以少維護一套系統。

2) consul 作為註冊中心、相比zookeeper 作為註冊中心,有了更友好的web頁面,如果有版本或回滾的一些操作就更完美了。

3)client 會定時拉取consul config server 值,與本地值對比

configwatch 類核心**

@override

public void start()

}

@timed("consul.watch-config-keys")

public void watchconfigkeyvalues()

try

log.trace("watching consul for context '" + context + "' with index "

+ currentindex);

// use the consul acl token if found

string acltoken = this.properties.getacltoken();

if (stringutils.isempty(acltoken))

response> response = this.consul.getkvvalues(context,

acltoken,

new queryparams(this.properties.getwatch().getwaittime(),

currentindex));

// if response.value == null, response was a 404, otherwise it was a

// 200

// reducing churn if there wasn't anything

if (response.getvalue() != null && !response.getvalue().isempty())

else if (log.istraceenabled())

this.consulindexes.put(context, newindex);

}else if (log.istraceenabled())

}else if (log.istraceenabled())

}catch (exception e)

else if (log.istraceenabled())

else if (log.iswarnenabled())

}} }

this.firsttime = false;

}

consul作為SpringCloud註冊中心

一 consul 簡介 consul是一種服務發現和配置工具。consul具有分布式,高可用性和極高的可擴充套件性。github 注意 在本地啟用consul 需要通過cmd 命令列,詳見 二 springcloud 用 consul作為註冊中心 2.1 服務提供方 1 pom依賴 1.8 gree...

Consul實踐之Consul是什麼

consul是乙個兩年前由hashicorp組織發起的開源專案,因此至今有兩年以上的歷史。consul由go語言開發,部署起來非常容易,只需要極少的可執行程式和配置檔案,具有綠色 輕量級的特點。consul有一組元件,因此能提供多種功能,目前主要的功能就是服務發現,其他的附加功能,比如自動編排,ke...

Consul服務註冊中心 Consul工作原理

consul工作原理如下圖所示 當服務producer啟動時,會將自己的ip host等資訊通過傳送請求告知consul,consul接收到producer的註冊資訊後,每個10s 預設 會向producer傳送乙個健康檢查的請求,檢驗producer是否監控。當consumer請求producer...