go使用etcd3构建微服务发现和配置管理
Aug 16, 2016 · 4 min read
[
golang
etcd
分布式
微服务
]
今天Go 1.7正式版发布了!
etcd是一个高可用的键值存储系统,主要用于共享配置和服务发现。etcd是由CoreOS开发并维护的,灵感来自于 ZooKeeper 和 Doozer,它使用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过期时,租约将被销毁,同时附加到这个租约上的键也被删除。
➜ etcd etcdctl --help
NAME:
etcdctl - A simple command line client for etcd3.
USAGE:
etcdctl
VERSION:
3.0.0+git
COMMANDS:
alarm disarmDisarms all alarms
alarm listLists all alarms
auth disableDisables authentication
auth enableEnables authentication
compactionCompacts the event history in etcd
defragDefragments the storage of the etcd members with given endpoints
delRemoves the specified key or range of keys [key, range_end)
electObserves and participates in leader election
endpoint healthChecks the healthiness of endpoints specified in `--endpoints` flag
endpoint statusPrints out the status of endpoints specified in `--endpoints` flag
getGets the key or a range of keys
helpHelp about any command
lease grantCreates leases
lease keep-aliveKeeps leases alive (renew)
lease revokeRevokes leases
lockAcquires a named lock
make-mirrorMakes a mirror at the destination etcd cluster
member addAdds a member into the cluster
member listLists all members in the cluster
member removeRemoves a member from the cluster
member updateUpdates a member in the cluster
migrateMigrates keys in a v2 store to a mvcc store
putPuts the given key into the store
role addAdds a new role
role deleteDeletes a role
role getGets detailed information of a role
role grant-permissionGrants a key to a role
role listLists all roles
role revoke-permissionRevokes a key from a role
snapshot restoreRestores an etcd member snapshot to an etcd directory
snapshot saveStores an etcd node backend snapshot to a given file
snapshot statusGets backend snapshot status of a given file
txnTxn processes all the requests in one transaction
user addAdds a new user
user deleteDeletes a user
user getGets detailed information of a user
user grant-roleGrants a role to a user
user listLists all users
user passwdChanges password of user
user revoke-roleRevokes a role from a user
versionPrints the version of etcdctl
watchWatches 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=5stimeout for short running command (excluding dial timeout)
--dial-timeout=2sdial 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 中使用
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#L220log.Println("事务&超时")
ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
_, err = cli.Txn(ctx).
If(clientv3.Compare(clientv3.Value("key"), ">", "abc")). // txn value comparisons are lexicalThen(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.golog.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中是安全的
参考: