smallnest / gitalk

gitalk for colobu
19 stars 0 forks source link

Etcd使用go module的灾难 #266

Open utterances-bot opened 1 year ago

utterances-bot commented 1 year ago

Etcd使用go module的灾难

最近几个月,或者最近一年,使用etcd做开发的朋友,如果你开启了go module的功能的话,难道没有出现翻车的现象吗?或者, 你go get -u .更新一下项目依赖试试看。

https://colobu.com/2020/04/09/accidents-of-etcd-and-go-module/

chenyang8094 commented 1 year ago

兄台,问题解决了没有,etcd的mod是真的灾难

leopold7 commented 1 year ago

简直跟我的心路历程一模一样,项目grpc也没法降级到1.26,没法子了QAQ

chenyang8094 commented 1 year ago

实测有效: 在go.mod中写入(以当前官方最新版本为例):

require (
   go.etcd.io/etcd/client/v3 v3.5.9
   go.etcd.io/etcd/server/v3 v3.5.9
   go.etcd.io/etcd/v3 v3.5.9
)

然后源文件里可以用如下方式import:

import (
         clientv3 "go.etcd.io/etcd/client/v3"
    "go.etcd.io/etcd/server/v3/embed"
)
leopold7 commented 1 year ago

感谢回复 @chenyang8094 想请教下tls证书的写法。参数都没有变,原先是可以执行的,到最新版之后put就假死了,是写法不对了吗? 代码如下:

type Config struct {
    Endpoints []string
    CertFile  string
    KeyFile   string
    CAFile    string
}
func GetConn() (cli *clientv3.Client, err error) {
    cert, err := tls.LoadX509KeyPair(config.CertFile, config.KeyFile)
    if err != nil {
        return nil, nil
    }
    caCert, err := os.ReadFile(config.CAFile)
    if err != nil {
        return nil, nil
    }
    caPool := x509.NewCertPool()
    caPool.AppendCertsFromPEM(caCert)
    tlsConfig := &tls.Config{
        Certificates: []tls.Certificate{cert},
        RootCAs:      caPool,
    }
    config := clientv3.Config{
        Endpoints:   config.Endpoints,
        TLS:         tlsConfig,
        DialTimeout: time.Duration(5) * time.Millisecond,
    }
    cli, err = clientv3.New(config)
    if err != nil {
        return nil, nil
    }
    return cli, nil
}

错误如下:

{"level":"warn","ts":"2023-07-05T14:06:25.474739+0800","logger":"etcd-client","caller":"v3@v3.5.9/retry_interceptor.go:62","msg":"retrying of unary invoker failed","target":"etcd-endpoints://0xc0000c0000/192.168.0.67:2379","attempt":0,"error":"
rpc error: code = DeadlineExceeded desc = latest balancer error: last connection error: connection error: desc = \"transport: Error while dialing reading server HTTP response: unexpected EOF\""}
chenyang8094 commented 1 year ago

感谢回复 @chenyang8094 想请教下tls证书的写法。参数都没有变,原先是可以执行的,到最新版之后put就假死了,是写法不对了吗? 代码如下:

type Config struct {
  Endpoints []string
  CertFile  string
  KeyFile   string
  CAFile    string
}
func GetConn() (cli *clientv3.Client, err error) {
  cert, err := tls.LoadX509KeyPair(config.CertFile, config.KeyFile)
  if err != nil {
      return nil, nil
  }
  caCert, err := os.ReadFile(config.CAFile)
  if err != nil {
      return nil, nil
  }
  caPool := x509.NewCertPool()
  caPool.AppendCertsFromPEM(caCert)
  tlsConfig := &tls.Config{
      Certificates: []tls.Certificate{cert},
      RootCAs:      caPool,
  }
  config := clientv3.Config{
      Endpoints:   config.Endpoints,
      TLS:         tlsConfig,
      DialTimeout: time.Duration(5) * time.Millisecond,
  }
  cli, err = clientv3.New(config)
  if err != nil {
      return nil, nil
  }
  return cli, nil
}

错误如下:

{"level":"warn","ts":"2023-07-05T14:06:25.474739+0800","logger":"etcd-client","caller":"v3@v3.5.9/retry_interceptor.go:62","msg":"retrying of unary invoker failed","target":"etcd-endpoints://0xc0000c0000/192.168.0.67:2379","attempt":0,"error":"
rpc error: code = DeadlineExceeded desc = latest balancer error: last connection error: connection error: desc = \"transport: Error while dialing reading server HTTP response: unexpected EOF\""}

因为我自己没有使用tls,没遇到这个问题。看你的错误应该是网络连接被关闭了,不清楚是否是客户端版本和etcd服务端版本不一致导致的(确切的说是底层的grpc版本不一致),这个建议你去etcd社区或者grpc社区问问。

leopold7 commented 1 year ago

感谢回复 @chenyang8094 ,已经找到解决方法了,但不太清楚根本原因。 我的etcd在服务器上,本机调试是异地Windows,使用了WireGuard代理连接,加上本地代理导致本地网络比较复杂,本地的代理服务器似乎不支持新版本的SSL连接,在服务器上go build后是可以执行的。

leopold7 commented 1 year ago

已找到根本原因,我的goland编译器开启了sock5代理,代码使用了http代理,把goland改为http代理或者关闭代理就好了QAQ

lesismal commented 1 year ago

好像不只是一年,两三年前写服务注册发现的demo就遇到过,竟然还没搞好点呢。。