Go gRPC,未定义:RegisterChatServiceServer

时间:2024-04-20

浏览:243

作者:佚名

编程并不是一个机械性的工作,而是需要有思考,有创新的工作,语法是固定的,但解决问题的思路则是依靠人的思维,这就需要我们坚持学习和更新自己的知识。今天golang学习网就整理分享《Go gRPC,未定义:RegisterChatServiceServer》,文章讲解的知识点主要包括,如果你对Golang方面的知识点感兴趣,就不要错过golang学习网,在这可以对大家的知识积累有所帮助,助力开发能力的提升。

问题内容

尝试首先使用 go 进行 grpc 构建,所以我尝试了以下操作:

1.1。安装所需的 go 库

ps d:\grpc> go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
ps d:\grpc> go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest

1.2。将 gopath/bin 添加到 path,以便从命令行识别 protoc-gen-go 和 protoc-gen-grpc 命令

2.1。从以下位置下载了 protoc 二进制文件 for windows: https://github.com/protocolbuffers/protobuf/releases/

2.2。解压 protoc zip 文件,并将 bin 文件夹添加到路径

3.1。我的原型文件写为:

// file: grpc/chat/chat.proto
syntax = "proto3";
package chat;
option go_package = "github.com/hajsf/grpc/chat";

message messagerequest {
  string body = 1;
}

message messageresponse {
    string body = 1;
  }

service chatservice {
  rpc sayhello(messagerequest) returns (messageresponse) {}
}

3.3。编译我的原型文件以从 grpc 文件夹中获取 chat.pb.go :

ps d:\grpc> protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative chat.proto

因此生成了 2 个文件:chat.pb.go 和 chat_grpc.pb.go

3.4。将 chat.go 文件添加为:

// file: grpc/chat/chat.go
package main

import (
    "log"

    chat "github.com/hajsf/grpc/chat"
    "golang.org/x/net/context"
)

type server struct {
}

func (s *server) sayhello(ctx context.context, in *chat.messagerequest) (*chat.messageresponse, error) {
    log.printf("receive message body from client: %s", in.body)
    return &chat.messageresponse{body: "hello from the server!"}, nil
}
  • 将我的服务器文件构建为:
  • // file: grpc/server/main.go
    package main
    
    import (
        "fmt"
        "log"
        "net"
    
        "github.com/hajsf/grpc/chat"
        "google.golang.org/grpc"
    )
    
    func main() {
    
        fmt.println("go grpc beginners tutorial!")
    
        lis, err := net.listen("tcp", fmt.sprintf(":%d", 9000))
        if err != nil {
            log.fatalf("failed to listen: %v", err)
        }
    
        s := chat.server{}
    
        grpcserver := grpc.newserver()
    
        chat.registerchatserviceserver(grpcserver, &s)
    
        if err := grpcserver.serve(lis); err != nil {
            log.fatalf("failed to serve: %s", err)
        }
    }
    
  • 将我的客户端文件构建为:
  • // file: grpc/client/main.go
    package main
    
    import (
        "log"
    
        "golang.org/x/net/context"
        "google.golang.org/grpc"
    
        "github.com/hajsf/grpc/chat"
    )
    
    func main() {
    
        var conn *grpc.clientconn
        conn, err := grpc.dial(":9000", grpc.withinsecure())
        if err != nil {
            log.fatalf("did not connect: %s", err)
        }
        defer conn.close()
    
        c := chat.newchatserviceclient(conn) // why not found?
    
        response, err := c.sayhello(context.background(), &chat.messagerequest{body: "hello from client!"})
        if err != nil {
            log.fatalf("error when calling sayhello: %s", err)
        }
        log.printf("response from server: %s", response.body)
    
    }
    
  • 在上述 3 个文件夹中,chat、server 和 client go 模块已创建为:
  • ps d:\grpc> go mod init github.com/hajsf/grpc/<filenale>
    ps d:\grpc> go mod tidy
    
  • 尝试运行 server,但出现以下错误:
  • ps d:\grpc\server> go run github.com/hajsf/grpc/server
    # github.com/hajsf/grpc/server
    .\main.go:21:12: undefined: chat.server
    .\main.go:25:7: undefined: chat.registerchatserviceserver
    
  • 尝试运行 client,但出现以下错误:
  • PS D:\grpc\client> go run github.com/hajsf/grpc/client
    # github.com/hajsf/grpc/client
    .\main.go:21:12: undefined: chat.NewChatServiceClient
    

    代码作为 github 存储库加载:hajsf/grpc (github.com)


    正确答案


    为您生成了服务器端吗? 也许你把它放错了地方。

    对于最新的协议版本 22.1,命令如下所示:

    protoc \
        --go_out=./chat \ #sub package path and name
        --go_opt=paths=source_relative \
        --go-grpc_out=./chat \ #sub package path and name
        --go-grpc_opt=paths=source_relative \
        -I=$PWD $PWD/chat.proto #it says path is mandatory now
    

    它在我需要的包中为我生成了两个文件。这两个文件都是需要的。

    理论要掌握,实操不能落!以上关于《Go gRPC,未定义:RegisterChatServiceServer》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!

    本文内容来源于互联网,如有侵权请联系删除。

    相关文章

    更多

    热门专题

    更多
    首页