API在gRPC中是如何实现的?

huangapple go评论93阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年2月5日 23:50:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75353548.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定