gRPC入门
gRPC是一个高性能,开源的rpc框架
先说一下rpc
举个例子
type Person struct{
}
//结构体实现函数
func (this *Person) GetAdd(In int, Out *int) error{
*Out = In + 100
return nil
}
func main(){
//1.结构体实例化对象
person := new(Person)
//2.rpc注册
rpc.Register(person)
//3.rpc注册网络
rpc.HandleHTTP()
//4.创建网络链接
link, err := net.Listen("tcp", "127.0.0.1:8080")
if err!=nil{
fmt.Println("失败",err)
return
}
//5.守护等待网络链接
http.Serve(link, nil)
}
然后我们来创建客户端来访问连接
package main
func main(){
//1.连接服务端
cli,err := rpc.DialHTTP("tcp", "127.0.0.1:8080")
if err!=nil{
fmt.Println(err)
return
}
//2.调用服务端函数
var result int
err = cli.Call("Person.GetAdd", 99, &result)
if err!=nil{
fmt.Println(err)
return
}
fmt.Println(result)
}
现在我们的服务端和客户端都写好了
先开启服务端来监听
然后客户端连接服务端
然后调用Person的GetAdd方法,传入99
然后服务端接收到连接,然后执行GetAdd方法
返回结果199
然后我们看一下grpc
我们看一下google.golang.org包
然后打开/grpc/examples/hellworld
我们用这里的helloworld里面的示例测试一下
先运行服务端
打开greeter_server,运行 go run main.go
开启服务端
然后打开greeter_client,
go run main.go
成功打印hello world
那么grpc测试成功了
我们可以看一下examples的helloworld
里面的server和client
greeter_server
/*
*
* Copyright 2015 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
//go:generate protoc -I ../helloworld --go_out=plugins=grpc:../helloworld ../helloworld/helloworld.proto
package main
import (
"log"
"net"
"golang.org/x/net/context"
"google.golang.org/grpc"
pb "google.golang.org/grpc/examples/helloworld/helloworld"
"google.golang.org/grpc/reflection"
)
const (
port = ":50051"
)
// server is used to implement helloworld.GreeterServer.
type server struct{}
// SayHello implements helloworld.GreeterServer
func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
return &pb.HelloReply{Message: "Hello " + in.Name}, nil
}
func main() {
lis, err := net.Listen("tcp", port)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
pb.RegisterGreeterServer(s, &server{})
// Register reflection service on gRPC server.
reflection.Register(s)
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}
然后是
greeter_client
/*
*
* Copyright 2015 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package main
import (
"log"
"os"
"golang.org/x/net/context"
"google.golang.org/grpc"
pb "google.golang.org/grpc/examples/helloworld/helloworld"
)
const (
address = "localhost:50051"
defaultName = "world"
)
func main() {
// Set up a connection to the server.
conn, err := grpc.Dial(address, grpc.WithInsecure())
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()
c := pb.NewGreeterClient(conn)
// Contact the server and print out its response.
name := defaultName
if len(os.Args) > 1 {
name = os.Args[1]
}
r, err := c.SayHello(context.Background(), &pb.HelloRequest{Name: name})
if err != nil {
log.Fatalf("could not greet: %v", err)
}
log.Printf("Greeting: %s", r.Message)
}
然后
我们来写protobuf代码
syntax = "proto3";
package hello;
service HelloServer{
rpc GetAdd(HelloRequest) returns(HelloResponse){}
}
message HelloRequest{
int64 num = 1;
}
message HelloResponse{
int64 result = 1;
}
写完了protobuf代码
那么我们编译一下
protoc --go_out=./ *.proto
然后就生成了我们的hello.pb.go
我们来看一下hello.pb.go
// Code generated by protoc-gen-go. DO NOT EDIT.
// source: hello.proto
package hello
import (
fmt "fmt"
proto "github.com/golang/protobuf/proto"
math "math"
)
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
type HelloRequest struct {
Num int64 `protobuf:"varint,1,opt,name=num,proto3" json:"num,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *HelloRequest) Reset() { *m = HelloRequest{} }
func (m *HelloRequest) String() string { return proto.CompactTextString(m) }
func (*HelloRequest) ProtoMessage() {}
func (*HelloRequest) Descriptor() ([]byte, []int) {
return fileDescriptor_61ef911816e0a8ce, []int{0}
}
func (m *HelloRequest) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_HelloRequest.Unmarshal(m, b)
}
func (m *HelloRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_HelloRequest.Marshal(b, m, deterministic)
}
func (m *HelloRequest) XXX_Merge(src proto.Message) {
xxx_messageInfo_HelloRequest.Merge(m, src)
}
func (m *HelloRequest) XXX_Size() int {
return xxx_messageInfo_HelloRequest.Size(m)
}
func (m *HelloRequest) XXX_DiscardUnknown() {
xxx_messageInfo_HelloRequest.DiscardUnknown(m)
}
var xxx_messageInfo_HelloRequest proto.InternalMessageInfo
func (m *HelloRequest) GetNum() int64 {
if m != nil {
return m.Num
}
return 0
}
type HelloResponse struct {
Result int64 `protobuf:"varint,1,opt,name=result,proto3" json:"result,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *HelloResponse) Reset() { *m = HelloResponse{} }
func (m *HelloResponse) String() string { return proto.CompactTextString(m) }
func (*HelloResponse) ProtoMessage() {}
func (*HelloResponse) Descriptor() ([]byte, []int) {
return fileDescriptor_61ef911816e0a8ce, []int{1}
}
func (m *HelloResponse) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_HelloResponse.Unmarshal(m, b)
}
func (m *HelloResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_HelloResponse.Marshal(b, m, deterministic)
}
func (m *HelloResponse) XXX_Merge(src proto.Message) {
xxx_messageInfo_HelloResponse.Merge(m, src)
}
func (m *HelloResponse) XXX_Size() int {
return xxx_messageInfo_HelloResponse.Size(m)
}
func (m *HelloResponse) XXX_DiscardUnknown() {
xxx_messageInfo_HelloResponse.DiscardUnknown(m)
}
var xxx_messageInfo_HelloResponse proto.InternalMessageInfo
func (m *HelloResponse) GetResult() int64 {
if m != nil {
return m.Result
}
return 0
}
func init() {
proto.RegisterType((*HelloRequest)(nil), "hello.HelloRequest")
proto.RegisterType((*HelloResponse)(nil), "hello.HelloResponse")
}
func init() { proto.RegisterFile("hello.proto", fileDescriptor_61ef911816e0a8ce) }
var fileDescriptor_61ef911816e0a8ce = []byte{
// 139 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xe2, 0xce, 0x48, 0xcd, 0xc9,
0xc9, 0xd7, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0x05, 0x73, 0x94, 0x14, 0xb8, 0x78, 0x3c,
0x40, 0x8c, 0xa0, 0xd4, 0xc2, 0xd2, 0xd4, 0xe2, 0x12, 0x21, 0x01, 0x2e, 0xe6, 0xbc, 0xd2, 0x5c,
0x09, 0x46, 0x05, 0x46, 0x0d, 0xe6, 0x20, 0x10, 0x53, 0x49, 0x9d, 0x8b, 0x17, 0xaa, 0xa2, 0xb8,
0x20, 0x3f, 0xaf, 0x38, 0x55, 0x48, 0x8c, 0x8b, 0xad, 0x28, 0xb5, 0xb8, 0x34, 0xa7, 0x04, 0xaa,
0x0a, 0xca, 0x33, 0x72, 0xe1, 0xe2, 0x06, 0x2b, 0x0c, 0x4e, 0x2d, 0x2a, 0x4b, 0x2d, 0x12, 0x32,
0xe5, 0x62, 0x73, 0x4f, 0x2d, 0x71, 0x4c, 0x49, 0x11, 0x12, 0xd6, 0x83, 0x58, 0x8c, 0x6c, 0x91,
0x94, 0x08, 0xaa, 0x20, 0xc4, 0x6c, 0x25, 0x86, 0x24, 0x36, 0xb0, 0xf3, 0x8c, 0x01, 0x01, 0x00,
0x00, 0xff, 0xff, 0x6f, 0x66, 0x63, 0x77, 0xad, 0x00, 0x00, 0x00,
}
那么现在我们来生成grpc的go文件
protoc --go_out=plugins=grpc:./ *.proto
我们来前后对比一下
protoc --go_out=./ *.proto 生成的是 119行的代码
protoc --go_out=plugins=grpc:./ *.proto 生成的是193行的代码
加入grpc之后,生成了更多的代码
来看一下
// Code generated by protoc-gen-go. DO NOT EDIT.
// source: hello.proto
package hello
import (
context "context"
fmt "fmt"
proto "github.com/golang/protobuf/proto"
grpc "google.golang.org/grpc"
math "math"
)
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
type HelloRequest struct {
Num int64 `protobuf:"varint,1,opt,name=num,proto3" json:"num,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *HelloRequest) Reset() { *m = HelloRequest{} }
func (m *HelloRequest) String() string { return proto.CompactTextString(m) }
func (*HelloRequest) ProtoMessage() {}
func (*HelloRequest) Descriptor() ([]byte, []int) {
return fileDescriptor_61ef911816e0a8ce, []int{0}
}
func (m *HelloRequest) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_HelloRequest.Unmarshal(m, b)
}
func (m *HelloRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_HelloRequest.Marshal(b, m, deterministic)
}
func (m *HelloRequest) XXX_Merge(src proto.Message) {
xxx_messageInfo_HelloRequest.Merge(m, src)
}
func (m *HelloRequest) XXX_Size() int {
return xxx_messageInfo_HelloRequest.Size(m)
}
func (m *HelloRequest) XXX_DiscardUnknown() {
xxx_messageInfo_HelloRequest.DiscardUnknown(m)
}
var xxx_messageInfo_HelloRequest proto.InternalMessageInfo
func (m *HelloRequest) GetNum() int64 {
if m != nil {
return m.Num
}
return 0
}
type HelloResponse struct {
Result int64 `protobuf:"varint,1,opt,name=result,proto3" json:"result,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *HelloResponse) Reset() { *m = HelloResponse{} }
func (m *HelloResponse) String() string { return proto.CompactTextString(m) }
func (*HelloResponse) ProtoMessage() {}
func (*HelloResponse) Descriptor() ([]byte, []int) {
return fileDescriptor_61ef911816e0a8ce, []int{1}
}
func (m *HelloResponse) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_HelloResponse.Unmarshal(m, b)
}
func (m *HelloResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_HelloResponse.Marshal(b, m, deterministic)
}
func (m *HelloResponse) XXX_Merge(src proto.Message) {
xxx_messageInfo_HelloResponse.Merge(m, src)
}
func (m *HelloResponse) XXX_Size() int {
return xxx_messageInfo_HelloResponse.Size(m)
}
func (m *HelloResponse) XXX_DiscardUnknown() {
xxx_messageInfo_HelloResponse.DiscardUnknown(m)
}
var xxx_messageInfo_HelloResponse proto.InternalMessageInfo
func (m *HelloResponse) GetResult() int64 {
if m != nil {
return m.Result
}
return 0
}
func init() {
proto.RegisterType((*HelloRequest)(nil), "hello.HelloRequest")
proto.RegisterType((*HelloResponse)(nil), "hello.HelloResponse")
}
func init() { proto.RegisterFile("hello.proto", fileDescriptor_61ef911816e0a8ce) }
var fileDescriptor_61ef911816e0a8ce = []byte{
// 139 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xe2, 0xce, 0x48, 0xcd, 0xc9,
0xc9, 0xd7, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0x05, 0x73, 0x94, 0x14, 0xb8, 0x78, 0x3c,
0x40, 0x8c, 0xa0, 0xd4, 0xc2, 0xd2, 0xd4, 0xe2, 0x12, 0x21, 0x01, 0x2e, 0xe6, 0xbc, 0xd2, 0x5c,
0x09, 0x46, 0x05, 0x46, 0x0d, 0xe6, 0x20, 0x10, 0x53, 0x49, 0x9d, 0x8b, 0x17, 0xaa, 0xa2, 0xb8,
0x20, 0x3f, 0xaf, 0x38, 0x55, 0x48, 0x8c, 0x8b, 0xad, 0x28, 0xb5, 0xb8, 0x34, 0xa7, 0x04, 0xaa,
0x0a, 0xca, 0x33, 0x72, 0xe1, 0xe2, 0x06, 0x2b, 0x0c, 0x4e, 0x2d, 0x2a, 0x4b, 0x2d, 0x12, 0x32,
0xe5, 0x62, 0x73, 0x4f, 0x2d, 0x71, 0x4c, 0x49, 0x11, 0x12, 0xd6, 0x83, 0x58, 0x8c, 0x6c, 0x91,
0x94, 0x08, 0xaa, 0x20, 0xc4, 0x6c, 0x25, 0x86, 0x24, 0x36, 0xb0, 0xf3, 0x8c, 0x01, 0x01, 0x00,
0x00, 0xff, 0xff, 0x6f, 0x66, 0x63, 0x77, 0xad, 0x00, 0x00, 0x00,
}
// Reference imports to suppress errors if they are not otherwise used.
var _ context.Context
var _ grpc.ClientConn
// This is a compile-time assertion to ensure that this generated file
// is compatible with the grpc package it is being compiled against.
const _ = grpc.SupportPackageIsVersion4
// HelloServerClient is the client API for HelloServer service.
//
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream.
type HelloServerClient interface {
GetAdd(ctx context.Context, in *HelloRequest, opts ...grpc.CallOption) (*HelloResponse, error)
}
type helloServerClient struct {
cc *grpc.ClientConn
}
func NewHelloServerClient(cc *grpc.ClientConn) HelloServerClient {
return &helloServerClient{cc}
}
func (c *helloServerClient) GetAdd(ctx context.Context, in *HelloRequest, opts ...grpc.CallOption) (*HelloResponse, error) {
out := new(HelloResponse)
err := c.cc.Invoke(ctx, "/hello.HelloServer/GetAdd", in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// HelloServerServer is the server API for HelloServer service.
type HelloServerServer interface {
GetAdd(context.Context, *HelloRequest) (*HelloResponse, error)
}
func RegisterHelloServerServer(s *grpc.Server, srv HelloServerServer) {
s.RegisterService(&_HelloServer_serviceDesc, srv)
}
func _HelloServer_GetAdd_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.(HelloServerServer).GetAdd(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/hello.HelloServer/GetAdd",
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(HelloServerServer).GetAdd(ctx, req.(*HelloRequest))
}
return interceptor(ctx, in, info, handler)
}
var _HelloServer_serviceDesc = grpc.ServiceDesc{
ServiceName: "hello.HelloServer",
HandlerType: (*HelloServerServer)(nil),
Methods: []grpc.MethodDesc{
{
MethodName: "GetAdd",
Handler: _HelloServer_GetAdd_Handler,
},
},
Streams: []grpc.StreamDesc{},
Metadata: "hello.proto",
}