- 介绍
- 介绍 - 资料收集整理
- 安装 - Linux单机部署
- 官方文档(翻译)
- 官方文档(翻译) - 开发指南
- 开发指南 - 搭建本地集群
- 开发指南 - 和etcd交互
- 开发指南 - API 参考文档
- 开发指南 - API 并发参考文档
- 开发指南 - gRPC 网关
- 开发指南 - gRPC命名与发现
- 开发指南 - 试验性的API和特性
- 开发指南 - 系统限制
- 官方文档(翻译) - 操作etcd集群
- 操作etcd集群 - 搭建etcd集群
- 搭建etcd集群 - 运行时重配置
- 搭建etcd集群 - 运行时重配置的设计
- 操作etcd集群 - 搭建etcd网关
- 操作etcd集群 - 在容器内运行etcd集群
- 操作etcd集群 - 配置
- 操作etcd集群 - 加密(TODO)
- 操作etcd集群 - 维护
- 操作etcd集群 - 理解失败
- 操作etcd集群 - 灾难恢复
- 操作etcd集群 - 性能
- 操作etcd集群 - 版本
- 操作etcd集群 - 支持平台
- 官方文档(翻译) - 学习
- 学习 - 理解数据模型
- 学习 - 理解API
- 学习 - 术语
- 学习 - API保证
- API参考文档(翻译)
- API参考文档(翻译) - KV service
- KV service - Range方法
- KV service - Put方法
- KV service - DeleteRange方法
- KV service - Txn方法
- KV service - Compact方法
- API参考文档(翻译) - Watch service
- Watch service - Watch方法
- API参考文档(翻译) - Lease service
- Lease service - LeaseGrant方法
- Lease service - LeaseRevoke方法
- Lease service - LeaseKeepAlive方法
开发指南 - gRPC命名与发现
etcd provides a gRPC resolver to support an alternative name system that fetches endpoints from etcd for discovering gRPC services. The underlying mechanism is based on watching updates to keys prefixed with the service name.
Using etcd discovery with go-grpc
The etcd client provides a gRPC resolver for resolving gRPC endpoints with an etcd backend. The resolver is initialized with an etcd client and given a target for resolution:
import (
"github.com/coreos/etcd/clientv3"
etcdnaming "github.com/coreos/etcd/clientv3/naming"
"google.golang.org/grpc"
)
...
cli, cerr := clientv3.NewFromURL("http://localhost:2379")
r := &etcdnaming.GRPCResolver{Client: cli}
b := grpc.RoundRobin(r)
conn, gerr := grpc.Dial("my-service", grpc.WithBalancer(b))
Managing service endpoints
The etcd resolver treats all keys under the prefix of the resolution target following a “/“ (e.g., “my-service/“) with JSON-encoded go-grpc naming.Update
values as potential service endpoints. Endpoints are added to the service by creating new keys and removed from the service by deleting keys.
Adding an endpoint
New endpoints can be added to the service through etcdctl
:
ETCDCTL_API=3 etcdctl put my-service/1.2.3.4 '{"Addr":"1.2.3.4","Metadata":"..."}'
The etcd client’s GRPCResolver.Update
method can also register new endpoints with a key matching the Addr
:
r.Update(context.TODO(), "my-service", naming.Update{Op: naming.Add, Addr: "1.2.3.4", Metadata: "..."})
Deleting an endpoint
Hosts can be deleted from the service through etcdctl
:
ETCDCTL_API=3 etcdctl del my-service/1.2.3.4
The etcd client’s GRPCResolver.Update
method also supports deleting endpoints:
r.Update(context.TODO(), "my-service", naming.Update{Op: naming.Delete, Addr: "1.2.3.4"})
Registering an endpoint with a lease
Registering an endpoint with a lease ensures that if the host can’t maintain a keepalive heartbeat (e.g., its machine fails), it will be removed from the service:
lease=`ETCDCTL_API=3 etcdctl lease grant 5 | cut -f2 -d' '`
ETCDCTL_API=3 etcdctl put --lease=$lease my-service/1.2.3.4 '{"Addr":"1.2.3.4","Metadata":"..."}'
ETCDCTL_API=3 etcdctl lease keep-alive $lease
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论