grpc使用例子说明
- 第一步,定义一个hello.proto文件,描述服务
syntax="proto3";
service HelloService {
rpc SayHello (HelloRequest) returns (HelloResponse);
}
message HelloRequest {
string greeting = 1;
}
message HelloResponse {
string reply = 1;
}
- 第二步,使用grpc插件生成golang代码
protoc --go_out=plugins=grpc:${DistinationDir} ${SourceDir}
生成的核心代码如下
client代码
//根据HelloService生成的客户端接口
type HelloServiceClient interface {
SayHello(ctx context.Context, in *HelloRequest, opts ...grpc.CallOption) (*HelloResponse, error)
}
//HelloService客户端,实现了HelloServiceClient接口
type helloServiceClient struct {
cc *grpc.ClientConn
}
//创建一个HelloService客户端对象
func NewHelloServiceClient(cc *grpc.ClientConn) HelloServiceClient {
return &helloServiceClient{cc}
}
//实现HelloServiceClient接口的SayHello,是调用远程服务SayHello方法的具体实现
func (c *helloServiceClient) SayHello(ctx context.Context, in *HelloRequest, opts ...grpc.CallOption) (*HelloResponse, error) {
out := new(HelloResponse)
err := c.cc.Invoke(ctx, "/HelloService/SayHello", in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
server代码
// HelloServiceServer is the server API for HelloService service.
//根据HelloService服务生成的server接口
type HelloServiceServer interface {
SayHello(context.Context, *HelloRequest) (*HelloResponse, error)
}
//注册HelloServiceServer
func RegisterHelloServiceServer(s *grpc.Server, srv HelloServiceServer) {
s.RegisterService(&_HelloService_serviceDesc, srv)
}
//HelloServiceServer接口描述
var _HelloService_serviceDesc = grpc.ServiceDesc{
ServiceName: "HelloService",
HandlerType: (*HelloServiceServer)(nil),
Methods: []grpc.MethodDesc{
{
MethodName: "SayHello",
Handler: _HelloService_SayHello_Handler,
},
},
Streams: []grpc.StreamDesc{},
Metadata: "hello.proto",
}
//SayHello方法处理器
func _HelloService_SayHello_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(HelloRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(HelloServiceServer).SayHello(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/HelloService/SayHello",
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(HelloServiceServer).SayHello(ctx, req.(*HelloRequest))
}
return interceptor(ctx, in, info, handler)
}
- 第三步,实现定义的服务接口
//实现接口HelloServiceServer
type server struct {
}
func (s *server)SayHello(ctx context.Context,request *hello.HelloRequest) (*hello.HelloResponse, error){
fmt.Println("进入SayHello方法")
fmt.Println(request)
return &hello.HelloResponse{Reply:"reply"},nil
}
- 第四步,注册服务,并启动
func main() {
listener,err:=net.Listen("tcp","localhost:50001")
if err!=nil{
panic(err)
}
注册服务
s:=grpc.NewServer()
var service server
hello.RegisterHelloServiceServer(s,&service)
//启动服务
if err=s.Serve(listener);err!=nil{
panic(err)
}
}
- 第五步,客户端调用服务
func main() {
conn,err:=grpc.Dial("localhost:50001",grpc.WithInsecure())
if err!=nil{
panic(err)
}
defer conn.Close()
cli:=hello.NewHelloServiceClient(conn)
res,err:=cli.SayHello(context.Background(),&hello.HelloRequest{
Greeting:"hello world",
})
if err!=nil{
panic(err)
}
fmt.Println(res)
}