go-micro之etcd集群
一:下载etcd安装包
etcd-v3.4.7-windows-amd64
二:在etcd的压缩文件内创建3个启动配置文件
配置内容如下
.\etcd.exe --name etcd01 ^ --data-dir .\data\etcd01 ^ --advertise-client-urls http://127.0.0.1:2379 ^ --listen-client-urls http://127.0.0.1:2379 ^ --listen-peer-urls http://127.0.0.1:2380 ^ --initial-advertise-peer-urls http://127.0.0.1:2380 ^ --initial-cluster-token etcd-cluster-1 ^ --initial-cluster etcd01=http://127.0.0.1:2380,etcd02=http://127.0.0.1:2381,etcd03=http://127.0.0.1:2382 ^ --initial-cluster-state new pause
.\etcd.exe --name etcd02 ^ --data-dir .\data\etcd02 ^ --advertise-client-urls http://127.0.0.1:3379 ^ --listen-client-urls http://127.0.0.1:3379 ^ --listen-peer-urls http://127.0.0.1:2381 ^ --initial-advertise-peer-urls http://127.0.0.1:2381 ^ --initial-cluster-token etcd-cluster-1 ^ --initial-cluster etcd01=http://127.0.0.1:2380,etcd02=http://127.0.0.1:2381,etcd03=http://127.0.0.1:2382 ^ --initial-cluster-state new pause
三:查看节点状态
etcdctl.exe --write-out=table --endpoints=http://127.0.0.1:2379,http://127.0.0.1:3379,http://127.0.0.1:4379 endpoint status
+-----------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
| ENDPOINT | ID | VERSION | DB SIZE | IS LEADER | IS LEARNER | RAFT TERM | RAFT INDEX | RAFT APPLIED INDEX | ERRORS |
+-----------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
| http://127.0.0.1:2379 | bf9071f4639c75cc | 3.4.4 | 115 kB | true | false | 13 | 99 | 99 | |
| http://127.0.0.1:3379 | e7b968b9fb1bc003 | 3.4.4 | 115 kB | false | false | 13 | 99 | 99 | |
| http://127.0.0.1:4379 | 19ac17627e3e396f | 3.4.4 | 106 kB | false | false | 13 | 99 | 99 | |
+-----------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
etcd启动
线上etcd和本地启动有讲究了,如果etcd是单独的服务器,那么在不加任何参数的情况下直接启动,那基本是调不通的。
$ ./service --registry=etcd --registry_address=xx.xx.xx.xx:2379
2020-03-17 17:04:42 Starting [service] go.micro.srv.user
2020-03-17 17:04:42 Server [grpc] Listening on [::]:48493
2020-03-17 17:04:42 Registry [etcd] Registering node: go.micro.srv.user-f32a2950-8e59-44d4-ac86-f4e1ec103395
{"level":"warn","ts":"2020-03-17T17:04:47.849+0800","caller":"clientv3/retry_interceptor.go:61","msg":"retrying of unary invoker failed","target":"endpoint://client-e45decee-12bf-4a9b-a7ab-f92eece39420/xx.xx.xx.xx:2379","attempt":0,"error":"rpc error: code = DeadlineExceeded desc = latest connection error: connection error: desc = \"transport: Error while dialing dial tcp xx.xx.xx.xx:2379: connect: connection refused\""}
2020-03-17 17:04:47 Server register error: %!(EXTRA context.deadlineExceededError=context deadline exceeded)
这就是错误示例,etcd拒绝连接。 为了能顺利看到胜利的结果,需要这样启动etcd:
$ ./etcd --listen-client-urls http://0.0.0.0:2379 --advertise-client-urls http://0.0.0.0:2379 --listen-peer-urls http://0.0.0.0:2380 --initial-advertise-peer-urls http://0.0.0.0:2380 --initial-cluster my-etcd-1=http://0.0.0.0:2380
将ip为0.0.0.0可以理解为不限制连接机器(真正的生产不推荐这样设置)。 服务启动参数--registry_address=xx.xx.xx.xx:2379
不能带http://
。
详细go-micro使用etcd服务注册发现链接:https://github.com/yuedun/micro-service/blob/master/README.md
Take a small step every day