在微服务中服务发现是必不可少的,此时ETCD中间件就是一种可选项,其实ETCD除了服务发现功能,还有:
ETCD 采用 raft 算法,实现分布式数据的一致性和高可用,遵循CP原则,支持10 k/s 的并发读写操作。
安装的方法有很多:
安装模式有单点和集群两种;
直接yum下最简单了:
yum install etcd
如果服务器上已经装有docker了,可以docker安装:
docker pull bitnami/etcd:latest
docker network create app-tier --driver bridge
docker run -d --name etcd-server \
--network app-tier \
--publish 2379:2379 \
--publish 2380:2380 \
--env ALLOW_NONE_AUTHENTICATION=yes \
--env ETCD_ADVERTISE_CLIENT_URLS=http://etcd-server:2379 \
bitnami/etcd:latest
这里采用二进制的方式,首先到github下载自己所要的版本,本人选的v3.5.11。
ETCD集群部署有三种模式:
由于是测试使用,所以在一台Linux服务器上基于静态配置搭建了个三节点的集群。
配置项指标见官网
节点1:
mkdir -p /usr/local/etcd-v3.5.11/etcd1/data
vim /usr/local/etcd-v3.5.11/etcd1/conf.yml
name: etcd1
data-dir: /usr/local/etcd-v3.5.11/etcd1/data
initial-advertise-peer-urls: http://127.0.0.1:2380
listen-peer-urls: http://127.0.0.1:2380
listen-client-urls: http://172.20.101.222:2379,http://127.0.0.1:2379
advertise-client-urls: http://127.0.0.1:2379
initial-cluster-token: test-etcd-cluster
initial-cluster: etcd1=http://127.0.0.1:2380,etcd2=http://127.0.0.1:2370,etcd3=http://127.0.0.1:2360
initial-cluster-state: new
节点2:
mkdir -p /usr/local/etcd-v3.5.11/etcd2/data
vim /usr/local/etcd-v3.5.11/etcd2/conf.yml
name: etcd2
data-dir: /usr/local/etcd-v3.5.11/etcd2/data
initial-advertise-peer-urls: http://127.0.0.1:2370
listen-peer-urls: http://127.0.0.1:2370
listen-client-urls: http://172.20.101.222:2369,http://127.0.0.1:2369
advertise-client-urls: http://127.0.0.1:2369
initial-cluster-token: test-etcd-cluster
initial-cluster: etcd1=http://127.0.0.1:2380,etcd2=http://127.0.0.1:2370,etcd3=http://127.0.0.1:2360
initial-cluster-state: new
节点3:
mkdir -p /usr/local/etcd-v3.5.11/etcd3/data
vim /usr/local/etcd-v3.5.11/etcd3/conf.yml
name: etcd2
data-dir: /usr/local/etcd-v3.5.11/etcd3/data
initial-advertise-peer-urls: http://127.0.0.1:2360
listen-peer-urls: http://127.0.0.1:2360
listen-client-urls: http://172.20.101.222:2359,http://127.0.0.1:2359
advertise-client-urls: http://127.0.0.1:2359
initial-cluster-token: test-etcd-cluster
initial-cluster: etcd1=http://127.0.0.1:2380,etcd2=http://127.0.0.1:2370,etcd3=http://127.0.0.1:2360
initial-cluster-state: new
启动:
/usr/local/etcd-v3.5.11/etcd --config-file /usr/local/etcd-v3.5.11/etcd1/conf.yml
/usr/local/etcd-v3.5.11/etcd --config-file /usr/local/etcd-v3.5.11/etcd2/conf.yml
/usr/local/etcd-v3.5.11/etcd --config-file /usr/local/etcd-v3.5.11/etcd2/conf.yml
默认的etcdctrl使用的是v2版本的API,我们需要设置环境变量来使用v3版本的API:
//window
set ETCDCTL_API=3
//linux
export ETCDCTL_API=3
验证:
[root@test etcd-v3.5.11]# /usr/local/etcd-v3.5.11/etcdctl put /test/hello helloworld
OK
[root@test etcd-v3.5.11]# /usr/local/etcd-v3.5.11/ get /test/hello
/test/hello
helloworld
至此就可以用于测试使用了,当然还支持配置校验,证书等功能。