- 案例源码仓库地址: https://github.com/Rodert/go-demo
- 官方文档: https://etcd.io/
- 视频教程: https://space.bilibili.com/404747369
文章目录
- 介绍
- 使用场景
- 安装&搭建
- 搭建 ETCD
- 与 ETCD 交互
- 集群
- Go+ETCD 编码
介绍
谈使用场景之前,看看他有哪些功能
官方定义是这样的:
etcd is a strongly consistent, distributed key-value store that provides a reliable way to store data that needs to be accessed by a distributed system or cluster of machines. It gracefully handles leader elections during network partitions and can tolerate machine failure, even in the leader node.
使用场景
-
服务发现
-
配置中心
-
分布式锁
安装&搭建
搭建 ETCD
环境: mac
# 安装
brew install etcd
# 启动
brew services start etcd
# 检查状态
brew services list | grep etcd
# 停止
brew services stop etcd
# 卸载
brew uninstall etcd
与 ETCD 交互
# 检查工具是否安装
etcdctl version
# 写入
etcdctl put mykey "myvalue"
# 获取
etcdctl get mykey
# 删除
etcdctl del mykey
集群
# 查看集群状态
etcdctl cluster-health
Go+ETCD 编码
安装依赖
go get go.etcd.io/etcd/client/v3
编码
https://github.com/Rodert/go-demo
执行
go run main.go
执行结果: