上传有路由代码

This commit is contained in:
liwei1dao 2022-05-31 10:00:20 +08:00
parent e45751e8f7
commit 8fe33b1347
10 changed files with 413 additions and 53 deletions

View File

@ -1,9 +1,10 @@
2022/05/30 18:28:38.741 info rpcx/service.go:90 Sys log Init success !
2022/05/30 18:28:38.758 info rpcx/service.go:95 Sys event Init success !
2022/05/30 18:28:38.758 info rpcx/service.go:100 Sys registry Init success !
2022/05/30 18:28:38.758 info rpcx/service.go:105 Sys rpcx Init success !
2022/05/30 18:28:38.763 info services/servicebase.go:20 init sys.cache success!
2022/05/30 18:28:38.763 info cbase/servicebase.go:58 服务[worker_1] 初始化完成!
2022/05/30 18:28:38.763 info cbase/servicebase.go:80 服务[worker_1:1.0.0.0] 启动完成!
2022/05/30 18:28:38.764 debug m_comps/gate_comp.go:74 RegisterFunctionName:Login
2022/05/30 18:28:38.766 info registry/consul.go:253 发现新的服务【worker_1:1.0.0.0】
2022/05/31 09:47:11.195 info rpcx/service.go:90 Sys log Init success !
2022/05/31 09:47:11.211 info rpcx/service.go:95 Sys event Init success !
2022/05/31 09:47:11.211 info rpcx/service.go:100 Sys registry Init success !
2022/05/31 09:47:11.211 info rpcx/service.go:105 Sys rpcx Init success !
2022/05/31 09:47:11.216 info services/servicebase.go:20 init sys.cache success!
2022/05/31 09:47:11.216 info cbase/servicebase.go:58 服务[worker_1] 初始化完成!
2022/05/31 09:47:11.216 info cbase/servicebase.go:80 服务[worker_1:1.0.0.0] 启动完成!
2022/05/31 09:47:13.793 debug s_comps/comp_gateroute.go:53 注册用户路由【Login】
2022/05/31 09:47:13.796 info registry/consul.go:253 发现新的服务【gate_1:1.0.0.0】
2022/05/31 09:47:13.958 info registry/consul.go:253 发现新的服务【worker_1:1.0.0.0】

View File

@ -1,6 +1,10 @@
package comm
import "github.com/liwei1dao/lego/core"
import (
"reflect"
"github.com/liwei1dao/lego/core"
)
const (
SC_ServiceGateRouteComp core.S_Comps = "SC_GateRouteComp" //s_comps.ISC_GateRouteComp
@ -17,14 +21,14 @@ const ( //Rpc
type ISC_GateRouteComp interface {
core.IServiceComp
RegisterRoute()
RegisterRoute(methodName string, msg reflect.Type, fn reflect.Method)
}
//用户会话
type IUserSession interface {
GetSessionId() string
GetIP() string
GetGateId() string
GetGatewayServiceId() string
SendMsg(ServiceMethod string, msg interface{}) (err error)
Close() (err error)
}

40
comm/usersession.go Normal file
View File

@ -0,0 +1,40 @@
package comm
import (
"github.com/liwei1dao/lego/base"
)
func NewUserSession(service base.IRPCXService, ip, sessionId, gatewayServiceId string, uid uint32) IUserSession {
return &UserSession{
IP: ip,
SessionId: sessionId,
GatewayServiceId: gatewayServiceId,
UserId: uid,
service: service,
}
}
type UserSession struct {
IP string
SessionId string
GatewayServiceId string //用户所在网关服务
UserId uint32
service base.IRPCXService
}
func (this *UserSession) GetSessionId() string {
return this.SessionId
}
func (this *UserSession) GetIP() string {
return this.IP
}
func (this *UserSession) GetGatewayServiceId() string {
return this.GatewayServiceId
}
func (this *UserSession) SendMsg(ServiceMethod string, msg interface{}) (err error) {
return
}
func (this *UserSession) Close() (err error) {
return
}

View File

@ -2,6 +2,7 @@ package m_comps
import (
"context"
"go_dreamfactory/comm"
"reflect"
"unicode"
"unicode/utf8"
@ -9,10 +10,10 @@ import (
"github.com/liwei1dao/lego/base"
"github.com/liwei1dao/lego/core"
"github.com/liwei1dao/lego/core/cbase"
"github.com/liwei1dao/lego/sys/log"
)
var typeOfContext = reflect.TypeOf((*context.Context)(nil)).Elem()
var typeOfSession = reflect.TypeOf((*comm.IUserSession)(nil)).Elem()
var typeOfError = reflect.TypeOf((*error)(nil)).Elem()
/*
@ -21,36 +22,30 @@ var typeOfError = reflect.TypeOf((*error)(nil)).Elem()
type MComp_GateComp struct {
cbase.ModuleCompBase
service base.IRPCXService
comp core.IModuleComp
}
func (this *MComp_GateComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
this.ModuleCompBase.Init(service, module, comp, options)
this.service = service.(base.IRPCXService)
this.suitableMethods(reflect.TypeOf(comp))
this.comp = comp
return
}
// func (this *MComp_GateComp) Start() (err error) {
// if err = this.ModuleCompBase.Start(); err != nil {
// return
// }
// isRegisterLocalRoute := false
// //注册本地路由
func (this *MComp_GateComp) Start() (err error) {
if err = this.ModuleCompBase.Start(); err != nil {
return
}
var comp core.IServiceComp
//注册远程路由
if comp, err = this.service.GetComp(comm.SC_ServiceGateRouteComp); err != nil {
return
}
this.suitableMethods(comp.(comm.ISC_GateRouteComp), reflect.TypeOf(this.comp))
return
}
// //注册远程路由
// cc, e := this.service.GetComp(comm.SC_ServiceGateRouteComp)
// if e == nil {
// cc.(comm.ISC_GateRouteComp).RegisterRoute(this.ComId, this.comp.ReceiveMsg)
// isRegisterLocalRoute = true
// }
// if !isRegisterLocalRoute {
// return fmt.Errorf("MC_GateComp 未成功注册路由!")
// }
// return
// }
func (this *MComp_GateComp) suitableMethods(typ reflect.Type) {
func (this *MComp_GateComp) suitableMethods(scomp comm.ISC_GateRouteComp, typ reflect.Type) {
for m := 0; m < typ.NumMethod(); m++ {
method := typ.Method(m)
mtype := method.Type
@ -71,7 +66,7 @@ func (this *MComp_GateComp) suitableMethods(typ reflect.Type) {
// Second arg need not be a pointer.
argType := mtype.In(2)
if !this.isExportedOrBuiltinType(argType) {
if !argType.Implements(typeOfSession) {
continue
}
// Third arg must be a pointer.
@ -91,8 +86,7 @@ func (this *MComp_GateComp) suitableMethods(typ reflect.Type) {
if returnType := mtype.Out(0); returnType != typeOfError {
continue
}
log.Debugf("RegisterFunctionName:%s", mname)
this.service.RegisterFunctionName(mname, method)
scomp.RegisterRoute(mname, argType, method)
}
}
@ -100,8 +94,6 @@ func (this *MComp_GateComp) isExportedOrBuiltinType(t reflect.Type) bool {
for t.Kind() == reflect.Ptr {
t = t.Elem()
}
// PkgPath will be non-empty even for an exported type,
// so we need to check the type name as well.
return this.isExported(t.Name()) || t.PkgPath() == ""
}

View File

@ -18,7 +18,7 @@ func (this *User_Comp) Init(service core.IService, module core.IModule, comp cor
return
}
func (t *User_Comp) Login(ctx context.Context, req comm.IUserSession, rsp *pb.UserLoginReq) error {
func (this *User_Comp) Login(ctx context.Context, session comm.IUserSession, rsp *pb.UserLoginReq) error {
return nil
}

262
pb/comm.pb.go Normal file
View File

@ -0,0 +1,262 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.28.0
// protoc v3.20.0
// source: comm.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 UserMessage struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Ip string `protobuf:"bytes,1,opt,name=Ip,proto3" json:"Ip,omitempty"`
UserSessionId string `protobuf:"bytes,2,opt,name=UserSessionId,proto3" json:"UserSessionId,omitempty"`
UserId uint32 `protobuf:"varint,3,opt,name=UserId,proto3" json:"UserId,omitempty"`
GatewayServiceId string `protobuf:"bytes,4,opt,name=GatewayServiceId,proto3" json:"GatewayServiceId,omitempty"`
Method string `protobuf:"bytes,5,opt,name=Method,proto3" json:"Method,omitempty"`
Message []byte `protobuf:"bytes,6,opt,name=Message,proto3" json:"Message,omitempty"`
}
func (x *UserMessage) Reset() {
*x = UserMessage{}
if protoimpl.UnsafeEnabled {
mi := &file_comm_proto_msgTypes[0]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *UserMessage) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*UserMessage) ProtoMessage() {}
func (x *UserMessage) ProtoReflect() protoreflect.Message {
mi := &file_comm_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 UserMessage.ProtoReflect.Descriptor instead.
func (*UserMessage) Descriptor() ([]byte, []int) {
return file_comm_proto_rawDescGZIP(), []int{0}
}
func (x *UserMessage) GetIp() string {
if x != nil {
return x.Ip
}
return ""
}
func (x *UserMessage) GetUserSessionId() string {
if x != nil {
return x.UserSessionId
}
return ""
}
func (x *UserMessage) GetUserId() uint32 {
if x != nil {
return x.UserId
}
return 0
}
func (x *UserMessage) GetGatewayServiceId() string {
if x != nil {
return x.GatewayServiceId
}
return ""
}
func (x *UserMessage) GetMethod() string {
if x != nil {
return x.Method
}
return ""
}
func (x *UserMessage) GetMessage() []byte {
if x != nil {
return x.Message
}
return nil
}
type UserMessageReply struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Code int32 `protobuf:"varint,1,opt,name=Code,proto3" json:"Code,omitempty"`
Msg string `protobuf:"bytes,2,opt,name=Msg,proto3" json:"Msg,omitempty"`
}
func (x *UserMessageReply) Reset() {
*x = UserMessageReply{}
if protoimpl.UnsafeEnabled {
mi := &file_comm_proto_msgTypes[1]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *UserMessageReply) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*UserMessageReply) ProtoMessage() {}
func (x *UserMessageReply) ProtoReflect() protoreflect.Message {
mi := &file_comm_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 UserMessageReply.ProtoReflect.Descriptor instead.
func (*UserMessageReply) Descriptor() ([]byte, []int) {
return file_comm_proto_rawDescGZIP(), []int{1}
}
func (x *UserMessageReply) GetCode() int32 {
if x != nil {
return x.Code
}
return 0
}
func (x *UserMessageReply) GetMsg() string {
if x != nil {
return x.Msg
}
return ""
}
var File_comm_proto protoreflect.FileDescriptor
var file_comm_proto_rawDesc = []byte{
0x0a, 0x0a, 0x63, 0x6f, 0x6d, 0x6d, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0xb9, 0x01, 0x0a,
0x0b, 0x55, 0x73, 0x65, 0x72, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x12, 0x0e, 0x0a, 0x02,
0x49, 0x70, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x49, 0x70, 0x12, 0x24, 0x0a, 0x0d,
0x55, 0x73, 0x65, 0x72, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x49, 0x64, 0x18, 0x02, 0x20,
0x01, 0x28, 0x09, 0x52, 0x0d, 0x55, 0x73, 0x65, 0x72, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e,
0x49, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x18, 0x03, 0x20, 0x01,
0x28, 0x0d, 0x52, 0x06, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x12, 0x2a, 0x0a, 0x10, 0x47, 0x61,
0x74, 0x65, 0x77, 0x61, 0x79, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x49, 0x64, 0x18, 0x04,
0x20, 0x01, 0x28, 0x09, 0x52, 0x10, 0x47, 0x61, 0x74, 0x65, 0x77, 0x61, 0x79, 0x53, 0x65, 0x72,
0x76, 0x69, 0x63, 0x65, 0x49, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x4d, 0x65, 0x74, 0x68, 0x6f, 0x64,
0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x4d, 0x65, 0x74, 0x68, 0x6f, 0x64, 0x12, 0x18,
0x0a, 0x07, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x18, 0x06, 0x20, 0x01, 0x28, 0x0c, 0x52,
0x07, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x22, 0x38, 0x0a, 0x10, 0x55, 0x73, 0x65, 0x72,
0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x12, 0x12, 0x0a, 0x04,
0x43, 0x6f, 0x64, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x04, 0x43, 0x6f, 0x64, 0x65,
0x12, 0x10, 0x0a, 0x03, 0x4d, 0x73, 0x67, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x4d,
0x73, 0x67, 0x42, 0x06, 0x5a, 0x04, 0x2e, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74,
0x6f, 0x33,
}
var (
file_comm_proto_rawDescOnce sync.Once
file_comm_proto_rawDescData = file_comm_proto_rawDesc
)
func file_comm_proto_rawDescGZIP() []byte {
file_comm_proto_rawDescOnce.Do(func() {
file_comm_proto_rawDescData = protoimpl.X.CompressGZIP(file_comm_proto_rawDescData)
})
return file_comm_proto_rawDescData
}
var file_comm_proto_msgTypes = make([]protoimpl.MessageInfo, 2)
var file_comm_proto_goTypes = []interface{}{
(*UserMessage)(nil), // 0: UserMessage
(*UserMessageReply)(nil), // 1: UserMessageReply
}
var file_comm_proto_depIdxs = []int32{
0, // [0:0] is the sub-list for method output_type
0, // [0:0] 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_comm_proto_init() }
func file_comm_proto_init() {
if File_comm_proto != nil {
return
}
if !protoimpl.UnsafeEnabled {
file_comm_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*UserMessage); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_comm_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*UserMessageReply); 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_comm_proto_rawDesc,
NumEnums: 0,
NumMessages: 2,
NumExtensions: 0,
NumServices: 0,
},
GoTypes: file_comm_proto_goTypes,
DependencyIndexes: file_comm_proto_depIdxs,
MessageInfos: file_comm_proto_msgTypes,
}.Build()
File_comm_proto = out.File
file_comm_proto_rawDesc = nil
file_comm_proto_goTypes = nil
file_comm_proto_depIdxs = nil
}

View File

@ -1,3 +1,16 @@
syntax = "proto3";
option go_package = ".;pb";
message UserMessage {
string Ip = 1;
string UserSessionId = 2;
uint32 UserId = 3;
string GatewayServiceId = 4;
string Method = 5;
bytes Message = 6;
}
message UserMessageReply {
int32 Code = 1;
string Msg = 2;
}

View File

@ -25,5 +25,5 @@ def buildProto(pbpath,outpath,pbfile):
with io.open(file,"w",encoding='utf-8') as f:
f.write(file_data)
buildProto('./pb/proto','./pb','comm')
buildProto('./pb/proto','./pb','user_msg')

View File

@ -1,36 +1,82 @@
package s_comps
import (
"fmt"
"context"
"go_dreamfactory/comm"
"go_dreamfactory/pb"
"reflect"
"sync"
"github.com/liwei1dao/lego/base"
"github.com/liwei1dao/lego/core"
"github.com/liwei1dao/lego/core/cbase"
"github.com/liwei1dao/lego/sys/log"
"google.golang.org/protobuf/proto"
)
func NewGateRouteComp() comm.ISC_GateRouteComp {
comp := new(SComp_GateRouteComp)
return comp
}
type msghandle struct {
msgType reflect.Type
fn reflect.Method
}
type SComp_GateRouteComp struct {
cbase.ServiceCompBase
Service base.IRPCXService
service base.IRPCXService
mrlock sync.RWMutex
msghandles map[string]*msghandle
}
func (this *SComp_GateRouteComp) GetName() core.S_Comps {
return comm.SC_ServiceGateRouteComp
}
func (this *SComp_GateRouteComp) Init(service core.IService, comp core.IServiceComp, options core.ICompOptions) (err error) {
if s, ok := service.(base.IRPCXService); !ok {
return fmt.Errorf("SC_GateRouteComp Init service is no IRPCXService")
} else {
this.Service = s
}
err = this.ServiceCompBase.Init(service, comp, options)
this.service = service.(base.IRPCXService)
this.msghandles = make(map[string]*msghandle)
return err
}
func (this *SComp_GateRouteComp) Start() (err error) {
err = this.ServiceCompBase.Start()
// this.Service.RegisterFunctionName(string(comm.Rpc_GateRoute), this.ReceiveMsg) //注册网关路由接收接口
this.service.RegisterFunctionName(string(comm.Rpc_GateRoute), this.ReceiveMsg) //注册网关路由接收接口
return
}
// func (this *SComp_GateRouteComp) ReceiveMsg(ctx context.Context, args comm.IUserMessage, reply *Reply) error {
//注册路由
func (this *SComp_GateRouteComp) RegisterRoute(methodName string, msg reflect.Type, fn reflect.Method) {
log.Debugf("注册用户路由【%s】", methodName)
if _, ok := this.msghandles[methodName]; ok {
log.Errorf("重复 注册网关消息【%s】", methodName)
return
}
this.mrlock.Lock()
this.msghandles[methodName] = &msghandle{
msgType: msg,
fn: fn,
}
this.mrlock.Unlock()
}
// return nil
// }
func (this *SComp_GateRouteComp) ReceiveMsg(ctx context.Context, args *pb.UserMessage, reply *pb.UserMessageReply) error {
this.mrlock.RLock()
msghandle, ok := this.msghandles[args.Method]
this.mrlock.RUnlock()
if ok {
session := comm.NewUserSession(this.service, args.Ip, args.UserSessionId, args.GatewayServiceId, args.UserId)
msg := reflect.New(msghandle.msgType.Elem()).Interface()
if err := proto.Unmarshal(args.Message, msg.(proto.Message)); err != nil {
log.Errorf("UserMessage:%s Unmarshal err:%v", args.Method, err)
return err
}
msghandle.fn.Func.Call([]reflect.Value{reflect.ValueOf(ctx), reflect.ValueOf(session), reflect.ValueOf(msg)})
} else {
reply.Code = int32(core.ErrorCode_ReqParameterError)
}
return nil
}

View File

@ -4,6 +4,7 @@ import (
"flag"
"go_dreamfactory/modules/web"
"go_dreamfactory/services"
"go_dreamfactory/services/s_comps"
"github.com/liwei1dao/lego"
"github.com/liwei1dao/lego/base/rpcx"
@ -21,6 +22,7 @@ func main() {
rpcx.SetVersion("1.0.0.0"),
)
s.OnInstallComp( //装备组件
s_comps.NewGateRouteComp(),
)
lego.Run(s, //运行模块
web.NewModule())