etcd1,2,3 组成集群
etcd0 单机
deltaprojects/etcdkeeper etcd可视化管理工具
# cd etcd/
docker compose up .
version: '3.8'
networks:
etcd_net:
name: etcd_net
services:
etcd0:
image: quay.io/coreos/etcd
container_name: etcd0
command: etcd -name etcd0 -advertise-client-urls http://0.0.0.0:2379 -listen-client-urls http://0.0.0.0:2379 -listen-peer-urls http://0.0.0.0:2380
ports:
- 2379:2379
- 2479:2380
volumes:
- ./etcd0/data:/etcd-data
networks:
- etcd_net
etcd1:
image: quay.io/coreos/etcd
container_name: etcd1
command: etcd -name etcd1 -advertise-client-urls http://0.0.0.0:2379 -listen-client-urls http://0.0.0.0:2379 -listen-peer-urls http://0.0.0.0:2380 -initial-cluster-token etcd-cluster -initial-cluster "etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380" -initial-cluster-state new
ports:
- 2380:2379
- 2480:2380
volumes:
- ./etcd1/data:/etcd-data
networks:
- etcd_net
etcd2:
image: quay.io/coreos/etcd
container_name: etcd2
command: etcd -name etcd2 -advertise-client-urls http://0.0.0.0:2379 -listen-client-urls http://0.0.0.0:2379 -listen-peer-urls http://0.0.0.0:2380 -initial-cluster-token etcd-cluster -initial-cluster "etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380" -initial-cluster-state new
ports:
- 2381:2379
- 2481:2380
volumes:
- ./etcd2/data:/etcd-data
networks:
- etcd_net
etcd3:
image: quay.io/coreos/etcd
container_name: etcd3
command: etcd -name etcd3 -advertise-client-urls http://0.0.0.0:2379 -listen-client-urls http://0.0.0.0:2379 -listen-peer-urls http://0.0.0.0:2380 -initial-cluster-token etcd-cluster -initial-cluster "etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380" -initial-cluster-state new
ports:
- 2382:2379
- 2482:2380
volumes:
- ./etcd3/data:/etcd-data
networks:
- etcd_net
etcdkeeper:
image: deltaprojects/etcdkeeper
container_name: etcdkeeper
ports:
- 8088:8080
links:
- etcd0
- etcd1
- etcd2
- etcd3
networks:
- etcd_net
package main
import (
"context"
"fmt"
"go.etcd.io/etcd/client/v3"
"time"
)
func main() {
cli := InitEtcd([]string{"http://127.0.0.1:2482", "http://127.0.0.1:2480", "http://127.0.0.1:2481"})
defer cli.Close()
ctx0, _ := context.WithTimeout(context.Background(), time.Second*10)
go Watch(cli, ctx0, "/node")
time.Sleep(time.Second)
ctx, _ := context.WithTimeout(context.Background(), time.Second*5)
err := Put(cli, ctx, "/node", "1")
if err != nil {
panic(err)
}
ctx, _ = context.WithTimeout(context.Background(), time.Second*5)
err = Read(cli, ctx, "/node")
if err != nil {
panic(err)
}
ctx, _ = context.WithTimeout(context.Background(), time.Second*5)
err = Put(cli, ctx, "/node", "2")
if err != nil {
panic(err)
}
ctx, _ = context.WithTimeout(context.Background(), time.Second*5)
err = Read(cli, ctx, "/node")
if err != nil {
panic(err)
}
ctx, _ = context.WithTimeout(context.Background(), time.Second*5)
err = Delete(cli, ctx, "/node")
if err != nil {
panic(err)
}
select {
case <-ctx0.Done():
}
}
func InitEtcd(addr []string) *clientv3.Client {
cli, err := clientv3.New(clientv3.Config{
Endpoints: addr,
DialTimeout: 5 * time.Second,
})
if err != nil {
panic(fmt.Sprintf("connect to etcd failed, err:%v\n", err))
}
fmt.Println("connect to etcd success")
return cli
}
func Watch(cli *clientv3.Client, ctx context.Context, key string) {
rch := cli.Watch(ctx, key, clientv3.WithPrefix()) // type WatchChan <-chan WatchResponse
for wresp := range rch {
for _, ev := range wresp.Events {
fmt.Printf("Type: %s Key:%s Value:%s\n", ev.Type, ev.Kv.Key, ev.Kv.Value)
}
}
}
func Put(cli *clientv3.Client, ctx context.Context, key string, value string) error {
defer ctx.Done()
_, err := cli.Put(ctx, key, value)
return err
}
func Read(cli *clientv3.Client, ctx context.Context, key string) error {
defer ctx.Done()
resp, err := cli.Get(ctx, key)
if err != nil {
return err
}
for _, ev := range resp.Kvs {
fmt.Printf("Type: %s Key:%s Value:%s\n", "READ~~~~", ev.Key, ev.Value)
}
return nil
}
func Delete(cli *clientv3.Client, ctx context.Context, key string) error {
defer ctx.Done()
_, err := cli.Delete(ctx, key)
return err
}
/*
GOROOT=/Users/zyj/go/go1.16 #gosetup
GOPATH=/Users/zyj/go #gosetup
/Users/zyj/go/go1.16/bin/go build -o /private/var/folders/18/c2k36qyx3hl6d3dg8ccs876r0000gn/T/___go_build_lib_demo_etcd lib_demo/etcd #gosetup
/private/var/folders/18/c2k36qyx3hl6d3dg8ccs876r0000gn/T/___go_build_lib_demo_etcd #gosetup
connect to etcd success
Type: PUT Key:/node Value:1
Type: READ~~~~ Key:/node Value:1
Type: PUT Key:/node Value:2
Type: READ~~~~ Key:/node Value:2
Type: DELETE Key:/node Value:
Process finished with exit code 0
*/