亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

gRPC GO 單一通用服務處理程序

gRPC GO 單一通用服務處理程序

Go
慕斯709654 2022-07-25 11:24:05
我使用protoreflect編寫了 http-to-gRPC-gateway。但是我沒有看到任何簡單的選項來做相反的事情 - gRPC-to-HTTP-gateway。其中一個想法是將 GRPC 暴露給外部,但使用自定義網關通過 MessageQ 調用內部微服務以避免負載平衡、發現服務,并且可以將 GRPC 流式轉換為持久或使用反射來調用方法而不是使用生成的服務器存根以下是我到目前為止得到的func main() {    lis, err := net.Listen("tcp", ":9091")    grpcServer := grpc.NewServer(grpc.UnknownServiceHandler(GenericHandler))    //pb.RegisterGreeterServer(grpcServer, &TestService{})    grpcServer.Serve(lis)}func GenericHandler(srv interface{}, stream grpc.ServerStream) error {    fullMethodName, ok := grpc.MethodFromServerStream(stream)    log.Printf("Method: %v, %v\n", fullMethodName, ok)    //how to get protobuf payload from stream    //Response:=Invoke Method via Reflection or http or MessageQueue - passing either the raw protocal buffer or as json    //how to return Response    return nil}我看到實現這一點的唯一方法是理解并重新實現實際的處理程序processUnaryRPC
查看完整描述

1 回答

?
叮當貓咪

TA貢獻1776條經驗 獲得超12個贊

得到它的工作,感謝protoreflect


沒有錯誤處理的工作樣本


//Parse protofile, create grpc.ServiceDesc, register

func (s *GRPCService) LoadSpec(protoFileName string) {

    p := protoparse.Parser{}

    fdlist, _ := p.ParseFiles(protoFileName)

    for _, fd := range fdlist {

        for _, rsd := range fd.GetServices() {

            s.sdMap[rsd.GetName()] = rsd

            gsd := grpc.ServiceDesc{ServiceName: rsd.GetName(), HandlerType: (*interface{})(nil)}

            for _, m := range rsd.GetMethods() {

                gsd.Methods = append(gsd.Methods, grpc.MethodDesc{MethodName: m.GetName(), Handler: s.Handler})

            }

            s.grpcServer.RegisterService(&gsd, s)

        }

    }

}


func (s *GRPCService) Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {

    stream := grpc.ServerTransportStreamFromContext(ctx)

    arr := strings.Split(stream.Method(), "/")

    serviceName := arr[1]

    methodName := arr[2]

    service := s.sdMap[serviceName]

    method := service.FindMethodByName(methodName)

    input := dynamic.NewMessage(method.GetInputType())


    dec(input)

    jsonInput, err := input.MarshalJSON()

    log.Printf("Input:%s Err:%v \n", jsonInput, err)

    //jsonOutput:=invokeServiceViaReflectionOrHttp(jsonInput)

    jsonOutput := `{"message":"response"}`


    output := dynamic.NewMessage(method.GetOutputType())

    output.UnmarshalJSON([]byte(jsonOutput))

    return output, nil

}


查看完整回答
反對 回復 2022-07-25
  • 1 回答
  • 0 關注
  • 124 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號