From d531939903e14fdc191263433214515fc12d8a93 Mon Sep 17 00:00:00 2001 From: kercylan98 Date: Wed, 31 Jan 2024 12:09:20 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=A7=BB=E9=99=A4=20modular=20=E5=8C=85?= =?UTF-8?q?=E7=9A=84=E8=87=AA=E5=8A=A8=E6=B3=A8=E5=85=A5=EF=BC=8C=E4=BC=98?= =?UTF-8?q?=E5=8C=96=20modular.Service=20=E6=8E=A5=E5=8F=A3=E8=AF=B4?= =?UTF-8?q?=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modular/example/internal/service/expose/attack.go | 2 ++ modular/example/internal/service/expose/login.go | 2 ++ .../example/internal/service/services/attack/attack.go | 5 +++-- modular/example/internal/service/services/login/login.go | 5 +++-- modular/modular.go | 9 --------- modular/service.go | 7 +++++++ 6 files changed, 17 insertions(+), 13 deletions(-) diff --git a/modular/example/internal/service/expose/attack.go b/modular/example/internal/service/expose/attack.go index d7b2766..77b4fbc 100644 --- a/modular/example/internal/service/expose/attack.go +++ b/modular/example/internal/service/expose/attack.go @@ -1,5 +1,7 @@ package expose +var AttackExpose Attack + type Attack interface { Name() string } diff --git a/modular/example/internal/service/expose/login.go b/modular/example/internal/service/expose/login.go index 6beef39..22659c4 100644 --- a/modular/example/internal/service/expose/login.go +++ b/modular/example/internal/service/expose/login.go @@ -1,5 +1,7 @@ package expose +var LoginExpose Login + type Login interface { Name() string } diff --git a/modular/example/internal/service/services/attack/attack.go b/modular/example/internal/service/services/attack/attack.go index 4b11524..bb7bbe7 100644 --- a/modular/example/internal/service/services/attack/attack.go +++ b/modular/example/internal/service/services/attack/attack.go @@ -11,15 +11,16 @@ type Service struct { } func (a *Service) OnInit() { + expose.AttackExpose = a a.name = "attack" } func (a *Service) OnPreload() { - fmt.Println(a.name, "call", a.Login.Name()) + a.Login = expose.LoginExpose } func (a *Service) OnMount() { - + fmt.Println("attack service mounted, call", a.Login.Name(), "service") } func (a *Service) Name() string { diff --git a/modular/example/internal/service/services/login/login.go b/modular/example/internal/service/services/login/login.go index 99b3de3..002f48f 100644 --- a/modular/example/internal/service/services/login/login.go +++ b/modular/example/internal/service/services/login/login.go @@ -11,15 +11,16 @@ type Service struct { } func (l *Service) OnInit() { + expose.LoginExpose = l l.name = "login" } func (l *Service) OnPreload() { - fmt.Println(l.name, "call", l.Attack.Name()) + l.Attack = expose.AttackExpose } func (l *Service) OnMount() { - + fmt.Println("attack service mounted, call", l.Attack.Name(), "service") } func (l *Service) Name() string { diff --git a/modular/modular.go b/modular/modular.go index 58fc55d..e695f14 100644 --- a/modular/modular.go +++ b/modular/modular.go @@ -47,15 +47,6 @@ func Run() { // OnPreload for i := 0; i < len(m.services); i++ { s := m.services[i] - for f := 0; f < s.vof.Elem().NumField(); f++ { - field := s.vof.Elem().Field(f) - for _, v := range tvm { - if v.Type().AssignableTo(field.Type()) { - field.Set(v) - break - } - } - } s.instance.OnPreload() log.Info(fmt.Sprintf("service %s preloaded", s.name)) } diff --git a/modular/service.go b/modular/service.go index 55b443e..eea158b 100644 --- a/modular/service.go +++ b/modular/service.go @@ -4,6 +4,13 @@ import "reflect" // Service 模块化服务接口,所有的服务均需要实现该接口,在服务的生命周期内发生任何错误均应通过 panic 阻止服务继续运行 // - 生命周期示例: OnInit -> OnPreload -> OnMount +// +// 在 Golang 中,包与包之间互相引用会导致循环依赖,因此在模块化应用程序中,所有的服务均不应该直接引用其他服务。 +// +// 服务应该在 OnInit 阶段将不依赖其他服务的内容初始化完成,并且如果服务需要暴露给其他服务调用,那么也应该在 OnInit 阶段完成对外暴露。 +// - 暴露方式可参考 modular/example +// +// 在 OnPreload 阶段,服务应该完成对其依赖服务的依赖注入,最终在 OnMount 阶段完成对服务功能的定义、路由的声明等。 type Service interface { // OnInit 服务初始化阶段,该阶段不应该依赖其他任何服务 OnInit()