Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
816 additions
and
18 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
## etcd注意事项 | ||
### go.mod | ||
- 第一步: | ||
`go mod init` | ||
- 第二步 | ||
`go mod tidy`时,出现下述错误: | ||
`/etcd imports | ||
github.com/coreos/etcd/clientv3 tested by | ||
github.com/coreos/etcd/clientv3.test imports | ||
github.com/coreos/etcd/auth imports | ||
github.com/coreos/etcd/mvcc/backend imports | ||
github.com/coreos/bbolt: github.com/coreos/bbolt@v1.3.5: parsing go.mod: | ||
module declares its path as: go.etcd.io/bbolt | ||
but was required as: github.com/coreos/bbolt` | ||
|
||
### 解决方案 | ||
- 修改go.mod | ||
`go mod edit -replace github.com/coreos/bbolt@v1.3.4=go.etcd.io/bbolt@v1.3.4` | ||
- 运行 | ||
`go mod tidy` | ||
- 如果出现下述错误 | ||
`imports | ||
google.golang.org/grpc/naming: module google.golang.org/grpc@latest found (v1.32.0), but does not contain package google.golang.org/grpc/naming | ||
` | ||
- 解决方案还是要修改go.mod | ||
`go mod edit -replace google.golang.org/grpc@v1.32.0=google.golang.org/grpc@v1.26.0` | ||
- 重新下载 | ||
`go mod tidy` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package conf | ||
|
||
import ( | ||
"fmt" | ||
"github.com/coreos/etcd/clientv3" | ||
"time" | ||
) | ||
|
||
// etcd - conf | ||
|
||
var ( | ||
etcdCli *clientv3.Client | ||
) | ||
|
||
func Init(address []string) (err error) { | ||
etcdCli, err = clientv3.New(clientv3.Config{ | ||
Endpoints: address, | ||
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") | ||
|
||
return | ||
} | ||
|
||
func GetCli() *clientv3.Client { | ||
return etcdCli | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package get | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/coreos/etcd/clientv3" | ||
"time" | ||
) | ||
|
||
// etcd - client get示例 | ||
// use etcd/clientv3 | ||
|
||
func GetInfo(cli *clientv3.Client, key string) (err error) { | ||
// get | ||
ctx, cancel := context.WithTimeout(context.Background(), time.Second) | ||
resp, err := cli.Get(ctx, key) | ||
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) | ||
} | ||
return | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
module wing.com/magic-golang/etcd | ||
|
||
go 1.17 | ||
|
||
replace github.com/coreos/bbolt v1.3.4 => go.etcd.io/bbolt v1.3.4 | ||
|
||
replace ( | ||
google.golang.org/grpc v1.32.0 => google.golang.org/grpc v1.26.0 | ||
google.golang.org/grpc v1.43.0 => google.golang.org/grpc v1.26.0 | ||
) | ||
|
||
require github.com/coreos/etcd v3.3.27+incompatible | ||
|
||
require ( | ||
github.com/coreos/bbolt v1.3.4 // 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/dustin/go-humanize v1.0.0 // indirect | ||
github.com/gogo/protobuf v1.3.2 // indirect | ||
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect | ||
github.com/golang/protobuf v1.5.2 // indirect | ||
github.com/google/btree v1.0.1 // indirect | ||
github.com/google/uuid v1.3.0 // indirect | ||
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect | ||
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect | ||
github.com/jonboulle/clockwork v0.2.2 // indirect | ||
github.com/pkg/errors v0.9.1 // indirect | ||
github.com/prometheus/client_golang v1.12.0 // indirect | ||
github.com/soheilhy/cmux v0.1.5 // indirect | ||
github.com/tmc/grpc-websocket-proxy v0.0.0-20220101234140-673ab2c3ae75 // indirect | ||
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 // indirect | ||
go.uber.org/atomic v1.7.0 // indirect | ||
go.uber.org/multierr v1.6.0 // indirect | ||
go.uber.org/zap v1.20.0 // indirect | ||
golang.org/x/crypto v0.0.0-20220112180741-5e0467b6c7ce // indirect | ||
golang.org/x/net v0.0.0-20220121210141-e204ce36a2ba // indirect | ||
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 // indirect | ||
golang.org/x/text v0.3.7 // indirect | ||
golang.org/x/time v0.0.0-20211116232009-f0f3c7e86c11 // indirect | ||
google.golang.org/genproto v0.0.0-20220118154757-00ab72f36ad5 // indirect | ||
google.golang.org/grpc v1.43.0 // indirect | ||
google.golang.org/protobuf v1.27.1 // indirect | ||
gopkg.in/yaml.v2 v2.4.0 // indirect | ||
sigs.k8s.io/yaml v1.3.0 // indirect | ||
) |
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"wing.com/magic-golang/etcd/conf" | ||
"wing.com/magic-golang/etcd/get" | ||
"wing.com/magic-golang/etcd/put" | ||
"wing.com/magic-golang/etcd/watch" | ||
) | ||
|
||
// etcd 示例 | ||
|
||
func main() { | ||
// 1. 初始化etcd | ||
err := conf.Init([]string{"127.0.0.1:2379"}) | ||
if err != nil { | ||
fmt.Printf("Init etcd connection failed, error is %v.\n", err) | ||
return | ||
} | ||
|
||
// 2. put操作 | ||
err = put.PutData(conf.GetCli(), "test", "hello") | ||
if err != nil { | ||
fmt.Printf("etcd put data failed, error is %v.\n", err) | ||
return | ||
} | ||
|
||
// 3. get操作 | ||
err = get.GetInfo(conf.GetCli(), "test") | ||
if err != nil { | ||
fmt.Printf("etcd put data failed, error is %v.\n", err) | ||
return | ||
} | ||
|
||
// 4. watch操作 | ||
watch.WatchKey(conf.GetCli(), "test") | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package put | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/coreos/etcd/clientv3" | ||
"time" | ||
) | ||
|
||
// etcd - client put示例 | ||
// use etcd/clientv3 | ||
|
||
func PutData(cli *clientv3.Client, key, value string) (err error) { | ||
ctx, cancel := context.WithTimeout(context.Background(), time.Second) | ||
_, err = cli.Put(ctx, key, value) | ||
cancel() | ||
if err != nil { | ||
fmt.Printf("put to etcd failed, err:%v\n", err) | ||
return | ||
} | ||
return | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package watch | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/coreos/etcd/clientv3" | ||
) | ||
|
||
// watch demo | ||
|
||
func WatchKey(cli *clientv3.Client, key string) { | ||
// watch key change | ||
rch := cli.Watch(context.Background(), key) // <-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) | ||
} | ||
} | ||
} |
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
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
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
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
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
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
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
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
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
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
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