1.proto
syntax = "proto3";
option go_package = ".;proto";
service Greeter {
rpc SayHello (HelloRequest) returns (HelloReply);
}
//将session放入 放入cookie中 http协议
message HelloRequest {
string name = 1;
}
message HelloReply {
string message = 1;
}
2.server
package main
import (
"OldPackageTest/grpc_interceptor/proto"
"context"
"fmt"
"google.golang.org/grpc"
"net"
)
type Server struct {
}
func (s *Server) SayHello(ctx context.Context, request *proto.HelloRequest) (*proto.HelloReply, error) {
return &proto.HelloReply{
Message: "hello " + request.Name,
}, nil
}
func main() {
var interceptor grpc.UnaryServerInterceptor
interceptor = func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
//继续处理请求
fmt.Println("接收到新请求")
res, err := handler(ctx, req)
fmt.Println("请求处理完成")
return res, err
}
var opts []grpc.ServerOption
opts = append(opts, grpc.UnaryInterceptor(interceptor))
g := grpc.NewServer(opts...)
proto.RegisterGreeterServer(g, &Server{})
lis, err := net.Listen("tcp", "0.0.0.0:50051")
if err != nil {
panic("failed to listen" + err.Error())
}
err = g.Serve(lis)
if err != nil {
panic("failed to start" + err.Error())
}
}
package main
import (
"OldPackageTest/grpc_test/proto"
"context"
"fmt"
"google.golang.org/grpc"
"net"
)
type Server struct {
}
func (s *Server) SayHello(ctx context.Context, request *proto.HelloRequest) (*proto.HelloReply, error) {
return &proto.HelloReply{
Message: "hello " + request.Name,
}, nil
}
func main() {
interceptor := func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
fmt.Println("接收到了一个新的请求")
res,err := handler(ctx, req)
fmt.Println("请求已经完成")
return res,err
}
opt := grpc.UnaryInterceptor(interceptor)
g := grpc.NewServer(opt)
proto.RegisterGreeterServer(g, &Server{})
lis, err := net.Listen("tcp", "0.0.0.0:8080")
if err != nil {
panic("failed to listen" + err.Error())
}
err = g.Serve(lis)
if err != nil {
panic("failed to start" + err.Error())
}
}
3.client
package main
import (
"OldPackageTest/grpc_interceptor/proto"
"context"
"fmt"
"google.golang.org/grpc"
"time"
)
func interceptor(ctx context.Context,method string,req,reply interface{},cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error{
start := time.Now()
err := invoker(ctx, method, req, reply, cc, opts...)
if err != nil {
return err
}
fmt.Printf("耗时: %s\n",time.Since(start))
return err
}
func main() {
//stream
var opts []grpc.DialOption
opts = append(opts,grpc.WithInsecure())
//指定客户端 interceptor
opts = append(opts,grpc.WithUnaryInterceptor(interceptor))
conn,err := grpc.Dial("localhost:50051",opts...)
if err != nil{
panic(err)
}
defer conn.Close()
c := proto.NewGreeterClient(conn)
r, err := c.SayHello(context.Background(), &proto.HelloRequest{Name: "Anwma"})
if err != nil {
panic(err)
}
fmt.Println(r.Message)
}
package main
import (
"OldPackageTest/grpc_test/proto"
"context"
"fmt"
"google.golang.org/grpc"
"time"
)
func main() {
//stream
interceptor := func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error{
start := time.Now()
err := invoker(ctx, method, req, reply, cc, opts...)
if err != nil {
return err
}
fmt.Printf("耗时: %s\n",time.Since(start))
return err
}
var opts []grpc.DialOption
opts = append(opts,grpc.WithInsecure())
opts = append(opts,grpc.WithUnaryInterceptor(interceptor))
//opt := grpc.WithUnaryInterceptor(interceptor)
//conn, err := grpc.Dial("127.0.0.1:8080", grpc.WithInsecure(),opt)
conn, err := grpc.Dial("127.0.0.1:8080", opts...)
if err != nil {
panic(err)
}
defer conn.Close()
c := proto.NewGreeterClient(conn)
r, err := c.SayHello(context.Background(), &proto.HelloRequest{Name: "Anwma"})
if err != nil {
panic(err)
}
fmt.Println(r.Message)
}
4. 拦截器的应用场景