Skip to content

Commit

Permalink
调整:注册实例移到单独的方法中
Browse files Browse the repository at this point in the history
  • Loading branch information
steden committed Sep 26, 2023
1 parent aeb68e0 commit e4c0b61
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 33 deletions.
35 changes: 2 additions & 33 deletions module.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package rabbit

import (
"github.com/farseer-go/fs/configure"
"github.com/farseer-go/fs/container"
"github.com/farseer-go/fs/core"
"github.com/farseer-go/fs/flog"
"github.com/farseer-go/fs/modules"
)

Expand All @@ -17,35 +14,7 @@ func (module Module) DependsModule() []modules.FarseerModule {

func (module Module) Initialize() {
rabbitConfigs := configure.ParseConfigs[rabbitConfig]("Rabbit")
for _, rabbitConfig := range rabbitConfigs {
if rabbitConfig.Server.Server == "" {
_ = flog.Error("Rabbit配置缺少Server节点")
continue
}
if rabbitConfig.Server.MaxChannelCount == 0 {
rabbitConfig.Server.MaxChannelCount = 2048
}
if rabbitConfig.Server.MinChannelCount == 0 {
rabbitConfig.Server.MinChannelCount = 10
}

// 遍历交换器
for _, exchange := range rabbitConfig.Exchange {
if exchange.ExchangeName == "" {
_ = flog.Errorf("Rabbit配置:%s 缺少ExchangeName", rabbitConfig.Server.Server)
continue
}

// 注册生产者
productIns := newProduct(rabbitConfig.Server, exchange)
container.RegisterInstance[IProduct](productIns, exchange.ExchangeName)

// 注册消费者
consumerIns := newConsumer(rabbitConfig.Server, exchange)
container.RegisterInstance[IConsumer](consumerIns, exchange.ExchangeName)
}

// 注册健康检查
container.RegisterInstance[core.IHealthCheck](&healthCheck{server: rabbitConfig.Server}, rabbitConfig.Server.Server)
for _, config := range rabbitConfigs {
Register(config)
}
}
39 changes: 39 additions & 0 deletions register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package rabbit

import (
"github.com/farseer-go/fs/container"
"github.com/farseer-go/fs/core"
"github.com/farseer-go/fs/flog"
)

func Register(config rabbitConfig) {
if config.Server.Server == "" {
_ = flog.Error("Rabbit配置缺少Server节点")
return
}
if config.Server.MaxChannelCount == 0 {
config.Server.MaxChannelCount = 2048
}
if config.Server.MinChannelCount == 0 {
config.Server.MinChannelCount = 10
}

// 遍历交换器
for _, exchange := range config.Exchange {
if exchange.ExchangeName == "" {
_ = flog.Errorf("Rabbit配置:%s 缺少ExchangeName", config.Server.Server)
continue
}

// 注册生产者
productIns := newProduct(config.Server, exchange)
container.RegisterInstance[IProduct](productIns, exchange.ExchangeName)

// 注册消费者
consumerIns := newConsumer(config.Server, exchange)
container.RegisterInstance[IConsumer](consumerIns, exchange.ExchangeName)
}

// 注册健康检查
container.RegisterInstance[core.IHealthCheck](&healthCheck{server: config.Server}, config.Server.Server)
}

0 comments on commit e4c0b61

Please sign in to comment.