網站首頁 編程語言 正文
前言
我們在前面介紹了 etcd 的整體架構。學習客戶端與 etcd 服務端的通信以及 etcd 集群節點的內部通信接口對于我們更好地使用和掌握 etcd 組件很有幫助,也是所必需了解的內容。我們將會介紹 etcd 的 gRPC 通信接口以及客戶端的實踐。
etcd clientv3 客戶端
etcd 客戶端 clientv3 接入的示例將會以 Go 客戶端為主,讀者需要準備好基本的開發環境。
首先是 etcd clientv3 的初始化,我們根據指定的 etcd 節點,建立客戶端與 etcd 集群的連接。
cli,err := clientv3.New(clientv3.Config{ Endpoints:[]string{"localhost:2379"}, DialTimeout: 5 * time.Second, })
如上的代碼實例化了一個 client,這里需要傳入的兩個參數:
- Endpoints:etcd 的多個節點服務地址,因為我是單點本機測試,所以只傳 1 個。
- DialTimeout:創建 client 的首次連接超時,這里傳了 5 秒,如果 5 秒都沒有連接成功就會返回 err;值得注意的是,一旦 client 創建成功,我們就不用再關心后續底層連接的狀態了,client 內部會重連。
etcd 客戶端初始化
解決完包依賴之后,我們初始化 etcd 客戶端??蛻舳顺跏蓟a如下所示:
// client_init_test.go package client import ( "context" "fmt" "go.etcd.io/etcd/clientv3" "testing" "time" ) // 測試客戶端連接 func TestEtcdClientInit(t *testing.T) { var ( config clientv3.Config client *clientv3.Client err error ) // 客戶端配置 config = clientv3.Config{ // 節點配置 Endpoints: []string{"localhost:2379"}, DialTimeout: 5 * time.Second, } // 建立連接 if client, err = clientv3.New(config); err != nil { fmt.Println(err) } else { // 輸出集群信息 fmt.Println(client.Cluster.MemberList(context.TODO())) } client.Close() }
如上的代碼,預期的執行結果如下:
=== RUN ? TestEtcdClientInit
&{cluster_id:14841639068965178418 member_id:10276657743932975437 raft_term:3 ?[ID:10276657743932975437 name:"default" peerURLs:"http://localhost:2380" clientURLs:"http://0.0.0.0:2379" ] {} [] 0} <nil>
--- PASS: TestEtcdClientInit (0.08s)
PASS
可以看到 clientv3 與 etcd Server 的節點 localhost:2379 成功建立了連接,并且輸出了集群的信息,下面我們就可以對 etcd 進行操作了。
client 定義
接著我們來看一下 client 的定義:
type Client struct { Cluster KV Lease Watcher Auth Maintenance // Username is a user name for authentication. Username string // Password is a password for authentication. Password string }
注意,這里顯示的都是可導出的模塊結構字段,代表了客戶端能夠使用的幾大核心模塊,其具體功能介紹如下:
- Cluster:向集群里增加 etcd 服務端節點之類,屬于管理員操作。
- KV:我們主要使用的功能,即操作 K-V。
- Lease:租約相關操作,比如申請一個 TTL=10 秒的租約。
- Watcher:觀察訂閱,從而監聽最新的數據變化。
- Auth:管理 etcd 的用戶和權限,屬于管理員操作。
- Maintenance:維護 etcd,比如主動遷移 etcd 的 leader 節點,屬于管理員操作
原文鏈接:https://juejin.cn/post/7092378745746489374
相關推薦
- 2022-02-27 通用mapper的查詢,selectByPrimaryKey、select、selectByExam
- 2022-12-08 C++中cout輸出中文信息亂碼問題及解決_C 語言
- 2022-10-10 VMware?Workstation與Device/Credential?Guard不兼容的解決_V
- 2022-04-08 c++11中std::move函數的使用_C 語言
- 2022-10-02 react中路由和按需加載的問題_React
- 2022-07-17 oracle數據庫去除重復數據常用的方法總結_oracle
- 2022-04-14 Python中字典的相關操作介紹_python
- 2022-10-26 Go?語言數據結構之雙鏈表學習教程_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支