go-go-micro服务发现etcd
etcd 是一个分布式一致性k-v存储系统,可用于服务注册发现与共享配置,具有以下优点:
简单: 基于HTTP+JSON的API让你可以用CURL命令就可以轻松使用。安全: 可以选择SSL客户认证机制。快速: 每个实例每秒支持一千次写操作。可信: 使用 Ralf 算法充分实现了分布式。一般服务启动时需要加载一些配置信息,如数据库访问地址,连接配置,这些配置信息每个服务都差不多,如果通过读取配置文件进行配置会存在要写多份配置文件,且每次更改时这些配置文件都要更改,且更改配置后,需要重启服务后才能生效,这些无疑让配置极不灵活,如果将配置信息放入到 etcd 中,程序启动时进行加载并运行,同时监听配置文件的更改,当配置文件发生更改时,自动将旧值替换新值,这样无疑简化程序配置,更方便于服务部署。
双击 etcd.exe 运行
测试一下
D:\etcd-v3.4.1-windows-amd64 λ etcdctl put aaa 123 OK D:\etcd-v3.4.1-windows-amd64 λ etcdctl get aaa aaa 123etcd 命令
etcdctl的使用[v3版本] - https://blog.csdn.net/huwh_/article/details/80225902etcd 命令行 - https://www.cnblogs.com/breg/p/5756558.html默认是使用v2的api,如果想使用v3 api每次打开窗口都需要敲一行命令
三个 etcd 的服务地址分别是:
127.0.0.1:2379 127.0.0.1:12380 // infra1 127.0.0.1:22379 127.0.0.1:22380 // infra2 127.0.0.3:32379 127.0.0.2:32380 // infra2启动三个 etcd 服务. go 客户端连端口是 client, etcd 服务之间的通信的端口是 peer
// infra1 $ 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 // infra2 $ 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 // infra3 $ 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查看集群
$ etcdctl member list 8211f1d0f64f3269, started, infra1, http://127.0.0.1:12380, http://127.0.0.1:2379, false 91bc3c398fb3c146, started, infra2, http://127.0.0.1:22380, http://127.0.0.1:22379, false fd422379fda50e48, started, infra3, http://127.0.0.1:32380, http://127.0.0.1:32379, falsego 客户端测试.
运行 监听. test_etcdv3_03_watch.go.
运行 put get. test_etcdv3_03_put-get.go.
不同ip机子的启动示例. 参考: 浅入深出ETCD之【集群部署与golang客户端使用】 - https://www.jianshu.com/p/600f14e9e443
// 192.168.4.224 $ ./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
// 192.168.4.225 $ ./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
// 192.168.4.226 $ ./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 ```测试代码 test_etcdv3.go
package main import ( "github.com/micro/go-micro" "github.com/micro/go-plugins/registry/etcdv3" "log" "time" ) func main() { registre := etcdv3.NewRegistry() service := micro.NewService( micro.Registry(registre), micro.Name("greeter"), micro.RegisterTTL(time.Second*30), micro.RegisterInterval(time.Second*15), ) service.Init() if err := service.Run(); err != nil { log.Fatal(err) } }修改 etcdv3 仓库引用. 参考: https://zhuanlan.zhihu.com/p/79897640. 不然会报错: etcd auth.go too many errors
需要修改 GoWinEnv\src\github.com\micro\go-plugins\registry\etcdv3 下的几个文件. 修改仓库引用 github.com/coreos/etcd/clientv3 为 go.etcd.io/etcd/clientv3
watcher.go
// "github.com/coreos/etcd/clientv3" // 去掉 "go.etcd.io/etcd/clientv3" // 新加etcdv3.go
// "github.com/coreos/etcd/clientv3" // 去掉 "go.etcd.io/etcd/clientv3" // 新加 // "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes" // 去掉 "go.etcd.io/etcd/etcdserver/api/v3rpc/rpctypes" // 新加删掉 GoWinEnv\src\go.etcd.io\etcd\vendor 路径下的 golang.org 目录. 不然运行时会报错: You may have two independent copies of golang.org/x/net/trace in your binary
运行测试. go run
> go run e:\ws_go\GoWinEnv\src\GoMicro\test_etcdv3\test_etcdv3.go < 2019-10-02 20:11:26.000556 I | Transport [http] Listening on [::]:4759 2019-10-02 20:11:26.001528 I | Broker [http] Connected to [::]:4760 2019-10-02 20:11:26.018482 I | Registry [etcdv3] Registering node: greeter-e4715ce2-9522-499e-9b12-07f100e56786etcd auth.go too many errors
参考: https://zhuanlan.zhihu.com/p/79897640
修改 etcdv3 仓库引用
报错: You may have two independent copies of golang.org/x/net/trace in your binary
参考:
https://blog.csdn.net/wo252618378/article/details/93677059https://segmentfault.com/a/1190000014501241启动 etcd 报错: Only one usage of each socket address (protocol/network address/port) is normally permitted
可能使用的本机地址是 localhost, 换成 127.0.0.1 试试