工作内容中需要在etcd中设置一个很大很大的key,记录一下具体步骤。
-
安装etcd,mac用户
brew install etcd即可 -
启动etcd,etcd --max-request-bytes=10485760 设置的最大size是10MB,也是etcd官方内置hardcode的最大值,不设置这个值,默认是1.5MB
-
编写代码,纯测试代码,没有处理ctx超时
package main
import (
"context"
"fmt"
clientv3 "go.etcd.io/etcd/client/v3"
"strings"
"time"
)
func main() {
cli, err := clientv3.New(clientv3.Config{
Endpoints: []string{"localhost:2379", "localhost:22379", "localhost:32379"},
DialTimeout: 5 * time.Second,
MaxCallSendMsgSize: 10485760,
})
if err != nil {
// handle error!
}
defer cli.Close()
ctx, _ := context.WithTimeout(context.Background(), 100*time.Second)
_, err = cli.Put(ctx, "sample_key", "sample_value")
if err != nil {
fmt.Println(err)
}
fmt.Println("set sample done")
oneMBString := strings.Repeat("a", 1024*1024)
// 生成5MB的字符串
fiveMBString := strings.Repeat(oneMBString, 5)
_, err = cli.Put(ctx, "large_key", fiveMBString)
if err != nil {
fmt.Println(err)
}
fmt.Println("set large done")
value, _ := cli.Get(ctx, "large_key")
fmt.Print(value)
}
运行结果设置成功