2024-06-12 13:46:14 +08:00
|
|
|
package mqServer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"trasfer_middleware/cmd/rpc/etc"
|
|
|
|
"trasfer_middleware/cmd/rpc/internal/queue/mq"
|
2024-06-18 16:34:14 +08:00
|
|
|
mq1 "trasfer_middleware/until/mq"
|
2024-06-12 13:46:14 +08:00
|
|
|
"trasfer_middleware/until/sysLog"
|
|
|
|
)
|
|
|
|
|
|
|
|
type RocketMq struct {
|
|
|
|
mqConfig *etc.RockerMqConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRocketmq(config *etc.RockerMqConfig) *RocketMq {
|
|
|
|
return &RocketMq{
|
|
|
|
mqConfig: config,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-18 16:34:14 +08:00
|
|
|
func (n RocketMq) Consume(c context.Context, handler map[string]mq.Message) error {
|
|
|
|
manager := mq1.NewConsumerManager(nil)
|
|
|
|
connConf := &mq1.ConsumerConnConfig{
|
|
|
|
NameServers: n.mqConfig.Host[0],
|
|
|
|
AccessKey: n.mqConfig.AccessKey,
|
|
|
|
SecretKey: n.mqConfig.SecretKey,
|
2024-06-12 13:46:14 +08:00
|
|
|
}
|
|
|
|
|
2024-06-18 16:34:14 +08:00
|
|
|
for topic, _ := range handler {
|
|
|
|
err := manager.Subscribe(c, connConf,
|
|
|
|
&mq1.ConsumerConfig{
|
|
|
|
TopicName: topic,
|
|
|
|
GroupName: topic + "_consumer",
|
|
|
|
PerCoroutineCnt: 2,
|
|
|
|
},
|
|
|
|
func(ctx context.Context, message *mq1.ConsumerMessage) error {
|
|
|
|
err := handler[message.Topic].MessageHandler(0, nil, message.Body)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("链接失败:%v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
sysLog.ErrQueueLog(context.Background(), err)
|
|
|
|
}
|
|
|
|
|
2024-06-12 13:46:14 +08:00
|
|
|
}
|
2024-06-18 16:34:14 +08:00
|
|
|
err := manager.Start(c)
|
|
|
|
defer manager.Stop(c)
|
2024-06-12 13:46:14 +08:00
|
|
|
if err != nil {
|
2024-06-18 16:34:14 +08:00
|
|
|
sysLog.ErrQueueLog(context.Background(), fmt.Errorf("启动失败:%v", err))
|
2024-06-12 13:46:14 +08:00
|
|
|
}
|
|
|
|
select {}
|
|
|
|
}
|