從剛入職接觸Zookeeper開始,簡單的認爲ZK是作爲服務註冊與訂閱使用的微服務架構組件,zookeeper的入門比較簡單,主要就是zoo.cfg文件的配置,還有可視化工具zkui可以很感官的看到服務的註冊信息,很方便。
後來又使用ZK進行動態參數配置中心,充分利用了watcher監聽通知的功能,但是集成的方式需要自己進行代碼維護邏輯,使得開發面向了過程,需要對每一個需要監聽的節點創建一個watcher,然後再實現add/update/delete等方法,對開發人員的要求較高,使用頻率不怎麼大。
再後來公司框架集成Springcloud,開始學習使用EUREKA和Consul,下面是從https://blog.csdn.net/u013068377/article/details/78933654摘過來的
Feature | Consul | zookeeper | etcd(沒用過) | euerka |
---|---|---|---|---|
服務健康檢查 | 服務狀態,內存,硬盤等 | (弱)長連接,keepalive | 連接心跳 | 可配支持 |
多數據中心 | 支持 | — | — | — |
kv存儲服務 | 支持 | 支持 | 支持 | — |
一致性 | raft | paxos | raft | — |
cap | ca | cp | cp | ap |
使用接口(多語言能力) | 支持http和dns | 客戶端 | http/grpc | http(sidecar) |
watch支持 | 全量/支持long polling | 支持 | 支持 long polling | 支持 long polling/大部分增量 |
自身監控 | metrics | — | metrics | metrics |
安全 | acl /https | acl | https支持(弱) | — |
spring cloud集成 | 已支持 | 已支持 | 已支持 | 已支持 |
因爲現在主要使用Consul進行服務發現,其他的先略過。
一、Consul的安裝
Consul中心和Zk類似是個客戶端執行工具,從官網很容易下載:https://www.consul.io/
https://www.consul.io/downloads.html
直接配置環境變量,或者在加壓文件上shift+右擊彈出命令框,輸入consul即可
consul需要服務器的性能要求
類型 | 中央處理器 | 內存 | 磁盤 | 典型的雲實例類型 |
---|---|---|---|---|
小 | 2核心 | 8-16 GB RAM | 50GB | AWS:m5.large,m5.xlarge |
Azure:Standard_A4_v2,Standard_A8_v2 | ||||
GCE:n1-standard-8,n1-standard-16 | ||||
大 | 4-8核心 | 32-64 GB RAM | 100GB | AWS:m5.2xlarge,m5.4xlarge |
Azure:Standard_D4_v3,Standard_D5_v3 | ||||
GCE:n1-standard-32,n1-standard-64 |
小尺寸適用於大多數初始生產部署或開發/測試環境。大尺寸適用於工作負載持續較高的生產環境。
consul集羣架構:
上面這圖是官網獲得的機構圖,他分爲了兩塊數據中心,也就是兩個集羣中心。每個通常是三到五臺服務器。Consul通過WAN鏈接連接的獨立集羣支持多數據中心部署。在某些情況下,還可以在同一LAN環境中部署兩個或多個Consul集羣。
主要了解一下節點之間的網絡通訊:
代理以客戶端或服務器模式運行,都採用的是Gossip協議規範(Redis集羣也是如此規範)。初始探測每秒通過UDP發送。如果節點無法在內部進行確認200ms
,則代理將通過TCP進行ping操作。如果TCP探測失敗(10秒超時),它會要求可配置數量的隨機節點探測同一節點(也稱爲間接探測)。如果對等體沒有關於節點狀態的響應,則該代理被標記爲關閉。
代理的狀態直接影響服務發現結果。如果代理已關閉,則它正在監視的服務也將標記爲關閉。
在跨越L2段的較大網絡中,流量通常穿過防火牆和/或路由器。必須更新ACL或防火牆規則以允許以下端口:
名稱 | 港口 | 旗 | 描述 |
---|---|---|---|
服務器RPC | 8300 | 服務器用於處理來自其他代理的傳入請求。僅限TCP。 | |
Serf LAN | 8301 | 用於處理局域網中的Gossip協議。所有代理都使用TCP和UDP。 | |
Serf WAN | 8302 | -1 禁用(在Consul 1.0.7中可用) |
服務器用於通過LAN和WAN連接到其他服務器。TCP和UDP。 |
HTTP API | 8500 | -1 禁用 |
客戶端用於與HTTP API通信。僅限TCP。 |
DNS接口 | 8600 | -1 禁用 |
如數據中心設計部分所述,網絡區域和網段可用於防止在不同子網之間打開防火牆端口。
默認情況下,代理僅偵聽本地接口上的HTTP和DNS流量。
二、Consul的簡單介紹
比較詳情的https://segmentfault.com/a/1190000005026002
這裏主要詳細講解Consul的一些命令操作符
控制檯輸入:consul
C:\Users\liuguangyu>consul
Usage: consul [--version] [--help] <command> [<args>]
Available commands are:
acl Interact with Consul's ACLs #與控制訪問列表相關聯
agent Runs a Consul agent #啓動代理
catalog Interact with the catalog #與目錄相關
connect Interact with Consul Connect #與連接相關
debug Records a debugging archive for operators #debug方式
event Fire a new event #
exec Executes a command on Consul nodes
force-leave Forces a member of the cluster to enter the "left" state
info Provides debugging information for operators.
intention Interact with Connect service intentions
join Tell Consul agent to join cluster #手動將節點加入集羣
keygen Generates a new encryption key
keyring Manages gossip layer encryption keys
kv Interact with the key-value store
leave Gracefully leaves the Consul cluster and shuts down
lock Execute a command holding a lock
maint Controls node or service maintenance mode
members Lists the members of a Consul cluster
monitor Stream logs from a Consul agent
operator Provides cluster-level tools for Consul operators
reload Triggers the agent to reload configuration files
rtt Estimates network round trip time between nodes
services Interact with services
snapshot Saves, restores and inspects snapshots of Consul server state
validate Validate config files/directories
version Prints the Consul version
watch Watch for changes in Consul #類似ZK的watcher
1、關於acl的概念查看https://blog.csdn.net/ningmeng_bumeng/article/details/78614362,主要用於網絡流量控制,具體在consul上創建策略的方式可以查看官方文檔https://learn.hashicorp.com/consul/advanced/day-1-operations/acl-guide
2、consul的啓動一般都是通過代理啓動,代理指明瞭客戶端的地址,未完待續。。