1.proto

  1. syntax = "proto3";
  2. option go_package = ".;proto";
  3. service Greeter {
  4. rpc SayHello (HelloRequest) returns (HelloReply);
  5. }
  6. //将session放入 放入cookie中 http协议
  7. message HelloRequest {
  8. string name = 1;
  9. }
  10. message HelloReply {
  11. string message = 1;
  12. }

2.server

  1. package main
  2. import (
  3. "OldPackageTest/grpc_interceptor/proto"
  4. "context"
  5. "fmt"
  6. "google.golang.org/grpc"
  7. "net"
  8. )
  9. type Server struct {
  10. }
  11. func (s *Server) SayHello(ctx context.Context, request *proto.HelloRequest) (*proto.HelloReply, error) {
  12. return &proto.HelloReply{
  13. Message: "hello " + request.Name,
  14. }, nil
  15. }
  16. func main() {
  17. var interceptor grpc.UnaryServerInterceptor
  18. interceptor = func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
  19. //继续处理请求
  20. fmt.Println("接收到新请求")
  21. res, err := handler(ctx, req)
  22. fmt.Println("请求处理完成")
  23. return res, err
  24. }
  25. var opts []grpc.ServerOption
  26. opts = append(opts, grpc.UnaryInterceptor(interceptor))
  27. g := grpc.NewServer(opts...)
  28. proto.RegisterGreeterServer(g, &Server{})
  29. lis, err := net.Listen("tcp", "0.0.0.0:50051")
  30. if err != nil {
  31. panic("failed to listen" + err.Error())
  32. }
  33. err = g.Serve(lis)
  34. if err != nil {
  35. panic("failed to start" + err.Error())
  36. }
  37. }
  1. package main
  2. import (
  3. "OldPackageTest/grpc_test/proto"
  4. "context"
  5. "fmt"
  6. "google.golang.org/grpc"
  7. "net"
  8. )
  9. type Server struct {
  10. }
  11. func (s *Server) SayHello(ctx context.Context, request *proto.HelloRequest) (*proto.HelloReply, error) {
  12. return &proto.HelloReply{
  13. Message: "hello " + request.Name,
  14. }, nil
  15. }
  16. func main() {
  17. interceptor := func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
  18. fmt.Println("接收到了一个新的请求")
  19. res,err := handler(ctx, req)
  20. fmt.Println("请求已经完成")
  21. return res,err
  22. }
  23. opt := grpc.UnaryInterceptor(interceptor)
  24. g := grpc.NewServer(opt)
  25. proto.RegisterGreeterServer(g, &Server{})
  26. lis, err := net.Listen("tcp", "0.0.0.0:8080")
  27. if err != nil {
  28. panic("failed to listen" + err.Error())
  29. }
  30. err = g.Serve(lis)
  31. if err != nil {
  32. panic("failed to start" + err.Error())
  33. }
  34. }

3.client

  1. package main
  2. import (
  3. "OldPackageTest/grpc_interceptor/proto"
  4. "context"
  5. "fmt"
  6. "google.golang.org/grpc"
  7. "time"
  8. )
  9. func interceptor(ctx context.Context,method string,req,reply interface{},cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error{
  10. start := time.Now()
  11. err := invoker(ctx, method, req, reply, cc, opts...)
  12. if err != nil {
  13. return err
  14. }
  15. fmt.Printf("耗时: %s\n",time.Since(start))
  16. return err
  17. }
  18. func main() {
  19. //stream
  20. var opts []grpc.DialOption
  21. opts = append(opts,grpc.WithInsecure())
  22. //指定客户端 interceptor
  23. opts = append(opts,grpc.WithUnaryInterceptor(interceptor))
  24. conn,err := grpc.Dial("localhost:50051",opts...)
  25. if err != nil{
  26. panic(err)
  27. }
  28. defer conn.Close()
  29. c := proto.NewGreeterClient(conn)
  30. r, err := c.SayHello(context.Background(), &proto.HelloRequest{Name: "Anwma"})
  31. if err != nil {
  32. panic(err)
  33. }
  34. fmt.Println(r.Message)
  35. }
  1. package main
  2. import (
  3. "OldPackageTest/grpc_test/proto"
  4. "context"
  5. "fmt"
  6. "google.golang.org/grpc"
  7. "time"
  8. )
  9. func main() {
  10. //stream
  11. interceptor := func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error{
  12. start := time.Now()
  13. err := invoker(ctx, method, req, reply, cc, opts...)
  14. if err != nil {
  15. return err
  16. }
  17. fmt.Printf("耗时: %s\n",time.Since(start))
  18. return err
  19. }
  20. var opts []grpc.DialOption
  21. opts = append(opts,grpc.WithInsecure())
  22. opts = append(opts,grpc.WithUnaryInterceptor(interceptor))
  23. //opt := grpc.WithUnaryInterceptor(interceptor)
  24. //conn, err := grpc.Dial("127.0.0.1:8080", grpc.WithInsecure(),opt)
  25. conn, err := grpc.Dial("127.0.0.1:8080", opts...)
  26. if err != nil {
  27. panic(err)
  28. }
  29. defer conn.Close()
  30. c := proto.NewGreeterClient(conn)
  31. r, err := c.SayHello(context.Background(), &proto.HelloRequest{Name: "Anwma"})
  32. if err != nil {
  33. panic(err)
  34. }
  35. fmt.Println(r.Message)
  36. }

4. 拦截器的应用场景