Skip to content

Instantly share code, notes, and snippets.

@kingluo

kingluo/fix.md Secret

Last active January 22, 2023 13:58
Show Gist options
  • Save kingluo/2a6af0b600cc9804870985458c472350 to your computer and use it in GitHub Desktop.
Save kingluo/2a6af0b600cc9804870985458c472350 to your computer and use it in GitHub Desktop.
Use google.protobuf.Struct in grpc-transcode plugin

Setup APISIX route

cd /opt

git clone https://github.com/grpc/grpc-go

cd /opt/grpc-go/examples/helloworld/helloworld/

git clone https://github.com/protocolbuffers/protobuf

protoc --descriptor_set_out=helloworld.pb --include_imports \
    --proto_path=$PWD --proto_path=$PWD/protobuf/src helloworld.proto

curl http://127.0.0.1:9180/apisix/admin/protos/1 \
-H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
    "content" : "'"$(base64 -w0 helloworld.pb)"'"
}'

curl http://127.0.0.1:9180/apisix/admin/routes/1 \
-H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
   "uri":"/grpctest",
   "plugins":{
      "grpc-transcode":{
         "proto_id":"1",
         "service":"helloworld.Greeter",
         "method":"SayHello"
      }
   },
   "upstream":{
      "scheme":"grpc",
      "type":"roundrobin",
      "nodes":{
         "127.0.0.1:50051":1
      }
   }
}'

Run modified helloworld grpc server

cd /opt/grpc-go/examples/helloworld/helloworld/

protoc --go_out=. --go_opt=paths=source_relative \
    --go-grpc_out=. --go-grpc_opt=paths=source_relative \
    helloworld.proto

cd /opt/grpc-go/examples/greeter_server/helloworld/

go run server.go

Test

curl -X POST -H 'content-type: application/json' \
-v --raw http://127.0.0.1:9080/grpctest -d '
{
    "type":"7",
    "serviceurl" :"xxxx",
    "reqdata" :{
        "fields": {
            "foo": {"string_value":"xxx"},
            "bar": {"number_value":666}
        }
    },
    "source":"3"
}'

grpc-go server logs:

2023/01/22 19:10:40 server listening at [::]:50051
2023/01/22 19:10:42 Received: reqdata:{fields:{key:"bar" value:{number_value:666}} fields:{key:"foo" value:{string_value:"xxx"}}} type:"7" serviceurl:"xxxx" source:"3"
2023/01/22 19:10:42 {"reqdata":{"bar":666,"foo":"xxx"},"type":"7","serviceurl":"xxxx","source":"3"}
// Copyright 2015 gRPC authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
syntax = "proto3";
option go_package = "google.golang.org/grpc/examples/helloworld/helloworld";
option java_multiple_files = true;
option java_package = "io.grpc.examples.helloworld";
option java_outer_classname = "HelloWorldProto";
package helloworld;
import "google/protobuf/struct.proto";
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
google.protobuf.Struct reqdata = 1;
string type = 2;
string serviceurl = 3;
string source = 4;
int32 page = 5;
int32 size = 6;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
/*
*
* Copyright 2015 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
// Package main implements a server for Greeter service.
package main
import (
"context"
"flag"
"fmt"
"log"
"net"
"encoding/json"
"google.golang.org/grpc"
pb "google.golang.org/grpc/examples/helloworld/helloworld"
)
var (
port = flag.Int("port", 50051, "The server port")
)
// server is used to implement helloworld.GreeterServer.
type server struct {
pb.UnimplementedGreeterServer
}
// SayHello implements helloworld.GreeterServer
func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
log.Printf("Received: %+v", in)
b, err := json.Marshal(in)
if err != nil {
fmt.Println("error:", err)
}
log.Println(string(b))
return &pb.HelloReply{Message: "Hello "}, nil
}
func main() {
flag.Parse()
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *port))
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
pb.RegisterGreeterServer(s, &server{})
log.Printf("server listening at %v", lis.Addr())
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment