-
-
Save linnv/aebee312b140dd4cee56e153a1a691e5 to your computer and use it in GitHub Desktop.
how to solve go mod issue of etcd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module etcdmain | |
go 1.16 | |
// cd $GOPATH | |
// git clone -b v3.5.0 https://github.com/etcd-io/etcd.git | |
// cd etcd | |
// pwd | |
// 如在go 代码文件中 引用包名为 `import go.etcd.io/etcd/client/v3`,则在mod文件需要增加replace的内容`go.etcd.io/etcd/client/v3 => $etcd绝对路径/client/v3`,这里`/Users/jialinwu/go12/etcd`为clone下来的etcd的绝对路径, 整体如下 | |
//in go file `import go.etcd.io/etcd/client/v3`,then you need to edit go.mod and add `replace go.etcd.io/etcd/client/v3 => /Users/jialinwu/go12/etcd/client/v3` | |
replace ( | |
go.etcd.io/etcd/api/v3 => /Users/jialinwu/go12/etcd/api | |
go.etcd.io/etcd/client/pkg/v3 => /Users/jialinwu/go12/etcd/client/pkg | |
go.etcd.io/etcd/client/v2 => /Users/jialinwu/go12/etcd/client/v2 | |
go.etcd.io/etcd/client/v3 => /Users/jialinwu/go12/etcd/client/v3 | |
go.etcd.io/etcd/etcdctl/v3 => /Users/jialinwu/go12/etcd/etcdctl | |
go.etcd.io/etcd/etcdutl/v3 => /Users/jialinwu/go12/etcd/etcdutl | |
go.etcd.io/etcd/pkg/v3 => /Users/jialinwu/go12/etcd/pkg | |
go.etcd.io/etcd/raft/v3 => /Users/jialinwu/go12/etcd/raft | |
go.etcd.io/etcd/server/v3 => /Users/jialinwu/go12/etcd/server | |
) | |
require ( | |
github.com/linnv/logx v1.3.1 | |
go.etcd.io/etcd/client/v3 v3.5.0 | |
go.uber.org/multierr v1.7.0 // indirect | |
golang.org/x/net v0.0.0-20210525063256-abc453219eb5 // indirect | |
google.golang.org/grpc v1.38.0 | |
gopkg.in/yaml.v2 v2.4.0 // indirect | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"log" | |
"os" | |
"os/signal" | |
"time" | |
"github.com/linnv/logx" | |
"google.golang.org/grpc" | |
clientv3 "go.etcd.io/etcd/client/v3" | |
"go.etcd.io/etcd/client/v3/concurrency" | |
) | |
func main() { | |
go func() { | |
es := []string{"https://192.168.1.216:2379", "https://192.168.1.204:2379", "https://192.168.1.203:2379"} | |
config := clientv3.Config{ | |
Endpoints: es, | |
DialOptions: []grpc.DialOption{grpc.WithBlock()}, | |
DialTimeout: 10 * time.Second, | |
} | |
client, err := clientv3.New(config) | |
if err != nil { | |
log.Fatal(fmt.Sprintf("%s %s", err, es)) | |
} else { | |
fmt.Println("connect success") | |
defer client.Close() | |
} | |
logx.Debugf("start NewSession waiting for etcd to work\n") | |
sess, err := concurrency.NewSession(client, concurrency.WithTTL(3)) | |
if err != nil { | |
log.Fatal(err) | |
} | |
elec := concurrency.NewElection(sess, "eeee") | |
startLeaderGet := time.Now() | |
logx.Debugf("get leader") | |
currentLeader1, err := elec.Leader(context.Background()) | |
logx.Debugf("[time usage]LeaderGet costs %d millisecons actually %v\n", time.Since(startLeaderGet).Nanoseconds()/1000000, time.Since(startLeaderGet)) | |
logx.Debugf("currentLeader1: %+v\n", currentLeader1) | |
logx.Debugf("err: %+v\n", err) | |
}() | |
sigChan := make(chan os.Signal, 2) | |
signal.Notify(sigChan, os.Interrupt, os.Kill) | |
log.Print("use c-c to exit: \n") | |
<-sigChan | |
os.Exit(0) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment