Artem Makolov Asked:2021-12-02 05:31:32 +0800 CST2021-12-02 05:31:32 +0800 CST 2021-12-02 05:31:32 +0800 CST 带有私钥和 Go 的外部 grpc 服务器 772 如何通过 go 连接到外部 grpc 服务器? сервер 1 个回答 Voted Best Answer Senior Pomidor 2021-12-02T21:34:49+08:002021-12-02T21:34:49+08:00 首先,我们将指出您需要连接的位置,我省略了所有错误检查。 conn, err := grpc.Dial("127.0.0.1:1234") 可以使用 ServerOptions 配置 gRPC 客户端。例如,grpc.Dial("127.0.0.1:1234", grpc.WithInsecure(), grpc.WithBlock()) 但这还不是全部。我们需要处理 protobuff。为此,请注册我们的 protobuff pb "google.golang.org/grpc/examples/helloworld/helloworld" c := pb.NewGreeterClient(conn) 现在我们可以向实现这个 protobuff 的服务器发送消息。 ctx, cancel := context.WithTimeout(context.Background(), time.Second) r, err := c.SayHello(ctx, &pb.HelloRequest{Name: name}) if err != nil { log.Fatalf("could not greet: %v", err) } log.Printf("Greeting: %s", r.GetMessage()) SayHello、HelloRequest、GetMessage 方法在 protobuff 本身中实现。 您可以在官方文档中阅读更多相关信息。代码示例在这里
首先,我们将指出您需要连接的位置,我省略了所有错误检查。
可以使用 ServerOptions 配置 gRPC 客户端。例如,
grpc.Dial("127.0.0.1:1234", grpc.WithInsecure(), grpc.WithBlock())
但这还不是全部。我们需要处理 protobuff。为此,请注册我们的 protobuff
pb "google.golang.org/grpc/examples/helloworld/helloworld"
现在我们可以向实现这个 protobuff 的服务器发送消息。
SayHello、HelloRequest、GetMessage 方法在 protobuff 本身中实现。
您可以在官方文档中阅读更多相关信息。代码示例在这里