You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
109 lines
2.6 KiB
109 lines
2.6 KiB
package main |
|
|
|
import ( |
|
"context" |
|
"fmt" |
|
"sig-pub/api/pb" |
|
"sig-pub/pkg/grpc/session" |
|
|
|
"github.com/bytedance/sonic" |
|
"github.com/jhump/protoreflect/v2/grpcdynamic" |
|
"github.com/jhump/protoreflect/v2/grpcreflect" |
|
"google.golang.org/grpc" |
|
"google.golang.org/protobuf/encoding/protojson" |
|
"google.golang.org/protobuf/proto" |
|
"google.golang.org/protobuf/types/dynamicpb" |
|
|
|
"google.golang.org/grpc/credentials/insecure" |
|
refv1 "google.golang.org/grpc/reflection/grpc_reflection_v1" |
|
) |
|
|
|
func main() { |
|
testGeneric() |
|
} |
|
|
|
func testGeneric() { |
|
// gin: json body -> io.ReadAll(c.Request.Body) |
|
|
|
// conf := config.MustLoadConfig(new(config.Configuration), "config/config.toml") |
|
|
|
// etcdClient, err := clientv3.New(conf.Etcd) |
|
// if err != nil { |
|
// panic(err) |
|
// } |
|
|
|
// dis := discovery.NewEtcdDiscovery(etcdClient) |
|
// resolver, err := dis.Resolver() |
|
// if err != nil { |
|
// panic(err) |
|
// } |
|
|
|
// url := fmt.Sprintf("%s:///%s", discovery.EtcdSchema, "Market") |
|
|
|
cli, err := grpc.NewClient("127.0.0.1:8001", |
|
// grpc.WithResolvers(resolver), |
|
grpc.WithTransportCredentials(insecure.NewCredentials()), |
|
) |
|
if err != nil { |
|
panic(err) |
|
} |
|
ctx := context.Background() |
|
// refClient := grpcreflect.NewClientV1Alpha(ctx, cli) |
|
client := grpcreflect.NewClientV1(ctx, refv1.NewServerReflectionClient(cli)) |
|
services, err := client.ListServices() |
|
if err != nil { |
|
panic(err) |
|
} else { |
|
for _, svr := range services { |
|
fmt.Println(svr.Name(), svr.Parent(), svr) |
|
} |
|
} |
|
|
|
marketServiceSymbol, err := client.FileContainingSymbol("Market") |
|
if err != nil { |
|
panic(err) |
|
} |
|
marketService := marketServiceSymbol.Services().ByName("Market") |
|
|
|
fmt.Println(marketServiceSymbol.Services().Len()) |
|
|
|
method := marketService.Methods().ByName("GetTradeInstance") |
|
|
|
req := &pb.ReqGetTradeInstance{InstId: "BTC_USDT"} |
|
reqBytes, err := proto.Marshal(req) |
|
if err != nil { |
|
panic(err) |
|
} |
|
_ = reqBytes |
|
reqJsonBytes, err := sonic.Marshal(req) |
|
if err != nil { |
|
panic(err) |
|
} |
|
_ = reqJsonBytes |
|
|
|
msgDesc := method.Input() |
|
msg := dynamicpb.NewMessage(msgDesc) |
|
// msg.Set(msgDesc.Fields().ByJSONName("instId"), protoreflect.ValueOf("DOGE-USDT")) |
|
|
|
// if err := proto.Unmarshal(reqBytes, msg); err != nil { |
|
// panic(err) |
|
// } |
|
|
|
if err := protojson.Unmarshal(reqJsonBytes, msg); err != nil { |
|
panic(err) |
|
} |
|
|
|
stub := grpcdynamic.NewStub(cli) |
|
|
|
ctx = session.PutSubject(ctx, session.NewRpcSubject("123456")) |
|
r, err := stub.InvokeRpc(ctx, method, msg) |
|
if err != nil { |
|
panic(err) |
|
} |
|
rr, ok := r.(*dynamicpb.Message) |
|
fmt.Println(ok, rr) |
|
fmt.Printf("resp: %T, %#v\n", r, r) |
|
bytes, err := sonic.Marshal(r) |
|
|
|
fmt.Printf("resp: %s, err=%v\n", string(bytes), err) |
|
}
|
|
|