mirror of
https://github.com/chai2010/advanced-go-programming-book.git
synced 2025-05-24 04:22:22 +00:00
ch4-04: 完善
This commit is contained in:
parent
249a2f0895
commit
f227bb7900
@ -1,5 +1,104 @@
|
||||
# 4.4. GRPC入门
|
||||
|
||||
GRPC是Google公司基于Protobuf开发的跨语言的开源RPC框架。GRPC基于HTTP/2协议设计,可以基于一个HTTP/2链接提供多个服务,对于移动设备更加友好。本节将讲述GRPC的简单用法。
|
||||
|
||||
## GRPC入门
|
||||
|
||||
如果从Protobuf的角度看,GRPC只不过是针对service接口一个生成代码生成器。我们在本章的第二节中一节手工实现了一个简单的Protobuf代码生成器插件,只不过当时生成的代码是适配标准库的RPC框架的。
|
||||
|
||||
创建hello.proto文件,定义HelloService接口:
|
||||
|
||||
```proto
|
||||
syntax = "proto3";
|
||||
|
||||
package main;
|
||||
|
||||
message String {
|
||||
string value = 1;
|
||||
}
|
||||
|
||||
service HelloService {
|
||||
rpc Hello (String) returns (String);
|
||||
}
|
||||
```
|
||||
|
||||
使用protoc-gen-go内置的grpc插件生成GRPC代码:
|
||||
|
||||
```
|
||||
$ protoc --go_out=plugins=grpc:. hello.proto
|
||||
```
|
||||
|
||||
GRPC插件会为服务端和客户端生成不同的接口:
|
||||
|
||||
```go
|
||||
type HelloServiceServer interface {
|
||||
Hello(context.Context, *String) (*String, error)
|
||||
}
|
||||
|
||||
type HelloServiceClient interface {
|
||||
Hello(ctx context.Context, in *String, opts ...grpc.CallOption) (*String, error)
|
||||
}
|
||||
```
|
||||
|
||||
GRPC通过context.Context参数,为每个方法调用提供了上下文支持。客户端在调用方法的时候,可以通过可选的grpc.CallOption类型的参数提供额外的上下文信息。
|
||||
|
||||
基于服务端的HelloServiceServer接口可以重新实现HelloService服务:
|
||||
|
||||
```go
|
||||
type HelloServiceImpl struct{}
|
||||
|
||||
func (p *HelloServiceImpl) Hello(ctx context.Context, args *String) (*String, error) {
|
||||
reply := &String{Value: "hello:" + args.GetValue()}
|
||||
return reply, nil
|
||||
}
|
||||
```
|
||||
|
||||
GRPC服务的启动流程和标准库的RPC服务启动流程类似:
|
||||
|
||||
```go
|
||||
func main() {
|
||||
grpcServer := grpc.NewServer()
|
||||
RegisterHelloServiceServer(grpcServer, &HelloServiceImpl{})
|
||||
|
||||
lis, err := net.Listen("tcp", ":1234")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
grpcServer.Serve(lis)
|
||||
}
|
||||
```
|
||||
|
||||
首先是通过`grpc.NewServer()`构造一个GRPC服务对象,然后通过GRPC插件生成的RegisterHelloServiceServer函数注册我们实现的HelloServiceImpl服务。然后通过`grpcServer.Serve(lis)`在一个监听端口上提供GRPC服务。
|
||||
|
||||
然后就可以通过客户端链接GRPC服务了:
|
||||
|
||||
```go
|
||||
func main() {
|
||||
conn, err := grpc.Dial("localhost:1234", grpc.WithInsecure())
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
client := NewHelloServiceClient(conn)
|
||||
reply, err := client.Hello(context.Background(), &String{Value: "hello"})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Println(reply.GetValue())
|
||||
}
|
||||
```
|
||||
|
||||
其中grpc.Dial负责和GRPC服务建立链接,然后NewHelloServiceClient函数基于已经建立的链接构造HelloServiceClient对象。返回的client其实是一个HelloServiceClient接口对象,通过接口定义的方法就可以调用服务端对应的GRPC服务提供的方法。
|
||||
|
||||
GRPC和标准库的RPC框架还有一个区别,GRPC生成的接口并不支持异步调用。
|
||||
|
||||
## GRPC流
|
||||
|
||||
TODO
|
||||
|
||||
## 认证
|
||||
|
||||
TODO
|
||||
|
||||
<!--
|
||||
|
2
examples/ch4-04-grpc/basic/client/Makefile
Normal file
2
examples/ch4-04-grpc/basic/client/Makefile
Normal file
@ -0,0 +1,2 @@
|
||||
default:
|
||||
protoc --go_out=plugins=grpc:. hello.proto
|
151
examples/ch4-04-grpc/basic/client/hello.pb.go
Normal file
151
examples/ch4-04-grpc/basic/client/hello.pb.go
Normal file
@ -0,0 +1,151 @@
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// source: hello.proto
|
||||
|
||||
package main
|
||||
|
||||
import proto "github.com/golang/protobuf/proto"
|
||||
import fmt "fmt"
|
||||
import math "math"
|
||||
|
||||
import (
|
||||
context "golang.org/x/net/context"
|
||||
grpc "google.golang.org/grpc"
|
||||
)
|
||||
|
||||
// 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 String struct {
|
||||
Value string `protobuf:"bytes,1,opt,name=value,proto3" json:"value,omitempty"`
|
||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
||||
XXX_unrecognized []byte `json:"-"`
|
||||
XXX_sizecache int32 `json:"-"`
|
||||
}
|
||||
|
||||
func (m *String) Reset() { *m = String{} }
|
||||
func (m *String) String() string { return proto.CompactTextString(m) }
|
||||
func (*String) ProtoMessage() {}
|
||||
func (*String) Descriptor() ([]byte, []int) {
|
||||
return fileDescriptor_hello_5dd9d59ecabc789f, []int{0}
|
||||
}
|
||||
func (m *String) XXX_Unmarshal(b []byte) error {
|
||||
return xxx_messageInfo_String.Unmarshal(m, b)
|
||||
}
|
||||
func (m *String) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
|
||||
return xxx_messageInfo_String.Marshal(b, m, deterministic)
|
||||
}
|
||||
func (dst *String) XXX_Merge(src proto.Message) {
|
||||
xxx_messageInfo_String.Merge(dst, src)
|
||||
}
|
||||
func (m *String) XXX_Size() int {
|
||||
return xxx_messageInfo_String.Size(m)
|
||||
}
|
||||
func (m *String) XXX_DiscardUnknown() {
|
||||
xxx_messageInfo_String.DiscardUnknown(m)
|
||||
}
|
||||
|
||||
var xxx_messageInfo_String proto.InternalMessageInfo
|
||||
|
||||
func (m *String) GetValue() string {
|
||||
if m != nil {
|
||||
return m.Value
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func init() {
|
||||
proto.RegisterType((*String)(nil), "main.String")
|
||||
}
|
||||
|
||||
// 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
|
||||
|
||||
// HelloServiceClient is the client API for HelloService service.
|
||||
//
|
||||
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream.
|
||||
type HelloServiceClient interface {
|
||||
Hello(ctx context.Context, in *String, opts ...grpc.CallOption) (*String, error)
|
||||
}
|
||||
|
||||
type helloServiceClient struct {
|
||||
cc *grpc.ClientConn
|
||||
}
|
||||
|
||||
func NewHelloServiceClient(cc *grpc.ClientConn) HelloServiceClient {
|
||||
return &helloServiceClient{cc}
|
||||
}
|
||||
|
||||
func (c *helloServiceClient) Hello(ctx context.Context, in *String, opts ...grpc.CallOption) (*String, error) {
|
||||
out := new(String)
|
||||
err := c.cc.Invoke(ctx, "/main.HelloService/Hello", in, out, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// HelloServiceServer is the server API for HelloService service.
|
||||
type HelloServiceServer interface {
|
||||
Hello(context.Context, *String) (*String, error)
|
||||
}
|
||||
|
||||
func RegisterHelloServiceServer(s *grpc.Server, srv HelloServiceServer) {
|
||||
s.RegisterService(&_HelloService_serviceDesc, srv)
|
||||
}
|
||||
|
||||
func _HelloService_Hello_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(String)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(HelloServiceServer).Hello(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: "/main.HelloService/Hello",
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(HelloServiceServer).Hello(ctx, req.(*String))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
var _HelloService_serviceDesc = grpc.ServiceDesc{
|
||||
ServiceName: "main.HelloService",
|
||||
HandlerType: (*HelloServiceServer)(nil),
|
||||
Methods: []grpc.MethodDesc{
|
||||
{
|
||||
MethodName: "Hello",
|
||||
Handler: _HelloService_Hello_Handler,
|
||||
},
|
||||
},
|
||||
Streams: []grpc.StreamDesc{},
|
||||
Metadata: "hello.proto",
|
||||
}
|
||||
|
||||
func init() { proto.RegisterFile("hello.proto", fileDescriptor_hello_5dd9d59ecabc789f) }
|
||||
|
||||
var fileDescriptor_hello_5dd9d59ecabc789f = []byte{
|
||||
// 107 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, 0xc9, 0x4d, 0xcc, 0xcc, 0x53, 0x92, 0xe3,
|
||||
0x62, 0x0b, 0x2e, 0x29, 0xca, 0xcc, 0x4b, 0x17, 0x12, 0xe1, 0x62, 0x2d, 0x4b, 0xcc, 0x29, 0x4d,
|
||||
0x95, 0x60, 0x54, 0x60, 0xd4, 0xe0, 0x0c, 0x82, 0x70, 0x8c, 0x8c, 0xb9, 0x78, 0x3c, 0x40, 0x9a,
|
||||
0x82, 0x53, 0x8b, 0xca, 0x32, 0x93, 0x53, 0x85, 0x94, 0xb9, 0x58, 0xc1, 0x7c, 0x21, 0x1e, 0x3d,
|
||||
0x90, 0x7e, 0x3d, 0x88, 0x66, 0x29, 0x14, 0x5e, 0x12, 0x1b, 0xd8, 0x06, 0x63, 0x40, 0x00, 0x00,
|
||||
0x00, 0xff, 0xff, 0xa1, 0x2a, 0xa2, 0x5a, 0x70, 0x00, 0x00, 0x00,
|
||||
}
|
11
examples/ch4-04-grpc/basic/client/hello.proto
Normal file
11
examples/ch4-04-grpc/basic/client/hello.proto
Normal file
@ -0,0 +1,11 @@
|
||||
syntax = "proto3";
|
||||
|
||||
package main;
|
||||
|
||||
message String {
|
||||
string value = 1;
|
||||
}
|
||||
|
||||
service HelloService {
|
||||
rpc Hello (String) returns (String);
|
||||
}
|
49
examples/ch4-04-grpc/basic/client/main.go
Normal file
49
examples/ch4-04-grpc/basic/client/main.go
Normal file
@ -0,0 +1,49 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
type HelloServiceImpl struct{}
|
||||
|
||||
func (p *HelloServiceImpl) Hello(ctx context.Context, args *String) (*String, error) {
|
||||
reply := &String{Value: "hello:" + args.GetValue()}
|
||||
return reply, nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
go startGrpcServer()
|
||||
doClientWork()
|
||||
}
|
||||
|
||||
func startGrpcServer() {
|
||||
grpcServer := grpc.NewServer()
|
||||
RegisterHelloServiceServer(grpcServer, &HelloServiceImpl{})
|
||||
|
||||
lis, err := net.Listen("tcp", ":1234")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
grpcServer.Serve(lis)
|
||||
}
|
||||
|
||||
func doClientWork() {
|
||||
conn, err := grpc.Dial("localhost:1234", grpc.WithInsecure())
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
client := NewHelloServiceClient(conn)
|
||||
reply, err := client.Hello(context.Background(), &String{Value: "hello"})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Println(reply.GetValue())
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user