Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gf/g: The grpc service of the framework lacks service reflection #3952

Open
wisonlau opened this issue Nov 20, 2024 · 2 comments
Open

gf/g: The grpc service of the framework lacks service reflection #3952

wisonlau opened this issue Nov 20, 2024 · 2 comments
Labels

Comments

@wisonlau
Copy link

wisonlau commented Nov 20, 2024

What do you want to ask?

谷歌官方提供了grpc服务反射proto文件,可以用来把所有接口服务都列出来,而不需要客户端用proto文件去请求自己的服务.这在开发调试的时候可以节约大量的时间,以及能非常方便让k8s监控服务接口.
以下是官方的proto文件:
https://github.com/grpc/grpc-proto/blob/master/grpc/reflection/v1/reflection.proto

不知道框架有没隐藏实现了

@gqcn
Copy link
Member

gqcn commented Nov 22, 2024

No, we did not hide the service list from grpc service. The server of package grpcx is a raw grpc server indeed, it does no extra hidden work.

@wisonlau
Copy link
Author

wisonlau commented Nov 27, 2024

package grpc_reflection_v1

import (
	"context"
	"errors"
	"io"

	"google.golang.org/grpc"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
)

// ServerReflectionServer 实现了 ServerReflection 服务。
type ServerReflectionServer struct {
	grpc.UnimplementedServerReflectionServer
}

// ServerReflectionInfo 实现了双向流式RPC。
func (s *ServerReflectionServer) ServerReflectionInfo(stream ServerReflection_ServerReflectionInfoServer) error {
	for {
		req, err := stream.Recv()
		if err == io.EOF {
			return nil
		}
		if err != nil {
			return status.Errorf(codes.Internal, "failed to receive request: %v", err)
		}

		resp, err := s.handleRequest(req)
		if err != nil {
			return err
		}

		if err := stream.Send(resp); err != nil {
			return status.Errorf(codes.Unknown, "failed to send response: %v", err)
		}
	}
}

// handleRequest 处理接收到的请求并生成响应。
func (s *ServerReflectionServer) handleRequest(req *ServerReflectionRequest) (*ServerReflectionResponse, error) {
	switch {
	case req.FileByFilename != "":
		// 处理通过文件名查找proto文件的请求
	case req.FileContainingSymbol != "":
		// 处理通过完全限定符号名查找proto文件的请求
	case req.FileContainingExtension != nil:
		// 处理通过扩展类型和字段号查找proto文件的请求
	case req.AllExtensionNumbersOfType != "":
		// 处理查找给定消息类型所有已知扩展的请求
	case req.ListServices != "":
		// 处理列出所有注册服务的请求
	default:
		return nil, status.Errorf(codes.InvalidArgument, "unknown request type")
	}

	// 根据请求类型生成相应的响应
	return &ServerReflectionResponse{}, nil
}

// 注册 ServerReflection 服务到 gRPC 服务器。
func RegisterServerReflectionServer(s *grpc.Server, srv ServerReflectionServer) {
	grpc.RegisterService(&ServerReflection_ServiceDesc, srv)
}

// ServerReflection_ServiceDesc 是 ServerReflection 服务的描述符。
var ServerReflection_ServiceDesc = grpc.ServiceDesc{
	ServiceName: "grpc.reflection.v1.ServerReflection",
	HandlerType: (*ServerReflectionServer)(nil),
	Methods:     []grpc.MethodDesc{},
	Streams: []grpc.StreamDesc{
		{
			StreamName:    "ServerReflectionInfo",
			Handler:       _ServerReflection_ServerReflectionInfo_Handler,
			ServerStreams: true,
			ClientStreams: true,
		},
	},
	Metadata: "grpc_reflection_v1/reflection.proto",
}

按照外面的gin demo,我要在goframe里面要怎么使用.因为goframe根据上面那个proto生成出来的文件报错

image
image

应该是goframe框架未实现双向流

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants