Skip to content

Commit

Permalink
add etcd
Browse files Browse the repository at this point in the history
  • Loading branch information
memory125 committed Jan 24, 2022
1 parent 0c33851 commit fe86af0
Show file tree
Hide file tree
Showing 19 changed files with 816 additions and 18 deletions.
28 changes: 28 additions & 0 deletions etcd/Note.md
@@ -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`
32 changes: 32 additions & 0 deletions etcd/conf/init.go
@@ -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
}
26 changes: 26 additions & 0 deletions etcd/get/get.go
@@ -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
}
47 changes: 47 additions & 0 deletions etcd/go.mod
@@ -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
)
587 changes: 587 additions & 0 deletions etcd/go.sum

Large diffs are not rendered by default.

37 changes: 37 additions & 0 deletions etcd/main.go
@@ -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")
}
22 changes: 22 additions & 0 deletions etcd/put/put.go
@@ -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
}
19 changes: 19 additions & 0 deletions etcd/watch/watch.go
@@ -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)
}
}
}
6 changes: 3 additions & 3 deletions fundamental/29-mapslice.go
Expand Up @@ -2,7 +2,7 @@ package main

import "fmt"

func main() {
func main() {
mapSlice1()
mapSlice2()
}
Expand All @@ -13,7 +13,7 @@ func mapSlice1() {
for index, value := range mapSlice {
fmt.Printf("index:%d value:%v\n", index, value)
}
fmt.Println("after init")
fmt.Println("after conf")
// 对切片中的map元素进行初始化
mapSlice[0] = make(map[string]string, 10)
mapSlice[0]["name"] = "小王子"
Expand All @@ -29,7 +29,7 @@ func mapSlice2() {
// 声明一个键为string,值为string切片的map
var sliceMap = make(map[string][]string, 3)
fmt.Println(sliceMap)
fmt.Println("after init")
fmt.Println("after conf")
// 键
key := "中国"
value, ok := sliceMap[key]
Expand Down
2 changes: 1 addition & 1 deletion mysql/connect/connect.go
Expand Up @@ -3,7 +3,7 @@ package main
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql" // init()
_ "github.com/go-sql-driver/mysql" // conf()
)

// MySQL - 连接示例
Expand Down
2 changes: 1 addition & 1 deletion mysql/delete/delete.go
Expand Up @@ -3,7 +3,7 @@ package main
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql" // init()
_ "github.com/go-sql-driver/mysql" // conf()
)

// MySQL - Delete删除示例
Expand Down
2 changes: 1 addition & 1 deletion mysql/injection/injection.go
Expand Up @@ -2,7 +2,7 @@ package main

import (
"fmt"
_ "github.com/go-sql-driver/mysql" // init()
_ "github.com/go-sql-driver/mysql" // conf()
"github.com/jmoiron/sqlx"
)

Expand Down
2 changes: 1 addition & 1 deletion mysql/insert/insert.go
Expand Up @@ -3,7 +3,7 @@ package main
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql" // init()
_ "github.com/go-sql-driver/mysql" // conf()
)

// MySQL - Insert插入示例
Expand Down
2 changes: 1 addition & 1 deletion mysql/prepare/prepare.go
Expand Up @@ -3,7 +3,7 @@ package main
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql" // init()
_ "github.com/go-sql-driver/mysql" // conf()
)

// MySQL - 预处理
Expand Down
2 changes: 1 addition & 1 deletion mysql/query/query.go
Expand Up @@ -3,7 +3,7 @@ package main
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql" // init()
_ "github.com/go-sql-driver/mysql" // conf()
)

// MySQL - Query查询示例
Expand Down
2 changes: 1 addition & 1 deletion mysql/sqlx/sqlx.go
Expand Up @@ -2,7 +2,7 @@ package main

import (
"fmt"
_ "github.com/go-sql-driver/mysql" // init()
_ "github.com/go-sql-driver/mysql" // conf()
"github.com/jmoiron/sqlx"
)

Expand Down
2 changes: 1 addition & 1 deletion mysql/transaction/transaction.go
Expand Up @@ -3,7 +3,7 @@ package main
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql" // init()
_ "github.com/go-sql-driver/mysql" // conf()
)

// MySQL - 事务示例
Expand Down
2 changes: 1 addition & 1 deletion mysql/update/update.go
Expand Up @@ -3,7 +3,7 @@ package main
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql" // init()
_ "github.com/go-sql-driver/mysql" // conf()
)

// MySQL - Update更新示例
Expand Down
12 changes: 6 additions & 6 deletions package/calcdemo.go
Expand Up @@ -7,9 +7,9 @@ package main
- 单行导入和多行导入
- 导入包的时候可以指定别名
- 导入包不想使用包内部的标识符,需要使用匿名导入
- 每个包导入的时候会自动执行一个名为`init()`的函数,它没有参数也没有返回值,不能手动调用
- `init()`的执行顺序:变量声明 > init() > main()
*/
- 每个包导入的时候会自动执行一个名为`conf()`的函数,它没有参数也没有返回值,不能手动调用
- `conf()`的执行顺序:变量声明 > conf() > main()
*/

import (
calc "code.wing.com/package/calc"
Expand All @@ -21,16 +21,16 @@ var x int = 20

const pi = 3.1415926

func init() {
func init() {
fmt.Printf("x = %v, pi = %v.\n", x, pi)
}

func main() {
func main() {
ret := calc.Add(50, 80)
/*
-------------------------Module cals---------------------
x = 20, pi = 3.1415926.
ret: 130
*/
*/
fmt.Println("ret: ", ret)
}

0 comments on commit fe86af0

Please sign in to comment.