grpc.WithInsecure 已弃用:使用 insecure.NewCredentials() 代替

shw*_*ick 28 go grpc grpc-go

嘿,我正在尝试使用 Go 和 Grpc 制作一个小型测试客户端,

opts := grpc.WithInsecure()
    cc, err := grpc.Dial("localhost:9950", opts)
    if err != nil {
        log.Fatal(err)
    }
Run Code Online (Sandbox Code Playgroud)

函数WithInsecure()调用给出警告:

grpc.WithInsecure 已弃用:使用 insecure.NewCredentials() 代替。

我不确定如何使用这个新函数调用,某处有示例吗?谢谢

bla*_*een 70

该函数insecure.NewCredentials返回 的实现credentials.TransportCredentials

您可以将它用作DialOptionwith grpc.WithTransportCredentials

grpc.Dial(":9950", grpc.WithTransportCredentials(insecure.NewCredentials()))
Run Code Online (Sandbox Code Playgroud)

  • @MajorEccles 它位于“google.golang.org/grpc/credentials/insecure” (5认同)
  • 作为 v1.44.0(2022 年 1 月 25 日)的更新,它不再是实验性的。此外,“staticcheck”linter 也报告了这一点。 (3认同)