知识卡片

结构图卡 · 11500.d · 生成模型:未记录

gRPC生成代码类图

理解等级:中级

生成的服务端接口定义了业务应实现的签名,注册函数把实现交给 grpc.Server 分派。生成的客户端返回 HelloServiceClient 接口,背后是持有 ClientConn 的 helloServiceClient。图中虚线三角表示 Go 隐式实现,依赖箭头表示需要连接或注册依赖。附录手写服务端示例的响应名不一致,这里以生成的 HelloResponse 签名为准。

结构图:

classDiagram
class HelloServiceServer {
  <<interface>>
  +SayHello(context.Context, *HelloRequest) (*HelloResponse, error)
}
class HelloServiceClient {
  <<interface>>
  +SayHello(context.Context ctx, *HelloRequest in, ...grpc.CallOption opts) (*HelloResponse, error)
}
class helloServiceClient {
  -ClientConn cc
  +SayHello(context.Context ctx, *HelloRequest in, ...grpc.CallOption opts) (*HelloResponse, error)
}
class ClientConn
class Server
class RegisterHelloServiceServer {
  <<函数 非类型>>
  +RegisterHelloServiceServer(*grpc.Server s, HelloServiceServer srv)
}
class NewHelloServiceClient {
  <<构造函数 非类型>>
  +NewHelloServiceClient(*grpc.ClientConn cc) HelloServiceClient
}
HelloServiceClient <|.. helloServiceClient : 隐式实现
helloServiceClient --> ClientConn : 持有
RegisterHelloServiceServer ..> HelloServiceServer : 接收实现
RegisterHelloServiceServer ..> Server : 注册
NewHelloServiceClient ..> ClientConn : 接收连接
NewHelloServiceClient ..> helloServiceClient : 构造
这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    参考来源

    - 位置:《区块链原理、设计与应用》附录D《ProtoBuf与gRPC》(来源项目 book-source,`extracted/区块链原理、设计与应用/_epub-src/OEBPS/Text/chapter131.xhtml`,正文块 39) - 结论依据:原文给出客户端接口、实现、构造和连接;前一部分给出服务端接口、注册函数和旧版代码示例。 - 原始内容:type HelloServiceClient interface { SayHello(ctx context.Context, in *HelloRequest, opts ...grpc.CallOption) (