英文:
How is the api implemented in 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 // 初始化后只读
}
// GetFeature返回给定点的特征。
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)
}
我认为除了为每个grpc服务打开一个单独的端口之外,还应该有其他选项。
在grpc中如何实现API?
英文:
I used the official documentation https://grpc.io/docs/languages/go/basics/, but after implementation, questions arose.
When I create a TCP server I have to specify 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 is routing and api 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)
}
I think there should be options other than opening a separate port for each grpc service.
How is the api implemented in grpc?
答案1
得分: 2
如果您想在不同的服务中使用相同的地址,您可以在启动grpc服务器之前重新注册其他服务。
grpcServer := grpc.NewServer(opts...)
pb.RegisterRouteGuideServer(grpcServer, newServer())
// 在这里注册其他服务,使用相同的 'grpcServer'
grpcServer.Serve(lis)
这个stackoverflow的帖子可能会作为一个示例帮助您实现您想要的功能。该问题提供了一个示例代码,我认为与您的需求相符。
https://stackoverflow.com/questions/52634217/access-multiple-grpc-services-over-the-same-connection
英文:
If you want to use the same address for a different service, you can just re-register the other service before initiating the grpc server.
grpcServer := grpc.NewServer(opts...)
pb.RegisterRouteGuideServer(grpcServer, newServer())
#register other server here with the same 'grpcServer'
grpcServer.Serve(lis)
This stackoverflow thread would probably help as an example of what you want to achieve. The question provided a sample code that I believe align with what you asked.
https://stackoverflow.com/questions/52634217/access-multiple-grpc-services-over-the-same-connection
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论