golang安装etcd第三方库clientv3 报错

golang安装etcd第三方库clientv3 报错

问题描述

测试程序额源码:

package main

import (
"context"
"fmt"
"time"

"go.etcd.io/etcd/clientv3"
)

// etcd client put/get demo
// use etcd/clientv3

func main() {
	cli, err := clientv3.New(clientv3.Config{
		Endpoints:   []string{"127.0.0.1:2379"},
		DialTimeout: 5 * time.Second,
	})
	if err != nil {
		// handle error!
		fmt.Printf("connect to etcd failed, err:%v\n", err)
		return
	}
	fmt.Println("connect to etcd success")
	defer cli.Close()
	// put
	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	_, err = cli.Put(ctx, "q1mi", "dsb")
	cancel()
	if err != nil {
		fmt.Printf("put to etcd failed, err:%v\n", err)
		return
	}
	// get
	ctx, cancel = context.WithTimeout(context.Background(), time.Second)
	resp, err := cli.Get(ctx, "q1mi",clientv3.WithPrefix())
	cancel()
	if err != nil {
		fmt.Printf("get from etcd failed, err:%v\n", err)
		return
	}
	for _, ev := range resp.Kvs {
		fmt.Printf("%s:%s\n", ev.Key, ev.Value)
	}
}

在go 1.13安装etcd第三方库clientv3时,在执行完go get go.etcd.io/etcd/clientv3后报错

E:\goProject\src\github.com\etcd_demo>go get go.etcd.io/etcd/clientv3
go: finding github.com/coreos/pkg latest
go: finding github.com/coreos/go-systemd latest
# github.com/coreos/etcd/clientv3/balancer/resolver/endpoint
..\..\..\pkg\mod\github.com\coreos\etcd@v3.3.20+incompatible\clientv3\balancer\resolver\endpoint\endpoint.go:114:78: undefined: resolver.BuildOption
..\..\..\pkg\mod\github.com\coreos\etcd@v3.3.20+incompatible\clientv3\balancer\resolver\endpoint\endpoint.go:182:31: undefined: resolver.ResolveNowOpt
ion
tv3\balancer\picker\roundrobin_balanced.go:55:54: undefined: balancer.PickOptions

此时go.mod文件内容为:

module github.com/etcd_demo

go 1.13

require (
	github.com/coreos/bbolt v1.3.4 // indirect
	github.com/coreos/etcd v3.3.20+incompatible // indirect
	github.com/coreos/go-semver v0.3.0 // indirect
	github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf // indirect
	github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f // indirect
	github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
	github.com/gogo/protobuf v1.3.1 // indirect
	github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
	github.com/google/btree v1.0.0 // indirect
	github.com/google/uuid v1.1.1 // indirect
	github.com/gorilla/websocket v1.4.2 // indirect
	github.com/grpc-ecosystem/go-grpc-middleware v1.2.0 // indirect
	github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
	github.com/grpc-ecosystem/grpc-gateway v1.14.4 // indirect
	github.com/jonboulle/clockwork v0.1.0 // indirect
	github.com/prometheus/client_golang v1.6.0 // indirect
	github.com/soheilhy/cmux v0.1.4 // indirect
	github.com/tmc/grpc-websocket-proxy v0.0.0-20200427203606-3cfed13b9966 // indirect
	github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 // indirect
	go.etcd.io/bbolt v1.3.4 // indirect
	go.etcd.io/etcd v3.3.20+incompatible
	go.uber.org/zap v1.15.0 // indirect
	golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1 // indirect
	google.golang.org/grpc v1.29.1 // indirect
	sigs.k8s.io/yaml v1.2.0 // indirect
)

此时执行程序报如下错误:

E:\goProject\src\github.com\etcd_demo>go run main.go
go: finding google.golang.org/genproto v0.0.0-20190927181202-20e1ac93f88c
go: finding golang.org/x/net v0.0.0-20191002035440-2ec189313ef0
# github.com/coreos/etcd/clientv3/balancer/resolver/endpoint
..\..\..\pkg\mod\github.com\coreos\etcd@v3.3.20+incompatible\clientv3\balancer\resolver\endpoint\endpoint.go:114:78: undefined: resolver.BuildOption
..\..\..\pkg\mod\github.com\coreos\etcd@v3.3.20+incompatible\clientv3\balancer\resolver\endpoint\endpoint.go:182:31: undefined: resolver.ResolveNowOpt
ion
# github.com/coreos/etcd/clientv3/balancer/picker
..\..\..\pkg\mod\github.com\coreos\etcd@v3.3.20+incompatible\clientv3\balancer\picker\err.go:37:44: undefined: balancer.PickOptions
..\..\..\pkg\mod\github.com\coreos\etcd@v3.3.20+incompatible\clientv3\balancer\picker\roundrobin_balanced.go:55:54: undefined: balancer.PickOptions

解决方法

直接在go.mod文件中添加

replace google.golang.org/grpc => google.golang.org/grpc v1.26.0

再次编译运行即可

E:\goProject\src\github.com\etcd_demo>go run main.go
go: downloading google.golang.org/grpc v1.26.0
go: extracting google.golang.org/grpc v1.26.0
go: finding google.golang.org/grpc v1.26.0
connect to etcd success
q1mi:dsb

问题分析

原因 etcd3.3.20 的 release 版本要求 grpc 的版本是 v1.26.0 之前的。而此时 go.mod 里面的 google.golang.org/grpc 是 v1.29.1
解决方法:在 go.mod 里添加一句

replace google.golang.org/grpc => google.golang.org/grpc v1.26.0


2020 5.6 更新

前两天在安装测试etcd时碰到上面报错,今天把项目里应用etcd模块时也碰到了一样的问题。

项目结构如下:
mark

解决方法:

很简单,在main的目录下再次执行以下前面的步骤即可

go mod init 
go mod edit -replace github.com/coreos/bbolt@v1.3.4=go.etcd.io/bbolt@v1.3.4
go mod edit -replace google.golang.org/grpc@v1.29.1=google.golang.org/grpc@v1.26.0
go mod tidy
go run main.go

相关连接:
https://learnku.com/articles/43758
https://my.oschina.net/u/2321997/blog/4258724/print
https://colobu.com/2020/04/09/accidents-of-etcd-and-go-module/

posted @ 2020-05-05 17:56  wind-zhou  Views(4153)  Comments(0Edit  收藏  举报