第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

gRPC GO 單通用服務(wù)處理程序

gRPC GO 單通用服務(wù)處理程序

Go
哆啦的時光機(jī) 2022-08-01 11:05:23
我使用protoreflect來編寫http-to-gRPC-gateway。但我沒有看到任何簡單的選擇來做相反的事情 - gRPC-to-HTTP-gateway。其中一個想法是向外部公開GRPC,但使用自定義網(wǎng)關(guān)通過MessageQ調(diào)用內(nèi)部微服務(wù),以避免負(fù)載平衡,發(fā)現(xiàn)服務(wù)并且可以將GRPC流轉(zhuǎn)換為持久或使用反射來調(diào)用方法,而不是使用生成的服務(wù)器存根以下是我到目前為止得到的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}我看到實現(xiàn)這一目標(biāo)的唯一方法是理解并重新實現(xiàn)實際的處理程序過程UnaryRPC
查看完整描述

1 回答

?
慕蓋茨4494581

TA貢獻(xiàn)1850條經(jīng)驗 獲得超11個贊

讓它工作,多虧了原型反射


無錯誤處理的工作樣本


//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

}


查看完整回答
反對 回復(fù) 2022-08-01
  • 1 回答
  • 0 關(guān)注
  • 199 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號