{"id":22885034,"url":"https://github.com/mark8s/etcd","last_synced_at":"2025-07-23T11:04:57.748Z","repository":{"id":165567594,"uuid":"640925402","full_name":"mark8s/etcd","owner":"mark8s","description":null,"archived":false,"fork":false,"pushed_at":"2023-05-16T02:35:04.000Z","size":32,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-31T17:59:42.527Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mark8s.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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}},"created_at":"2023-05-15T12:14:43.000Z","updated_at":"2023-05-16T02:35:07.000Z","dependencies_parsed_at":null,"dependency_job_id":"40da321d-5178-44c7-b000-3cd15618e90b","html_url":"https://github.com/mark8s/etcd","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/mark8s/etcd","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mark8s%2Fetcd","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mark8s%2Fetcd/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mark8s%2Fetcd/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mark8s%2Fetcd/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mark8s","download_url":"https://codeload.github.com/mark8s/etcd/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mark8s%2Fetcd/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266665781,"owners_count":23964972,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-23T02:00:09.312Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"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":[],"created_at":"2024-12-13T19:31:08.216Z","updated_at":"2025-07-23T11:04:57.721Z","avatar_url":"https://github.com/mark8s.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# etcd\n\n[etcd](https://github.com/etcd-io/etcd) is a distributed reliable key-value store for the most critical data of a distributed system, with a focus on being:\n\n- Simple: well-defined, user-facing API (gRPC)\n- Secure: automatic TLS with optional client cert authentication\n- Fast: benchmarked 10,000 writes/sec\n- Reliable: properly distributed using Raft\n\n## 安装\n\n地址： https://github.com/etcd-io/etcd/releases/\n\n启动etcd：\n\n```shell\nraft2023/05/15 20:13:53 INFO: 8e9e05c52164694d is starting a new election at term 1\nraft2023/05/15 20:13:53 INFO: 8e9e05c52164694d became candidate at term 2\nraft2023/05/15 20:13:53 INFO: 8e9e05c52164694d received MsgVoteResp from 8e9e05c52164694d at term 2\nraft2023/05/15 20:13:53 INFO: 8e9e05c52164694d became leader at term 2\nraft2023/05/15 20:13:53 INFO: raft.node: 8e9e05c52164694d elected leader 8e9e05c52164694d at term 2\n2023-05-15 20:13:53.916380 I | etcdserver: setting up the initial cluster version to 3.4\n2023-05-15 20:13:53.916380 I | etcdserver: published {Name:default ClientURLs:[http://localhost:2379]} to cluster cdf818194e3a8c32\n2023-05-15 20:13:53.916380 I | embed: ready to serve client requests\n2023-05-15 20:13:53.916380 N | etcdserver/membership: set the initial cluster version to 3.4\n2023-05-15 20:13:53.917379 N | embed: serving insecure client requests on 127.0.0.1:2379, this is strongly discouraged!\n2023-05-15 20:13:53.917379 I | etcdserver/api: enabled capabilities for version 3.4\n```\n\netcdctl版本：\n\n```shell\n$ etcdctl version\netcdctl version: 3.4.26\nAPI version: 3.4\n```\n\n## API\n\n### 写入key\n\n```shell\n$ etcdctl put mark \"hello mark\"\nOK\n```\n\n### 读取key\n\n```shell\n$ etcdctl get mark\nmark\nhello mark\n```\n\n### 删除key\n\n```shell\n$ etcdctl del mark\n1\n\n$ etcdctl get mark\n\n```\n\n### 监听key\n\n```shell\n$ etcdctl watch mark\nPUT\nmark      \nhello mark\nPUT\nmark             \nhello hello mark \nPUT\nmark\nhello hello hello mark\n```\n\n另一个终端中执行：\n\n```shell\nmark@LAPTOP-VH57ARI1 MINGW64 ~\n$ etcdctl put mark \"hello mark\"\nOK\n\nmark@LAPTOP-VH57ARI1 MINGW64 ~\n$ etcdctl put mark \"hello hello mark \"\nOK\n\nmark@LAPTOP-VH57ARI1 MINGW64 ~\n$ etcdctl put mark \"hello hello hello mark \"\nOK\n```\n\n### 设置租约（Grant leases）\n\n当一个key被绑定到一个租约上时，它的生命周期与租约的生命周期绑定。\n\n值得注意的地方，一个租约可以绑定多个key，当租约过期后，所有key值会被删除。\n\n当一个租约只绑定了一个key时，想删除这个key，最好的办法是撤销它的租约，而不是直接删除这个key。\n\n创建一个租约：\n```shell\n$ etcdctl lease grant -h\n\n$ etcdctl lease grant 60s\nError: bad TTL (strconv.ParseInt: parsing \"60s\": invalid syntax)\n\n# 设置一个60s的租约\n$ etcdctl lease grant 60\nlease 694d881f54d5810c granted with TTL(60s)\n\n# 将租约与 mark这个key绑定\n$ etcdctl put --lease=694d881f54d5810c mark mark\nOK\n\n$ etcdctl get mark\nmark\nmark\n\n$ etcdctl get mark\nmark\nmark\n\n\n# 60s后，获取不到mark了\n$ etcdctl get mark\n\n$ etcdctl get mark\n```\n\n### 主动撤销租约（Revoke leases）\n撤销租约将删除其所有绑定的key\n\n```shell\n$ etcdctl lease grant 60\nlease 694d881f54d5811f granted with TTL(60s)\n\n$ etcdctl put mark mark --lease 694d881f54d5811f\nOK\n\n$ etcdctl lease revoke 694d881f54d5811f\nlease 694d881f54d5811f revoked\n\n$ etcdctl get mark\n\n```\n\n### 续租约（Keep leases alive）\n\n通过刷新其TTL来保持租约的有效，使其不会过期。\n\n```shell\n$ etcdctl lease grant 60\nlease 694d881f54d58124 granted with TTL(60s)\n\n$ etcdctl put mark mark --lease 694d881f54d58124\nOK\n\n#续租约，自动定时执行续租约，续约成功后每次租约为60秒\n$ etcdctl lease keep-alive 694d881f54d58124\nlease 694d881f54d58124 keepalived with TTL(60)\nlease 694d881f54d58124 keepalived with TTL(60)\nlease 694d881f54d58124 keepalived with TTL(60)\n```\n\n### 获取租约信息（Get lease information）\n\n获取租约信息，以便续租或查看租约是否仍然存在或已过期\n\n```shell\n# --keys 指定为 leaseId\n$ etcdctl lease timetolive --keys 694d881f54d58124\nlease 694d881f54d58124 granted with TTL(60s), remaining(58s), attached keys([mark])\n\n$ etcdctl lease timetolive --keys 694d881f54d58124\nlease 694d881f54d58124 granted with TTL(60s), remaining(55s), attached keys([mark])\n\n$ etcdctl lease timetolive --keys 694d881f54d58124\nlease 694d881f54d58124 granted with TTL(60s), remaining(53s), attached keys([mark])\n\n$ etcdctl lease timetolive --keys 694d881f54d58124\nlease 694d881f54d58124 granted with TTL(60s), remaining(51s), attached keys([mark])\n\n$ etcdctl lease timetolive --keys 694d881f54d58124\nlease 694d881f54d58124 granted with TTL(60s), remaining(50s), attached keys([mark])\n```\n\n## 应用场景\n\n- 服务发现\n- 配置中心\n- 负载均衡\n- 分布式锁\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmark8s%2Fetcd","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmark8s%2Fetcd","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmark8s%2Fetcd/lists"}