mirror of
https://github.com/chai2010/advanced-go-programming-book.git
synced 2025-05-24 20:52:22 +00:00
add ch4-04-grpc/grpc-pubsub example
This commit is contained in:
parent
47ac7a8d78
commit
e323f6c40a
27
vendor/gobook.examples/ch4-04-grpc/grpc-pubsub/clientPub/clientPub.go
vendored
Normal file
27
vendor/gobook.examples/ch4-04-grpc/grpc-pubsub/clientPub/clientPub.go
vendored
Normal file
@ -0,0 +1,27 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"google.golang.org/grpc"
|
||||
"log"
|
||||
."gobook.examples/ch4-04-grpc/grpc-pubsub/helloservice"
|
||||
"context"
|
||||
)
|
||||
|
||||
func main() {
|
||||
conn, err := grpc.Dial("localhost:1234", grpc.WithInsecure())
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
client := NewPubsubServiceClient(conn)
|
||||
|
||||
_, err = client.Publish(context.Background(), &String{Value: "golang: hello Go"})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
_, err = client.Publish(context.Background(), &String{Value: "docker: hello Docker"})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
36
vendor/gobook.examples/ch4-04-grpc/grpc-pubsub/clientSub/clientSub.go
vendored
Normal file
36
vendor/gobook.examples/ch4-04-grpc/grpc-pubsub/clientSub/clientSub.go
vendored
Normal file
@ -0,0 +1,36 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"google.golang.org/grpc"
|
||||
"log"
|
||||
."gobook.examples/ch4-04-grpc/grpc-pubsub/helloservice"
|
||||
"context"
|
||||
"io"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
conn, err := grpc.Dial("localhost:1234", grpc.WithInsecure())
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
client := NewPubsubServiceClient(conn)
|
||||
stream, err := client.Subscribe(context.Background(), &String{Value: "golang:"})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
for {
|
||||
reply, err := stream.Recv()
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
fmt.Println(reply.GetValue())
|
||||
}
|
||||
}
|
214
vendor/gobook.examples/ch4-04-grpc/grpc-pubsub/helloservice/hello.pb.go
vendored
Normal file
214
vendor/gobook.examples/ch4-04-grpc/grpc-pubsub/helloservice/hello.pb.go
vendored
Normal file
@ -0,0 +1,214 @@
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// source: hello.proto
|
||||
|
||||
package helloservice
|
||||
|
||||
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_0af1fa4e228d172c, []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), "helloservice.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
|
||||
|
||||
// PubsubServiceClient is the client API for PubsubService service.
|
||||
//
|
||||
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream.
|
||||
type PubsubServiceClient interface {
|
||||
Publish(ctx context.Context, in *String, opts ...grpc.CallOption) (*String, error)
|
||||
Subscribe(ctx context.Context, in *String, opts ...grpc.CallOption) (PubsubService_SubscribeClient, error)
|
||||
}
|
||||
|
||||
type pubsubServiceClient struct {
|
||||
cc *grpc.ClientConn
|
||||
}
|
||||
|
||||
func NewPubsubServiceClient(cc *grpc.ClientConn) PubsubServiceClient {
|
||||
return &pubsubServiceClient{cc}
|
||||
}
|
||||
|
||||
func (c *pubsubServiceClient) Publish(ctx context.Context, in *String, opts ...grpc.CallOption) (*String, error) {
|
||||
out := new(String)
|
||||
err := c.cc.Invoke(ctx, "/helloservice.PubsubService/Publish", in, out, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *pubsubServiceClient) Subscribe(ctx context.Context, in *String, opts ...grpc.CallOption) (PubsubService_SubscribeClient, error) {
|
||||
stream, err := c.cc.NewStream(ctx, &_PubsubService_serviceDesc.Streams[0], "/helloservice.PubsubService/Subscribe", opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
x := &pubsubServiceSubscribeClient{stream}
|
||||
if err := x.ClientStream.SendMsg(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := x.ClientStream.CloseSend(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return x, nil
|
||||
}
|
||||
|
||||
type PubsubService_SubscribeClient interface {
|
||||
Recv() (*String, error)
|
||||
grpc.ClientStream
|
||||
}
|
||||
|
||||
type pubsubServiceSubscribeClient struct {
|
||||
grpc.ClientStream
|
||||
}
|
||||
|
||||
func (x *pubsubServiceSubscribeClient) Recv() (*String, error) {
|
||||
m := new(String)
|
||||
if err := x.ClientStream.RecvMsg(m); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// PubsubServiceServer is the server API for PubsubService service.
|
||||
type PubsubServiceServer interface {
|
||||
Publish(context.Context, *String) (*String, error)
|
||||
Subscribe(*String, PubsubService_SubscribeServer) error
|
||||
}
|
||||
|
||||
func RegisterPubsubServiceServer(s *grpc.Server, srv PubsubServiceServer) {
|
||||
s.RegisterService(&_PubsubService_serviceDesc, srv)
|
||||
}
|
||||
|
||||
func _PubsubService_Publish_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.(PubsubServiceServer).Publish(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: "/helloservice.PubsubService/Publish",
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(PubsubServiceServer).Publish(ctx, req.(*String))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _PubsubService_Subscribe_Handler(srv interface{}, stream grpc.ServerStream) error {
|
||||
m := new(String)
|
||||
if err := stream.RecvMsg(m); err != nil {
|
||||
return err
|
||||
}
|
||||
return srv.(PubsubServiceServer).Subscribe(m, &pubsubServiceSubscribeServer{stream})
|
||||
}
|
||||
|
||||
type PubsubService_SubscribeServer interface {
|
||||
Send(*String) error
|
||||
grpc.ServerStream
|
||||
}
|
||||
|
||||
type pubsubServiceSubscribeServer struct {
|
||||
grpc.ServerStream
|
||||
}
|
||||
|
||||
func (x *pubsubServiceSubscribeServer) Send(m *String) error {
|
||||
return x.ServerStream.SendMsg(m)
|
||||
}
|
||||
|
||||
var _PubsubService_serviceDesc = grpc.ServiceDesc{
|
||||
ServiceName: "helloservice.PubsubService",
|
||||
HandlerType: (*PubsubServiceServer)(nil),
|
||||
Methods: []grpc.MethodDesc{
|
||||
{
|
||||
MethodName: "Publish",
|
||||
Handler: _PubsubService_Publish_Handler,
|
||||
},
|
||||
},
|
||||
Streams: []grpc.StreamDesc{
|
||||
{
|
||||
StreamName: "Subscribe",
|
||||
Handler: _PubsubService_Subscribe_Handler,
|
||||
ServerStreams: true,
|
||||
},
|
||||
},
|
||||
Metadata: "hello.proto",
|
||||
}
|
||||
|
||||
func init() { proto.RegisterFile("hello.proto", fileDescriptor_hello_0af1fa4e228d172c) }
|
||||
|
||||
var fileDescriptor_hello_0af1fa4e228d172c = []byte{
|
||||
// 135 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, 0xe2, 0x01, 0x73, 0x8a, 0x53, 0x8b, 0xca, 0x32,
|
||||
0x93, 0x53, 0x95, 0xe4, 0xb8, 0xd8, 0x82, 0x4b, 0x8a, 0x32, 0xf3, 0xd2, 0x85, 0x44, 0xb8, 0x58,
|
||||
0xcb, 0x12, 0x73, 0x4a, 0x53, 0x25, 0x18, 0x15, 0x18, 0x35, 0x38, 0x83, 0x20, 0x1c, 0xa3, 0x46,
|
||||
0x46, 0x2e, 0xde, 0x80, 0xd2, 0xa4, 0xe2, 0xd2, 0xa4, 0x60, 0x88, 0x0e, 0x21, 0x53, 0x2e, 0xf6,
|
||||
0x80, 0xd2, 0xa4, 0x9c, 0xcc, 0xe2, 0x0c, 0x21, 0x11, 0x3d, 0x64, 0xb3, 0xf4, 0x20, 0x06, 0x49,
|
||||
0x61, 0x15, 0x15, 0xb2, 0xe4, 0xe2, 0x0c, 0x2e, 0x4d, 0x2a, 0x4e, 0x2e, 0xca, 0x4c, 0x4a, 0x25,
|
||||
0x45, 0xa3, 0x01, 0x63, 0x12, 0x1b, 0xd8, 0xe1, 0xc6, 0x80, 0x00, 0x00, 0x00, 0xff, 0xff, 0x6e,
|
||||
0xf2, 0xde, 0x40, 0xc7, 0x00, 0x00, 0x00,
|
||||
}
|
14
vendor/gobook.examples/ch4-04-grpc/grpc-pubsub/helloservice/hello.proto
vendored
Normal file
14
vendor/gobook.examples/ch4-04-grpc/grpc-pubsub/helloservice/hello.proto
vendored
Normal file
@ -0,0 +1,14 @@
|
||||
syntax = "proto3";
|
||||
|
||||
package helloservice;
|
||||
|
||||
message String {
|
||||
string value = 1;
|
||||
}
|
||||
|
||||
service PubsubService {
|
||||
rpc Publish (String) returns (String);
|
||||
rpc Subscribe (String) returns (stream String);
|
||||
}
|
||||
|
||||
//protoc --go_out=plugins=grpc:. hello.proto
|
BIN
vendor/gobook.examples/ch4-04-grpc/grpc-pubsub/server/.server.go.swp
vendored
Normal file
BIN
vendor/gobook.examples/ch4-04-grpc/grpc-pubsub/server/.server.go.swp
vendored
Normal file
Binary file not shown.
71
vendor/gobook.examples/ch4-04-grpc/grpc-pubsub/server/server.go
vendored
Normal file
71
vendor/gobook.examples/ch4-04-grpc/grpc-pubsub/server/server.go
vendored
Normal file
@ -0,0 +1,71 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"time"
|
||||
"google.golang.org/grpc"
|
||||
"net"
|
||||
"log"
|
||||
."gobook.examples/ch4-04-grpc/grpc-pubsub/helloservice"
|
||||
"github.com/docker/docker/pkg/pubsub"
|
||||
"context"
|
||||
"strings"
|
||||
)
|
||||
|
||||
|
||||
type PubsubService struct {
|
||||
pub *pubsub.Publisher
|
||||
}
|
||||
|
||||
func NewPubsubService() *PubsubService {
|
||||
return &PubsubService{
|
||||
pub: pubsub.NewPublisher(100*time.Millisecond, 10),
|
||||
}
|
||||
}
|
||||
|
||||
func (p *PubsubService) Publish(
|
||||
ctx context.Context, arg *String,
|
||||
) (*String, error) {
|
||||
p.pub.Publish(arg.GetValue())
|
||||
//debug
|
||||
//reply := &String{Value: "<Publish> " + arg.GetValue()}
|
||||
//fmt.Println(reply.GetValue())
|
||||
return &String{}, nil
|
||||
}
|
||||
|
||||
|
||||
func (p *PubsubService) Subscribe(
|
||||
arg *String, stream PubsubService_SubscribeServer,
|
||||
) error {
|
||||
ch := p.pub.SubscribeTopic(func(v interface{}) bool {
|
||||
if key, ok := v.(string); ok {
|
||||
//debug
|
||||
//fmt.Printf("<debug> %t %s %s %t\n",
|
||||
// ok,arg.GetValue(),key,strings.HasPrefix(key,arg.GetValue()))
|
||||
if strings.HasPrefix(key,arg.GetValue()) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
})
|
||||
|
||||
for v := range ch {
|
||||
if err := stream.Send(&String{Value: v.(string)}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
grpcServer := grpc.NewServer()
|
||||
RegisterPubsubServiceServer(grpcServer,NewPubsubService())
|
||||
|
||||
lis, err := net.Listen("tcp", ":1234")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
grpcServer.Serve(lis)
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user