rocketmq 发送与接收
//接收消息 const num = 60 func SubcribeMessage6() { // 创建一个consumer实例 c, err := rocketmq.NewPushConsumer(consumer.WithNameServer(EndPoint), consumer.WithConsumerModel(consumer.Clustering), consumer.WithGroupName("ProducerGroupNamecpayuwan"), ) // 订阅topic baty := func(ctx context.Context, msgs ...*primitive.MessageExt) (consumer.ConsumeResult, error) { for msg := range msgs { if string(msgs[msg].Body) != "" { userInfo := strings.Split(string(msgs[msg].Body), "_") if len(userInfo) != 4 { logrus.Error("用户id参数错误") return consumer.ConsumeSuccess, nil } AppId, _ := strconv.Atoi(userInfo[0]) Id, _ := strconv.ParseInt(userInfo[3], 10, 64) yuwanInfo := new(model.YuwanInfo) yuwanInfo.Id = Id yuwanInfo.AppId = AppId has10, err10 := yuwanInfo.YuwanInfo() if err10 != nil { logrus.Error("查询想玩数据失败:失败", err10.Error()) return consumer.ConsumeSuccess, nil } if !has10 { logrus.Error("想玩 数据不存在") return consumer.ConsumeSuccess, nil } RewardDataJson := make(map[string]interface{}, 0) err3 := json.Unmarshal([]byte(yuwanInfo.RewardDataJson), &RewardDataJson) if err3 != nil { logrus.Error("鱼丸json转换错误") return consumer.ConsumeSuccess, nil } rewardRule := RewardDataJson["rewardTypeText"].(string) //用户完成了游戏什么条件 advertName := RewardDataJson["appName"].(string) //游戏名称 userMoney := RewardDataJson["userMoney"].(string) //用户所获得的钱 mediaMoney := RewardDataJson["mediaMoney"].(string) //商户获得的钱,包含用户 drawNumber := new(model.DrawNumber) drawNumber.UserId = yuwanInfo.UserId drawNumber.AppId = yuwanInfo.AppId has6, err6 := drawNumber.DrawNumber() if err6 != nil { logrus.Error("抽卡查询用户剩余次数:失败", err6.Error()) return consumer.ConsumeSuccess, nil } if !has6 { logrus.Error("账单不存在") return consumer.ConsumeSuccess, nil } userMoneyFl, _ := strconv.ParseFloat(userMoney, 64) mediaMoneyFl, _ := strconv.ParseFloat(mediaMoney, 64) UserAddIntegralChange := new(model.AddIntegralChange) UserAddIntegralChange.UserId = yuwanInfo.UserId UserAddIntegralChange.AppId = yuwanInfo.AppId UserAddIntegralChange.CreateTime = time.Now().Format(utils.TimeLayout) UserAddIntegralChange.TodayTime = time.Now().Format(utils.TimeLayout3) UserAddIntegralChange.GameName = advertName UserAddIntegralChange.QuestNum = rewardRule UserAddIntegralChange.RecIntegral = userMoneyFl UserAddIntegralChange.RecReason = "[试玩红包]" UserAddIntegralChange.RevenueType = "Y" UserUpdateDrawNumber := new(model.UpdateGoldNumber) UserUpdateDrawNumber.Version = drawNumber.Version UserUpdateDrawNumber.Diamond = drawNumber.Diamond + userMoneyFl UserUpdateDrawNumber.AppId = drawNumber.AppId UserUpdateDrawNumber.UserId = drawNumber.UserId UserUpdateDrawNumber.UpdateTime = time.Now().Format(utils.TimeLayout) err2 := UserUpdateDrawNumber.UpdateGoldNumber4(UserAddIntegralChange, Id) if err2 != nil { drawNumber2 := new(model.DrawNumber) drawNumber2.UserId = yuwanInfo.UserId drawNumber2.AppId = yuwanInfo.AppId has60, err60 := drawNumber2.DrawNumber() if err60 != nil { logrus.Error("抽卡查询用户剩余次数:失败", err60.Error()) break } if !has60 { logrus.Error("用户账单不存在") break } UserUpdateDrawNumber2 := new(model.UpdateGoldNumber) UserUpdateDrawNumber2.Version = drawNumber2.Version UserUpdateDrawNumber2.Diamond = drawNumber2.Diamond + userMoneyFl UserUpdateDrawNumber2.AppId = drawNumber2.AppId UserUpdateDrawNumber2.UserId = drawNumber2.UserId UserUpdateDrawNumber2.UpdateTime = time.Now().Format(utils.TimeLayout) err20 := UserUpdateDrawNumber2.UpdateGoldNumber(UserAddIntegralChange, Id) if err20 != nil { logrus.Error("修改用户数据失败:", err2.Error()) return consumer.ConsumeSuccess, err20 } logrus.Error("修改用户数据失败:", err2.Error()) } userInfo1 := new(model.UserInfo) has, err9 := userInfo1.UserInfo(yuwanInfo.UserId) if err9 != nil { logrus.Error("查询用户vip等级失败:", err9.Error()) return consumer.ConsumeSuccess, err2 } if !has { logrus.Error("暂无用户等级:") } if userInfo1.Rate > 0 { Rate := float64(userInfo1.Rate) / 100.00 residue := userMoneyFl / mediaMoneyFl * 100 fmt.Println(residue, "222222222222222222222222222222222222") IntegralChangeList := new(model.IntegralChangeList) IntegralChangeList.GameId = Id has55, err55 := IntegralChangeList.IntegralChangeList() if err55 != nil { logrus.Error("查询用户订单是否添加失败:", err55.Error()) return consumer.ConsumeSuccess, err2 } if has55 { return consumer.ConsumeSuccess, nil } if residue < num { result := userMoneyFl * Rate drawNumber1 := new(model.DrawNumber) drawNumber1.UserId = yuwanInfo.UserId drawNumber1.AppId = yuwanInfo.AppId has7, err7 := drawNumber1.DrawNumber() if err7 != nil { logrus.Error("抽卡查询用户剩余次数:失败1", err7.Error()) return consumer.ConsumeSuccess, nil } if !has7 { logrus.Error("账单不存在1") return consumer.ConsumeSuccess, nil } UserAddIntegralChange1 := new(model.AddIntegralChange) UserAddIntegralChange1.UserId = yuwanInfo.UserId UserAddIntegralChange1.AppId = yuwanInfo.AppId UserAddIntegralChange1.CreateTime = time.Now().Format(utils.TimeLayout) UserAddIntegralChange1.TodayTime = time.Now().Format(utils.TimeLayout3) UserAddIntegralChange1.GameName = advertName UserAddIntegralChange1.QuestNum = rewardRule UserAddIntegralChange1.RecIntegral = result UserAddIntegralChange1.RecReason = fmt.Sprintf("[%s收益]", userInfo1.VipName) UserAddIntegralChange1.RevenueType = "Y" UserAddIntegralChange1.GameId = Id UserAddIntegralChange1.Status = 1 UserUpdateDrawNumber1 := new(model.UpdateGoldNumber) UserUpdateDrawNumber1.Version = drawNumber1.Version UserUpdateDrawNumber1.Diamond = drawNumber1.Diamond + result UserUpdateDrawNumber1.AppId = drawNumber1.AppId UserUpdateDrawNumber1.UserId = drawNumber1.UserId UserUpdateDrawNumber1.UpdateTime = time.Now().Format(utils.TimeLayout) err8 := UserUpdateDrawNumber1.UpdateGoldNumberExtra(UserAddIntegralChange1) if err8 != nil { UserAddIntegralChange1.Status = 2 err4 := UserAddIntegralChange1.AddIntegralChange() if err4 != nil { logrus.Error("抽卡查询用户剩余次数:失败1", err4.Error()) return consumer.ConsumeSuccess, nil } return consumer.ConsumeSuccess, nil } } } } } return consumer.ConsumeSuccess, nil } // 订阅主题、消费 err = c.Subscribe(Cpayuwan, consumer.MessageSelector{}, baty) if err != nil { panic(fmt.Sprintf("订阅消息错误: %s", err.Error())) } // 启动consumer err = c.Start() if err != nil { panic(fmt.Sprintf("消费者开启动错误: %s", err.Error())) return } }
//发送消息 func SendSyncMessage7(message string) { t7 := uuid.NewV4()//高并发保证name唯一 // 创建一个producer实例 p, err1 := rocketmq.NewProducer( producer.WithNameServer(EndPoint), producer.WithRetry(3), producer.WithGroupName("ProducerGroupNameqingwa"), producer.WithInstanceName(fmt.Sprintf("%d", t7)), producer.WithSendMsgTimeout(7000), ) if err1 != nil { fmt.Printf("创建实例失败: %s", err1.Error()) panic(fmt.Sprintf("创建实例失败: %s", err1.Error())) } // 启动 err := p.Start() if err != nil { panic(fmt.Sprintf("发送消息错误: %s", err.Error())) } // 发送消息 result, err3 := p.SendSync(context.Background(), &primitive.Message{ Topic: Qingwa, Body: []byte(message), }) if err3 != nil { panic(fmt.Sprintf("发送消息错误: %s", err3.Error())) } fmt.Println(result.String()) if err = p.Shutdown(); err != nil { panic("关闭 producer 失败") } return }