diff --git a/Makefile b/Makefile index a8e3622c..35491979 100644 --- a/Makefile +++ b/Makefile @@ -10,9 +10,19 @@ build: dev_tool: # install protoc go install github.com/golang/protobuf/protoc-gen-go@v1.5.2 + go install github.com/byebyebruce/natsrpc/cmd/protoc-gen-natsrpc@develop + +test: + go test ./... .PHONY: gen # gen 生成代码 gen: - cd protocol/proto && make gen + protoc \ + --proto_path=gs/api \ + --go_out=paths=source_relative:gs/api \ + --natsrpc_out=paths=source_relative:gs/api \ + gs/api/*.proto + #cd protocol/proto && make gen + diff --git a/cmd/gm/application.toml b/cmd/gm/application.toml index 7cb48b62..770ee5ad 100644 --- a/cmd/gm/application.toml +++ b/cmd/gm/application.toml @@ -9,4 +9,5 @@ track_line = true url = "mongodb://mongo:27017" [mq] -nats_url = "nats://nats1:4222,nats://nats2:4222,nats://nats3:4222" +#nats_url = "nats://nats1:4222,nats://nats2:4222,nats://nats3:4222" +nats_url = "nats://127.0.0.1:4222" diff --git a/gm/app/app.go b/gm/app/app.go index f9e65c3e..f389ab32 100644 --- a/gm/app/app.go +++ b/gm/app/app.go @@ -9,7 +9,10 @@ import ( "hk4e/common/config" "hk4e/gm/controller" + "hk4e/gm/rpc_client" "hk4e/pkg/logger" + + "github.com/nats-io/nats.go" ) func Run(ctx context.Context, configFile string) error { @@ -18,7 +21,18 @@ func Run(ctx context.Context, configFile string) error { logger.InitLogger("gm", config.CONF.Logger) logger.LOG.Info("gm start") - _ = controller.NewController() + conn, err := nats.Connect(config.CONF.MQ.NatsUrl) + if err != nil { + logger.LOG.Error("connect nats error: %v", err) + return err + } + defer conn.Close() + + rpc, err := rpc_client.New(conn) + if err != nil { + return err + } + _ = controller.NewController(rpc) c := make(chan os.Signal, 1) signal.Notify(c, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT) diff --git a/gm/controller/controller.go b/gm/controller/controller.go index b2827402..6b028dbb 100644 --- a/gm/controller/controller.go +++ b/gm/controller/controller.go @@ -5,16 +5,19 @@ import ( "strconv" "hk4e/common/config" + "hk4e/gm/rpc_client" "hk4e/pkg/logger" "github.com/gin-gonic/gin" ) type Controller struct { + rpc *rpc_client.Client } -func NewController() (r *Controller) { +func NewController(rpc *rpc_client.Client) (r *Controller) { r = new(Controller) + r.rpc = rpc go r.registerRouter() return r } diff --git a/gm/controller/gm_controller.go b/gm/controller/gm_controller.go index f52f746d..28c46437 100644 --- a/gm/controller/gm_controller.go +++ b/gm/controller/gm_controller.go @@ -1,6 +1,9 @@ package controller import ( + "net/http" + + "hk4e/gs/api" "hk4e/pkg/logger" "github.com/gin-gonic/gin" @@ -17,5 +20,14 @@ func (c *Controller) gmCmd(context *gin.Context) { if err != nil { return } + rep, err := c.rpc.Cmd(context.Request.Context(), &api.CmdRequest{ + FuncName: gmCmdReq.FuncName, + Param: gmCmdReq.Param, + }) + if err != nil { + context.JSON(http.StatusInternalServerError, err) + return + } + context.JSON(http.StatusOK, rep) logger.LOG.Info("%v", gmCmdReq) } diff --git a/gm/rpc_client/client.go b/gm/rpc_client/client.go new file mode 100644 index 00000000..388591ec --- /dev/null +++ b/gm/rpc_client/client.go @@ -0,0 +1,29 @@ +// Package rpc_client rpc客户端 +package rpc_client + +import ( + "hk4e/gs/api" + + "github.com/nats-io/nats.go" + "github.com/nats-io/nats.go/encoders/protobuf" +) + +// Client rpc客户端 +type Client struct { + api.GMNATSRPCClient +} + +// New 构造 +func New(conn *nats.Conn) (*Client, error) { + enc, err := nats.NewEncodedConn(conn, protobuf.PROTOBUF_ENCODER) + if err != nil { + return nil, err + } + cli, err := api.NewGMNATSRPCClient(enc) + if err != nil { + return nil, err + } + return &Client{ + GMNATSRPCClient: cli, + }, nil +} diff --git a/go.mod b/go.mod index dff033c6..0566225e 100644 --- a/go.mod +++ b/go.mod @@ -41,7 +41,10 @@ require github.com/golang-jwt/jwt/v4 v4.4.0 // csv require github.com/jszwec/csvutil v1.7.1 -require github.com/spf13/cobra v1.6.1 +require ( + github.com/byebyebruce/natsrpc v0.5.5-0.20221125150611-56cd29a4e335 + github.com/spf13/cobra v1.6.1 +) require ( github.com/davecgh/go-spew v1.1.1 // indirect @@ -50,7 +53,7 @@ require ( github.com/go-playground/universal-translator v0.17.0 // indirect github.com/go-playground/validator/v10 v10.2.0 // indirect github.com/go-stack/stack v1.8.0 // indirect - github.com/golang/protobuf v1.5.0 // indirect + github.com/golang/protobuf v1.5.2 // indirect github.com/golang/snappy v0.0.1 // indirect github.com/gorilla/websocket v1.4.2 // indirect github.com/inconshreveable/mousetrap v1.0.1 // indirect diff --git a/go.sum b/go.sum index d9b7a1c0..179b1f46 100644 --- a/go.sum +++ b/go.sum @@ -3,6 +3,10 @@ github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/arl/statsviz v0.5.1 h1:3HY0ZEB738JtguWsD1Tf1pFJZiCcWUmYRq/3OTYKaSI= github.com/arl/statsviz v0.5.1/go.mod h1:zDnjgRblGm1Dyd7J5YlbH7gM1/+HRC+SfkhZhQb5AnM= +github.com/byebyebruce/natsrpc v0.5.4 h1:K1nrEzohCKVPoJUNGXyrzi2ITh5oPr5MpSTSLcD1Pac= +github.com/byebyebruce/natsrpc v0.5.4/go.mod h1:w61gLVOQWr/Tq/1wxSOMLxDPbH66rEo8jEHMh7j3qjo= +github.com/byebyebruce/natsrpc v0.5.5-0.20221125150611-56cd29a4e335 h1:V5qahA5kDL/TBnlwvYjemR5du/uQ7q75qkBBlTc4rXI= +github.com/byebyebruce/natsrpc v0.5.5-0.20221125150611-56cd29a4e335/go.mod h1:w61gLVOQWr/Tq/1wxSOMLxDPbH66rEo8jEHMh7j3qjo= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= @@ -40,8 +44,9 @@ github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrU github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= -github.com/golang/protobuf v1.5.0 h1:LUVKkCeviFUMKqHa4tXIIij/lbhnMbP7Fn5wKdKkRh4= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw= +github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4= github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= @@ -184,8 +189,8 @@ golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3 golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= golang.org/x/tools v0.0.0-20190531172133-b3315ee88b7d/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= @@ -201,6 +206,7 @@ google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miE google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= 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.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw= google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/gs/api/api.natsrpc.pb.go b/gs/api/api.natsrpc.pb.go new file mode 100644 index 00000000..ffc14ea5 --- /dev/null +++ b/gs/api/api.natsrpc.pb.go @@ -0,0 +1,56 @@ +// Code generated by protoc-gen-natsrpc. DO NOT EDIT. +// versions: +// - protoc-gen-natsrpc v0.5.0 +// source: api.proto + +package api + +import ( + context "context" + fmt "fmt" + natsrpc "github.com/byebyebruce/natsrpc" + nats_go "github.com/nats-io/nats.go" + proto "google.golang.org/protobuf/proto" +) + +var _ = new(context.Context) +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = natsrpc.Version +var _ = nats_go.Version + +// GM 服务 +type GMNATSRPCServer interface { + Cmd(ctx context.Context, req *CmdRequest) (*CmdReply, error) +} + +// RegisterGMNATSRPCServer register GM service +func RegisterGMNATSRPCServer(server *natsrpc.Server, s GMNATSRPCServer, opts ...natsrpc.ServiceOption) (natsrpc.IService, error) { + return server.Register("hk4e.gs.api.GM", s, opts...) +} + +// GM 服务 +type GMNATSRPCClient interface { + Cmd(ctx context.Context, req *CmdRequest, opt ...natsrpc.CallOption) (*CmdReply, error) +} + +type _GMNATSRPCClient struct { + c *natsrpc.Client +} + +// NewGMNATSRPCClient +func NewGMNATSRPCClient(enc *nats_go.EncodedConn, opts ...natsrpc.ClientOption) (GMNATSRPCClient, error) { + c, err := natsrpc.NewClient(enc, "hk4e.gs.api.GM", opts...) + if err != nil { + return nil, err + } + ret := &_GMNATSRPCClient{ + c: c, + } + return ret, nil +} +func (c *_GMNATSRPCClient) Cmd(ctx context.Context, req *CmdRequest, opt ...natsrpc.CallOption) (*CmdReply, error) { + rep := &CmdReply{} + err := c.c.Request(ctx, "Cmd", req, rep, opt...) + return rep, err +} diff --git a/gs/api/api.pb.go b/gs/api/api.pb.go new file mode 100644 index 00000000..b0f70d44 --- /dev/null +++ b/gs/api/api.pb.go @@ -0,0 +1,228 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.26.0 +// protoc v3.14.0 +// source: api.proto + +package api + +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 CmdRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + FuncName string `protobuf:"bytes,1,opt,name=func_name,json=funcName,proto3" json:"func_name,omitempty"` + Param []string `protobuf:"bytes,2,rep,name=param,proto3" json:"param,omitempty"` +} + +func (x *CmdRequest) Reset() { + *x = CmdRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_api_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *CmdRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*CmdRequest) ProtoMessage() {} + +func (x *CmdRequest) ProtoReflect() protoreflect.Message { + mi := &file_api_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 CmdRequest.ProtoReflect.Descriptor instead. +func (*CmdRequest) Descriptor() ([]byte, []int) { + return file_api_proto_rawDescGZIP(), []int{0} +} + +func (x *CmdRequest) GetFuncName() string { + if x != nil { + return x.FuncName + } + return "" +} + +func (x *CmdRequest) GetParam() []string { + if x != nil { + return x.Param + } + return nil +} + +type CmdReply struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Code int32 `protobuf:"varint,1,opt,name=code,proto3" json:"code,omitempty"` // 0 表示成功 + Message string `protobuf:"bytes,2,opt,name=message,proto3" json:"message,omitempty"` +} + +func (x *CmdReply) Reset() { + *x = CmdReply{} + if protoimpl.UnsafeEnabled { + mi := &file_api_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *CmdReply) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*CmdReply) ProtoMessage() {} + +func (x *CmdReply) ProtoReflect() protoreflect.Message { + mi := &file_api_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 CmdReply.ProtoReflect.Descriptor instead. +func (*CmdReply) Descriptor() ([]byte, []int) { + return file_api_proto_rawDescGZIP(), []int{1} +} + +func (x *CmdReply) GetCode() int32 { + if x != nil { + return x.Code + } + return 0 +} + +func (x *CmdReply) GetMessage() string { + if x != nil { + return x.Message + } + return "" +} + +var File_api_proto protoreflect.FileDescriptor + +var file_api_proto_rawDesc = []byte{ + 0x0a, 0x09, 0x61, 0x70, 0x69, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x06, 0x67, 0x73, 0x2e, + 0x61, 0x70, 0x69, 0x22, 0x3f, 0x0a, 0x0a, 0x43, 0x6d, 0x64, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, + 0x74, 0x12, 0x1b, 0x0a, 0x09, 0x66, 0x75, 0x6e, 0x63, 0x5f, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, + 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x66, 0x75, 0x6e, 0x63, 0x4e, 0x61, 0x6d, 0x65, 0x12, 0x14, + 0x0a, 0x05, 0x70, 0x61, 0x72, 0x61, 0x6d, 0x18, 0x02, 0x20, 0x03, 0x28, 0x09, 0x52, 0x05, 0x70, + 0x61, 0x72, 0x61, 0x6d, 0x22, 0x38, 0x0a, 0x08, 0x43, 0x6d, 0x64, 0x52, 0x65, 0x70, 0x6c, 0x79, + 0x12, 0x12, 0x0a, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x04, + 0x63, 0x6f, 0x64, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x18, + 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x32, 0x33, + 0x0a, 0x02, 0x47, 0x4d, 0x12, 0x2d, 0x0a, 0x03, 0x43, 0x6d, 0x64, 0x12, 0x12, 0x2e, 0x67, 0x73, + 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x43, 0x6d, 0x64, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, + 0x10, 0x2e, 0x67, 0x73, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x43, 0x6d, 0x64, 0x52, 0x65, 0x70, 0x6c, + 0x79, 0x22, 0x00, 0x42, 0x11, 0x5a, 0x0f, 0x68, 0x6b, 0x34, 0x65, 0x2f, 0x67, 0x73, 0x2f, 0x61, + 0x70, 0x69, 0x3b, 0x61, 0x70, 0x69, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_api_proto_rawDescOnce sync.Once + file_api_proto_rawDescData = file_api_proto_rawDesc +) + +func file_api_proto_rawDescGZIP() []byte { + file_api_proto_rawDescOnce.Do(func() { + file_api_proto_rawDescData = protoimpl.X.CompressGZIP(file_api_proto_rawDescData) + }) + return file_api_proto_rawDescData +} + +var file_api_proto_msgTypes = make([]protoimpl.MessageInfo, 2) +var file_api_proto_goTypes = []interface{}{ + (*CmdRequest)(nil), // 0: gs.api.CmdRequest + (*CmdReply)(nil), // 1: gs.api.CmdReply +} +var file_api_proto_depIdxs = []int32{ + 0, // 0: gs.api.GM.Cmd:input_type -> gs.api.CmdRequest + 1, // 1: gs.api.GM.Cmd:output_type -> gs.api.CmdReply + 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_api_proto_init() } +func file_api_proto_init() { + if File_api_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_api_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*CmdRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_api_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*CmdReply); 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_api_proto_rawDesc, + NumEnums: 0, + NumMessages: 2, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_api_proto_goTypes, + DependencyIndexes: file_api_proto_depIdxs, + MessageInfos: file_api_proto_msgTypes, + }.Build() + File_api_proto = out.File + file_api_proto_rawDesc = nil + file_api_proto_goTypes = nil + file_api_proto_depIdxs = nil +} diff --git a/gs/api/api.proto b/gs/api/api.proto new file mode 100644 index 00000000..66083ee1 --- /dev/null +++ b/gs/api/api.proto @@ -0,0 +1,22 @@ +syntax = "proto3"; + +package gs.api; +option go_package = "hk4e/gs/api;api"; + +//import "natsrpc.proto"; +//import "testdata.proto"; + +// GM 服务 +service GM { + rpc Cmd (CmdRequest) returns (CmdReply) {} +} + +message CmdRequest { + string func_name = 1; + repeated string param = 2; +} + +message CmdReply { + int32 code = 1; // 0 表示成功 + string message = 2; +} diff --git a/gs/app/app.go b/gs/app/app.go index 5f63b9d2..610f8665 100644 --- a/gs/app/app.go +++ b/gs/app/app.go @@ -14,8 +14,11 @@ import ( "hk4e/gs/dao" "hk4e/gs/game" "hk4e/gs/mq" + "hk4e/gs/service" "hk4e/pkg/logger" "hk4e/protocol/cmd" + + "github.com/nats-io/nats.go" ) func Run(ctx context.Context, configFile string) error { @@ -28,6 +31,13 @@ func Run(ctx context.Context, configFile string) error { gdc.InitGameDataConfig() + conn, err := nats.Connect(config.CONF.MQ.NatsUrl) + if err != nil { + logger.LOG.Error("connect nats error: %v", err) + return err + } + defer conn.Close() + db, err := dao.NewDao() if err != nil { panic(err) @@ -37,7 +47,7 @@ func Run(ctx context.Context, configFile string) error { netMsgInput := make(chan *cmd.NetMsg, 10000) netMsgOutput := make(chan *cmd.NetMsg, 10000) - messageQueue := mq.NewMessageQueue(netMsgInput, netMsgOutput) + messageQueue := mq.NewMessageQueue(conn, netMsgInput, netMsgOutput) messageQueue.Start() defer messageQueue.Close() @@ -48,6 +58,12 @@ func Run(ctx context.Context, configFile string) error { c := make(chan os.Signal, 1) signal.Notify(c, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT) + s, err := service.NewService(conn) + if err != nil { + return err + } + defer s.Close() + for { select { case <-ctx.Done(): diff --git a/gs/mq/mq.go b/gs/mq/mq.go index fa1cba93..1cf2a933 100644 --- a/gs/mq/mq.go +++ b/gs/mq/mq.go @@ -1,7 +1,6 @@ package mq import ( - "hk4e/common/config" "hk4e/pkg/logger" "hk4e/protocol/cmd" @@ -18,16 +17,11 @@ type MessageQueue struct { cmdProtoMap *cmd.CmdProtoMap } -func NewMessageQueue(netMsgInput chan *cmd.NetMsg, netMsgOutput chan *cmd.NetMsg) (r *MessageQueue) { +func NewMessageQueue(conn *nats.Conn, netMsgInput chan *cmd.NetMsg, netMsgOutput chan *cmd.NetMsg) (r *MessageQueue) { r = new(MessageQueue) - conn, err := nats.Connect(config.CONF.MQ.NatsUrl) - if err != nil { - logger.LOG.Error("connect nats error: %v", err) - return nil - } r.natsConn = conn r.natsMsgChan = make(chan *nats.Msg, 10000) - _, err = r.natsConn.ChanSubscribe("GS_HK4E", r.natsMsgChan) + _, err := r.natsConn.ChanSubscribe("GS_HK4E", r.natsMsgChan) if err != nil { logger.LOG.Error("nats subscribe error: %v", err) return nil diff --git a/gs/service/gm.go b/gs/service/gm.go new file mode 100644 index 00000000..6b481feb --- /dev/null +++ b/gs/service/gm.go @@ -0,0 +1,23 @@ +package service + +import ( + "context" + "fmt" + + "hk4e/gs/api" + "hk4e/gs/game" +) + +var _ api.GMNATSRPCServer = (*GMService)(nil) + +type GMService struct { + g *game.GameManager +} + +func (s *GMService) Cmd(ctx context.Context, req *api.CmdRequest) (*api.CmdReply, error) { + //TODO implement me + fmt.Println("Cmd", req.FuncName, req.Param) + return &api.CmdReply{ + Message: "TODO", + }, nil +} diff --git a/gs/service/service.go b/gs/service/service.go new file mode 100644 index 00000000..ab2f3807 --- /dev/null +++ b/gs/service/service.go @@ -0,0 +1,34 @@ +package service + +import ( + "hk4e/gs/api" + + "github.com/byebyebruce/natsrpc" + "github.com/nats-io/nats.go" + "github.com/nats-io/nats.go/encoders/protobuf" +) + +type Service struct{} + +func NewService(conn *nats.Conn) (*Service, error) { + enc, err := nats.NewEncodedConn(conn, protobuf.PROTOBUF_ENCODER) + if err != nil { + return nil, err + } + svr, err := natsrpc.NewServer(enc) + if err != nil { + return nil, err + } + gs := &GMService{} + _, err = api.RegisterGMNATSRPCServer(svr, gs) + if err != nil { + return nil, err + } + return &Service{}, nil +} + +// Close 关闭 +func (s *Service) Close() { + // TODO + +} diff --git a/third_party/README.md b/third_party/README.md new file mode 100644 index 00000000..baa8ba71 --- /dev/null +++ b/third_party/README.md @@ -0,0 +1,2 @@ +# third_party +本文件夹为第三方依赖,任何内容都不可修改 \ No newline at end of file diff --git a/third_party/natsrpc/natsrpc.proto b/third_party/natsrpc/natsrpc.proto new file mode 100644 index 00000000..fd7a0f65 --- /dev/null +++ b/third_party/natsrpc/natsrpc.proto @@ -0,0 +1,31 @@ +syntax = "proto3"; + +package natsrpc; +option go_package = "github.com/byebyebruce/natsrpc;natsrpc"; + +import "google/protobuf/descriptor.proto"; + +extend google.protobuf.ServiceOptions { + bool serviceAsync = 43230; // service异步handler + bool clientAsync = 43231; // client异步请求 +} + +extend google.protobuf.MethodOptions { + bool publish = 2360; // false表示request(需要返回值),true表示广播(不需要返回值) +} + +// Empty 空值 +message Empty {} + +// Request 请求 +message Request { + bytes payload = 1; // 包体 + map header = 2; // 包头 +} + +// Reply 返回 +message Reply { + bytes payload = 1; // 包体 + string error = 2; // 错误 +} +