【发布时间】:2021-12-06 12:49:17
【问题描述】:
我正在使用 gRPC
和 Golang
。
在成功运行他们的HelloWorld tutorial
后,需要我添加第二个函数,编译 protoc 文件,我决定进行一些进一步的更改。
我的代码已包含在内,但它似乎无法正常工作,因为它希望 pb
变量是 HelloWorld 对象或其他东西,如下面命令行中的错误消息所示。有人可以告诉我哪里出了问题,应该是一些非常小的变化。
我在下面的原始帖子代码中修正了一些错别字。
命令行:
go run server/server.go
# command-line-arguments
server/server.go:24:71: undefined: helloworld.TheGRPCNotificationMessage
命令行协议文件:
协议 --go_out=。 --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative GRPCNotification/GRPCNotification.proto
GRPCNotification.proto
syntax = "proto3";
import "google/protobuf/empty.proto";
option go_package = "google.golang.org/grpc/examples/helloworld/helloworld";
option java_multiple_files = true;
option java_package = "io.grpc.examples.helloworld";
option java_outer_classname = "HelloWorldProto";
package grpcnotificationpackage;
// The greeting service definition.
service TheGRPCNotificationService {
// Sends a notification and nothing sent back
rpc SendNotificationWithGRPC (TheGRPCNotificationMessage) returns (google.protobuf.Empty);
// Receives a notification and does not reply
rpc ReceiveNotificationWithGRPC (stream TheGRPCNotificationMessage) returns (google.protobuf.Empty);
}
// The request notification message
message TheGRPCNotificationMessage {
string message = 1;
// ... add more here
}
// Since nothing gets returned from server we need this empty
message Empty {
// Nothing goes here
}
Client.go
// Package main implements a client for Greeter service.
package main
import (
"context"
"log"
"time"
"google.golang.org/grpc"
pb "google.golang.org/grpc/examples/helloworld/helloworld"
)
const (
address = "localhost:50051"
defaultName = "world"
)
func main() {
// Set up a connection to the server.
conn, err := grpc.Dial(address, grpc.WithInsecure(), grpc.WithBlock())
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()
c := pb.NewGreeterClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
r, err := c.SendNotificationWithGRPC(ctx, &pb.TheGRPCNotificationMessage{Message: "Horse"})
if err != nil {
log.Fatalf("could not greet: %v", err)
}
log.Printf("Sent a message, please check it reached server...")
}
Server.go
package main
import (
"context"
"log"
"net"
"google.golang.org/grpc"
pb "google.golang.org/grpc/examples/helloworld/helloworld"
)
const (
port = ":50051"
)
// server is used to implement helloworld.GreeterServer
type server struct {
pb.UnimplementedGreeterServer
}
// SendNotificationWithGRPC implements helloworld.GreeterServer <--- Problem?
func (s *server) ReceiveNotificationWithGRPC(ctx context.Context, in *pb.TheGRPCNotificationMessage) {
log.Printf("Received: %v", in.Name)
}
func main() {
lis, err := net.Listen("tcp", port)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
pb.RegisterGreeterServer(s, &server{})
log.Printf("server listening at %v", lis.Addr())
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}
我希望示例教程没有让我如此粗略地粘住它!
【问题讨论】: