go使用etcd3构建微服务发现和配置管理

今天Go 1.7正式版发布了!

etcd是一个高可用的键值存储系统,主要用于共享配置和服务发现。etcd是由CoreOS开发并维护的,灵感来自于 ZooKeeperDoozer,它使用Go语言编写,并通过Raft一致性算法处理日志复制以保证强一致性。

go-etcd 这个已经被废弃,建议使用官方的 github.com/coreos/etcd/clientv3

安装&运行etcd

brew install etcd

etcd

使用etcdctl命令

etcdctl 是一个命令行客户端,它能提供一些简洁的命令,供用户直接跟 etcd 服务打交道,而无需基于 HTTP API 方式。这在某些情况下将很方便,例如对服务进行测试或者手动修改数据库内容。也推荐在刚接触 etcd 时通过 etcdctl 命令来熟悉相关操作,这些操作跟 HTTP API 实际上是对应的。

安装 go get github.com/coreos/etcd/etcdctl 也可以直接下载etcd二进制 (包含etcd、etcdctl)

注意:目前本文测试使用的是etcd v3,一定不要忘记在环境变量中设置 export ETCDCTL_API=3 否则etcdctl默认使用的是v2与v3是完全隔离的(同一个etcd服务,不同的存储引擎)。

API V2与V3区别

  • 事务:ETCD V3提供了多键条件事务(multi-key conditional transactions),应用各种需要使用事务代替原来的Compare-And-Swap操作。
  • 平键空间(Flat key space):ETCD V3不再使用目录结构,只保留键。例如:”/a/b/c/“是一个键,而不是目录。V3中提供了前缀查询,来获取符合前缀条件的所有键值,这变向实现了V2中查询一个目录下所有子目录和节点的功能。
  • 简洁的响应:像DELETE这类操作成功后将不再返回操作前的值。如果希望获得删除前的值,可以使用事务,来实现一个原子操作,先获取键值,然后再删除。
  • 租约:租约代替了V2中的TTL实现,TTL绑定到一个租约上,键再附加到这个租约上。当TTL过期时,租约将被销毁,同时附加到这个租约上的键也被删除。
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
➜  etcd etcdctl --help
NAME:
etcdctl - A simple command line client for etcd3.

USAGE:
etcdctl

VERSION:
3.0.0+git

COMMANDS:
alarm disarm Disarms all alarms
alarm list Lists all alarms
auth disable Disables authentication
auth enable Enables authentication
compaction Compacts the event history in etcd
defrag Defragments the storage of the etcd members with given endpoints
del Removes the specified key or range of keys [key, range_end)
elect Observes and participates in leader election
endpoint health Checks the healthiness of endpoints specified in `--endpoints` flag
endpoint status Prints out the status of endpoints specified in `--endpoints` flag
get Gets the key or a range of keys
help Help about any command
lease grant Creates leases
lease keep-alive Keeps leases alive (renew)
lease revoke Revokes leases
lock Acquires a named lock
make-mirror Makes a mirror at the destination etcd cluster
member add Adds a member into the cluster
member list Lists all members in the cluster
member remove Removes a member from the cluster
member update Updates a member in the cluster
migrate Migrates keys in a v2 store to a mvcc store
put Puts the given key into the store
role add Adds a new role
role delete Deletes a role
role get Gets detailed information of a role
role grant-permission Grants a key to a role
role list Lists all roles
role revoke-permission Revokes a key from a role
snapshot restore Restores an etcd member snapshot to an etcd directory
snapshot save Stores an etcd node backend snapshot to a given file
snapshot status Gets backend snapshot status of a given file
txn Txn processes all the requests in one transaction
user add Adds a new user
user delete Deletes a user
user get Gets detailed information of a user
user grant-role Grants a role to a user
user list Lists all users
user passwd Changes password of user
user revoke-role Revokes a role from a user
version Prints the version of etcdctl
watch Watches events stream on keys or prefixes

OPTIONS:
--cacert="" verify certificates of TLS-enabled secure servers using this CA bundle
--cert="" identify secure client using this TLS certificate file
--command-timeout=5s timeout for short running command (excluding dial timeout)
--dial-timeout=2s dial timeout for client connections
--endpoints=[127.0.0.1:2379] gRPC endpoints
--hex[=false] print byte strings as hex encoded strings
--insecure-skip-tls-verify[=false] skip server certificate verification
--insecure-transport[=true] disable transport security for client connections
--key="" identify secure client using this TLS key file
--user="" username[:password] for authentication (prompt if password is not supplied)
-w, --write-out="simple" set the output format (simple, json, etc..)

存储 etcdctl put sensors "{aa:1, bb: 2}"

获取 etcdctl get sensors

监视 etcdctl watch sensors

获取所有值(或指定前缀 )etcdctl get --prefix=true ""

Golang 中使用

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
package main

import (
"context"
"fmt"
"log"
"time"

"github.com/coreos/etcd/clientv3"
)

var (
dialTimeout = 5 * time.Second
requestTimeout = 2 * time.Second
endpoints = []string{"127.0.0.1:2379"}
)

func main() {
cli, err := clientv3.New(clientv3.Config{
Endpoints: endpoints,
DialTimeout: dialTimeout,
})
if err != nil {
log.Fatal(err)
}
defer cli.Close()

log.Println("存储值")
if _, err := cli.Put(context.TODO(), "sensors", `{sensor01:{topic:"w_sensor01"}}`); err != nil {
log.Fatal(err)
}

log.Println("获取值")
if resp, err := cli.Get(context.TODO(), "sensors"); err != nil {
log.Fatal(err)
} else {
log.Println("resp: ", resp)
}

// see https://github.com/coreos/etcd/blob/master/clientv3/example_kv_test.go#L220
log.Println("事务&超时")
ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
_, err = cli.Txn(ctx).
If(clientv3.Compare(clientv3.Value("key"), ">", "abc")). // txn value comparisons are lexical
Then(clientv3.OpPut("key", "XYZ")). // this runs, since 'xyz' > 'abc'
Else(clientv3.OpPut("key", "ABC")).
Commit()
cancel()
if err != nil {
log.Fatal(err)
}

// see https://github.com/coreos/etcd/blob/master/clientv3/example_watch_test.go
log.Println("监视")
rch := cli.Watch(context.Background(), "", clientv3.WithPrefix())
for wresp := range rch {
for _, ev := range wresp.Events {
fmt.Printf("%s %q : %q\n", ev.Type, ev.Kv.Key, ev.Kv.Value)
}
}
}

关于context包

context包,由google官方开发,标准库中net、net/http、os/exec都用到了context,etcd中也使用了context,并发编程用途广泛,现已集成到标准库中。

  • 不要传入一个nil的Context,你不确定你要用什么 Context 的时候传一个 context.TODO()
  • context 包提供了 Background()TODO() 两个空的Context
  • 由于context模式用途广泛,Go 1.7将原来的golang.org/x/net/context包挪入了标准库中 context
  • 不要把Context存在一个结构体当中,显式地传入函数。Context变量需要作为第一个参数使用,一般命名为ctx
  • Context在多个goroutine中是安全的

参考: