This commit is contained in:
李子铭 2025-03-08 15:54:18 +08:00
parent d8c8ec8f6e
commit 527544f989
4 changed files with 47 additions and 28 deletions

View File

@ -67,6 +67,8 @@ wechatNotifyMQ:
topic: "notify" topic: "notify"
groupId: "GID_market_pro" groupId: "GID_market_pro"
tag: "voucher_notify_dev" tag: "voucher_notify_dev"
tags:
- voucher_notify_dev
isOpenConsumer: false #是否启动消费 true/false isOpenConsumer: false #是否启动消费 true/false
registerTagUrl: "https://wpcallbacks.api.1688sup.com/wechatPay/register_tag" registerTagUrl: "https://wpcallbacks.api.1688sup.com/wechatPay/register_tag"

View File

@ -555,6 +555,7 @@ type WechatNotifyMQ struct {
Topic string `protobuf:"bytes,6,opt,name=topic,proto3" json:"topic,omitempty"` Topic string `protobuf:"bytes,6,opt,name=topic,proto3" json:"topic,omitempty"`
Tag string `protobuf:"bytes,7,opt,name=tag,proto3" json:"tag,omitempty"` Tag string `protobuf:"bytes,7,opt,name=tag,proto3" json:"tag,omitempty"`
GroupId string `protobuf:"bytes,8,opt,name=groupId,proto3" json:"groupId,omitempty"` GroupId string `protobuf:"bytes,8,opt,name=groupId,proto3" json:"groupId,omitempty"`
Tags []string `protobuf:"bytes,9,rep,name=tags,proto3" json:"tags,omitempty"`
RegisterTagUrl string `protobuf:"bytes,10,opt,name=registerTagUrl,proto3" json:"registerTagUrl,omitempty"` RegisterTagUrl string `protobuf:"bytes,10,opt,name=registerTagUrl,proto3" json:"registerTagUrl,omitempty"`
IsOpenConsumer bool `protobuf:"varint,11,opt,name=isOpenConsumer,proto3" json:"isOpenConsumer,omitempty"` IsOpenConsumer bool `protobuf:"varint,11,opt,name=isOpenConsumer,proto3" json:"isOpenConsumer,omitempty"`
} }
@ -647,6 +648,13 @@ func (x *WechatNotifyMQ) GetGroupId() string {
return "" return ""
} }
func (x *WechatNotifyMQ) GetTags() []string {
if x != nil {
return x.Tags
}
return nil
}
func (x *WechatNotifyMQ) GetRegisterTagUrl() string { func (x *WechatNotifyMQ) GetRegisterTagUrl() string {
if x != nil { if x != nil {
return x.RegisterTagUrl return x.RegisterTagUrl
@ -1129,7 +1137,7 @@ var file_conf_conf_proto_rawDesc = []byte{
0x6f, 0x72, 0x67, 0x4e, 0x6f, 0x18, 0x09, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x6f, 0x72, 0x67, 0x6f, 0x72, 0x67, 0x4e, 0x6f, 0x18, 0x09, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x6f, 0x72, 0x67,
0x4e, 0x6f, 0x12, 0x1c, 0x0a, 0x09, 0x6e, 0x6f, 0x74, 0x69, 0x66, 0x79, 0x55, 0x72, 0x6c, 0x18, 0x4e, 0x6f, 0x12, 0x1c, 0x0a, 0x09, 0x6e, 0x6f, 0x74, 0x69, 0x66, 0x79, 0x55, 0x72, 0x6c, 0x18,
0x0a, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x6e, 0x6f, 0x74, 0x69, 0x66, 0x79, 0x55, 0x72, 0x6c, 0x0a, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x6e, 0x6f, 0x74, 0x69, 0x66, 0x79, 0x55, 0x72, 0x6c,
0x22, 0xc6, 0x02, 0x0a, 0x0e, 0x57, 0x65, 0x63, 0x68, 0x61, 0x74, 0x4e, 0x6f, 0x74, 0x69, 0x66, 0x22, 0xda, 0x02, 0x0a, 0x0e, 0x57, 0x65, 0x63, 0x68, 0x61, 0x74, 0x4e, 0x6f, 0x74, 0x69, 0x66,
0x79, 0x4d, 0x51, 0x12, 0x20, 0x0a, 0x0b, 0x61, 0x63, 0x63, 0x65, 0x73, 0x73, 0x4b, 0x65, 0x79, 0x79, 0x4d, 0x51, 0x12, 0x20, 0x0a, 0x0b, 0x61, 0x63, 0x63, 0x65, 0x73, 0x73, 0x4b, 0x65, 0x79,
0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0b, 0x61, 0x63, 0x63, 0x65, 0x73, 0x73, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0b, 0x61, 0x63, 0x63, 0x65, 0x73, 0x73,
0x4b, 0x65, 0x79, 0x49, 0x64, 0x12, 0x28, 0x0a, 0x0f, 0x61, 0x63, 0x63, 0x65, 0x73, 0x73, 0x4b, 0x4b, 0x65, 0x79, 0x49, 0x64, 0x12, 0x28, 0x0a, 0x0f, 0x61, 0x63, 0x63, 0x65, 0x73, 0x73, 0x4b,
@ -1144,18 +1152,19 @@ var file_conf_conf_proto_rawDesc = []byte{
0x18, 0x06, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x12, 0x10, 0x0a, 0x18, 0x06, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x12, 0x10, 0x0a,
0x03, 0x74, 0x61, 0x67, 0x18, 0x07, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x74, 0x61, 0x67, 0x12, 0x03, 0x74, 0x61, 0x67, 0x18, 0x07, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x74, 0x61, 0x67, 0x12,
0x18, 0x0a, 0x07, 0x67, 0x72, 0x6f, 0x75, 0x70, 0x49, 0x64, 0x18, 0x08, 0x20, 0x01, 0x28, 0x09, 0x18, 0x0a, 0x07, 0x67, 0x72, 0x6f, 0x75, 0x70, 0x49, 0x64, 0x18, 0x08, 0x20, 0x01, 0x28, 0x09,
0x52, 0x07, 0x67, 0x72, 0x6f, 0x75, 0x70, 0x49, 0x64, 0x12, 0x26, 0x0a, 0x0e, 0x72, 0x65, 0x67, 0x52, 0x07, 0x67, 0x72, 0x6f, 0x75, 0x70, 0x49, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x74, 0x61, 0x67,
0x69, 0x73, 0x74, 0x65, 0x72, 0x54, 0x61, 0x67, 0x55, 0x72, 0x6c, 0x18, 0x0a, 0x20, 0x01, 0x28, 0x73, 0x18, 0x09, 0x20, 0x03, 0x28, 0x09, 0x52, 0x04, 0x74, 0x61, 0x67, 0x73, 0x12, 0x26, 0x0a,
0x09, 0x52, 0x0e, 0x72, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x54, 0x61, 0x67, 0x55, 0x72, 0x0e, 0x72, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x54, 0x61, 0x67, 0x55, 0x72, 0x6c, 0x18,
0x6c, 0x12, 0x26, 0x0a, 0x0e, 0x69, 0x73, 0x4f, 0x70, 0x65, 0x6e, 0x43, 0x6f, 0x6e, 0x73, 0x75, 0x0a, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0e, 0x72, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x54,
0x6d, 0x65, 0x72, 0x18, 0x0b, 0x20, 0x01, 0x28, 0x08, 0x52, 0x0e, 0x69, 0x73, 0x4f, 0x70, 0x65, 0x61, 0x67, 0x55, 0x72, 0x6c, 0x12, 0x26, 0x0a, 0x0e, 0x69, 0x73, 0x4f, 0x70, 0x65, 0x6e, 0x43,
0x6e, 0x43, 0x6f, 0x6e, 0x73, 0x75, 0x6d, 0x65, 0x72, 0x22, 0x3a, 0x0a, 0x04, 0x4c, 0x6f, 0x67, 0x6f, 0x6e, 0x73, 0x75, 0x6d, 0x65, 0x72, 0x18, 0x0b, 0x20, 0x01, 0x28, 0x08, 0x52, 0x0e, 0x69,
0x73, 0x12, 0x1a, 0x0a, 0x08, 0x62, 0x75, 0x73, 0x69, 0x6e, 0x65, 0x73, 0x73, 0x18, 0x01, 0x20, 0x73, 0x4f, 0x70, 0x65, 0x6e, 0x43, 0x6f, 0x6e, 0x73, 0x75, 0x6d, 0x65, 0x72, 0x22, 0x3a, 0x0a,
0x01, 0x28, 0x09, 0x52, 0x08, 0x62, 0x75, 0x73, 0x69, 0x6e, 0x65, 0x73, 0x73, 0x12, 0x16, 0x0a, 0x04, 0x4c, 0x6f, 0x67, 0x73, 0x12, 0x1a, 0x0a, 0x08, 0x62, 0x75, 0x73, 0x69, 0x6e, 0x65, 0x73,
0x06, 0x61, 0x63, 0x63, 0x65, 0x73, 0x73, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x61, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x62, 0x75, 0x73, 0x69, 0x6e, 0x65, 0x73,
0x63, 0x63, 0x65, 0x73, 0x73, 0x42, 0x17, 0x5a, 0x15, 0x76, 0x6f, 0x75, 0x63, 0x68, 0x65, 0x72, 0x73, 0x12, 0x16, 0x0a, 0x06, 0x61, 0x63, 0x63, 0x65, 0x73, 0x73, 0x18, 0x02, 0x20, 0x01, 0x28,
0x2f, 0x63, 0x70, 0x6e, 0x2f, 0x63, 0x6f, 0x6e, 0x66, 0x3b, 0x63, 0x6f, 0x6e, 0x66, 0x62, 0x06, 0x09, 0x52, 0x06, 0x61, 0x63, 0x63, 0x65, 0x73, 0x73, 0x42, 0x17, 0x5a, 0x15, 0x76, 0x6f, 0x75,
0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, 0x63, 0x68, 0x65, 0x72, 0x2f, 0x63, 0x70, 0x6e, 0x2f, 0x63, 0x6f, 0x6e, 0x66, 0x3b, 0x63, 0x6f,
0x6e, 0x66, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
} }
var ( var (

View File

@ -93,6 +93,7 @@ message WechatNotifyMQ {
string topic = 6; string topic = 6;
string tag = 7; string tag = 7;
string groupId = 8; string groupId = 8;
repeated string tags = 9;
string registerTagUrl = 10; string registerTagUrl = 10;
bool isOpenConsumer = 11; bool isOpenConsumer = 11;
} }

View File

@ -30,9 +30,9 @@ func NewWechatNotifyConsumer(
} }
} }
// Start 启动消息消费
func (w *WechatNotifyConsumer) Start(ctx context.Context) error { func (w *WechatNotifyConsumer) Start(ctx context.Context) error {
if !w.conf.WechatNotifyMQ.IsOpenConsumer { if !w.conf.WechatNotifyMQ.IsOpenConsumer {
log.Warnf("wechat notify MQ is not open")
return nil return nil
} }
@ -52,12 +52,19 @@ func (w *WechatNotifyConsumer) Start(ctx context.Context) error {
// 您在控制台创建的Group ID。 // 您在控制台创建的Group ID。
groupId := w.conf.WechatNotifyMQ.GroupId groupId := w.conf.WechatNotifyMQ.GroupId
tag := w.conf.WechatNotifyMQ.Tag
client := mq_http_sdk.NewAliyunMQClient(endpoint, accessKeyId, accessKeySecret, "") client := mq_http_sdk.NewAliyunMQClient(endpoint, accessKeyId, accessKeySecret, "")
// 为每个 tag 启动一个消费协程
for _, tag := range w.conf.WechatNotifyMQ.Tags {
mqConsumer := client.GetConsumer(instanceId, topic, groupId, tag) mqConsumer := client.GetConsumer(instanceId, topic, groupId, tag)
go w.consumeMessages(ctx, mqConsumer, tag)
}
return nil
}
// consumeMessages 消费消息的具体逻辑
func (w *WechatNotifyConsumer) consumeMessages(ctx context.Context, mqConsumer mq_http_sdk.MQConsumer, tag string) {
for { for {
endChan := make(chan int) endChan := make(chan int)
respChan := make(chan mq_http_sdk.ConsumeMessageResponse) respChan := make(chan mq_http_sdk.ConsumeMessageResponse)
@ -69,7 +76,6 @@ func (w *WechatNotifyConsumer) Start(ctx context.Context) error {
// 处理业务逻辑。 // 处理业务逻辑。
var handles []string var handles []string
for _, v := range resp.Messages { for _, v := range resp.Messages {
handles = append(handles, v.ReceiptHandle) handles = append(handles, v.ReceiptHandle)
log.Warnf("接收消息成功 wechat notify messageTag:%s, message: %s", v.MessageTag, v.MessageBody) log.Warnf("接收消息成功 wechat notify messageTag:%s, message: %s", v.MessageTag, v.MessageBody)
@ -128,6 +134,7 @@ func (w *WechatNotifyConsumer) Start(ctx context.Context) error {
} }
} }
// Stop 停止消息消费
func (w *WechatNotifyConsumer) Stop(ctx context.Context) error { func (w *WechatNotifyConsumer) Stop(ctx context.Context) error {
fmt.Println("关闭 wechat consumer 中...") fmt.Println("关闭 wechat consumer 中...")
return nil return nil