介绍

谈使用场景之前,看看他有哪些功能

官方定义是这样的: 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.

如何学习一门新技术,十年 MarkDown 程序员怎么做_后端

使用场景

  • 服务发现
  • 配置中心
  • 分布式锁

安装&搭建

搭建 ETCD

环境: mac

# 安装
brew install etcd

# 启动
brew services start etcd

# 检查状态
brew services list | grep etcd

# 停止
brew services stop etcd

# 卸载
brew uninstall etcd
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

与 ETCD 交互

# 检查工具是否安装
etcdctl version

# 写入
etcdctl put mykey "myvalue"

# 获取
etcdctl get mykey

# 删除
etcdctl del mykey
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

集群

# 查看集群状态
etcdctl cluster-health
  • 1.
  • 2.

Go+ETCD 编码

安装依赖

go get go.etcd.io/etcd/client/v3
  • 1.

编码

github.com/Rodert/go-d…

执行

go run main.go
  • 1.

执行结果:

如何学习一门新技术,十年 MarkDown 程序员怎么做_GitHub_02