protobuf
- go 语言中 编译proto命令:
protoc --go_out=./ *proto`
service 服务名 {
rpc 函数名(参数:消息体) returns (返回值:消息)
}
message People {
string name = 1;
}
message Student {
int32 age = 2;
}
例:
service hello {
rpc HelloWorld(People) returns (Student);
}
``
#### 简单grpc程序:
![在这里插入图片描述](https://img-blog.csdnimg.cn/da44e3a8a60e4ea0b2c0ddc9363ee6b1.png)
person.proto
```proto
syntax = "proto3";
option go_package="../pb";
package pb;
message Teacher {
int32 age =1 ;
string name = 2;
}
service SayName {
rpc SayHello(Teacher) returns (Teacher);
}
//protoc --go_out=plugins=grpc:./ *.proto
service.go
package main
import (
"aagoproject/day01_gRPC/pb"
"context"
"fmt"
"google.golang.org/grpc"
"net"
)
type Children struct {
}
func (this *Children)SayHello(ctx context.Context,teacher *pb.Teacher) (*pb.Teacher,error) {
teacher.Name += "6666";
return teacher,nil;
}
func main() {
grpcServer :=grpc.NewServer()
pb.RegisterSayNameServer(grpcServer,new(Children))
listener,err :=net.Listen("tcp","127.0.0.1:8800")
if err !=nil{
fmt.Println("listen err")
return
}
defer listener.Close()
grpcServer.Serve(listener)
}
client.go