-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
540269f
commit a088b2d
Showing
6 changed files
with
145 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package mq | ||
|
||
import ( | ||
"context" | ||
"github.com/apache/rocketmq-client-go/v2" | ||
mqprimitive "github.com/apache/rocketmq-client-go/v2/primitive" | ||
"github.com/apache/rocketmq-client-go/v2/producer" | ||
"github.com/cloudwego/hertz/pkg/common/json" | ||
logx "github.com/xh-polaris/gopkg/util/log" | ||
"github.com/xh-polaris/openapi-core-api/biz/infrastructure/config" | ||
"github.com/xh-polaris/openapi-core-api/biz/infrastructure/consts" | ||
) | ||
|
||
func 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.WithRetry(2), | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = p.Start() | ||
if err != nil { | ||
return err | ||
} | ||
defer func() { | ||
err2 := p.Shutdown() | ||
if err2 != nil { | ||
logx.Error("producer关闭失败") | ||
} | ||
}() | ||
|
||
// 定义消息内容 | ||
var msgBody map[string]interface{} | ||
msgBody["txId"] = txId // 事务id | ||
msgBody["rate"] = rate // 折扣 | ||
msgBody["increment"] = increment // 接口余量增加量 | ||
msgBody["amount"] = amount // 用户余额扣减 | ||
msgBody["price"] = price // 购买时单价 | ||
|
||
msgBodyBytes, err := json.Marshal(msgBody) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
msg := &mqprimitive.Message{ | ||
Topic: consts.BuyTopic, | ||
Body: msgBodyBytes, | ||
} | ||
|
||
_, err = p.SendSync(context.Background(), msg) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.