• grpc的metadata---拦截器--验证器--状态码-yapi安装


    gRPC让我们可以像本地调用一样实现远程调用,对于每一次的RPC调用中,都可能会有一些在header中传递的数据,而这些数据就可以通过metadata来传递。
    metadata是以key-value的形式存储数据的,其中key是string类型,value是[]string类型,即一个字符串切片类型。metadata使得client和server能够为对方提供关于本次调用的一些信息,就像一次http请求的RequestHeader和ResponseHeader一样。http中header的生命周期是一次http请求,那么metadata的生命周期就是一次RPC调用。
    metadata机制:源数据可以做权限验证

    1. go中使用metadata

    项目源代码路径:https://github.com/grpc/grpc-go/tree/master/metadata

    项目文档:https://github.com/grpc/grpc-go/blob/master/Documentation/grpc-metadata.md

    使用的go包:“google.golang.org/grpc/metadata”

    新建methdata

    MD 类型实际上是map,key是string,value是string类型的slice。

    type MD map[string][]string
    
    • 1

    创建的时候可以像创建普通的map类型一样使用new关键字进行创建:

    //第一种方式
    md := metadata.New(map[string]string{"key1": "val1", "key2": "val2"})
     
    //第二种方式 key不区分大小写,会被统一转成小写
    md := metadata.Pairs(
        "key1", "val1",
        "key1", "val1-2", // "key1" will have map value []string{"val1", "val1-2"}
        "key2", "val2",
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    2)发送metadata

    md := metadata.Pairs("key", "val")
     
    // 新建一个有 metadata 的 context
    ctx := metadata.NewOutgoingContext(context.Background(), md)
     
    // 单向 RPC
    response, err := client.SomeRPC(ctx, someRequest)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    3)接收metadata

    func (s *server) SomeRPC(ctx context.Context, in *pb.SomeRequest) (*pb.SomeResponse, err) {
        md, ok := metadata.FromIncomingContext(ctx)
        // do something with metadata
        }
    
    • 1
    • 2
    • 3
    • 4

    client.go

    package main
    
    import (
    	"awesomeProject123/grpc_test/proto"
    	"context"
    	"fmt"
    	"google.golang.org/grpc"
    	"google.golang.org/grpc/metadata"
    )
    
    func main() {
    	//建立服务器连接
    	conn, err := grpc.Dial("127.0.0.1:8080", grpc.WithInsecure())
    
    	if err != nil {
    		panic(err)
    	}
    
    	defer conn.Close()
    
    	//调用protobuf的函数客户端
    	client := proto.NewGreeterClient(conn)
    
    	//md := metadata.Pairs("timestamp", time.Now().Format())
    	md := metadata.New(map[string]string{
    		"name":     "aaads",
    		"password": "asdasd",
    		//"name":     "好好学习",
    		//"password": "天天向上", 中文无法传过去
    	})
    
    	//新建一个有 metadata 的 context
    	ctx := metadata.NewOutgoingContext(context.Background(), md)
    
    	//调用sayHello
    	sayHello, err := client.SayHello(ctx, &proto.HelloRequest{
    		Name: "bobby",
    	})
    	if err != nil {
    		return
    	}
    
    	fmt.Println(sayHello.Message)
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    server.go

    package main
    
    import (
    	"awesomeProject123/grpc_test/proto"
    	"context"
    	"fmt"
    	"google.golang.org/grpc"
    	"google.golang.org/grpc/metadata"
    	"net"
    )
    
    type Server struct {
    	*proto.UnimplementedGreeterServer
    }
    
    func (s *Server) mustEmbedUnimplementedGreeterServer() {
    	//TODO implement me
    	panic("implement me")
    }
    
    // SayHello ctx context.Context协程
    func (s *Server) SayHello(ctx context.Context, request *proto.HelloRequest) (*proto.HelloReply, error) {
    
    	md, ok := metadata.FromIncomingContext(ctx)
    
    	if ok !=true{
    		fmt.Println("get metadata error")
    	}
    
    	if nameSlice, ok := md["name"]; ok {
    		fmt.Println(nameSlice)
    		for i, e := range nameSlice {
    			fmt.Println(i, e)
    		}
    	}
    
    	for key, val := range md {
    		fmt.Println(key, val)
    	}
    
    	return &proto.HelloReply{
    		Message: "hello" + request.Name,
    	}, nil
    }
    
    func main() {
    	//new服务
    	grpc1 := grpc.NewServer()
    
    	//注册
    	proto.RegisterGreeterServer(grpc1, &Server{})
    	//监听
    	listen, err := net.Listen("tcp", "0.0.0.0:8080")
    	if err != nil {
    		panic("failed  to listen:" + err.Error())
    	}
    
    	//启动 不会关闭
    	err = grpc1.Serve(listen)
    	if err != nil {
    		panic("failed  to start:" + err.Error())
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63

    proto

    syntax = "proto3";
    option go_package = ".;proto";
    service Greeter {
      rpc SayHello (HelloRequest) returns (HelloReply);
    }
    //将 sessionid放入 放入cookie中 http协议
    message HelloRequest {
      string name = 1;
    }
    
    message HelloReply {
      string message = 1;
    }
    
    //go语言中是生成一个文件, 也就只有python会生成两个文件
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    grpc拦截器-go语言–简单使用–一元拦截器

    拦截器功能:所有接口都具有的功能==>1 访问时长,用户登录情况,反扒策略,作为预处理
    拦截器应用场景

    https://github.com/grpc-ecosystem/go-grpc-middleware
    
    • 1

    server.go

    package main
    
    import (
    	"awesomeProject123/grpc_test/proto"
    	"context"
    	"fmt"
    	"google.golang.org/grpc"
    	"net"
    )
    
    type Server struct {
    	*proto.UnimplementedGreeterServer
    }
    
    func (s *Server) mustEmbedUnimplementedGreeterServer() {
    	//TODO implement me
    	panic("implement me")
    }
    
    // SayHello ctx context.Context协程
    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 any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp any, err error) {
    		fmt.Println("接收到了一个新的请求")
    		return handler(ctx, req)
    	}
    	//主要对于一元方法的拦截器
    	option := grpc.UnaryInterceptor(interceptor)
    	//new服务
    	grpc1 := grpc.NewServer(option)
    
    	//注册
    	proto.RegisterGreeterServer(grpc1, &Server{})
    	//监听
    	listen, err := net.Listen("tcp", "0.0.0.0:8080")
    	if err != nil {
    		panic("failed  to listen:" + err.Error())
    	}
    
    	//启动 不会关闭
    	err = grpc1.Serve(listen)
    	if err != nil {
    		panic("failed  to start:" + err.Error())
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51

    client.go

    package main
    
    import (
    	"awesomeProject123/grpc_test/proto"
    	"context"
    	"fmt"
    	"google.golang.org/grpc"
    	"time"
    )
    
    func main() {
    //拦截器
    	interceptor := func(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
    		start := time.Now()
    		err := invoker(ctx, method, req, reply, cc, opts...)
    		fmt.Printf("耗时:%s\n", time.Since(start))
    		return err
    	}
    
    	var opts []grpc.DialOption
    	opts = append(opts, grpc.WithInsecure())
    	opts = append(opts, grpc.WithUnaryInterceptor(interceptor))
    
    	//option := grpc.WithUnaryInterceptor(interceptor)
    
    	//建立服务器连接
    	//conn, err := grpc.Dial("127.0.0.1:8080", grpc.WithInsecure())
    	conn, err := grpc.Dial("127.0.0.1:8080", opts...)
    	if err != nil {
    		panic(err)
    	}
    
    	defer conn.Close()
    
    	//调用protobuf的函数传进客户端
    	client := proto.NewGreeterClient(conn)
    
    	//调用sayHello
    	sayHello, err := client.SayHello(context.Background(), &proto.HelloRequest{
    		Name: "bobby",
    	})
    	if err != nil {
    		return
    	}
    
    	fmt.Println(sayHello.Message)
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    通过拦截器和metadata实现grpc的auth认证(用户登录)–token认证

    1:数据是否适合携带到message中?
    2:你在携带的时候,每次调用都携带这两个信息吗?
    不侵入之前的业务逻辑,可以同时使用拦截器和grpc功能

    client.go–简单的使用

    package main
    
    import (
    	"awesomeProject123/grpc+token_auth_test/proto"
    	"context"
    	"fmt"
    	"google.golang.org/grpc"
    	"google.golang.org/grpc/metadata"
    	"time"
    )
    
    func main() {
    
    	interceptor := func(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
    		start := time.Now()
    
    		md := metadata.New(map[string]string{
    			"appid":  "101010",
    			"appkey": "I am key",
    		})
    		ctx = metadata.NewOutgoingContext(context.Background(), md)
    		//fmt.Println(opts)
    		err := invoker(ctx, method, req, reply, cc, opts...)
    		fmt.Printf("耗时:%s\n", time.Since(start))
    		return err
    	}
    
    	var opts []grpc.DialOption
    	opts = append(opts, grpc.WithInsecure())
    	opts = append(opts, grpc.WithUnaryInterceptor(interceptor))
    
    	//option := grpc.WithUnaryInterceptor(interceptor)
    
    	//建立服务器连接
    	//conn, err := grpc.Dial("127.0.0.1:8080", grpc.WithInsecure())
    	conn, err := grpc.Dial("127.0.0.1:8080", opts...)
    	if err != nil {
    		panic(err)
    	}
    
    	defer conn.Close()
    
    	//调用protobuf的函数传进客户端
    	client := proto.NewGreeterClient(conn)
    
    	//调用sayHello
    	sayHello, err := client.SayHello(context.Background(), &proto.HelloRequest{
    		Name: "bobby",
    	})
    	if err != nil {
    		return
    	}
    
    	fmt.Println(sayHello.Message)
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55

    server.go–简单的使用

    package main
    
    import (
    	"awesomeProject123/grpc+token_auth_test/proto"
    	"context"
    	"fmt"
    	"google.golang.org/grpc"
    	"google.golang.org/grpc/codes"
    	"google.golang.org/grpc/metadata"
    	"google.golang.org/grpc/status"
    	"net"
    )
    
    type Server struct {
    	*proto.UnimplementedGreeterServer
    }
    
    func (s *Server) mustEmbedUnimplementedGreeterServer() {
    	//TODO implement me
    	panic("implement me")
    }
    
    // SayHello ctx context.Context协程
    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 any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp any, err error) {
    		fmt.Println("接收到了一个新的请求")
    		md, ok := metadata.FromIncomingContext(ctx)
    
    		fmt.Println("aa", ok)
    		fmt.Println(md)
    		if ok != true {
    			//已经开始接触到grpc的错误的处理
    			fmt.Println("get metadata error")
    			return resp, status.Error(codes.Unauthenticated, "无token认证信息")
    		}
    
    		var (
    			appid  string
    			appkey string
    		)
    
    		if va1, ok := md["appid"]; ok {
    			fmt.Println(va1)
    			appid = va1[0]
    		}
    
    		if va1, ok := md["appkey"]; ok {
    			fmt.Println(va1)
    			appkey = va1[0]
    		}
    
    		if appid != "101010" || appkey != "I am key" {
    			return resp, status.Error(codes.Unauthenticated, "无token认证信息")
    		}
    
    		return handler(ctx, req)
    	}
    	//主要对于一元方法的拦截器
    	option := grpc.UnaryInterceptor(interceptor)
    	//new服务
    	grpc1 := grpc.NewServer(option)
    
    	//注册
    	proto.RegisterGreeterServer(grpc1, &Server{})
    	//监听
    	listen, err := net.Listen("tcp", "0.0.0.0:8080")
    	if err != nil {
    		panic("failed  to listen:" + err.Error())
    	}
    
    	//启动 不会关闭
    	err = grpc1.Serve(listen)
    	if err != nil {
    		panic("failed  to start:" + err.Error())
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82

    更简单的方法client.go

    package main
    
    import (
    	"awesomeProject123/grpc+token_auth_test/proto"
    	"context"
    	"fmt"
    	"google.golang.org/grpc"
    )
    
    type customCredential struct {
    }
    
    func (c customCredential) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) {
    
    	return map[string]string{
    		"appid":  "101010",
    		"appkey": "I am key",
    	}, nil
    }
    
    func (c customCredential) RequireTransportSecurity() bool {
    	return false
    }
    func main() {
    	var opts []grpc.DialOption
    	opts = append(opts, grpc.WithInsecure())
    	opts = append(opts, grpc.WithPerRPCCredentials(customCredential{}))
    
    	//option := grpc.WithUnaryInterceptor(interceptor)
    
    	//建立服务器连接
    	//conn, err := grpc.Dial("127.0.0.1:8080", grpc.WithInsecure())
    	conn, err := grpc.Dial("127.0.0.1:8080", opts...)
    	if err != nil {
    		panic(err)
    	}
    
    	defer conn.Close()
    
    	//调用protobuf的函数传进客户端
    	client := proto.NewGreeterClient(conn)
    
    	//调用sayHello
    	sayHello, err := client.SayHello(context.Background(), &proto.HelloRequest{
    		Name: "bobby",
    	})
    	if err != nil {
    		return
    	}
    
    	fmt.Println(sayHello.Message)
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52

    grpc的验证器

    https://github.com/bufbuild/protoc-gen-validate
    
    • 1

    windows安装

    https://oss.sonatype.org/content/repositories/snapshots/io/envoyproxy/protoc-gen-validate/protoc-gen-validate/0.4.2-SNAPSHOT/
    
    • 1

    需要新建validata.proto文件

    https://github.com/envoyproxy/protoc-gen-validate/blob/master/validate/validate.proto
    
    • 1

    使用命令

     protoc --go_out=. --go-grpc_out=. --validate_out="lang=go:." .\helloworld.proto 
    
    • 1

    validata.proto如下

    syntax = "proto2";
    package validate;
    
    option go_package = "github.com/envoyproxy/protoc-gen-validate/validate";
    option java_package = "io.envoyproxy.pgv.validate";
    
    import "google/protobuf/descriptor.proto";
    import "google/protobuf/duration.proto";
    import "google/protobuf/timestamp.proto";
    
    // Validation rules applied at the message level
    extend google.protobuf.MessageOptions {
      // Disabled nullifies any validation rules for this message, including any
      // message fields associated with it that do support validation.
      optional bool disabled = 1071;
    }
    
    // Validation rules applied at the oneof level
    extend google.protobuf.OneofOptions {
      // Required ensures that exactly one the field options in a oneof is set;
      // validation fails if no fields in the oneof are set.
      optional bool required = 1071;
    }
    
    // Validation rules applied at the field level
    extend google.protobuf.FieldOptions {
      // Rules specify the validations to be performed on this field. By default,
      // no validation is performed against a field.
      optional FieldRules rules = 1071;
    }
    
    // FieldRules encapsulates the rules for each type of field. Depending on the
    // field, the correct set should be used to ensure proper validations.
    message FieldRules {
      optional MessageRules message = 17;
      oneof type {
        // Scalar Field Types
        FloatRules    float    = 1;
        DoubleRules   double   = 2;
        Int32Rules    int32    = 3;
        Int64Rules    int64    = 4;
        UInt32Rules   uint32   = 5;
        UInt64Rules   uint64   = 6;
        SInt32Rules   sint32   = 7;
        SInt64Rules   sint64   = 8;
        Fixed32Rules  fixed32  = 9;
        Fixed64Rules  fixed64  = 10;
        SFixed32Rules sfixed32 = 11;
        SFixed64Rules sfixed64 = 12;
        BoolRules     bool     = 13;
        StringRules   string   = 14;
        BytesRules    bytes    = 15;
    
        // Complex Field Types
        EnumRules     enum     = 16;
        RepeatedRules repeated = 18;
        MapRules      map      = 19;
    
        // Well-Known Field Types
        AnyRules       any       = 20;
        DurationRules  duration  = 21;
        TimestampRules timestamp = 22;
      }
    }
    
    // FloatRules describes the constraints applied to `float` values
    message FloatRules {
      // Const specifies that this field must be exactly the specified value
      optional float const = 1;
    
      // Lt specifies that this field must be less than the specified value,
      // exclusive
      optional float lt = 2;
    
      // Lte specifies that this field must be less than or equal to the
      // specified value, inclusive
      optional float lte = 3;
    
      // Gt specifies that this field must be greater than the specified value,
      // exclusive. If the value of Gt is larger than a specified Lt or Lte, the
      // range is reversed.
      optional float gt = 4;
    
      // Gte specifies that this field must be greater than or equal to the
      // specified value, inclusive. If the value of Gte is larger than a
      // specified Lt or Lte, the range is reversed.
      optional float gte = 5;
    
      // In specifies that this field must be equal to one of the specified
      // values
      repeated float in = 6;
    
      // NotIn specifies that this field cannot be equal to one of the specified
      // values
      repeated float not_in = 7;
    }
    
    // DoubleRules describes the constraints applied to `double` values
    message DoubleRules {
      // Const specifies that this field must be exactly the specified value
      optional double const = 1;
    
      // Lt specifies that this field must be less than the specified value,
      // exclusive
      optional double lt = 2;
    
      // Lte specifies that this field must be less than or equal to the
      // specified value, inclusive
      optional double lte = 3;
    
      // Gt specifies that this field must be greater than the specified value,
      // exclusive. If the value of Gt is larger than a specified Lt or Lte, the
      // range is reversed.
      optional double gt = 4;
    
      // Gte specifies that this field must be greater than or equal to the
      // specified value, inclusive. If the value of Gte is larger than a
      // specified Lt or Lte, the range is reversed.
      optional double gte = 5;
    
      // In specifies that this field must be equal to one of the specified
      // values
      repeated double in = 6;
    
      // NotIn specifies that this field cannot be equal to one of the specified
      // values
      repeated double not_in = 7;
    }
    
    // Int32Rules describes the constraints applied to `int32` values
    message Int32Rules {
      // Const specifies that this field must be exactly the specified value
      optional int32 const = 1;
    
      // Lt specifies that this field must be less than the specified value,
      // exclusive
      optional int32 lt = 2;
    
      // Lte specifies that this field must be less than or equal to the
      // specified value, inclusive
      optional int32 lte = 3;
    
      // Gt specifies that this field must be greater than the specified value,
      // exclusive. If the value of Gt is larger than a specified Lt or Lte, the
      // range is reversed.
      optional int32 gt = 4;
    
      // Gte specifies that this field must be greater than or equal to the
      // specified value, inclusive. If the value of Gte is larger than a
      // specified Lt or Lte, the range is reversed.
      optional int32 gte = 5;
    
      // In specifies that this field must be equal to one of the specified
      // values
      repeated int32 in = 6;
    
      // NotIn specifies that this field cannot be equal to one of the specified
      // values
      repeated int32 not_in = 7;
    }
    
    // Int64Rules describes the constraints applied to `int64` values
    message Int64Rules {
      // Const specifies that this field must be exactly the specified value
      optional int64 const = 1;
    
      // Lt specifies that this field must be less than the specified value,
      // exclusive
      optional int64 lt = 2;
    
      // Lte specifies that this field must be less than or equal to the
      // specified value, inclusive
      optional int64 lte = 3;
    
      // Gt specifies that this field must be greater than the specified value,
      // exclusive. If the value of Gt is larger than a specified Lt or Lte, the
      // range is reversed.
      optional int64 gt = 4;
    
      // Gte specifies that this field must be greater than or equal to the
      // specified value, inclusive. If the value of Gte is larger than a
      // specified Lt or Lte, the range is reversed.
      optional int64 gte = 5;
    
      // In specifies that this field must be equal to one of the specified
      // values
      repeated int64 in = 6;
    
      // NotIn specifies that this field cannot be equal to one of the specified
      // values
      repeated int64 not_in = 7;
    }
    
    // UInt32Rules describes the constraints applied to `uint32` values
    message UInt32Rules {
      // Const specifies that this field must be exactly the specified value
      optional uint32 const = 1;
    
      // Lt specifies that this field must be less than the specified value,
      // exclusive
      optional uint32 lt = 2;
    
      // Lte specifies that this field must be less than or equal to the
      // specified value, inclusive
      optional uint32 lte = 3;
    
      // Gt specifies that this field must be greater than the specified value,
      // exclusive. If the value of Gt is larger than a specified Lt or Lte, the
      // range is reversed.
      optional uint32 gt = 4;
    
      // Gte specifies that this field must be greater than or equal to the
      // specified value, inclusive. If the value of Gte is larger than a
      // specified Lt or Lte, the range is reversed.
      optional uint32 gte = 5;
    
      // In specifies that this field must be equal to one of the specified
      // values
      repeated uint32 in = 6;
    
      // NotIn specifies that this field cannot be equal to one of the specified
      // values
      repeated uint32 not_in = 7;
    }
    
    // UInt64Rules describes the constraints applied to `uint64` values
    message UInt64Rules {
      // Const specifies that this field must be exactly the specified value
      optional uint64 const = 1;
    
      // Lt specifies that this field must be less than the specified value,
      // exclusive
      optional uint64 lt = 2;
    
      // Lte specifies that this field must be less than or equal to the
      // specified value, inclusive
      optional uint64 lte = 3;
    
      // Gt specifies that this field must be greater than the specified value,
      // exclusive. If the value of Gt is larger than a specified Lt or Lte, the
      // range is reversed.
      optional uint64 gt = 4;
    
      // Gte specifies that this field must be greater than or equal to the
      // specified value, inclusive. If the value of Gte is larger than a
      // specified Lt or Lte, the range is reversed.
      optional uint64 gte = 5;
    
      // In specifies that this field must be equal to one of the specified
      // values
      repeated uint64 in = 6;
    
      // NotIn specifies that this field cannot be equal to one of the specified
      // values
      repeated uint64 not_in = 7;
    }
    
    // SInt32Rules describes the constraints applied to `sint32` values
    message SInt32Rules {
      // Const specifies that this field must be exactly the specified value
      optional sint32 const = 1;
    
      // Lt specifies that this field must be less than the specified value,
      // exclusive
      optional sint32 lt = 2;
    
      // Lte specifies that this field must be less than or equal to the
      // specified value, inclusive
      optional sint32 lte = 3;
    
      // Gt specifies that this field must be greater than the specified value,
      // exclusive. If the value of Gt is larger than a specified Lt or Lte, the
      // range is reversed.
      optional sint32 gt = 4;
    
      // Gte specifies that this field must be greater than or equal to the
      // specified value, inclusive. If the value of Gte is larger than a
      // specified Lt or Lte, the range is reversed.
      optional sint32 gte = 5;
    
      // In specifies that this field must be equal to one of the specified
      // values
      repeated sint32 in = 6;
    
      // NotIn specifies that this field cannot be equal to one of the specified
      // values
      repeated sint32 not_in = 7;
    }
    
    // SInt64Rules describes the constraints applied to `sint64` values
    message SInt64Rules {
      // Const specifies that this field must be exactly the specified value
      optional sint64 const = 1;
    
      // Lt specifies that this field must be less than the specified value,
      // exclusive
      optional sint64 lt = 2;
    
      // Lte specifies that this field must be less than or equal to the
      // specified value, inclusive
      optional sint64 lte = 3;
    
      // Gt specifies that this field must be greater than the specified value,
      // exclusive. If the value of Gt is larger than a specified Lt or Lte, the
      // range is reversed.
      optional sint64 gt = 4;
    
      // Gte specifies that this field must be greater than or equal to the
      // specified value, inclusive. If the value of Gte is larger than a
      // specified Lt or Lte, the range is reversed.
      optional sint64 gte = 5;
    
      // In specifies that this field must be equal to one of the specified
      // values
      repeated sint64 in = 6;
    
      // NotIn specifies that this field cannot be equal to one of the specified
      // values
      repeated sint64 not_in = 7;
    }
    
    // Fixed32Rules describes the constraints applied to `fixed32` values
    message Fixed32Rules {
      // Const specifies that this field must be exactly the specified value
      optional fixed32 const = 1;
    
      // Lt specifies that this field must be less than the specified value,
      // exclusive
      optional fixed32 lt = 2;
    
      // Lte specifies that this field must be less than or equal to the
      // specified value, inclusive
      optional fixed32 lte = 3;
    
      // Gt specifies that this field must be greater than the specified value,
      // exclusive. If the value of Gt is larger than a specified Lt or Lte, the
      // range is reversed.
      optional fixed32 gt = 4;
    
      // Gte specifies that this field must be greater than or equal to the
      // specified value, inclusive. If the value of Gte is larger than a
      // specified Lt or Lte, the range is reversed.
      optional fixed32 gte = 5;
    
      // In specifies that this field must be equal to one of the specified
      // values
      repeated fixed32 in = 6;
    
      // NotIn specifies that this field cannot be equal to one of the specified
      // values
      repeated fixed32 not_in = 7;
    }
    
    // Fixed64Rules describes the constraints applied to `fixed64` values
    message Fixed64Rules {
      // Const specifies that this field must be exactly the specified value
      optional fixed64 const = 1;
    
      // Lt specifies that this field must be less than the specified value,
      // exclusive
      optional fixed64 lt = 2;
    
      // Lte specifies that this field must be less than or equal to the
      // specified value, inclusive
      optional fixed64 lte = 3;
    
      // Gt specifies that this field must be greater than the specified value,
      // exclusive. If the value of Gt is larger than a specified Lt or Lte, the
      // range is reversed.
      optional fixed64 gt = 4;
    
      // Gte specifies that this field must be greater than or equal to the
      // specified value, inclusive. If the value of Gte is larger than a
      // specified Lt or Lte, the range is reversed.
      optional fixed64 gte = 5;
    
      // In specifies that this field must be equal to one of the specified
      // values
      repeated fixed64 in = 6;
    
      // NotIn specifies that this field cannot be equal to one of the specified
      // values
      repeated fixed64 not_in = 7;
    }
    
    // SFixed32Rules describes the constraints applied to `sfixed32` values
    message SFixed32Rules {
      // Const specifies that this field must be exactly the specified value
      optional sfixed32 const = 1;
    
      // Lt specifies that this field must be less than the specified value,
      // exclusive
      optional sfixed32 lt = 2;
    
      // Lte specifies that this field must be less than or equal to the
      // specified value, inclusive
      optional sfixed32 lte = 3;
    
      // Gt specifies that this field must be greater than the specified value,
      // exclusive. If the value of Gt is larger than a specified Lt or Lte, the
      // range is reversed.
      optional sfixed32 gt = 4;
    
      // Gte specifies that this field must be greater than or equal to the
      // specified value, inclusive. If the value of Gte is larger than a
      // specified Lt or Lte, the range is reversed.
      optional sfixed32 gte = 5;
    
      // In specifies that this field must be equal to one of the specified
      // values
      repeated sfixed32 in = 6;
    
      // NotIn specifies that this field cannot be equal to one of the specified
      // values
      repeated sfixed32 not_in = 7;
    }
    
    // SFixed64Rules describes the constraints applied to `sfixed64` values
    message SFixed64Rules {
      // Const specifies that this field must be exactly the specified value
      optional sfixed64 const = 1;
    
      // Lt specifies that this field must be less than the specified value,
      // exclusive
      optional sfixed64 lt = 2;
    
      // Lte specifies that this field must be less than or equal to the
      // specified value, inclusive
      optional sfixed64 lte = 3;
    
      // Gt specifies that this field must be greater than the specified value,
      // exclusive. If the value of Gt is larger than a specified Lt or Lte, the
      // range is reversed.
      optional sfixed64 gt = 4;
    
      // Gte specifies that this field must be greater than or equal to the
      // specified value, inclusive. If the value of Gte is larger than a
      // specified Lt or Lte, the range is reversed.
      optional sfixed64 gte = 5;
    
      // In specifies that this field must be equal to one of the specified
      // values
      repeated sfixed64 in = 6;
    
      // NotIn specifies that this field cannot be equal to one of the specified
      // values
      repeated sfixed64 not_in = 7;
    }
    
    // BoolRules describes the constraints applied to `bool` values
    message BoolRules {
      // Const specifies that this field must be exactly the specified value
      optional bool const = 1;
    }
    
    // StringRules describe the constraints applied to `string` values
    message StringRules {
      // Const specifies that this field must be exactly the specified value
      optional string const = 1;
    
      // Len specifies that this field must be the specified number of
      // characters (Unicode code points). Note that the number of
      // characters may differ from the number of bytes in the string.
      optional uint64 len = 19;
    
      // MinLen specifies that this field must be the specified number of
      // characters (Unicode code points) at a minimum. Note that the number of
      // characters may differ from the number of bytes in the string.
      optional uint64 min_len = 2;
    
      // MaxLen specifies that this field must be the specified number of
      // characters (Unicode code points) at a maximum. Note that the number of
      // characters may differ from the number of bytes in the string.
      optional uint64 max_len = 3;
    
      // LenBytes specifies that this field must be the specified number of bytes
      // at a minimum
      optional uint64 len_bytes = 20;
    
      // MinBytes specifies that this field must be the specified number of bytes
      // at a minimum
      optional uint64 min_bytes = 4;
    
      // MaxBytes specifies that this field must be the specified number of bytes
      // at a maximum
      optional uint64 max_bytes = 5;
    
      // Pattern specifes that this field must match against the specified
      // regular expression (RE2 syntax). The included expression should elide
      // any delimiters.
      optional string pattern  = 6;
    
      // Prefix specifies that this field must have the specified substring at
      // the beginning of the string.
      optional string prefix   = 7;
    
      // Suffix specifies that this field must have the specified substring at
      // the end of the string.
      optional string suffix   = 8;
    
      // Contains specifies that this field must have the specified substring
      // anywhere in the string.
      optional string contains = 9;
    
      // NotContains specifies that this field cannot have the specified substring
      // anywhere in the string.
      optional string not_contains = 23;
    
      // In specifies that this field must be equal to one of the specified
      // values
      repeated string in     = 10;
    
      // NotIn specifies that this field cannot be equal to one of the specified
      // values
      repeated string not_in = 11;
    
      // WellKnown rules provide advanced constraints against common string
      // patterns
      oneof well_known {
        // Email specifies that the field must be a valid email address as
        // defined by RFC 5322
        bool email    = 12;
    
        // Hostname specifies that the field must be a valid hostname as
        // defined by RFC 1034. This constraint does not support
        // internationalized domain names (IDNs).
        bool hostname = 13;
    
        // Ip specifies that the field must be a valid IP (v4 or v6) address.
        // Valid IPv6 addresses should not include surrounding square brackets.
        bool ip       = 14;
    
        // Ipv4 specifies that the field must be a valid IPv4 address.
        bool ipv4     = 15;
    
        // Ipv6 specifies that the field must be a valid IPv6 address. Valid
        // IPv6 addresses should not include surrounding square brackets.
        bool ipv6     = 16;
    
        // Uri specifies that the field must be a valid, absolute URI as defined
        // by RFC 3986
        bool uri      = 17;
    
        // UriRef specifies that the field must be a valid URI as defined by RFC
        // 3986 and may be relative or absolute.
        bool uri_ref  = 18;
    
        // Address specifies that the field must be either a valid hostname as
        // defined by RFC 1034 (which does not support internationalized domain
        // names or IDNs), or it can be a valid IP (v4 or v6).
        bool address  = 21;
    
        // Uuid specifies that the field must be a valid UUID as defined by
        // RFC 4122
        bool uuid     = 22;
    
        // WellKnownRegex specifies a common well known pattern defined as a regex.
        KnownRegex well_known_regex = 24;
      }
    
      // This applies to regexes HTTP_HEADER_NAME and HTTP_HEADER_VALUE to enable
      // strict header validation.
      // By default, this is true, and HTTP header validations are RFC-compliant.
      // Setting to false will enable a looser validations that only disallows
      // \r\n\0 characters, which can be used to bypass header matching rules.
      optional bool strict = 25 [default = true];
    }
    
    // WellKnownRegex contain some well-known patterns.
    enum KnownRegex {
      UNKNOWN = 0;
    
      // HTTP header name as defined by RFC 7230.
      HTTP_HEADER_NAME = 1;
    
      // HTTP header value as defined by RFC 7230.
      HTTP_HEADER_VALUE = 2;
    }
    
    // BytesRules describe the constraints applied to `bytes` values
    message BytesRules {
      // Const specifies that this field must be exactly the specified value
      optional bytes const = 1;
    
      // Len specifies that this field must be the specified number of bytes
      optional uint64 len = 13;
    
      // MinLen specifies that this field must be the specified number of bytes
      // at a minimum
      optional uint64 min_len = 2;
    
      // MaxLen specifies that this field must be the specified number of bytes
      // at a maximum
      optional uint64 max_len = 3;
    
      // Pattern specifes that this field must match against the specified
      // regular expression (RE2 syntax). The included expression should elide
      // any delimiters.
      optional string pattern  = 4;
    
      // Prefix specifies that this field must have the specified bytes at the
      // beginning of the string.
      optional bytes  prefix   = 5;
    
      // Suffix specifies that this field must have the specified bytes at the
      // end of the string.
      optional bytes  suffix   = 6;
    
      // Contains specifies that this field must have the specified bytes
      // anywhere in the string.
      optional bytes  contains = 7;
    
      // In specifies that this field must be equal to one of the specified
      // values
      repeated bytes in     = 8;
    
      // NotIn specifies that this field cannot be equal to one of the specified
      // values
      repeated bytes not_in = 9;
    
      // WellKnown rules provide advanced constraints against common byte
      // patterns
      oneof well_known {
        // Ip specifies that the field must be a valid IP (v4 or v6) address in
        // byte format
        bool ip   = 10;
    
        // Ipv4 specifies that the field must be a valid IPv4 address in byte
        // format
        bool ipv4 = 11;
    
        // Ipv6 specifies that the field must be a valid IPv6 address in byte
        // format
        bool ipv6 = 12;
      }
    }
    
    // EnumRules describe the constraints applied to enum values
    message EnumRules {
      // Const specifies that this field must be exactly the specified value
      optional int32 const        = 1;
    
      // DefinedOnly specifies that this field must be only one of the defined
      // values for this enum, failing on any undefined value.
      optional bool  defined_only = 2;
    
      // In specifies that this field must be equal to one of the specified
      // values
      repeated int32 in           = 3;
    
      // NotIn specifies that this field cannot be equal to one of the specified
      // values
      repeated int32 not_in       = 4;
    }
    
    // MessageRules describe the constraints applied to embedded message values.
    // For message-type fields, validation is performed recursively.
    message MessageRules {
      // Skip specifies that the validation rules of this field should not be
      // evaluated
      optional bool skip     = 1;
    
      // Required specifies that this field must be set
      optional bool required = 2;
    }
    
    // RepeatedRules describe the constraints applied to `repeated` values
    message RepeatedRules {
      // MinItems specifies that this field must have the specified number of
      // items at a minimum
      optional uint64 min_items = 1;
    
      // MaxItems specifies that this field must have the specified number of
      // items at a maximum
      optional uint64 max_items = 2;
    
      // Unique specifies that all elements in this field must be unique. This
      // contraint is only applicable to scalar and enum types (messages are not
      // supported).
      optional bool   unique    = 3;
    
      // Items specifies the contraints to be applied to each item in the field.
      // Repeated message fields will still execute validation against each item
      // unless skip is specified here.
      optional FieldRules items = 4;
    }
    
    // MapRules describe the constraints applied to `map` values
    message MapRules {
      // MinPairs specifies that this field must have the specified number of
      // KVs at a minimum
      optional uint64 min_pairs = 1;
    
      // MaxPairs specifies that this field must have the specified number of
      // KVs at a maximum
      optional uint64 max_pairs = 2;
    
      // NoSparse specifies values in this field cannot be unset. This only
      // applies to map's with message value types.
      optional bool no_sparse = 3;
    
      // Keys specifies the constraints to be applied to each key in the field.
      optional FieldRules keys   = 4;
    
      // Values specifies the constraints to be applied to the value of each key
      // in the field. Message values will still have their validations evaluated
      // unless skip is specified here.
      optional FieldRules values = 5;
    }
    
    // AnyRules describe constraints applied exclusively to the
    // `google.protobuf.Any` well-known type
    message AnyRules {
      // Required specifies that this field must be set
      optional bool required = 1;
    
      // In specifies that this field's `type_url` must be equal to one of the
      // specified values.
      repeated string in     = 2;
    
      // NotIn specifies that this field's `type_url` must not be equal to any of
      // the specified values.
      repeated string not_in = 3;
    }
    
    // DurationRules describe the constraints applied exclusively to the
    // `google.protobuf.Duration` well-known type
    message DurationRules {
      // Required specifies that this field must be set
      optional bool required = 1;
    
      // Const specifies that this field must be exactly the specified value
      optional google.protobuf.Duration const = 2;
    
      // Lt specifies that this field must be less than the specified value,
      // exclusive
      optional google.protobuf.Duration lt = 3;
    
      // Lt specifies that this field must be less than the specified value,
      // inclusive
      optional google.protobuf.Duration lte = 4;
    
      // Gt specifies that this field must be greater than the specified value,
      // exclusive
      optional google.protobuf.Duration gt = 5;
    
      // Gte specifies that this field must be greater than the specified value,
      // inclusive
      optional google.protobuf.Duration gte = 6;
    
      // In specifies that this field must be equal to one of the specified
      // values
      repeated google.protobuf.Duration in = 7;
    
      // NotIn specifies that this field cannot be equal to one of the specified
      // values
      repeated google.protobuf.Duration not_in = 8;
    }
    
    // TimestampRules describe the constraints applied exclusively to the
    // `google.protobuf.Timestamp` well-known type
    message TimestampRules {
      // Required specifies that this field must be set
      optional bool required = 1;
    
      // Const specifies that this field must be exactly the specified value
      optional google.protobuf.Timestamp const = 2;
    
      // Lt specifies that this field must be less than the specified value,
      // exclusive
      optional google.protobuf.Timestamp lt = 3;
    
      // Lte specifies that this field must be less than the specified value,
      // inclusive
      optional google.protobuf.Timestamp lte = 4;
    
      // Gt specifies that this field must be greater than the specified value,
      // exclusive
      optional google.protobuf.Timestamp gt = 5;
    
      // Gte specifies that this field must be greater than the specified value,
      // inclusive
      optional google.protobuf.Timestamp gte = 6;
    
      // LtNow specifies that this must be less than the current time. LtNow
      // can only be used with the Within rule.
      optional bool lt_now  = 7;
    
      // GtNow specifies that this must be greater than the current time. GtNow
      // can only be used with the Within rule.
      optional bool gt_now  = 8;
    
      // Within specifies that this field must be within this duration of the
      // current time. This constraint can be used alone or with the LtNow and
      // GtNow rules.
      optional google.protobuf.Duration within = 9;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341
    • 342
    • 343
    • 344
    • 345
    • 346
    • 347
    • 348
    • 349
    • 350
    • 351
    • 352
    • 353
    • 354
    • 355
    • 356
    • 357
    • 358
    • 359
    • 360
    • 361
    • 362
    • 363
    • 364
    • 365
    • 366
    • 367
    • 368
    • 369
    • 370
    • 371
    • 372
    • 373
    • 374
    • 375
    • 376
    • 377
    • 378
    • 379
    • 380
    • 381
    • 382
    • 383
    • 384
    • 385
    • 386
    • 387
    • 388
    • 389
    • 390
    • 391
    • 392
    • 393
    • 394
    • 395
    • 396
    • 397
    • 398
    • 399
    • 400
    • 401
    • 402
    • 403
    • 404
    • 405
    • 406
    • 407
    • 408
    • 409
    • 410
    • 411
    • 412
    • 413
    • 414
    • 415
    • 416
    • 417
    • 418
    • 419
    • 420
    • 421
    • 422
    • 423
    • 424
    • 425
    • 426
    • 427
    • 428
    • 429
    • 430
    • 431
    • 432
    • 433
    • 434
    • 435
    • 436
    • 437
    • 438
    • 439
    • 440
    • 441
    • 442
    • 443
    • 444
    • 445
    • 446
    • 447
    • 448
    • 449
    • 450
    • 451
    • 452
    • 453
    • 454
    • 455
    • 456
    • 457
    • 458
    • 459
    • 460
    • 461
    • 462
    • 463
    • 464
    • 465
    • 466
    • 467
    • 468
    • 469
    • 470
    • 471
    • 472
    • 473
    • 474
    • 475
    • 476
    • 477
    • 478
    • 479
    • 480
    • 481
    • 482
    • 483
    • 484
    • 485
    • 486
    • 487
    • 488
    • 489
    • 490
    • 491
    • 492
    • 493
    • 494
    • 495
    • 496
    • 497
    • 498
    • 499
    • 500
    • 501
    • 502
    • 503
    • 504
    • 505
    • 506
    • 507
    • 508
    • 509
    • 510
    • 511
    • 512
    • 513
    • 514
    • 515
    • 516
    • 517
    • 518
    • 519
    • 520
    • 521
    • 522
    • 523
    • 524
    • 525
    • 526
    • 527
    • 528
    • 529
    • 530
    • 531
    • 532
    • 533
    • 534
    • 535
    • 536
    • 537
    • 538
    • 539
    • 540
    • 541
    • 542
    • 543
    • 544
    • 545
    • 546
    • 547
    • 548
    • 549
    • 550
    • 551
    • 552
    • 553
    • 554
    • 555
    • 556
    • 557
    • 558
    • 559
    • 560
    • 561
    • 562
    • 563
    • 564
    • 565
    • 566
    • 567
    • 568
    • 569
    • 570
    • 571
    • 572
    • 573
    • 574
    • 575
    • 576
    • 577
    • 578
    • 579
    • 580
    • 581
    • 582
    • 583
    • 584
    • 585
    • 586
    • 587
    • 588
    • 589
    • 590
    • 591
    • 592
    • 593
    • 594
    • 595
    • 596
    • 597
    • 598
    • 599
    • 600
    • 601
    • 602
    • 603
    • 604
    • 605
    • 606
    • 607
    • 608
    • 609
    • 610
    • 611
    • 612
    • 613
    • 614
    • 615
    • 616
    • 617
    • 618
    • 619
    • 620
    • 621
    • 622
    • 623
    • 624
    • 625
    • 626
    • 627
    • 628
    • 629
    • 630
    • 631
    • 632
    • 633
    • 634
    • 635
    • 636
    • 637
    • 638
    • 639
    • 640
    • 641
    • 642
    • 643
    • 644
    • 645
    • 646
    • 647
    • 648
    • 649
    • 650
    • 651
    • 652
    • 653
    • 654
    • 655
    • 656
    • 657
    • 658
    • 659
    • 660
    • 661
    • 662
    • 663
    • 664
    • 665
    • 666
    • 667
    • 668
    • 669
    • 670
    • 671
    • 672
    • 673
    • 674
    • 675
    • 676
    • 677
    • 678
    • 679
    • 680
    • 681
    • 682
    • 683
    • 684
    • 685
    • 686
    • 687
    • 688
    • 689
    • 690
    • 691
    • 692
    • 693
    • 694
    • 695
    • 696
    • 697
    • 698
    • 699
    • 700
    • 701
    • 702
    • 703
    • 704
    • 705
    • 706
    • 707
    • 708
    • 709
    • 710
    • 711
    • 712
    • 713
    • 714
    • 715
    • 716
    • 717
    • 718
    • 719
    • 720
    • 721
    • 722
    • 723
    • 724
    • 725
    • 726
    • 727
    • 728
    • 729
    • 730
    • 731
    • 732
    • 733
    • 734
    • 735
    • 736
    • 737
    • 738
    • 739
    • 740
    • 741
    • 742
    • 743
    • 744
    • 745
    • 746
    • 747
    • 748
    • 749
    • 750
    • 751
    • 752
    • 753
    • 754
    • 755
    • 756
    • 757
    • 758
    • 759
    • 760
    • 761
    • 762
    • 763
    • 764
    • 765
    • 766
    • 767
    • 768
    • 769
    • 770
    • 771
    • 772
    • 773
    • 774
    • 775
    • 776
    • 777
    • 778
    • 779
    • 780
    • 781
    • 782
    • 783
    • 784
    • 785
    • 786
    • 787
    • 788
    • 789
    • 790
    • 791
    • 792
    • 793
    • 794
    • 795
    • 796
    • 797

    helloworld.proto

    syntax = "proto3";
    
    import "validate.proto";
    option go_package=".;proto";
    
    service Greeter {
      rpc SayHello (Person) returns (Person);
    }
    
    message Person {
      uint64 id    = 1 [(validate.rules).uint64.gt    = 999];
      string email = 2 [(validate.rules).string.email = true];
      string mobile = 3 [(validate.rules).string = {
    //    pattern:   "^[^[0-9]A-Za-z]+( [^[0-9]A-Za-z]+)*$",
    //    max_bytes: 256,
        pattern:   "^1[3456789]\\d{9}$"
      }];
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    sever.go

    package main
    
    import (
    	"context"
    	"google.golang.org/grpc/codes"
    	"google.golang.org/grpc/status"
    	"net"
    
    	"google.golang.org/grpc"
    
    	"awesomeProject123/grpc_validate_test/proto"
    )
    
    type Server struct {
    	*proto.UnimplementedGreeterServer
    }
    
    func (s *Server) mustEmbedUnimplementedGreeterServer() {
    	//TODO implement me
    	panic("implement me")
    }
    
    func (s *Server) SayHello(ctx context.Context, request *proto.Person) (*proto.Person,
    	error) {
    	return &proto.Person{
    		Id: 32,
    	}, nil
    }
    
    type Validator interface {
    	Validate() error
    }
    
    func main() {
    
    	var interceptor grpc.UnaryServerInterceptor
    	interceptor = func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
    		// 继续处理请求
    		//(proto.Person) 可以满足person的验证, 所有的接口 还有其他的接口那怎么办==>Validator转换为接口
    		if r, ok := req.(Validator); ok {
    			if err := r.Validate(); err != nil {
    				return nil, status.Error(codes.InvalidArgument, err.Error())
    			}
    		}
    
    		return handler(ctx, req)
    	}
    	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 grpc:" + err.Error())
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61

    client.go

    package main
    
    import (
    	"awesomeProject123/grpc_validate_test/proto"
    	"context"
    	"fmt"
    	"google.golang.org/grpc"
    	"google.golang.org/grpc/metadata"
    )
    
    func main() {
    	//stream
    	conn, err := grpc.Dial("127.0.0.1:50051", grpc.WithInsecure())
    	if err != nil {
    		panic(err)
    	}
    	defer conn.Close()
    
    	c := proto.NewGreeterClient(conn)
    
    	//md := metadata.Pairs("timestamp", time.Now().Format(timestampFormat))
    	md := metadata.New(map[string]string{
    		"name":    "bobby",
    		"pasword": "imooc",
    	})
    	ctx := metadata.NewOutgoingContext(context.Background(), md)
    	rsp, err := c.SayHello(ctx, &proto.Person{
    		Id:     1000,
    		Email:  "bobby@chengpeng.com",
    		Mobile: "18888888888",
    	})
    	if err != nil {
    		panic(err)
    	}
    	fmt.Println(rsp.Id)
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    grpc状态码

    https://github.com/grpc/grpc/blob/master/doc/statuscodes.md
    
    • 1

    在实际开发中,很多时候我们会自己定义状态码
    服务器

    st := status.New(codes.InvalidArgument, "invalid username")
    或者
    尽量使用这种方法
    status.Error(codes.InvalidArgument, "invalid username")
    
    • 1
    • 2
    • 3
    • 4

    客户端

    st, ok := status.FromError(err)
    if !ok {
     // Error was not a status error
    }
    st.Message()
    st.Code()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    grpc的超时机制–>grpc的超时机制比较简单

    1:网络抖动
    2:网络拥塞
    3:服务器很慢

    ctx, _ := context.WithTimeout(context.Background(), time.Second*3)
    
    • 1

    在这里插入图片描述

    yapi安装

    git clone https://github.com/Ryan-Miao/docker-yapi.git
    
    • 1
  • 相关阅读:
    小程序虐我千百遍,我还怎么待她如初恋,回忆我五年的小程序开发之路
    【大数据Hive】hive 加载数据常用方案使用详解
    搭建自动化 Web 页面性能检测系统 —— 设计篇
    jenkins的安装和配置(flask结合jenkins半自动化部署流程)
    深入理解高级加密标准(Advanced Encryption Standard)
    Docker学习第一天
    OPT华东产业园封顶,机器视觉产业版图再扩大!
    C语言每日一练--Day(16)
    Java基础用Date类编写万年历
    用pyinstaller打包LGBM模型为ELF/EXE可执行文件
  • 原文地址:https://blog.csdn.net/qq_40432598/article/details/134411275