How is the API in grpc implemented?

王林
Release: 2024-02-15 16:40:09
forward
576 people have browsed it

How is the API in grpc implemented?

With the help of PHP editor Yuzai, let’s explore how the API in grpc is implemented. gRPC is a high-performance, open source remote procedure call (RPC) framework that uses Google's Protocol Buffers as the interface description language and supports multiple programming languages. The core mechanism of gRPC is based on the HTTP/2 protocol, which implements communication between the client and the server by serializing and deserializing messages. In this article, we'll take a closer look at how gRPC works, how messages are passed, and how to use it to build powerful distributed applications. let's start!

Question content

I used the official document https://grpc.io/docs/languages/go/basics/, but after implementation, a problem occurred. When I create the tcp server I have to specify the host and port (in my case mcrsrv-book:7561). But what if I want to implement another api for grpc? Do I need to start another server on a new port (e.g. mcrsrv-book:7562)? How are routing and APIs implemented in grpc?

My server code is:

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) }
Copy after login

I think there should be other options besides opening separate ports for each grpc service.

How is the API in grpc implemented?

Workaround

If you want to use the same address for a different service, just re-register the other service before starting the grpc server.

grpcServer := grpc.NewServer(opts...) pb.RegisterRouteGuideServer(grpcServer, newServer()) #register other server here with the same 'grpcServer' grpcServer.Serve(lis)
Copy after login

This stackoverflow thread may help you as an example of what you want to achieve. The question provides a sample code which I believe matches your requirements.

Access multiple grpc services through the same connection

The above is the detailed content of How is the API in grpc implemented?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!