go etcd clientV3 带tls demo
go 操作etcdV3
终端操作etcd链接:
https://www.cnblogs.com/zisefeizhu/p/15427799.html
安装etcd clientV3
$ get go.etcd.io/etcd/clientv3
# github.com/coreos/etcd/clientv3/balancer/resolver/endpoint
../../../go/pkg/mod/github.com/coreos/etcd@v3.3.27+incompatible/clientv3/balancer/resolver/endpoint/endpoint.go:114:78: undefined: resolver.BuildOption
../../../go/pkg/mod/github.com/coreos/etcd@v3.3.27+incompatible/clientv3/balancer/resolver/endpoint/endpoint.go:182:31: undefined: resolver.ResolveNowOption
# github.com/coreos/etcd/clientv3/balancer/picker
../../../go/pkg/mod/github.com/coreos/etcd@v3.3.27+incompatible/clientv3/balancer/picker/err.go:37:44: undefined: balancer.PickOptions
../../../go/pkg/mod/github.com/coreos/etcd@v3.3.27+incompatible/clientv3/balancer/picker/roundrobin_balanced.go:55:54: undefined: balancer.PickOptions
# 修复
$ go get -u -x google.golang.org/grpc@v1.26.0
etcd clientV3 配置TLS 证书
clientv3.Config struct 的 TLS 字段 :TLS 持有客户端安全凭证(如果有)
构建tls
// 证书
tlsInfo := transport.TLSInfo{
TrustedCAFile: etcdCa,
CertFile: etcdCert,
KeyFile: etcdCertKey,
}
_tlsConfig, err := tlsInfo.ClientConfig()
if err != nil {
fmt.Printf("tlsconfig failed, err:%v\n", err)
}
证书对应终端操作etcd链接
TrustedCAFile: --cacert
CertFile: --cert
KeyFile: --key
go etcd client 简单操作
cfg := clientv3.Config{
Endpoints: endpoints,
DialTimeout: dialTimeout,
TLS: _tlsConfig,
}
cli, err := clientv3.New(cfg)
if err != nil {
fmt.Printf("connect to etcd failed, err:%v\n", err)
return
}
defer cli.Close()
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
putResponse, err := cli.Put(ctx, "zise", "feizhu")
cancel()
if err != nil {
fmt.Printf("put to etcd failed, err:%v\n", err)
return
}
fmt.Println(putResponse)
ctx, cancel = context.WithTimeout(context.Background(), time.Second)
getResponse, err := cli.Get(ctx, "zise")
cancel()
if err != nil {
fmt.Printf("get to etcd failed, err:%v\n", err)
return
}
fmt.Println(getResponse)
deleteResponse, err := cli.Delete(cli.Ctx(), "zise")
if err != nil {
fmt.Printf("delete to etcd failed, err:%v\n", err)
return
}
fmt.Println(deleteResponse)
go etcd clientV3 demo
目录结构
源码
package main
import (
"context"
"fmt"
"go.etcd.io/etcd/clientv3"
"go.etcd.io/etcd/pkg/transport"
"os"
"time"
)
func init() {
var err error
dir, err = os.Getwd()
if err != nil {
fmt.Printf("getwd dir err:%v\n", err)
return
}
etcdCert = dir + "/tls/etcd/server.crt"
etcdCertKey = dir + "/tls/etcd/server.key"
etcdCa = dir + "/tls/etcd/ca.crt"
}
var (
dir string
dialTimeout = 5 * time.Second
// requestTimeout = 4 * time.Second
endpoints = []string{"https://1.1.1.45:2379", "https://1.1.1.46:2379", "https://1.1.1.47:2379"}
etcdCert string
etcdCertKey string
etcdCa string
)
func main() {
// 证书
tlsInfo := transport.TLSInfo{
TrustedCAFile: etcdCa,
CertFile: etcdCert,
KeyFile: etcdCertKey,
}
_tlsConfig, err := tlsInfo.ClientConfig()
if err != nil {
fmt.Printf("tlsconfig failed, err:%v\n", err)
}
cfg := clientv3.Config{
Endpoints: endpoints,
DialTimeout: dialTimeout,
TLS: _tlsConfig,
}
cli, err := clientv3.New(cfg)
if err != nil {
fmt.Printf("connect to etcd failed, err:%v\n", err)
return
}
defer cli.Close()
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
putResponse, err := cli.Put(ctx, "zise", "feizhu")
cancel()
if err != nil {
fmt.Printf("put to etcd failed, err:%v\n", err)
return
}
fmt.Println(putResponse)
ctx, cancel = context.WithTimeout(context.Background(), time.Second)
getResponse, err := cli.Get(ctx, "zise")
cancel()
if err != nil {
fmt.Printf("get to etcd failed, err:%v\n", err)
return
}
fmt.Println(getResponse)
deleteResponse, err := cli.Delete(cli.Ctx(), "zise")
if err != nil {
fmt.Printf("delete to etcd failed, err:%v\n", err)
return
}
fmt.Println(deleteResponse)
}
测试
$ go run main.go
{cluster_id:16977795579664915518 member_id:15527552572173850242 revision:382574450 raft_term:8330 <nil> {} [] 0}
{cluster_id:16977795579664915518 member_id:5537999761451951234 revision:382574450 raft_term:8330 [key:"zise" create_revision:382574450 mod_revision:382574450 version:1 value:"feizhu" ] false 1 {} [] 0}
{cluster_id:16977795579664915518 member_id:8044152919570031487 revision:382574451 raft_term:8330 1 [] {} [] 0}
过手如登山,一步一重天