This commit is contained in:
李子铭 2025-03-07 10:00:59 +08:00
parent 83f007b423
commit 5e584c1353
4 changed files with 119 additions and 54 deletions

View File

@ -59,6 +59,10 @@ message CmbOrderRequest {
string timestamp = 13 [json_name = "timestamp", (validate.rules).string = {min_len: 1,max_len: 20}]; string timestamp = 13 [json_name = "timestamp", (validate.rules).string = {min_len: 1,max_len: 20}];
} }
message CmbOrderReply { message CmbOrderReply {
// 1000 1001
string respCode = 1 [json_name = "respCode"];
//
string respMsg = 2 [json_name = "respMsg"];
// //
// 50 // 50
string codeNo = 9 [json_name = "codeNo"]; string codeNo = 9 [json_name = "codeNo"];
@ -71,6 +75,10 @@ message CmbQueryProductRequest {
string activityId = 9 [json_name = "activityId", (validate.rules).string = {min_len: 1,max_len: 32}]; string activityId = 9 [json_name = "activityId", (validate.rules).string = {min_len: 1,max_len: 32}];
} }
message CmbQueryProductReply { message CmbQueryProductReply {
// 1000 1001
string respCode = 1 [json_name = "respCode"];
//
string respMsg = 2 [json_name = "respMsg"];
// //
// //
string activityName = 9 [json_name = "activityName"]; string activityName = 9 [json_name = "activityName"];

View File

@ -24,22 +24,6 @@ func (v *VoucherBiz) PushOrderMQ(ctx context.Context, orderNo string) error {
return nil return nil
} }
func (v *VoucherBiz) PushQueryDelayMQ(ctx context.Context, orderNo string) error {
eventMap := v.bc.RocketMQ.EventMap["query"]
sendOption := []mq.SendOption{
mq.WithSendShardingKeysOption(orderNo),
mq.WithOpenTelemetryOption(trace.SpanFromContext(ctx).SpanContext().TraceID().String()),
mq.WithSendDelayLevelOption(300),
}
if err := v.MqSendMixRepo.SendSync(ctx, eventMap.Topic, []byte("{}"), sendOption...); err != nil {
return fmt.Errorf("查询入队投递失败[%v]", err)
}
return nil
}
func (v *VoucherBiz) PushNotifyMQ(ctx context.Context, orderNo, outRequestNo string) error { func (v *VoucherBiz) PushNotifyMQ(ctx context.Context, orderNo, outRequestNo string) error {
eventMap := v.bc.RocketMQ.EventMap["notify"] eventMap := v.bc.RocketMQ.EventMap["notify"]

78
internal/data/mq_test.go Normal file
View File

@ -0,0 +1,78 @@
package data
import (
"context"
"testing"
"voucher/internal/conf"
"voucher/internal/pkg/mq"
)
func Test_OrderProducer(t *testing.T) {
m := make(map[string]*conf.EventMap)
m["order"] = &conf.EventMap{
Topic: "order",
}
c := &conf.RocketMQ{
Addr: "http://rmq-cn-nwy3fn4ex09.cn-chengdu.rmq.aliyuncs.com:8080",
AccessKey: "Qecl4cea2IAZPKoD",
SecretKey: "Z3596KCFA9RAUR6k",
SecretToken: "",
EventMap: m,
}
mqx, err := buildMqProducer(c)
if err != nil {
t.Errorf("buildMqProducer() error = %v", err)
return
}
ctx := context.Background()
orderNo := ""
eventMap := c.EventMap["order"]
sendOption := []mq.SendOption{
mq.WithSendShardingKeysOption(orderNo),
}
if err = mqx.SendSync(ctx, eventMap.Topic, []byte("{}"), sendOption...); err != nil {
t.Errorf("入队失败 error = %v", err)
return
}
}
func Test_NotifyProducer(t *testing.T) {
m := make(map[string]*conf.EventMap)
m["notify"] = &conf.EventMap{
Topic: "notify",
}
c := &conf.RocketMQ{
Addr: "http://rmq-cn-nwy3fn4ex09.cn-chengdu.rmq.aliyuncs.com:8080",
AccessKey: "Qecl4cea2IAZPKoD",
SecretKey: "Z3596KCFA9RAUR6k",
SecretToken: "",
EventMap: m,
}
mqx, err := buildMqProducer(c)
if err != nil {
t.Errorf("buildMqProducer() error = %v", err)
return
}
ctx := context.Background()
orderNo := ""
eventMap := c.EventMap["notify"]
sendOption := []mq.SendOption{
mq.WithSendShardingKeysOption(orderNo),
}
if err = mqx.SendSync(ctx, eventMap.Topic, []byte("{}"), sendOption...); err != nil {
t.Errorf("入队失败 error = %v", err)
return
}
}

View File

@ -15,45 +15,45 @@ func (s *VoucherService) CmbOrder(ctx http.Context) error {
err error err error
) )
bizReply, err := s.cmbOrder(ctx) orderNo, err := s.cmbOrder(ctx)
if err != nil { if err != nil {
reply, err = s.CmbMixRepo.GetResponse( reply, err = s.CmbMixRepo.GetResponse(ctx, &bo.CmbResponseBo{
ctx,
&bo.CmbResponseBo{
RespCode: vo.CmbResponseStatusFail.GetValue(), RespCode: vo.CmbResponseStatusFail.GetValue(),
RespMsg: err.Error(), RespMsg: err.Error(),
BizContent: "", BizContent: "",
}, })
)
} else { } else {
bizReply := &v1.CmbOrderReply{
RespCode: vo.CmbResponseStatusSuccess.GetValue(),
RespMsg: "成功",
CodeNo: orderNo,
}
replyBizContent, _ := json.Marshal(bizReply) replyBizContent, _ := json.Marshal(bizReply)
reply, err = s.CmbMixRepo.GetResponse( reply, err = s.CmbMixRepo.GetResponse(ctx, &bo.CmbResponseBo{
ctx,
&bo.CmbResponseBo{
RespCode: vo.CmbResponseStatusSuccess.GetValue(), RespCode: vo.CmbResponseStatusSuccess.GetValue(),
RespMsg: "成功", RespMsg: "成功",
BizContent: string(replyBizContent), BizContent: string(replyBizContent),
}, })
)
} }
return ctx.JSON(200, reply) return ctx.JSON(200, reply)
} }
func (s *VoucherService) cmbOrder(ctx http.Context) (*v1.CmbOrderReply, error) { func (s *VoucherService) cmbOrder(ctx http.Context) (string, error) {
var req *v1.CmbRequest var req *v1.CmbRequest
if err := ctx.BindForm(&req); err != nil { if err := ctx.BindForm(&req); err != nil {
return nil, err return "", err
} }
if err := req.Validate(); err != nil { if err := req.Validate(); err != nil {
return nil, err return "", err
} }
bizContent, err := s.CmbMixRepo.OrderVerify(ctx, req) bizContent, err := s.CmbMixRepo.OrderVerify(ctx, req)
if err != nil { if err != nil {
return nil, err return "", err
} }
boReq := &bo.OrderCreateReqBo{ boReq := &bo.OrderCreateReqBo{
@ -66,12 +66,10 @@ func (s *VoucherService) cmbOrder(ctx http.Context) (*v1.CmbOrderReply, error) {
orderNo, err := s.VoucherBiz.CmbOrder(ctx, boReq) orderNo, err := s.VoucherBiz.CmbOrder(ctx, boReq)
if err != nil { if err != nil {
return nil, err return "", err
} }
return &v1.CmbOrderReply{ return orderNo, nil
CodeNo: orderNo,
}, nil
} }
func (s *VoucherService) CmbProductQuery(ctx http.Context) error { func (s *VoucherService) CmbProductQuery(ctx http.Context) error {
@ -83,9 +81,7 @@ func (s *VoucherService) CmbProductQuery(ctx http.Context) error {
bizReply, err := s.cmbProductQuery(ctx) bizReply, err := s.cmbProductQuery(ctx)
if err != nil { if err != nil {
reply, err = s.CmbMixRepo.GetResponse( reply, err = s.CmbMixRepo.GetResponse(ctx, &bo.CmbResponseBo{
ctx,
&bo.CmbResponseBo{
RespCode: vo.CmbResponseStatusFail.GetValue(), RespCode: vo.CmbResponseStatusFail.GetValue(),
RespMsg: err.Error(), RespMsg: err.Error(),
BizContent: "", BizContent: "",
@ -93,8 +89,7 @@ func (s *VoucherService) CmbProductQuery(ctx http.Context) error {
) )
} else { } else {
replyBizContent, _ := json.Marshal(bizReply) replyBizContent, _ := json.Marshal(bizReply)
reply, err = s.CmbMixRepo.GetResponse(ctx, reply, err = s.CmbMixRepo.GetResponse(ctx, &bo.CmbResponseBo{
&bo.CmbResponseBo{
RespCode: vo.CmbResponseStatusSuccess.GetValue(), RespCode: vo.CmbResponseStatusSuccess.GetValue(),
RespMsg: "成功", RespMsg: "成功",
BizContent: string(replyBizContent), BizContent: string(replyBizContent),