grpc中的api是如何实现的?

grpc中的api是如何实现的?

在php小编鱼仔的帮助下,我们来探究一下grpc中的api是如何实现的。gRPC是一个高性能、开源的远程过程调用(RPC)框架,它使用了Google的Protocol Buffers作为接口描述语言,并支持多种编程语言。gRPC的核心机制是基于HTTP/2协议,通过序列化和反序列化消息来实现客户端和服务器之间的通信。在本文中,我们将深入了解gRPC的工作原理、消息传递方式以及如何使用它来构建强大的分布式应用程序。让我们开始吧!

问题内容

我使用了官方文档https://grpc.io/docs/languages/go/basics/,但是实现后,出现了问题。 当我创建 tcp 服务器时,我必须指定主机和端口(在我的例子中为 mcrsrv-book:7561)。 但是如果我想为 grpc 实现另一个 api 该怎么办?我是否需要在新端口上启动另一台服务器(例如 mcrsrv-book:7562)? grpc中的路由和api是如何实现的?

我的服务器代码是:

type routeGuideServer struct { pb.UnimplementedRouteGuideServer savedFeatures []*pb.Response // read-only after initialized } // GetFeature returns the feature at the given point. func (s *routeGuideServer) GetFeature(ctx context.Context, request *pb.Request) (*pb.Response, error) { context := localContext.LocalContext{} book := bookRepository.FindOrFailBook(context, int(request.BookId)) return &pb.Response{ Name: book.Name, BookId: int32(book.BookId), AuthorId: int32(book.AuthorId), Category: book.Category, Description: "Описание", }, nil } func newServer() *routeGuideServer { s := &routeGuideServer{} return s } func SomeAction() { lis, err := net.Listen("tcp", fmt.Sprintf("mcrsrv-book:7561")) if err != nil { log.Fatalf("failed to listen: %v", err) } var opts []grpc.ServerOption grpcServer := grpc.NewServer(opts...) pb.RegisterRouteGuideServer(grpcServer, newServer()) grpcServer.Serve(lis) }登录后复制