metadata
proto 文件
syntax = "proto3";
package stream.v1;
option go_package = "goGinT/rpc/stream/v1;streamProto";
message StreamReqData {
string data = 1;
}
message StreamResData {
string data = 1;
}
service Greeter {
rpc GetStream(StreamReqData) returns (StreamResData);
}
server
package main
import (
"context"
"fmt"
metadataProto "goGinT/rpc/metadata/proto"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"net"
)
type server struct {
}
func (receiver *server) GetStream(ctx context.Context, req *metadataProto.StreamReqData) (*metadataProto.StreamResData, error) {
md, _ := metadata.FromIncomingContext(ctx)
for key, vale := range md {
fmt.Println(key, vale)
}
return &metadataProto.StreamResData{Data: "111"}, nil
}
func main() {
lis, err := net.Listen("tcp", ":8088")
if err != nil {
fmt.Println(err)
return
}
s := grpc.NewServer()
metadataProto.RegisterGreeterServer(s, &server{})
err = s.Serve(lis)
if err != nil {
fmt.Println(err)
return
}
}
client
package main
import (
"context"
"fmt"
metadataProto "goGinT/rpc/metadata/proto"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/metadata"
)
func getStream(c metadataProto.GreeterClient) {
md := metadata.New(map[string]string{
"name": "21313",
"sex": "sdasdasda",
})
ctx := metadata.NewOutgoingContext(context.Background(), md)
res, err := c.GetStream(ctx, &metadataProto.StreamReqData{
Data: "唐诗",
})
if err != nil {
fmt.Println(err)
return
}
fmt.Println(res)
}
func main() {
dial, err := grpc.Dial("localhost:8088", grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
fmt.Println(err)
return
}
defer func(dial *grpc.ClientConn) {
err := dial.Close()
if err != nil {
}
}(dial)
c := metadataProto.NewGreeterClient(dial)
getStream(c)
}