当前位置:网站首页>Use of metadata in golang grpc
Use of metadata in golang grpc
2022-07-05 23:01:00 【sweey_ lff】
gRPC Let's implement remote calls like local calls , For every time RPC in call , There may be some in header Data passed in , And these data can be passed through metadata To pass on .
metadata In order to key-value Storage of data in the form of , among key yes string type ,value yes []string type , That is, a string slice type .metadata bring client and server It can provide the other party with some information about this call , It's like once http Requested RequestHeader and ResponseHeader equally .http in header The life cycle of is a http request , that metadata The life cycle of is once RPC call .
1. go Use in metadata
Project source code path :https://github.com/grpc/grpc-go/tree/master/metadata
Project documentation :https://github.com/grpc/grpc-go/blob/master/Documentation/grpc-metadata.md
The use of go package :"google.golang.org/grpc/metadata"
1) newly build metadata
MD The type is actually map,key yes string,value yes string Type of slice.
type MD map[string][]stringWhen creating, you can create ordinary map Use the same type new Keyword to create :
// The first way
md := metadata.New(map[string]string{"key1": "val1", "key2": "val2"})
// The second way key Case insensitive , Will be uniformly converted to lowercase
md := metadata.Pairs(
"key1", "val1",
"key1", "val1-2", // "key1" will have map value []string{"val1", "val1-2"}
"key2", "val2",
)2) send out metadata
md := metadata.Pairs("key", "val")
// A new one with metadata Of context
ctx := metadata.NewOutgoingContext(context.Background(), md)
// A one-way RPC
response, err := client.SomeRPC(ctx, someRequest)3) receive metadata
func (s *server) SomeRPC(ctx context.Context, in *pb.SomeRequest) (*pb.SomeResponse, err) {
md, ok := metadata.FromIncomingContext(ctx)
// do something with metadata
}2.gRPC Use in metadata
1)proto
syntax = "proto3";
option go_package = "./;proto";
service Greeter {
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
message HelloRequest {
string name = 1;
}
message HelloReply {
string message = 1;
}Execute the command to compile the file , Generate .pb.go file :protoc -I . test.proto --go_out=plugins=grpc:.
2)client
package main
import (
"context"
"fmt"
"go-class/rpc/07metadata/proto"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)
func main() {
conn, err := grpc.Dial(":8083", grpc.WithInsecure())
if err != nil {
panic(err)
}
defer conn.Close()
c := proto.NewGreeterClient(conn)
// write in metadata***********
md := metadata.New(map[string]string{
"name": "lff",
"password": "123456",
})
ctx := metadata.NewOutgoingContext(context.Background(), md)
r, err := c.SayHello(ctx, &proto.HelloRequest{Name: "lff111"})
if err != nil {
panic(err)
}
fmt.Println(r.Message)
}3)server
package main
import (
"context"
"fmt"
"net"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"go-class/rpc/07metadata/proto"
)
type Server struct {}
func (s *Server) SayHello(ctx context.Context, req *proto.HelloRequest) (*proto.HelloReply, error) {
// obtain header*********
md, ok := metadata.FromIncomingContext(ctx)
if ok {
fmt.Println("get metadata error")
}
for key, val := range md {
fmt.Println(key, val)
}
// obtain header Medium name*********
//if nameSlice, ok := md["name"]; ok {
// fmt.Println(nameSlice)
// for i, e := range nameSlice {
// fmt.Println(i, e)
// }
//}
return &proto.HelloReply{
Message: "Hello " + req.Name,
}, nil
}
func main(){
g := grpc.NewServer()
proto.RegisterGreeterServer(g, &Server{})
lis, err := net.Listen("tcp", "127.0.0.1:8083")
if err != nil {
panic("failed to listen:" + err.Error())
}
err = g.Serve(lis)
if err != nil {
panic("failed to start grpc:" + err.Error())
}
}Start... First server, Start up client End , As a result, I saw the printed header

边栏推荐
- Event trigger requirements of the function called by the event trigger
- Negative sampling
- All expansion and collapse of a-tree
- Boring boring
- 我对新中台模型的一些经验思考总结
- LeetCode102. Sequence traversal of binary tree (output by layer and unified output)
- fibonacci search
- Metasploit(msf)利用ms17_010(永恒之蓝)出现Encoding::UndefinedConversionError问题
- Metasploit (MSF) uses MS17_ 010 (eternal blue) encoding:: undefined conversionerror problem
- Finally understand what dynamic planning is
猜你喜欢
![[screen recording] how to record in the OBS area](/img/34/bd06bd74edcdabaf678c8d7385cae9.jpg)
[screen recording] how to record in the OBS area

Metasploit(msf)利用ms17_010(永恒之蓝)出现Encoding::UndefinedConversionError问题

一文搞定JVM的内存结构

Arduino 测量交流电流

Arduino measures AC current

Common model making instructions

Yiwen gets rid of the garbage collector

一文搞定垃圾回收器

2022 R2 mobile pressure vessel filling review simulation examination and R2 mobile pressure vessel filling examination questions

TCC of distributed solutions
随机推荐
Global and Chinese markets of industrial pH meters 2022-2028: Research Report on technology, participants, trends, market size and share
Getting started stm32--gpio (running lantern) (nanny level)
Thinkphp5.1 cross domain problem solving
[speech processing] speech signal denoising and denoising based on Matlab GUI low-pass filter [including Matlab source code 1708]
Nail error code Encyclopedia
Common model making instructions
Leetcode daily question 1189 The maximum number of "balloons" simple simulation questions~
2022 registration examination for safety management personnel of hazardous chemical business units and simulated reexamination examination for safety management personnel of hazardous chemical busines
TCC of distributed solutions
实现反向代理客户端IP透传
Tiktok__ ac_ signature
Global and Chinese markets for welding products 2022-2028: Research Report on technology, participants, trends, market size and share
[digital signal denoising] improved wavelet modulus maxima digital signal denoising based on MATLAB [including Matlab source code 1710]
APK加固技术的演变,APK加固技术和不足之处
Nacos 的安装与服务的注册
How can easycvr cluster deployment solve the massive video access and concurrency requirements in the project?
Methods modified by static
Metasploit (MSF) uses MS17_ 010 (eternal blue) encoding:: undefined conversionerror problem
Matlab smooth curve connection scatter diagram
两数之和、三数之和(排序+双指针)