etcd - 一个分布式一致性键值存储系统
生活随笔
收集整理的這篇文章主要介紹了
etcd - 一个分布式一致性键值存储系统
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
etcd - 一個分布式一致性鍵值存儲系統
etcd是一個分布式一致性鍵值存儲系統,用于共享配置和服務發現,專注于:
簡單:良好定義的,面向用戶的API (gRPC)
安全: 帶有可選客戶端證書認證的自動TLS
快速:測試驗證,每秒10000寫入
可靠:使用Raft適當分布
etcd是Go編寫,并使用Raft一致性算法來管理高可用復制日志,架構如下圖所示:
下載安裝
$ mkdir -p $GOPATH/src/github.com/coreos $ cd !$ $ git clone https://github.com/coreos/etcd.git $ cd etcd $ ./build $ ./bin/etcd另外一種下載安裝的方法:
直接下載etcd二進制 (包含etcd、etcdctl)
https://github.com/coreos/etcd/releases
測試
$ cd $GOPATH $ ./bin/etcd$ cd $GOPATH $ ETCDCTL_API=3 ./bin/etcdctl put foo bar# 輸出結果顯示OK,表示安裝成功 OK搭建本地集群
$ go get github.com/mattn/goreman$ cd $GOPATH/src/github.com/coreos/etcd $ goreman -f Procfile start查看本地集群的服務器列表
$ cd $GOPATH/src/github.com/coreos/etcd$ ./bin/etcdctl member list# 顯示結果8211f1d0f64f3269: name=infra1 peerURLs=http://127.0.0.1:12380 clientURLs=http://127.0.0.1:2379 isLeader=false 91bc3c398fb3c146: name=infra2 peerURLs=http://127.0.0.1:22380 clientURLs=http://127.0.0.1:22379 isLeader=true fd422379fda50e48: name=infra3 peerURLs=http://127.0.0.1:32380 clientURLs=http://127.0.0.1:32379 isLeader=false存儲數據
export ETCDCTL_API=3$ ./bin/etcdctl put foo "Hello World!"OK$ ./bin/etcdctl get foofoo Hello World!$ ./bin/etcdctl --write-out="json" get foo{"header":{"cluster_id":17237436991929493444,"member_id":9372538179322589801,"revision":2,"raft_term":2},"kvs":[{"key":"Zm9v","create_revision":2,"mod_revision":2,"version":1,"value":"SGVsbG8gV29ybGQh"}],"count":1}根據前綴查詢
$ ./bin/etcdctl put web1 value1 $ ./bin/etcdctl put web2 value2 $ ./bin/etcdctl put web3 value3$ ./bin/etcdctl get web --prefixweb1 value1 web2 value2 web3 value3刪除數據
$ ./bin/etcdctl put key myvalue $ ./bin/etcdctl del key 1 $ ./bin/etcdctl get key // 查詢結果為空$ ./bin/etcdctl put k1 value1 $ ./bin/etcdctl put k2 value2 $ ./bin/etcdctl del k --prefix 2 $ ./bin/etcdctl get k --prefix // 查詢結果為空事務寫入
$ ./bin/etcdctl put user1 bad OK $ ./bin/etcdctl txn --interactivecompares: // 輸入以下內容,輸入結束按 兩次回車 value("user1") = "bad" //如果 user1 = bad,則執行 get user1 success requests (get, put, del): get user1 //如果 user1 != bad,則執行 put user1 good failure requests (get, put, del): put user1 good // 運行結果,執行 success SUCCESSuser1 bad$ ./bin/etcdctl txn --interactive compares: value("user1") = "111" // 如果 user1 = 111,則執行 get user1 success requests (get, put, del): get user1 //如果 user1 != 111,則執行 put user1 2222 failure requests (get, put, del): put user1 2222 // 運行結果,執行 failure FAILUREOK$ ./bin/etcdctl get user1 user1 2222watch
// 當 stock1 的數值改變( put 方法)的時候,watch 會收到通知 $ ./bin/etcdctl watch stock1 // 新打開終端 $ export ETCDCTL_API=3 $ ./bin/etcdctl put stock1 1000 //在watch 終端顯示 PUT stock1 1000$ ./bin/etcdctl watch stock --prefix $ ./bin/etcdctl put stock1 10 $ ./bin/etcdctl put stock2 20lease
$ ./bin/etcdctl lease grant 300 # lease 326963a02758b527 granted with TTL(300s)$ ./bin/etcdctl put sample value --lease=326963a02758b527 OK$ ./bin/etcdctl get sample$ ./bin/etcdctl lease keep-alive 326963a02758b520 $ ./bin/etcdctl lease revoke 326963a02758b527 lease 326963a02758b527 revoked# or after 300 seconds $ ./bin/etcdctl get sampleDistributed locks
//第一終端 $ ./bin/etcdctl lock mutex1 mutex1/326963a02758b52d# 第二終端 $ ./bin/etcdctl lock mutex1// 當第一個終端結束了,第二個終端會顯示 mutex1/326963a02758b531Elections
$ ./bin/etcdctl elect one p1one/326963a02758b539 p1# another client with the same name blocks $ ./bin/etcdctl elect one p2 //結束第一終端,第二終端顯示 one/326963a02758b53e p2Cluster status
集群狀態
$ ./bin/etcdctl --write-out=table endpoint status$ ./bin/etcdctl endpoint healthSnapshot
./bin/etcdctl snapshot save my.dbSnapshot saved at my.db./bin/etcdctl --write-out=table snapshot status my.dbMember
./bin/etcdctl member list -w table總結
以上是生活随笔為你收集整理的etcd - 一个分布式一致性键值存储系统的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Golang和Ethereum中的big
- 下一篇: 用Go语言建立一个简单的区块链part1