之前说了etcd的简介,命令行使用,一些基本原理。这次来说说现实一点的集群部署和golang版本的客户端使用。因为在实际使用过程中,etcd的节点肯定是需要2N+1个进行部署的,所以有必要说明一下集群的部署。

集群部署

网上有很多集群部署的教程,有的很复杂,其实对于我们实际使用来说,其实配置并不复杂,下面举例一种最简单的集群配置。(简单到你想不到~)

下载

https://github.com/etcd-io/etcd/releases
还是在github上面找到需要下载的版本
我使用的是etcd-v3.3.13-linux-amd64.tar.gz
使用wget下载到linux你喜欢的目录,或者本地下载完成之后上传均可。

部署

首先我找了三台机器,对应ip为
192.168.4.224
192.168.4.225
192.168.4.226
PS:提醒一下记得开发对应防火墙的端口

然后将下载的文件解压,之后进入解压后的目录,分别使用下面的命令启动。(注意下面的命令对应的是三台不同的机器,你需要修改对应为你自己的ip)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$ ./etcd --name infra0 --initial-advertise-peer-urls http://192.168.4.224:2380 \
--listen-peer-urls http://192.168.4.224:2380 \
--listen-client-urls http://192.168.4.224:2379,http://127.0.0.1:2379 \
--advertise-client-urls http://192.168.4.224:2379 \
--initial-cluster-token etcd-cluster-1 \
--initial-cluster infra0=http://192.168.4.224:2380,infra1=http://192.168.4.225:2380,infra2=http://192.168.4.226:2380 \
--initial-cluster-state new

$ ./etcd --name infra1 --initial-advertise-peer-urls http://192.168.4.225:2380 \
--listen-peer-urls http://192.168.4.225:2380 \
--listen-client-urls http://192.168.4.225:2379,http://127.0.0.1:2379 \
--advertise-client-urls http://192.168.4.225:2379 \
--initial-cluster-token etcd-cluster-1 \
--initial-cluster infra0=http://192.168.4.224:2380,infra1=http://192.168.4.225:2380,infra2=http://192.168.4.226:2380 \
--initial-cluster-state new

$ ./etcd --name infra2 --initial-advertise-peer-urls http://192.168.4.226:2380 \
--listen-peer-urls http://192.168.4.226:2380 \
--listen-client-urls http://192.168.4.226:2379,http://127.0.0.1:2379 \
--advertise-client-urls http://192.168.4.226:2379 \
--initial-cluster-token etcd-cluster-1 \
--initial-cluster infra0=http://192.168.4.224:2380,infra1=http://192.168.4.225:2380,infra2=http://192.168.4.226:2380 \
--initial-cluster-state new

至此,三个节点的集群部署完成。😂😂😂没错就是这么easy,没有网上说的那么复杂。

配置文件

如果你嫌弃每次使用这么长的命令进行启动,你可以将它写为配置文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 当前节点名称
name: infra1
# etcd数据保存目录
data-dir: /usr/local/etcd

# 供外部客户端使用的url
listen-client-urls: http://192.168.4.225:2379,http://127.0.0.1:2379
# 广播给外部客户端使用的url
advertise-client-urls: http://192.168.4.225:2379

# 集群内部通信使用的URL
listen-peer-urls: http://192.168.4.225:2380
# 广播给集群内其他成员访问的URL
initial-advertise-peer-urls: http://192.168.4.225:2380

# 集群的名称
initial-cluster-token: etcd-cluster-1
# 初始集群成员列表
initial-cluster: infra0=http://192.168.4.224:2380,infra1=http://192.168.4.225:2380,infra2=http://192.168.4.226:2380

#初始集群状态
initial-cluster-state: new

然后指定配置文件的路径进行启动就可以了

1
./etcd --config-file=conf.yml

其他部署策略

以上的部署一方面,我个人部署时使用的最简单方式,更简单的可能是使用yum进行etcd的下载。
当然上述方式也存在一些问题,现在的etcd相当于裸奔的情况:

  • 没有鉴权就想到于任何人知道ip和端口就可以连接上你的etcd,所以当前可能只适用于内网使用,服务通过内网ip进行访问(这个可以通过添加权限和用户来完成)
  • 当前通信是没有加密的
  • 当前etcd是利用静态ip来进行配置的,我认为这也是实际中用到最普通的情况,但是etcd还提供发现机制来进行部署和配置,更加灵活

等等,这些部署策略更多针对于线上,因为官方写的非常详细了,我感觉再写也就班门弄斧了。
https://doczhcn.gitbook.io/etcd/index/index-1/clustering

Golang客户端使用

这里来实际用代码操作一下etcd,还是和之前使用命令行一样,get/put/del/watch/lease用一下这些操作,其他操作请查看doc
https://godoc.org/github.com/coreos/etcd/clientv3

客户端下载

这里不建议使用go get进行下载,真的太慢了,可以直接从github上面下载之后放到对应目录快一些。https://github.com/etcd-io/etcd
下载解压之后放到gopath下对应:go/src/go.etcd.io/etcd

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main

import (
"context"
"fmt"
"go.etcd.io/etcd/clientv3"
"go.etcd.io/etcd/mvcc/mvccpb"
"time"
)

func main() {
// 配置客户端连接
client, err := clientv3.New(clientv3.Config{
// Endpoints: []string{"127.0.0.1:2379"},
Endpoints: []string{"192.168.4.224:2379", "192.168.4.225:2379", "192.168.4.226:2379"},
DialTimeout: 5 * time.Second,
})
if err != nil {
panic(err)
}
defer client.Close()

// 启动watch监听
watch := client.Watch(context.TODO(), "aaa")
go func() {
for {
watchResponse := <- watch
for _, ev := range watchResponse.Events {
switch ev.Type {
case mvccpb.DELETE:
fmt.Printf("监听到del:%s\n", ev.Kv.Key)
case mvccpb.PUT:
fmt.Printf("监听到put:%s, %s\n", ev.Kv.Key, ev.Kv.Value)
}
}
}
}()

// 新增
putResponse, err := client.Put(context.TODO(), "aaa", "xxx")
if err != nil {
fmt.Println(err)
return
}
fmt.Println(putResponse.Header.String())

// 查询
getResponse, err := client.Get(context.TODO(), "aaa")
if err != nil {
fmt.Println(err)
return
}
fmt.Println(getResponse.Kvs)

// 删除
deleteResponse, err := client.Delete(context.TODO(), "aaa")
if err != nil {
fmt.Println(err)
return
}
fmt.Println(deleteResponse.Header.String())

// 申请租约
grantResponse, err := client.Grant(context.TODO(), 10)
if err != nil {
fmt.Println(err)
return
}

// 使用租约
response, err := client.Put(context.TODO(), "aaa", "xxx", clientv3.WithLease(grantResponse.ID))
if err != nil {
fmt.Println(err)
return
}
fmt.Println(response.Header.String())

// 等待租约自动过期
time.Sleep(time.Second * 20)
}

大致能得到以下输出

监听到put:aaa, xxx
cluster_id:14841639068965178418 member_id:10276657743932975437 revision:53 raft_term:4
[key:”aaa” create_revision:53 mod_revision:53 version:1 value:”xxx” ]
监听到del:aaa
cluster_id:14841639068965178418 member_id:10276657743932975437 revision:54 raft_term:4
监听到put:aaa, xxx
cluster_id:14841639068965178418 member_id:10276657743932975437 revision:55 raft_term:4
监听到del:aaa

其实使用起来还是非常简单,我就不过多赘述了。