1. 安装和配置

linux

  1. # fetches this repo into $GOPATH
  2. go get -d github.com/envoyproxy/protoc-gen-validate
  3. # installs PGV into $GOPATH/bin
  4. go build

windows

点此下载:protoc-gen-validate.zip

将 zip文件中的exe文件拷贝到 go的根补录的bin目录下

生成python源码

  1. protoc -I . --go_out=plugins=grpc:. --validate_out="lang=go:." helloworld.proto

2. proto

  1. 新建validate.proto文件内容从 https://github.com/envoyproxy/protoc-gen-validate/blob/master/validate/validate.proto 拷贝
  2. 新建helloworl.proto文件 ```protobuf 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 name = 3 [(validate.rules).string = { pattern: “^[^[0-9]A-Za-z]+( [^[0-9]A-Za-z]+)*$”,max_bytes: 256,}];

}

  1. <a name="zDJmx"></a>
  2. ## 3. 服务端(server)
  3. ```go
  4. package main
  5. import (
  6. "context"
  7. "google.golang.org/grpc/codes"
  8. "google.golang.org/grpc/status"
  9. "net"
  10. "google.golang.org/grpc"
  11. "start/pgv_test/proto"
  12. )
  13. type Server struct{}
  14. func (s *Server) SayHello(ctx context.Context, request *proto.Person) (*proto.Person,
  15. error){
  16. return &proto.Person{
  17. Id: 32,
  18. }, nil
  19. }
  20. type Validator interface {
  21. Validate() error
  22. }
  23. func main(){
  24. var interceptor grpc.UnaryServerInterceptor
  25. interceptor = func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
  26. // 继续处理请求
  27. if r, ok := req.(Validator); ok {
  28. if err := r.Validate(); err != nil {
  29. return nil, status.Error(codes.InvalidArgument, err.Error())
  30. }
  31. }
  32. return handler(ctx, req)
  33. }
  34. var opts []grpc.ServerOption
  35. opts = append(opts, grpc.UnaryInterceptor(interceptor))
  36. g := grpc.NewServer(opts...)
  37. proto.RegisterGreeterServer(g, &Server{})
  38. lis, err := net.Listen("tcp", "0.0.0.0:50051")
  39. if err != nil{
  40. panic("failed to listen:"+err.Error())
  41. }
  42. err = g.Serve(lis)
  43. if err != nil{
  44. panic("failed to start grpc:"+err.Error())
  45. }
  46. }

4. 客户端(client)

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "google.golang.org/grpc"
  6. "start/pgv_test/proto"
  7. )
  8. type customCredential struct{}
  9. func main() {
  10. var opts []grpc.DialOption
  11. //opts = append(opts, grpc.WithUnaryInterceptor(interceptor))
  12. opts = append(opts, grpc.WithInsecure())
  13. conn, err := grpc.Dial("localhost:50051", opts...)
  14. if err != nil {
  15. panic(err)
  16. }
  17. defer conn.Close()
  18. c := proto.NewGreeterClient(conn)
  19. //rsp, _ := c.Search(context.Background(), &empty.Empty{})
  20. rsp, err := c.SayHello(context.Background(), &proto.Person{
  21. Email: "bobby",
  22. })
  23. if err != nil {
  24. panic(err)
  25. }
  26. fmt.Println(rsp.Id)
  27. }