From 436b1c7be0300e929c6a32ecf4c61f23aadb2199 Mon Sep 17 00:00:00 2001 From: yedf2 <120050102@qq.com> Date: Mon, 9 Aug 2021 13:38:47 +0800 Subject: [PATCH] grpc msg sent --- app/main.go | 3 + dtmcli/dtmcli.pb.go | 196 +++++++++++++++++++++++++++++++++--------- dtmcli/dtmcli.proto | 7 ++ dtmsvr/api_grpc.go | 8 +- dtmsvr/main.go | 16 ++-- examples/main_base.go | 12 +++ examples/types.go | 3 + 7 files changed, 188 insertions(+), 57 deletions(-) diff --git a/app/main.go b/app/main.go index 59a1f15..2cd44f6 100644 --- a/app/main.go +++ b/app/main.go @@ -53,6 +53,9 @@ func main() { } else if os.Args[1] == "msg" { // 启动msg示例 examples.MsgSetup(app) examples.MsgFireRequest() + } else if os.Args[1] == "msg_pb" { // 启动msg示例 + examples.MsgPbSetup(app) + examples.MsgPbFireRequest() } else if os.Args[1] == "all" { // 运行所有示例 examples.SagaSetup(app) examples.SagaWaitSetup(app) diff --git a/dtmcli/dtmcli.pb.go b/dtmcli/dtmcli.pb.go index 678a5da..4576116 100644 --- a/dtmcli/dtmcli.pb.go +++ b/dtmcli/dtmcli.pb.go @@ -20,6 +20,77 @@ const ( _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) ) +type DtmTransInfo struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Gid string `protobuf:"bytes,1,opt,name=Gid,proto3" json:"Gid,omitempty"` + TransType string `protobuf:"bytes,2,opt,name=TransType,proto3" json:"TransType,omitempty"` + BranchID string `protobuf:"bytes,3,opt,name=BranchID,proto3" json:"BranchID,omitempty"` + Dtm string `protobuf:"bytes,4,opt,name=Dtm,proto3" json:"Dtm,omitempty"` +} + +func (x *DtmTransInfo) Reset() { + *x = DtmTransInfo{} + if protoimpl.UnsafeEnabled { + mi := &file_dtmcli_dtmcli_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *DtmTransInfo) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*DtmTransInfo) ProtoMessage() {} + +func (x *DtmTransInfo) ProtoReflect() protoreflect.Message { + mi := &file_dtmcli_dtmcli_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 DtmTransInfo.ProtoReflect.Descriptor instead. +func (*DtmTransInfo) Descriptor() ([]byte, []int) { + return file_dtmcli_dtmcli_proto_rawDescGZIP(), []int{0} +} + +func (x *DtmTransInfo) GetGid() string { + if x != nil { + return x.Gid + } + return "" +} + +func (x *DtmTransInfo) GetTransType() string { + if x != nil { + return x.TransType + } + return "" +} + +func (x *DtmTransInfo) GetBranchID() string { + if x != nil { + return x.BranchID + } + return "" +} + +func (x *DtmTransInfo) GetDtm() string { + if x != nil { + return x.Dtm + } + return "" +} + // The request message containing the user's name. type DtmRequest struct { state protoimpl.MessageState @@ -29,14 +100,15 @@ type DtmRequest struct { Gid string `protobuf:"bytes,1,opt,name=Gid,proto3" json:"Gid,omitempty"` TransType string `protobuf:"bytes,2,opt,name=TransType,proto3" json:"TransType,omitempty"` QueryPrepared string `protobuf:"bytes,3,opt,name=QueryPrepared,proto3" json:"QueryPrepared,omitempty"` - Extra map[string]string `protobuf:"bytes,4,rep,name=Extra,proto3" json:"Extra,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"` - AppData []byte `protobuf:"bytes,5,opt,name=AppData,proto3" json:"AppData,omitempty"` + Method string `protobuf:"bytes,4,opt,name=Method,proto3" json:"Method,omitempty"` + Extra map[string]string `protobuf:"bytes,5,rep,name=Extra,proto3" json:"Extra,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"` + AppData []byte `protobuf:"bytes,6,opt,name=AppData,proto3" json:"AppData,omitempty"` } func (x *DtmRequest) Reset() { *x = DtmRequest{} if protoimpl.UnsafeEnabled { - mi := &file_dtmcli_dtmcli_proto_msgTypes[0] + mi := &file_dtmcli_dtmcli_proto_msgTypes[1] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -49,7 +121,7 @@ func (x *DtmRequest) String() string { func (*DtmRequest) ProtoMessage() {} func (x *DtmRequest) ProtoReflect() protoreflect.Message { - mi := &file_dtmcli_dtmcli_proto_msgTypes[0] + mi := &file_dtmcli_dtmcli_proto_msgTypes[1] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -62,7 +134,7 @@ func (x *DtmRequest) ProtoReflect() protoreflect.Message { // Deprecated: Use DtmRequest.ProtoReflect.Descriptor instead. func (*DtmRequest) Descriptor() ([]byte, []int) { - return file_dtmcli_dtmcli_proto_rawDescGZIP(), []int{0} + return file_dtmcli_dtmcli_proto_rawDescGZIP(), []int{1} } func (x *DtmRequest) GetGid() string { @@ -86,6 +158,13 @@ func (x *DtmRequest) GetQueryPrepared() string { return "" } +func (x *DtmRequest) GetMethod() string { + if x != nil { + return x.Method + } + return "" +} + func (x *DtmRequest) GetExtra() map[string]string { if x != nil { return x.Extra @@ -106,13 +185,14 @@ type DtmReply struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - DtmResult string `protobuf:"bytes,1,opt,name=DtmResult,proto3" json:"DtmResult,omitempty"` + DtmResult string `protobuf:"bytes,1,opt,name=DtmResult,proto3" json:"DtmResult,omitempty"` + DtmMessage string `protobuf:"bytes,2,opt,name=DtmMessage,proto3" json:"DtmMessage,omitempty"` } func (x *DtmReply) Reset() { *x = DtmReply{} if protoimpl.UnsafeEnabled { - mi := &file_dtmcli_dtmcli_proto_msgTypes[1] + mi := &file_dtmcli_dtmcli_proto_msgTypes[2] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -125,7 +205,7 @@ func (x *DtmReply) String() string { func (*DtmReply) ProtoMessage() {} func (x *DtmReply) ProtoReflect() protoreflect.Message { - mi := &file_dtmcli_dtmcli_proto_msgTypes[1] + mi := &file_dtmcli_dtmcli_proto_msgTypes[2] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -138,7 +218,7 @@ func (x *DtmReply) ProtoReflect() protoreflect.Message { // Deprecated: Use DtmReply.ProtoReflect.Descriptor instead. func (*DtmReply) Descriptor() ([]byte, []int) { - return file_dtmcli_dtmcli_proto_rawDescGZIP(), []int{1} + return file_dtmcli_dtmcli_proto_rawDescGZIP(), []int{2} } func (x *DtmReply) GetDtmResult() string { @@ -148,35 +228,52 @@ func (x *DtmReply) GetDtmResult() string { return "" } +func (x *DtmReply) GetDtmMessage() string { + if x != nil { + return x.DtmMessage + } + return "" +} + var File_dtmcli_dtmcli_proto protoreflect.FileDescriptor var file_dtmcli_dtmcli_proto_rawDesc = []byte{ 0x0a, 0x13, 0x64, 0x74, 0x6d, 0x63, 0x6c, 0x69, 0x2f, 0x64, 0x74, 0x6d, 0x63, 0x6c, 0x69, 0x2e, - 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x06, 0x64, 0x74, 0x6d, 0x63, 0x6c, 0x69, 0x22, 0xeb, 0x01, - 0x0a, 0x0a, 0x44, 0x74, 0x6d, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x10, 0x0a, 0x03, - 0x47, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x47, 0x69, 0x64, 0x12, 0x1c, - 0x0a, 0x09, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x54, 0x79, 0x70, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, - 0x09, 0x52, 0x09, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x54, 0x79, 0x70, 0x65, 0x12, 0x24, 0x0a, 0x0d, - 0x51, 0x75, 0x65, 0x72, 0x79, 0x50, 0x72, 0x65, 0x70, 0x61, 0x72, 0x65, 0x64, 0x18, 0x03, 0x20, - 0x01, 0x28, 0x09, 0x52, 0x0d, 0x51, 0x75, 0x65, 0x72, 0x79, 0x50, 0x72, 0x65, 0x70, 0x61, 0x72, - 0x65, 0x64, 0x12, 0x33, 0x0a, 0x05, 0x45, 0x78, 0x74, 0x72, 0x61, 0x18, 0x04, 0x20, 0x03, 0x28, - 0x0b, 0x32, 0x1d, 0x2e, 0x64, 0x74, 0x6d, 0x63, 0x6c, 0x69, 0x2e, 0x44, 0x74, 0x6d, 0x52, 0x65, - 0x71, 0x75, 0x65, 0x73, 0x74, 0x2e, 0x45, 0x78, 0x74, 0x72, 0x61, 0x45, 0x6e, 0x74, 0x72, 0x79, - 0x52, 0x05, 0x45, 0x78, 0x74, 0x72, 0x61, 0x12, 0x18, 0x0a, 0x07, 0x41, 0x70, 0x70, 0x44, 0x61, - 0x74, 0x61, 0x18, 0x05, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x07, 0x41, 0x70, 0x70, 0x44, 0x61, 0x74, - 0x61, 0x1a, 0x38, 0x0a, 0x0a, 0x45, 0x78, 0x74, 0x72, 0x61, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x12, - 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, - 0x79, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, - 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x3a, 0x02, 0x38, 0x01, 0x22, 0x28, 0x0a, 0x08, 0x44, - 0x74, 0x6d, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x12, 0x1c, 0x0a, 0x09, 0x44, 0x74, 0x6d, 0x52, 0x65, - 0x73, 0x75, 0x6c, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x44, 0x74, 0x6d, 0x52, - 0x65, 0x73, 0x75, 0x6c, 0x74, 0x32, 0x35, 0x0a, 0x03, 0x44, 0x74, 0x6d, 0x12, 0x2e, 0x0a, 0x04, - 0x43, 0x61, 0x6c, 0x6c, 0x12, 0x12, 0x2e, 0x64, 0x74, 0x6d, 0x63, 0x6c, 0x69, 0x2e, 0x44, 0x74, - 0x6d, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x10, 0x2e, 0x64, 0x74, 0x6d, 0x63, 0x6c, - 0x69, 0x2e, 0x44, 0x74, 0x6d, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x22, 0x00, 0x42, 0x1c, 0x5a, 0x1a, - 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x79, 0x65, 0x64, 0x66, 0x2f, - 0x64, 0x74, 0x6d, 0x2f, 0x64, 0x74, 0x6d, 0x63, 0x6c, 0x69, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, - 0x6f, 0x33, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x06, 0x64, 0x74, 0x6d, 0x63, 0x6c, 0x69, 0x22, 0x6c, 0x0a, + 0x0c, 0x44, 0x74, 0x6d, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x49, 0x6e, 0x66, 0x6f, 0x12, 0x10, 0x0a, + 0x03, 0x47, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x47, 0x69, 0x64, 0x12, + 0x1c, 0x0a, 0x09, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x54, 0x79, 0x70, 0x65, 0x18, 0x02, 0x20, 0x01, + 0x28, 0x09, 0x52, 0x09, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x54, 0x79, 0x70, 0x65, 0x12, 0x1a, 0x0a, + 0x08, 0x42, 0x72, 0x61, 0x6e, 0x63, 0x68, 0x49, 0x44, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, + 0x08, 0x42, 0x72, 0x61, 0x6e, 0x63, 0x68, 0x49, 0x44, 0x12, 0x10, 0x0a, 0x03, 0x44, 0x74, 0x6d, + 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x44, 0x74, 0x6d, 0x22, 0x83, 0x02, 0x0a, 0x0a, + 0x44, 0x74, 0x6d, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x10, 0x0a, 0x03, 0x47, 0x69, + 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x47, 0x69, 0x64, 0x12, 0x1c, 0x0a, 0x09, + 0x54, 0x72, 0x61, 0x6e, 0x73, 0x54, 0x79, 0x70, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, + 0x09, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x54, 0x79, 0x70, 0x65, 0x12, 0x24, 0x0a, 0x0d, 0x51, 0x75, + 0x65, 0x72, 0x79, 0x50, 0x72, 0x65, 0x70, 0x61, 0x72, 0x65, 0x64, 0x18, 0x03, 0x20, 0x01, 0x28, + 0x09, 0x52, 0x0d, 0x51, 0x75, 0x65, 0x72, 0x79, 0x50, 0x72, 0x65, 0x70, 0x61, 0x72, 0x65, 0x64, + 0x12, 0x16, 0x0a, 0x06, 0x4d, 0x65, 0x74, 0x68, 0x6f, 0x64, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, + 0x52, 0x06, 0x4d, 0x65, 0x74, 0x68, 0x6f, 0x64, 0x12, 0x33, 0x0a, 0x05, 0x45, 0x78, 0x74, 0x72, + 0x61, 0x18, 0x05, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x1d, 0x2e, 0x64, 0x74, 0x6d, 0x63, 0x6c, 0x69, + 0x2e, 0x44, 0x74, 0x6d, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x2e, 0x45, 0x78, 0x74, 0x72, + 0x61, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x52, 0x05, 0x45, 0x78, 0x74, 0x72, 0x61, 0x12, 0x18, 0x0a, + 0x07, 0x41, 0x70, 0x70, 0x44, 0x61, 0x74, 0x61, 0x18, 0x06, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x07, + 0x41, 0x70, 0x70, 0x44, 0x61, 0x74, 0x61, 0x1a, 0x38, 0x0a, 0x0a, 0x45, 0x78, 0x74, 0x72, 0x61, + 0x45, 0x6e, 0x74, 0x72, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, + 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, + 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x3a, 0x02, 0x38, + 0x01, 0x22, 0x48, 0x0a, 0x08, 0x44, 0x74, 0x6d, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x12, 0x1c, 0x0a, + 0x09, 0x44, 0x74, 0x6d, 0x52, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, + 0x52, 0x09, 0x44, 0x74, 0x6d, 0x52, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x12, 0x1e, 0x0a, 0x0a, 0x44, + 0x74, 0x6d, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, + 0x0a, 0x44, 0x74, 0x6d, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x32, 0x35, 0x0a, 0x03, 0x44, + 0x74, 0x6d, 0x12, 0x2e, 0x0a, 0x04, 0x43, 0x61, 0x6c, 0x6c, 0x12, 0x12, 0x2e, 0x64, 0x74, 0x6d, + 0x63, 0x6c, 0x69, 0x2e, 0x44, 0x74, 0x6d, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x10, + 0x2e, 0x64, 0x74, 0x6d, 0x63, 0x6c, 0x69, 0x2e, 0x44, 0x74, 0x6d, 0x52, 0x65, 0x70, 0x6c, 0x79, + 0x22, 0x00, 0x42, 0x1c, 0x5a, 0x1a, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, + 0x2f, 0x79, 0x65, 0x64, 0x66, 0x2f, 0x64, 0x74, 0x6d, 0x2f, 0x64, 0x74, 0x6d, 0x63, 0x6c, 0x69, + 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -191,16 +288,17 @@ func file_dtmcli_dtmcli_proto_rawDescGZIP() []byte { return file_dtmcli_dtmcli_proto_rawDescData } -var file_dtmcli_dtmcli_proto_msgTypes = make([]protoimpl.MessageInfo, 3) +var file_dtmcli_dtmcli_proto_msgTypes = make([]protoimpl.MessageInfo, 4) var file_dtmcli_dtmcli_proto_goTypes = []interface{}{ - (*DtmRequest)(nil), // 0: dtmcli.DtmRequest - (*DtmReply)(nil), // 1: dtmcli.DtmReply - nil, // 2: dtmcli.DtmRequest.ExtraEntry + (*DtmTransInfo)(nil), // 0: dtmcli.DtmTransInfo + (*DtmRequest)(nil), // 1: dtmcli.DtmRequest + (*DtmReply)(nil), // 2: dtmcli.DtmReply + nil, // 3: dtmcli.DtmRequest.ExtraEntry } var file_dtmcli_dtmcli_proto_depIdxs = []int32{ - 2, // 0: dtmcli.DtmRequest.Extra:type_name -> dtmcli.DtmRequest.ExtraEntry - 0, // 1: dtmcli.Dtm.Call:input_type -> dtmcli.DtmRequest - 1, // 2: dtmcli.Dtm.Call:output_type -> dtmcli.DtmReply + 3, // 0: dtmcli.DtmRequest.Extra:type_name -> dtmcli.DtmRequest.ExtraEntry + 1, // 1: dtmcli.Dtm.Call:input_type -> dtmcli.DtmRequest + 2, // 2: dtmcli.Dtm.Call:output_type -> dtmcli.DtmReply 2, // [2:3] is the sub-list for method output_type 1, // [1:2] is the sub-list for method input_type 1, // [1:1] is the sub-list for extension type_name @@ -215,7 +313,7 @@ func file_dtmcli_dtmcli_proto_init() { } if !protoimpl.UnsafeEnabled { file_dtmcli_dtmcli_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*DtmRequest); i { + switch v := v.(*DtmTransInfo); i { case 0: return &v.state case 1: @@ -227,6 +325,18 @@ func file_dtmcli_dtmcli_proto_init() { } } file_dtmcli_dtmcli_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*DtmRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_dtmcli_dtmcli_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*DtmReply); i { case 0: return &v.state @@ -245,7 +355,7 @@ func file_dtmcli_dtmcli_proto_init() { GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_dtmcli_dtmcli_proto_rawDesc, NumEnums: 0, - NumMessages: 3, + NumMessages: 4, NumExtensions: 0, NumServices: 1, }, diff --git a/dtmcli/dtmcli.proto b/dtmcli/dtmcli.proto index 6bae1df..4ce0802 100644 --- a/dtmcli/dtmcli.proto +++ b/dtmcli/dtmcli.proto @@ -9,6 +9,13 @@ service Dtm { rpc Call(DtmRequest) returns (DtmReply) {} } +message DtmTransInfo { + string Gid = 1; + string TransType = 2; + string BranchID = 3; + string Dtm = 4; +} + // The request message containing the user's name. message DtmRequest { string Gid = 1; diff --git a/dtmsvr/api_grpc.go b/dtmsvr/api_grpc.go index 78461a6..3470969 100644 --- a/dtmsvr/api_grpc.go +++ b/dtmsvr/api_grpc.go @@ -7,13 +7,13 @@ import ( pb "github.com/yedf/dtm/dtmcli" ) -// server is used to implement helloworld.GreeterServer. -type server struct { +// dtmServer is used to implement helloworld.GreeterServer. +type dtmServer struct { pb.UnimplementedDtmServer } // SayHello implements helloworld.GreeterServer -func (s *server) Call(ctx context.Context, in *pb.DtmRequest) (*pb.DtmReply, error) { +func (s *dtmServer) Call(ctx context.Context, in *pb.DtmRequest) (*pb.DtmReply, error) { log.Printf("Received: %v", in) - return &pb.DtmReply{}, nil + return &pb.DtmReply{DtmResult: "SUCCESS", DtmMessage: "ok"}, nil } diff --git a/dtmsvr/main.go b/dtmsvr/main.go index aa3f262..1aac341 100644 --- a/dtmsvr/main.go +++ b/dtmsvr/main.go @@ -2,7 +2,6 @@ package dtmsvr import ( "fmt" - "log" "net" "time" @@ -13,7 +12,7 @@ import ( ) var dtmsvrPort = 8080 -var dtmsvrGrpcPort = 50051 +var dtmsvrGrpcPort = 50081 // StartSvr StartSvr func StartSvr() { @@ -24,16 +23,13 @@ func StartSvr() { go app.Run(fmt.Sprintf(":%d", dtmsvrPort)) lis, err := net.Listen("tcp", fmt.Sprintf(":%d", dtmsvrGrpcPort)) - if err != nil { - log.Fatalf("failed to listen: %v", err) - } + dtmcli.FatalIfError(err) s := grpc.NewServer() - dtmcli.RegisterDtmServer(s, &server{}) - log.Printf("server listening at %v", lis.Addr()) + dtmcli.RegisterDtmServer(s, &dtmServer{}) + dtmcli.Logf("grpc listening at %v", lis.Addr()) go func() { - if err := s.Serve(lis); err != nil { - log.Fatalf("failed to serve: %v", err) - } + err := s.Serve(lis) + dtmcli.FatalIfError(err) }() time.Sleep(100 * time.Millisecond) diff --git a/examples/main_base.go b/examples/main_base.go index 1d63caf..23921a6 100644 --- a/examples/main_base.go +++ b/examples/main_base.go @@ -7,6 +7,7 @@ import ( "github.com/gin-gonic/gin" "github.com/yedf/dtm/common" "github.com/yedf/dtm/dtmcli" + grpc "google.golang.org/grpc" ) const ( @@ -14,11 +15,16 @@ const ( BusiAPI = "/api/busi" // BusiPort busi server port BusiPort = 8081 + // BusiPbPort busi server port + BusiPbPort = 50081 ) // Busi busi service url prefix var Busi string = fmt.Sprintf("http://localhost:%d%s", BusiPort, BusiAPI) +// DtmClient grpc client for dtm +var DtmClient dtmcli.DtmClient = nil + // BaseAppStartup base app startup func BaseAppStartup() *gin.Engine { dtmcli.Logf("examples starting") @@ -26,6 +32,12 @@ func BaseAppStartup() *gin.Engine { BaseAddRoute(app) dtmcli.Logf("Starting busi at: %d", BusiPort) go app.Run(fmt.Sprintf(":%d", BusiPort)) + + conn, err := grpc.Dial(DtmGrpcServer, grpc.WithInsecure(), grpc.WithBlock()) + dtmcli.FatalIfError(err) + DtmClient = dtmcli.NewDtmClient(conn) + dtmcli.Logf("dtm client inited") + time.Sleep(100 * time.Millisecond) return app } diff --git a/examples/types.go b/examples/types.go index 70d08db..25134b8 100644 --- a/examples/types.go +++ b/examples/types.go @@ -17,6 +17,9 @@ type M = map[string]interface{} // DtmServer dtm service address const DtmServer = "http://localhost:8080/api/dtmsvr" +// DtmGrpcServer dtm grpc service address +const DtmGrpcServer = "localhost:50081" + // TransReq transaction request payload type TransReq struct { Amount int `json:"amount"`