Skip to content

Commit

Permalink
feat: 添加了消息队列的配置及相关依赖注入
Browse files Browse the repository at this point in the history
  • Loading branch information
universero committed Nov 11, 2024
1 parent a088b2d commit 67fba84
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
3 changes: 2 additions & 1 deletion biz/application/service/charge.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type IChargeService interface {
type ChargeService struct {
ChargeClient openapi_charge.IOpenapiCharge
UserClient openapi_user.IOpenapiUser
Producer *mq.Producer
}

var ChargeServiceSet = wire.NewSet(
Expand Down Expand Up @@ -182,7 +183,7 @@ func (s *ChargeService) BuyFullInterface(ctx context.Context, req *core_api.BuyF
txId := primitive.NewObjectID().Hex() // 事务id

// 给消息队列发送对账消息
err := mq.SendBuyMsg(txId, -1*amount, rate, increment, fullInf.Price)
err := s.Producer.SendBuyMsg(txId, -1*amount, rate, increment, fullInf.Price)
if err != nil {
return util.FailResponse(nil, "消息发送失败"), err
}
Expand Down
16 changes: 14 additions & 2 deletions biz/infrastructure/mq/buy.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,22 @@ import (
"github.com/xh-polaris/openapi-core-api/biz/infrastructure/consts"
)

func SendBuyMsg(txId string, amount int64, rate int64, increment int64, price int64) error {
type IProducer interface {
SendBuyMsg(txId string, amount int64, rate int64, increment int64, price int64) error
}

type Producer struct {
Config *config.Config
}

func NewProducer(config *config.Config) *Producer {
return &Producer{Config: config}
}

func (pro *Producer) SendBuyMsg(txId string, amount int64, rate int64, increment int64, price int64) error {
// 创建一个Producer实例
p, err := rocketmq.NewProducer(
producer.WithNsResolver(mqprimitive.NewPassthroughResolver(config.GetConfig().RocketMQ.NameServers)),
producer.WithNsResolver(mqprimitive.NewPassthroughResolver(pro.Config.RocketMQ.NameServers)),
producer.WithRetry(2),
)
if err != nil {
Expand Down
9 changes: 8 additions & 1 deletion provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/google/wire"
"github.com/xh-polaris/openapi-core-api/biz/application/service"
"github.com/xh-polaris/openapi-core-api/biz/infrastructure/config"
"github.com/xh-polaris/openapi-core-api/biz/infrastructure/mq"
"github.com/xh-polaris/openapi-core-api/biz/infrastructure/rpc/openapi_charge"
"github.com/xh-polaris/openapi-core-api/biz/infrastructure/rpc/openapi_user"
)
Expand Down Expand Up @@ -48,9 +49,15 @@ var DomainSet = wire.NewSet()
var InfrastructureSet = wire.NewSet(
config.NewConfig,
RPCSet,
MQSet,
)

var MQSet = wire.NewSet(
mq.NewProducer,
)

var AllProvider = wire.NewSet(
ApplicationSet,
DomainSet,
InfrastructureSet)
InfrastructureSet,
)
3 changes: 3 additions & 0 deletions provider/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 67fba84

Please sign in to comment.