1. 程式人生 > >Go環境下,編譯運行etcd與goreman集群管理(1)

Go環境下,編譯運行etcd與goreman集群管理(1)

image darwin 輸出 slave 編譯運行 eight rem ima wid

Go環境下編譯運行etcd與goreman管理

近幾年了Go在比特幣、區塊鏈、雲服務等相關重要領域貢獻突出,作為IT行業的傳承“活到老、學到光頭”,保持學習心態。

周末放假,補充一二

主題:在Go環境下首試傳聞已久的etcd與goreman, 開源高性能KV集群服務,並提供共享配置、服務的註冊和發現,在當前微服務流行的年代,充當著中間存儲與代理服務的重要角色,除了與redis相對比功能相似外,etcd更貼近於微服務集成,得益於它的共享配置、服務的註冊和發現。

SO,試行一把並作記錄~~

1.安裝Golang

下載地址: https://studygolang.com/dl 各平臺版本按需自助,

此處for MAC: https://studygolang.com/dl/golang/go1.12.1.darwin-amd64.pkg

2.獲取etcd與goreman源碼

go get github.com/etcd-io/etcd   
go get github.com/mattn/goreman

3.編譯,並生成exe到$GOPATH/bin目錄,( go build編譯輸出到main文件同目錄,go install編譯輸出到$GOPATH/bin )

go install github.com/etcd-io/etcd          #KV服務
go install github.com
/etcd-io/etcd/etcdctl   #讀寫控件 go install github.com/mattn/goreman        #KV集群管理

4.啟動執行,啟動goreman需要一個集群配置來啟動和管理集群的etcd,並選中其中一個作為Master其余作為Slave

創建 $GOPATH/bin/Procfile文件

# Use goreman to run `go get github.com/mattn/goreman`
etcd1: etcd --name infra1 --listen-client-urls http://127.0.0.1:2379 --advertise-client-urls 
http://127.0.0.1:2379 --listen-peer-urls http://127.0.0.1:12380 --initial-advertise-peer-urls http://127.0.0.1:12380 --initial-cluster-token etcd-cluster-1 --initial-cluster ‘infra1=http://127.0.0.1:12380,infra2=http://127.0.0.1:22380,infra3=http://127.0.0.1:32380‘ --initial-cluster-state new --enable-pprof --logger=zap --log-outputs=stderr etcd2: etcd --name infra2 --listen-client-urls http://127.0.0.1:22379 --advertise-client-urls http://127.0.0.1:22379 --listen-peer-urls http://127.0.0.1:22380 --initial-advertise-peer-urls http://127.0.0.1:22380 --initial-cluster-token etcd-cluster-1 --initial-cluster ‘infra1=http://127.0.0.1:12380,infra2=http://127.0.0.1:22380,infra3=http://127.0.0.1:32380‘ --initial-cluster-state new --enable-pprof --logger=zap --log-outputs=stderr etcd3: etcd --name infra3 --listen-client-urls http://127.0.0.1:32379 --advertise-client-urls http://127.0.0.1:32379 --listen-peer-urls http://127.0.0.1:32380 --initial-advertise-peer-urls http://127.0.0.1:32380 --initial-cluster-token etcd-cluster-1 --initial-cluster ‘infra1=http://127.0.0.1:12380,infra2=http://127.0.0.1:22380,infra3=http://127.0.0.1:32380‘ --initial-cluster-state new --enable-pprof --logger=zap --log-outputs=stderr #proxy: etcd grpc-proxy start --endpoints=127.0.0.1:2379,127.0.0.1:22379,127.0.0.1:32379 --listen-addr=127.0.0.1:23790 --advertise-client-url=127.0.0.1:23790 --enable-pprof

執行命令

PS:~/go/bin goreman start 

技術分享圖片

附:更多轉閱 https://frank6866.gitbooks.io/linux/content/chapters/db/db-etcd-etcdctl.html

5.驗證結果

當前啟動集群:

http://127.0.0.1:2379
http://127.0.0.1:22379
http://127.0.0.1:32379

往其中一個服務添加一個key,然後在另外兩個服務讀取

# 添加一個Key,默認缺省endpoints服務為端口2379,可以不用寫
etcdctl put mykey "this is a hello world"


# 在2379上讀取
etcdctl get mykey

# 在22379上讀取
etcdctl --endpoints=http://localhost:22379 get mykey

# 在33379上讀
etcdctl --endpoints=http://localhost:32379 get mykey

技術分享圖片

Bingo~

Go環境下,編譯運行etcd與goreman集群管理(1)