编程语言
首页 > 编程语言> > etcd——源码编译、学习

etcd——源码编译、学习

作者:互联网

借一张图

 

1、下载

https://github.com/etcd-io/etcd

git clone https://github.com/nats-io/nats-server.git

 

2、编译

进入etcd目录,

 

mac/linux下,   make clean  &&   make build

在etcd/bin目录下,生成 etcd  etcdctl   etcdutl 三个可执行文件

 

在win10下,不用make的前提下,

go build -o bin\etcd.exe server\main.go

go build -o bin\etcdctl.exe etcdctl\main.go

 

出现\etcd\client\v3 目录下诸多文件报错,这些文件都用的目录引用,

把真实目录 \etcd\tests\integration\clientv3\examples下对应的文件直接替换 D:\git\go\etcd\etcd\client\v3 目录下的,

重新编译即可。

 

3、单节点启动

命令行下: 

etcd -data-dir ~/data.etcd -advertise-client-urls  http://0.0.0.0:2379 -listen-client-urls http://0.0.0.0:2379

在 goland ide 下,加启动参数

--data-dir bin\in-ide.etcd --advertise-client-urls http://0.0.0.0:2379 --listen-client-urls http://0.0.0.0:2379  

 

4、测试

etcdctl member list
etcdctl --endpoints=10.60.80.11:2379 member list

etcdctl --endpoints=10.60.80.11:2379 put foo hihi
etcdctl --endpoints=10.60.80.11:2379 get foo

 

5、win10下集群部署与测试

1、win 10 环境
下载  https://github.com/etcd-io/etcd/releases
解压 etcd-v3.4.20-windows-amd64.zip

2、启动命令见
https://blog.51cto.com/u_10125763/3700481

3、依次启动 start1.bat   start2.bat  start3.bat

4、查看集群状态
etcdctl --endpoints=[192.168.1.32:2379,192.168.1.32:3379,192.168.1.32:4379]   member list

etcdctl --endpoints=[192.168.1.32:2379,192.168.1.32:3379,192.168.1.32:4379]   endpoint health --cluster=true

etcdctl --endpoints=10.60.80.11:2379 member list

5、查看服务发现
etcdctl --endpoints=[192.168.1.32:2379,192.168.1.32:3379,192.168.1.32:4379]   get /game/sanguo-test/server_state/gateway/1660642593870881200
/game/sanguo-test/server_state/gateway/1660642593870881200

start1.bat

.\etcd.exe --name etcd01 ^
--data-dir .\data\etcd01 ^
--advertise-client-urls http://192.168.1.32:2379 ^
--listen-client-urls http://192.168.1.32:2379 ^
--listen-peer-urls http://192.168.1.32:2380 ^
--initial-advertise-peer-urls http://192.168.1.32:2380 ^
--initial-cluster-token etcd-cluster-1 ^
--initial-cluster etcd01=http://192.168.1.32:2380,etcd02=http://192.168.1.32:2381,etcd03=http://192.168.1.32:2382 ^
--initial-cluster-state new

pause

start2.bat

.\etcd.exe --name etcd02 ^
--data-dir .\data\etcd02 ^
--advertise-client-urls http://192.168.1.32:3379 ^
--listen-client-urls http://192.168.1.32:3379 ^
--listen-peer-urls http://192.168.1.32:2381 ^
--initial-advertise-peer-urls http://192.168.1.32:2381 ^
--initial-cluster-token etcd-cluster-1 ^
--initial-cluster etcd01=http://192.168.1.32:2380,etcd02=http://192.168.1.32:2381,etcd03=http://192.168.1.32:2382 ^
--initial-cluster-state new

pause

start3.bat

.\etcd.exe --name etcd03 ^
--data-dir .\data\etcd03 ^
--advertise-client-urls http://192.168.1.32:4379 ^
--listen-client-urls http://192.168.1.32:4379 ^
--listen-peer-urls http://192.168.1.32:2382 ^
--initial-advertise-peer-urls http://192.168.1.32:2382 ^
--initial-cluster-token etcd-cluster-1 ^
--initial-cluster etcd01=http://192.168.1.32:2380,etcd02=http://192.168.1.32:2381,etcd03=http://192.168.1.32:2382 ^
--initial-cluster-state new

pause

 

6、mac/linux下集群部署

创建执行文件

touch etcd-static.sh

在每个etcd节点上,指定集群成员。

# 指定集群token
# 如果你所在的网络环境配置了多个etcd集群,为了避免意外发生,最好使用-initial-cluster-token参数为每个集群单独配置一个token认证。
# 这样就可以确保每个集群和集群的成员都拥有独特的ID。 TOKEN=token-01 CLUSTER_STATE=new NAME_1=machine-1 NAME_2=machine-2 NAME_3=machine-3 HOST_1=192.168.199.140 HOST_2=192.168.199.141 HOST_3=192.168.199.142 CLUSTER=${NAME_1}=http://${HOST_1}:2380,${NAME_2}=http://${HOST_2}:2380,${NAME_3}=http://${HOST_3}:2380

每个节点添加一下各节点对应启动命令行

THIS_NAME=${NAME_1}
THIS_IP=${HOST_1}
/opt/etcd-v3.2.32-linux-amd64/etcd --data-dir=data.etcd --name ${THIS_NAME} \
    --initial-advertise-peer-urls http://${THIS_IP}:2380 --listen-peer-urls http://${THIS_IP}:2380 \
    --advertise-client-urls http://${THIS_IP}:2379 --listen-client-urls http://${THIS_IP}:2379 \
    --initial-cluster ${CLUSTER} \
    --initial-cluster-state ${CLUSTER_STATE} --initial-cluster-token ${TOKEN}
    
THIS_NAME=${NAME_2}
THIS_IP=${HOST_2}
/opt/etcd-v3.2.32-linux-amd64/etcd --data-dir=data.etcd --name ${THIS_NAME} \
    --initial-advertise-peer-urls http://${THIS_IP}:2380 --listen-peer-urls http://${THIS_IP}:2380 \
    --advertise-client-urls http://${THIS_IP}:2379 --listen-client-urls http://${THIS_IP}:2379 \
    --initial-cluster ${CLUSTER} \
    --initial-cluster-state ${CLUSTER_STATE} --initial-cluster-token ${TOKEN}
    
THIS_NAME=${NAME_3}
THIS_IP=${HOST_3}
/opt/etcd-v3.2.32-linux-amd64/etcd --data-dir=data.etcd --name ${THIS_NAME} \
    --initial-advertise-peer-urls http://${THIS_IP}:2380 --listen-peer-urls http://${THIS_IP}:2380 \
    --advertise-client-urls http://${THIS_IP}:2379 --listen-client-urls http://${THIS_IP}:2379 \
    --initial-cluster ${CLUSTER} \
    --initial-cluster-state ${CLUSTER_STATE} --initial-cluster-token ${TOKEN}

 

启动服务:sh etcd-static.sh

 

标签:http,urls,--,192.168,编译,源码,etcd,1.32
来源: https://www.cnblogs.com/xingchong/p/16594799.html