{"id":37205583,"url":"https://github.com/farseer-go/etcd","last_synced_at":"2026-01-14T23:41:06.437Z","repository":{"id":65389539,"uuid":"590422978","full_name":"farseer-go/etcd","owner":"farseer-go","description":"etcd client","archived":false,"fork":false,"pushed_at":"2025-07-25T06:26:46.000Z","size":47,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-07-25T12:03:05.683Z","etag":null,"topics":["etcd","etcd-client","etcd3","etcdv3"],"latest_commit_sha":null,"homepage":"https://farseer-go.gitee.io/","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/farseer-go.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2023-01-18T11:36:16.000Z","updated_at":"2025-07-25T06:26:49.000Z","dependencies_parsed_at":"2024-01-07T12:32:02.474Z","dependency_job_id":"4d3fc37c-00f5-42f9-adf5-dfea18f39c83","html_url":"https://github.com/farseer-go/etcd","commit_stats":null,"previous_names":[],"tags_count":22,"template":false,"template_full_name":null,"purl":"pkg:github/farseer-go/etcd","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/farseer-go%2Fetcd","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/farseer-go%2Fetcd/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/farseer-go%2Fetcd/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/farseer-go%2Fetcd/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/farseer-go","download_url":"https://codeload.github.com/farseer-go/etcd/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/farseer-go%2Fetcd/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28439491,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-14T22:37:52.437Z","status":"ssl_error","status_checked_at":"2026-01-14T22:37:31.496Z","response_time":107,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["etcd","etcd-client","etcd3","etcdv3"],"created_at":"2026-01-14T23:41:05.953Z","updated_at":"2026-01-14T23:41:06.427Z","avatar_url":"https://github.com/farseer-go.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# etcd client\n\u003e 包：`\"github.com/farseer-go/etcd\"`\n\u003e\n\u003e 模块：`etcd.Module`\n\n![](https://img.shields.io/github/stars/farseer-go?style=social)\n![](https://img.shields.io/github/license/farseer-go/etcd)\n![](https://img.shields.io/github/go-mod/go-version/farseer-go/etcd)\n![](https://img.shields.io/github/v/release/farseer-go/etcd)\n![go-version](https://img.shields.io/github/go-mod/go-version/farseer-go/etcd)\n![](https://img.shields.io/github/languages/code-size/farseer-go/etcd)\n[![Build](https://github.com/farseer-go/etcd/actions/workflows/build.yml/badge.svg)](https://github.com/farseer-go/etcd/actions/workflows/build.yml)\n![](https://goreportcard.com/badge/github.com/farseer-go/etcd)\n\n## 概述\netcd是比较流行的分布式组件之一，另外还有Zookeeper、Eureka、Nacos、Consul\n\n常用于在分布式平台中的服务注册与发现的场景。\n\n它有部署简单、使用方便、轻量等特性\n\n在GO阵营里面，一般会优先选择etcd，因为它是采用GO语言编写的。\n\n`farseer-go/etcd`可以让应用系统更加优雅的使用etcd：\n\n- `配置自动化`：通过配置，可以很方便的快速接入ETCD服务\n- `容器化操作`：使用IOC，我们可以很方便就能取到client。\n- `优雅的使用`：将常用的操作，整合到几个方法中，更加简单使用。\n\n## 配置\n_./farseer.yaml_\n```yaml\nEtcd:\n  default1: \"Server=127.0.0.1:2379|127.0.0.1:2379,DialTimeout=5000,Username=test,Password=test\"\n  default2: \"Server=127.0.0.1:2379|127.0.0.1:2379,DialTimeout=5000,Username=test,Password=test\"\n```\n配置文件支持同时配置多个不同服务端、不同的交换器设置。default1、default2是自定义的名称，**同时也是IOC的别名**\n\n配置说明：\n```go\ntype etcdConfig struct {\n\tServer               string // 服务端地址\n\tDialTimeout          int    // 连接超时时间（ms)\n\tDialKeepAliveTime    int    // 对服务器进行ping的时间（ms)\n\tDialKeepAliveTimeout int    // 客户端等待响应的超时时间（ms)\n\tMaxCallSendMsgSize   int    // 客户端的请求发送限制，单位是字节。0，则默认为2.0 MiB（2 * 1024 * 1024）。\n\tMaxCallRecvMsgSize   int    // 客户端的响应接收限制，单位是字节。0，则默认不限制\n\tUsername             string // 用户名\n\tPassword             string // 密码\n\tRejectOldCluster     bool   // 拒绝过时的集群创建客户端。\n\tPermitWithoutStream  bool   // 允许客户端在没有任何活动流（RPC）的情况下向服务器发送keepalive pings。\n}\n```\n配置的属性之间用`,`隔开组合成一个字符串，将被解析成etcdConfig对象。\n\n## Put\n保存KV\n```go\nclient := container.Resolve[etcd.IClient](\"default1\") // 取出default1的配置服务端\nputRsp, err := client.Put(\"/test/a1\", \"1\")\n```\n通过`container.Resolve`容器取出`etcd.IClient`接口的实现。\n\n参数值`default1`，是在`./farseer.yaml`中配置节点，意味着使用default1的配置服务端\n\n## Get\n可以支持按KEY完整匹配，或者按KEY的前缀匹配。\n```go\n// 根据KEY完整切尔西\nclient := container.Resolve[etcd.IClient](\"default1\")\nresult, err := client.Get(\"/test/a1\")\nflog.Info(result.Value) // print:1\n\n// 根据KEY前缀匹配\nresults, err := client.GetPrefixKey(\"/test\")\nflog.Info(results[\"/test/a1\"].Value)    // print:1\n```\n\n## Exists\n判断KEY是否存在\n```go\nclient := container.Resolve[etcd.IClient](\"default1\")\nclient.Exists(\"/test/a1\")\n```\n\n## Delete\n删除KEY\n```go\nclient := container.Resolve[etcd.IClient](\"default1\")\n_, _ = client.Delete(\"/test/a1\")\n```\n\n## Watch\n监控指定的KEY（即使KEY还没有创建也可以先监控起来）\n\n当这个KEY有任何的变化时，我们都可以拿到最新的数据状态\n```go\nclient := container.Resolve[etcd.IClient](\"default1\")\nctx, cancelFunc := context.WithCancel(context.Background())\n// 指定KEY\nclient.Watch(ctx, \"/test/a1\", func(event etcd.WatchEvent) {\n    flog.Info(event.Kv.Value)   // value\n    flog.Info(event.IsModify())\n    flog.Info(event.IsCreate())\n})\n\n// 指定KEY前缀\nclient.WatchPrefixKey(ctx, \"/test/\", func(event etcd.WatchEvent) {\n    flog.Info(event.Kv.Value)   // value\n    flog.Info(event.IsModify())\n    flog.Info(event.IsCreate())\n})\n```\n\n## Lock\n分布式锁\n```go\nclient := container.Resolve[etcd.IClient](\"default1\")\n\nunLock1, _ := client.Lock(\"/lock/1\", 3)\nflog.Info(\"上锁：unLock1\")\n\ngo func() {\n    time.Sleep(1000 * time.Millisecond)\n    flog.Info(\"解锁：unLock1\")\n    unLock1()\n}()\n\nunLock2, _ := client.Lock(\"/lock/1\", 3)\nflog.Info(\"上锁：unLock2\")\n\nunLock2()\nflog.Info(\"解锁：unLock2\")\n```\n打印结果：\n```\n2023-01-22 01:44:33 [Info] 上锁：unLock1\n2023-01-22 01:44:34 [Info] 解锁：unLock1\n2023-01-22 01:44:34 [Info] 上锁：unLock2\n2023-01-22 01:44:34 [Info] 解锁：unLock2\n```\n\n\u003e 同一时刻同一个KEY，只能有一个客户端能上锁成功。使用完后，需调用unLock()解锁\n\n## 租约\n```go\nclient := container.Resolve[etcd.IClient](\"default1\")\n\n// 先创建一个保持10秒的租约，拿到租约ID\nleaseID, _ := client.LeaseGrant(10)\n\n// 在给KV保存的时候，带上这个leaseID\n_, _ = client.PutLease(\"/test/lease1\", \"1\", leaseID)\n```\n\u003e 此后这个租约将在11秒后过期。（TTL + 1 秒）\n\n`自动持续续约：`\n```go\nctx, cancelFunc := context.WithCancel(context.Background())\n_ = client.LeaseKeepAlive(ctx, leaseID)\n```\n此时，KEY:`/test/lease1`，将会自动续约，不再需要我们手动去执行续约操作。\n\n`只续约一次：`\n```go\n_ = client.LeaseKeepAliveOnce(leaseID)\n```\n此时，只会续约一次，每次续约为10秒（创建租约时传了10）\n\n## 使用原生客户端\n有时候我们需要原生的client执行更多操作时，可以使用`Original`方法\n```go\nclient := container.Resolve[etcd.IClient](\"default\")\nclient.Original()\n```\n将返回：`etcdV3.Client`对象\n\n## 最后\n以上是常用的方法。完整的方法列表，可以查看：`etcd.IClient`接口","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffarseer-go%2Fetcd","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffarseer-go%2Fetcd","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffarseer-go%2Fetcd/lists"}