gRPC一元模式。

This commit is contained in:
guoqiang 2023-12-02 15:06:57 +08:00
parent 098a1bef81
commit c7772f241d
11 changed files with 495 additions and 0 deletions

3
.gitignore vendored
View File

@ -21,3 +21,6 @@
# Go workspace file
go.work
*/bin/*
.idea

12
Makefile Normal file
View File

@ -0,0 +1,12 @@
tidy:
go mod tidy
greet-gen:
protoc -Igreet/proto/ --go_out=./greet --go_opt=module=git.gqnotes.com/guoqiang/grpcexercises/greet --go-grpc_out=./greet --go-grpc_opt=module=git.gqnotes.com/guoqiang/grpcexercises/greet greet/proto/*.proto
greet-build:
go build -o ./greet/bin/server-osx ./greet/server
go build -o ./greet/bin/client-osx ./greet/client
.PHONY: tidy greet-gen greet-build

16
go.mod Normal file
View File

@ -0,0 +1,16 @@
module git.gqnotes.com/guoqiang/grpcexercises
go 1.21.4
require (
google.golang.org/grpc v1.59.0
google.golang.org/protobuf v1.31.0
)
require (
github.com/golang/protobuf v1.5.3 // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/text v0.12.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d // indirect
)

21
go.sum Normal file
View File

@ -0,0 +1,21 @@
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14=
golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI=
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc=
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d h1:uvYuEyMHKNt+lT4K3bN6fGswmK8qSvcreM3BwjDh+y4=
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d/go.mod h1:+Bk1OCOj40wS2hwAMA+aCW9ypzm63QTBBHp6lQ3p+9M=
google.golang.org/grpc v1.59.0 h1:Z5Iec2pjwb+LEOqzpB2MR12/eKFhDPhuqW91O+4bwUk=
google.golang.org/grpc v1.59.0/go.mod h1:aUPDwccQo6OTjy7Hct4AfBPD1GptF4fyUjIkQ9YtF98=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=

20
greet/client/Greet.go Normal file
View File

@ -0,0 +1,20 @@
package main
import (
"context"
"git.gqnotes.com/guoqiang/grpcexercises/greet/pb"
"log"
)
func Greet(client pb.GreetServiceClient) {
response, err := client.Greet(context.Background(), &pb.GreetRequest{
Greeting: "xiaoming",
})
if err != nil {
log.Fatalf("failed to call Greet: %v", err)
return
}
log.Printf("response:%+v\n", response)
}

25
greet/client/main.go Normal file
View File

@ -0,0 +1,25 @@
package main
import (
"git.gqnotes.com/guoqiang/grpcexercises/greet/pb"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"log"
)
func main() {
conn, err := grpc.Dial(":5632", grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
log.Fatalf("failed to dial: %v", err)
return
}
defer conn.Close()
// 实例化客户端
client := pb.NewGreetServiceClient(conn)
// 调用方法
Greet(client)
}

213
greet/pb/greet.pb.go Normal file
View File

@ -0,0 +1,213 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.31.0
// protoc v4.25.1
// source: greet.proto
package pb
import (
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
reflect "reflect"
sync "sync"
)
const (
// Verify that this generated code is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
// Verify that runtime/protoimpl is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
)
type GreetRequest struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Greeting string `protobuf:"bytes,1,opt,name=greeting,proto3" json:"greeting,omitempty"`
}
func (x *GreetRequest) Reset() {
*x = GreetRequest{}
if protoimpl.UnsafeEnabled {
mi := &file_greet_proto_msgTypes[0]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *GreetRequest) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*GreetRequest) ProtoMessage() {}
func (x *GreetRequest) ProtoReflect() protoreflect.Message {
mi := &file_greet_proto_msgTypes[0]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use GreetRequest.ProtoReflect.Descriptor instead.
func (*GreetRequest) Descriptor() ([]byte, []int) {
return file_greet_proto_rawDescGZIP(), []int{0}
}
func (x *GreetRequest) GetGreeting() string {
if x != nil {
return x.Greeting
}
return ""
}
type GreetResponse struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Result string `protobuf:"bytes,1,opt,name=result,proto3" json:"result,omitempty"`
}
func (x *GreetResponse) Reset() {
*x = GreetResponse{}
if protoimpl.UnsafeEnabled {
mi := &file_greet_proto_msgTypes[1]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *GreetResponse) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*GreetResponse) ProtoMessage() {}
func (x *GreetResponse) ProtoReflect() protoreflect.Message {
mi := &file_greet_proto_msgTypes[1]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use GreetResponse.ProtoReflect.Descriptor instead.
func (*GreetResponse) Descriptor() ([]byte, []int) {
return file_greet_proto_rawDescGZIP(), []int{1}
}
func (x *GreetResponse) GetResult() string {
if x != nil {
return x.Result
}
return ""
}
var File_greet_proto protoreflect.FileDescriptor
var file_greet_proto_rawDesc = []byte{
0x0a, 0x0b, 0x67, 0x72, 0x65, 0x65, 0x74, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x05, 0x70,
0x72, 0x6f, 0x74, 0x6f, 0x22, 0x2a, 0x0a, 0x0c, 0x47, 0x72, 0x65, 0x65, 0x74, 0x52, 0x65, 0x71,
0x75, 0x65, 0x73, 0x74, 0x12, 0x1a, 0x0a, 0x08, 0x67, 0x72, 0x65, 0x65, 0x74, 0x69, 0x6e, 0x67,
0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x67, 0x72, 0x65, 0x65, 0x74, 0x69, 0x6e, 0x67,
0x22, 0x27, 0x0a, 0x0d, 0x47, 0x72, 0x65, 0x65, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73,
0x65, 0x12, 0x16, 0x0a, 0x06, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28,
0x09, 0x52, 0x06, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x32, 0x44, 0x0a, 0x0c, 0x47, 0x72, 0x65,
0x65, 0x74, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x34, 0x0a, 0x05, 0x47, 0x72, 0x65,
0x65, 0x74, 0x12, 0x13, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x47, 0x72, 0x65, 0x65, 0x74,
0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x14, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e,
0x47, 0x72, 0x65, 0x65, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x42,
0x31, 0x5a, 0x2f, 0x67, 0x69, 0x74, 0x2e, 0x67, 0x71, 0x6e, 0x6f, 0x74, 0x65, 0x73, 0x2e, 0x63,
0x6f, 0x6d, 0x2f, 0x67, 0x75, 0x6f, 0x71, 0x69, 0x61, 0x6e, 0x67, 0x2f, 0x67, 0x72, 0x70, 0x63,
0x65, 0x78, 0x65, 0x72, 0x63, 0x69, 0x73, 0x65, 0x73, 0x2f, 0x67, 0x72, 0x65, 0x65, 0x74, 0x2f,
0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (
file_greet_proto_rawDescOnce sync.Once
file_greet_proto_rawDescData = file_greet_proto_rawDesc
)
func file_greet_proto_rawDescGZIP() []byte {
file_greet_proto_rawDescOnce.Do(func() {
file_greet_proto_rawDescData = protoimpl.X.CompressGZIP(file_greet_proto_rawDescData)
})
return file_greet_proto_rawDescData
}
var file_greet_proto_msgTypes = make([]protoimpl.MessageInfo, 2)
var file_greet_proto_goTypes = []interface{}{
(*GreetRequest)(nil), // 0: proto.GreetRequest
(*GreetResponse)(nil), // 1: proto.GreetResponse
}
var file_greet_proto_depIdxs = []int32{
0, // 0: proto.GreetService.Greet:input_type -> proto.GreetRequest
1, // 1: proto.GreetService.Greet:output_type -> proto.GreetResponse
1, // [1:2] is the sub-list for method output_type
0, // [0:1] is the sub-list for method input_type
0, // [0:0] is the sub-list for extension type_name
0, // [0:0] is the sub-list for extension extendee
0, // [0:0] is the sub-list for field type_name
}
func init() { file_greet_proto_init() }
func file_greet_proto_init() {
if File_greet_proto != nil {
return
}
if !protoimpl.UnsafeEnabled {
file_greet_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*GreetRequest); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_greet_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*GreetResponse); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
}
type x struct{}
out := protoimpl.TypeBuilder{
File: protoimpl.DescBuilder{
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_greet_proto_rawDesc,
NumEnums: 0,
NumMessages: 2,
NumExtensions: 0,
NumServices: 1,
},
GoTypes: file_greet_proto_goTypes,
DependencyIndexes: file_greet_proto_depIdxs,
MessageInfos: file_greet_proto_msgTypes,
}.Build()
File_greet_proto = out.File
file_greet_proto_rawDesc = nil
file_greet_proto_goTypes = nil
file_greet_proto_depIdxs = nil
}

109
greet/pb/greet_grpc.pb.go Normal file
View File

@ -0,0 +1,109 @@
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
// versions:
// - protoc-gen-go-grpc v1.3.0
// - protoc v4.25.1
// source: greet.proto
package pb
import (
context "context"
grpc "google.golang.org/grpc"
codes "google.golang.org/grpc/codes"
status "google.golang.org/grpc/status"
)
// This is a compile-time assertion to ensure that this generated file
// is compatible with the grpc package it is being compiled against.
// Requires gRPC-Go v1.32.0 or later.
const _ = grpc.SupportPackageIsVersion7
const (
GreetService_Greet_FullMethodName = "/proto.GreetService/Greet"
)
// GreetServiceClient is the client API for GreetService service.
//
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
type GreetServiceClient interface {
Greet(ctx context.Context, in *GreetRequest, opts ...grpc.CallOption) (*GreetResponse, error)
}
type greetServiceClient struct {
cc grpc.ClientConnInterface
}
func NewGreetServiceClient(cc grpc.ClientConnInterface) GreetServiceClient {
return &greetServiceClient{cc}
}
func (c *greetServiceClient) Greet(ctx context.Context, in *GreetRequest, opts ...grpc.CallOption) (*GreetResponse, error) {
out := new(GreetResponse)
err := c.cc.Invoke(ctx, GreetService_Greet_FullMethodName, in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// GreetServiceServer is the server API for GreetService service.
// All implementations must embed UnimplementedGreetServiceServer
// for forward compatibility
type GreetServiceServer interface {
Greet(context.Context, *GreetRequest) (*GreetResponse, error)
mustEmbedUnimplementedGreetServiceServer()
}
// UnimplementedGreetServiceServer must be embedded to have forward compatible implementations.
type UnimplementedGreetServiceServer struct {
}
func (UnimplementedGreetServiceServer) Greet(context.Context, *GreetRequest) (*GreetResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method Greet not implemented")
}
func (UnimplementedGreetServiceServer) mustEmbedUnimplementedGreetServiceServer() {}
// UnsafeGreetServiceServer may be embedded to opt out of forward compatibility for this service.
// Use of this interface is not recommended, as added methods to GreetServiceServer will
// result in compilation errors.
type UnsafeGreetServiceServer interface {
mustEmbedUnimplementedGreetServiceServer()
}
func RegisterGreetServiceServer(s grpc.ServiceRegistrar, srv GreetServiceServer) {
s.RegisterService(&GreetService_ServiceDesc, srv)
}
func _GreetService_Greet_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(GreetRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(GreetServiceServer).Greet(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: GreetService_Greet_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(GreetServiceServer).Greet(ctx, req.(*GreetRequest))
}
return interceptor(ctx, in, info, handler)
}
// GreetService_ServiceDesc is the grpc.ServiceDesc for GreetService service.
// It's only intended for direct use with grpc.RegisterService,
// and not to be introspected or modified (even as a copy)
var GreetService_ServiceDesc = grpc.ServiceDesc{
ServiceName: "proto.GreetService",
HandlerType: (*GreetServiceServer)(nil),
Methods: []grpc.MethodDesc{
{
MethodName: "Greet",
Handler: _GreetService_Greet_Handler,
},
},
Streams: []grpc.StreamDesc{},
Metadata: "greet.proto",
}

18
greet/proto/greet.proto Normal file
View File

@ -0,0 +1,18 @@
syntax = "proto3";
package proto;
option go_package = "git.gqnotes.com/guoqiang/grpcexercises/greet/pb";
service GreetService {
rpc Greet(GreetRequest) returns(GreetResponse) {}
}
message GreetRequest {
string greeting = 1;
}
message GreetResponse {
string result = 1;
}

21
greet/server/Greet.go Normal file
View File

@ -0,0 +1,21 @@
package main
import (
"context"
"git.gqnotes.com/guoqiang/grpcexercises/greet/pb"
"log"
"time"
)
// Greet greet
func (s *Server) Greet(ctx context.Context, req *pb.GreetRequest) (*pb.GreetResponse, error) {
log.Printf("Greet function was invoked with %v\n", req)
result := "Hello " + req.GetGreeting() + ", now time is:" + time.Now().String()
res := &pb.GreetResponse{
Result: result,
}
return res, nil
}

37
greet/server/main.go Normal file
View File

@ -0,0 +1,37 @@
package main
import (
"git.gqnotes.com/guoqiang/grpcexercises/greet/pb"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
"log"
"net"
)
func main() {
// 实现服务端逻辑监听5632端口
lis, err := net.Listen("tcp", ":5632")
if err != nil {
log.Fatalf("failed to listen: %v", err)
return
}
s := grpc.NewServer()
// 注册服务
pb.RegisterGreetServiceServer(s, &Server{})
reflection.Register(s)
log.Println("grpc server start")
if err = s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
return
}
}
type Server struct {
pb.UnimplementedGreetServiceServer
}