当前位置: 首页 > 知识库问答 >
问题:

Grpc Golang使用UnimplementedServer和其他嵌入式接口

张银龙
2023-03-14

我最近用一个现有的proto 3代码库更新了最新的protoc和Go插件,但在新的未实现的服务器功能上遇到了麻烦。用于Grpc服务器的结构已经嵌入了另一个接口,该接口描述了此服务实现的方法。在将UnimplementedServer引用嵌入到我的结构中之后,我从编译器那里得到了一个不明确的错误,它告诉我我不再实现我的服务方法了。我构建代码的方式是否存在一些问题?要使用libprotoc 3.17.3、protoc gen go v1.27.1和protoc gen go grpc 1.1.0复制的代码如下:api/proto/core\u服务。原型:


package testbed;

option go_package = "internal/proto";

service Core {
    rpc CreateThing(Thing) returns (ThingResponse) {};
}

message Thing {
    string name = 1;
}

message ThingResponse {
    int64 result = 1;
}

内部/核心。转到:


import (
    "context"

    "testbed.org/demo/internal/proto"
)

type CoreApi interface {
    CreateThing(ctx context.Context, t *proto.Thing) (*proto.ThingResponse, error)
}

内部/core_endpoints.go:


import (
    "context"

    "testbed.org/demo/internal/proto"
)

type CoreEndpoints struct {}

func (ce CoreEndpoints) CreateThing(_ context.Context, _ *proto.Thing) (*proto.ThingResponse, error) {
    return nil, nil
}

命令/服务。转到:


import (
    //"context"
    . "testbed.org/demo/internal"
    "testbed.org/demo/internal/proto"
    "google.golang.org/grpc"
)

type MyServer struct {
    CoreApi
    proto.UnimplementedCoreServer
}

func main() {
    mySvr := &MyServer{CoreApi: &CoreEndpoints{}}
    //_, _ = mySvr.CoreApi.CreateThing(context.Background(), &proto.Thing{})
    grpcSvr := grpc.NewServer()
    proto.RegisterCoreServer(grpcSvr, mySvr)
}

构建:

protoc -I api/proto/ api/proto/*.proto --go_out=. --go-grpc_out=.
go build -o bin/svc cmd/service.go
cmd/service.go:19:26: MyServer.CreateThing is ambiguous
cmd/service.go:19:26: cannot use mySvr (type *MyServer) as type "testbed.org/demo/internal/proto".CoreServer in argument to "testbed.org/demo/internal/proto".RegisterCoreServer:
    *MyServer does not implement "testbed.org/demo/internal/proto".CoreServer (missing CreateThing method)
gmake: *** [Makefile:8: service] Error 2

共有1个答案

朱和惬
2023-03-14

正如日志所说,“Myserver”没有实现coreserver接口。

type MyServer struct {
    endpoint CoreEndpoints
    proto.UnimplementedCoreServer
}

func (srv MyServer) CreateThing(ctx context.Context, in *proto.Thing)(*proto.ThingResponse, error) {
    return srv.endpoint.CreateThing(ctx,in)
}

 类似资料:
  • 问题内容: 我可以将H2,HSQLDB或任何其他嵌入式数据库与文件而不是文件中的数据库一起使用吗? 我打算在Android 上使用,它可以返回随机访问模式。 问题答案: H2支持可插入文件系统,该系统允许您访问zip或jar文件中的只读数据库。但是,当前没有文件系统实现。实施起来应该相对容易。最好的起点可能是FileSystemZip和FileObjectZip。

  • 其它平台接入 1.支付宝小程序 兑吧的积分商城为HTML5页面,需要通过小程序的< web-view />组件来加载。 组件使用条件 1.基础库 1.6.0 开始支持,低版本需做兼容 2.每个页面只能有一个,请不要渲染多个,webview会自动铺满整个页面,并覆盖其它组件。(具体请参考 小程序官方组件文档 )。 添加业务域名 web-view加载H5页面需要添加业务域名白名单 1.需要添加的业务域

  • 问题内容: 我正在做一个小实验,尝试为嵌套div替换背景颜色。 这是我打算实现的(没有内联样式): 我觉得我一定缺少明显的东西!我尝试过,但这似乎仅适用于一个级别。 这是针对生成div的实验,因此解决方案需要是无止境的(不是沿着div div div div div = white的界线)。我知道使用JavaScript非常简单,只需 寻找纯CSS解决方案即可 。 问题答案: 正如Lister先生

  • Interfaces and other types 接口与其它类型 接口 Interfaces in Go provide a way to specify the behavior of an object: if something can do this, then it can be used here. We’ve seen a couple of simple examples al

  • 5.11. 接口和其他类型 5.11.1. 接口 Go中的接口提供了一类对象的抽象。我们在前面已经看到了关于接口的一些例子。 我们可以给新定义的对象实现一个String方法,这样就可以用 Fprintf输出该类型的值。同样,Fprintf可以将 结果输出到任意实现了Write方法的对象。接口一般只包含一类方法, 并且以ed后缀的方式命名,例如io.Writer接口对应Write 方法实现。 一种类

  • 我正在尝试在一个新的Spring Boot应用程序中使用WebFlux反应类型。我在https://start.spring.io上使用了initializr,并选择了2.0.0快照版本。我添加了web reactive dependency,我所做的一切工作都很好。这是一个非常可靠的POC,目标是如何利用这些类型来更新我们的API,为了做到这一点,我们计划缓慢地替换阻塞和/或同步进程的每个部分,