package goroutine import ( "context" "fmt" clientv3 "go.etcd.io/etcd/client/v3" "goLearn/model" "time" ) func Runtask(ctx context.Context) { ctx2 := ctx.Value("value").(model.Task) fmt.Println(ctx2.Id) config := clientv3.Config{ Endpoints: []string{"127.0.0.1:2379"}, // etcd节点地址,集群模式可填多个 DialTimeout: 5 * time.Second, // 连接超时时间 // 如需认证,添加以下配置 // Username: "root", // Password: "123456", } // 2. 建立连接 client, err := clientv3.New(config) if err != nil { fmt.Printf("连接etcd失败: %v\n", err) return } defer client.Close() // 程序退出时关闭连接 fmt.Println("成功连接到etcd") }