From 99e4167c9675c8becb2452b16a8e85972786c276 Mon Sep 17 00:00:00 2001 From: NorthLan <6995syu@163.com> Date: Mon, 25 Apr 2022 01:08:56 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=90=84=E7=A7=8D?= =?UTF-8?q?=E9=94=99=E8=AF=AF,=E6=97=A5=E5=BF=97=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E4=B8=BA=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/gift/rpc/etc/gift.yaml | 10 +++++++--- app/gift/rpc/internal/config/config.go | 9 ++++++--- app/gift/rpc/internal/logic/collect_gift_logic.go | 6 ++++-- app/gift/rpc/msg_transfer/gift/msg_to_db.go | 4 ++++ app/gift/rpc/pb/gift.pb.go | 8 ++++---- app/gift/rpc/pb/gift.proto | 3 +-- app/pb/mq/mq.pb.go | 8 ++++---- app/pb/mq/mq.proto | 2 +- app/user_center/rpc/etc/user_center.yaml | 8 ++++++-- app/user_center/rpc/internal/config/config.go | 9 ++++++--- .../logic/platform_user/platform_user_retrieve.go | 2 +- .../rpc/msg_transfer/danmaku/cmd_manager.go | 2 +- app/user_center/rpc/msg_transfer/danmaku/msg_to_db.go | 6 +++++- 13 files changed, 50 insertions(+), 27 deletions(-) diff --git a/app/gift/rpc/etc/gift.yaml b/app/gift/rpc/etc/gift.yaml index 513e567..365f710 100644 --- a/app/gift/rpc/etc/gift.yaml +++ b/app/gift/rpc/etc/gift.yaml @@ -1,7 +1,7 @@ Name: gift.rpc -ListenOn: 127.0.0.1:10000 +ListenOn: 127.0.0.1:10002 DB: - DataSource: root:root@tcp(192.168.1.100:3306)/dmgame?charset=utf8mb4&loc=Asia%2FShanghai&parseTime=true + DataSource: root:root@tcp(127.0.0.1:3306)/dmgame?charset=utf8mb4&loc=Asia%2FShanghai&parseTime=true Etcd: Hosts: - 127.0.0.1:2379 @@ -10,4 +10,8 @@ Gift: Kafka: Addr: [ "127.0.0.1:9093" ] Topic: "gift" - ConsumerGroupId: "msgToDb" \ No newline at end of file + ConsumerGroupId: "msgToDb" +Log: + Mode: file + KeepDays: 7 + Level: error \ No newline at end of file diff --git a/app/gift/rpc/internal/config/config.go b/app/gift/rpc/internal/config/config.go index bc9ae92..eed1376 100644 --- a/app/gift/rpc/internal/config/config.go +++ b/app/gift/rpc/internal/config/config.go @@ -1,6 +1,9 @@ package config -import "github.com/zeromicro/go-zero/zrpc" +import ( + "github.com/zeromicro/go-zero/core/logx" + "github.com/zeromicro/go-zero/zrpc" +) type ( Kafka struct { @@ -10,8 +13,8 @@ type ( } Config struct { zrpc.RpcServerConf - - DB struct { + Log logx.LogConf + DB struct { DataSource string } diff --git a/app/gift/rpc/internal/logic/collect_gift_logic.go b/app/gift/rpc/internal/logic/collect_gift_logic.go index 352fde7..fcf2ffc 100644 --- a/app/gift/rpc/internal/logic/collect_gift_logic.go +++ b/app/gift/rpc/internal/logic/collect_gift_logic.go @@ -5,6 +5,7 @@ import ( "git.noahlan.cn/northlan/ntools-go/uuid" "github.com/pkg/errors" "live-service/app/gift/model" + "strconv" "live-service/app/gift/rpc/internal/svc" "live-service/app/gift/rpc/pb" @@ -28,7 +29,8 @@ func NewCollectGiftLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Colle // CollectGift 收集礼物,仅作为收集使用 func (l *CollectGiftLogic) CollectGift(in *pb.CollectGift) (*pb.Empty, error) { - dbGift, err := l.svcCtx.GiftModel.FindOneByGiftId(l.ctx, string(in.GiftId)) + giftIdStr := strconv.FormatInt(in.GiftId, 10) + dbGift, err := l.svcCtx.GiftModel.FindOneByGiftId(l.ctx, giftIdStr) if err == nil { return &pb.Empty{}, nil } @@ -39,7 +41,7 @@ func (l *CollectGiftLogic) CollectGift(in *pb.CollectGift) (*pb.Empty, error) { } dbGift = &model.Gift{ Id: uuid.NextId(), - GiftId: string(in.GiftId), + GiftId: giftIdStr, GiftName: in.GiftName, Platform: in.Platform, PPrice: in.TotalCoin, diff --git a/app/gift/rpc/msg_transfer/gift/msg_to_db.go b/app/gift/rpc/msg_transfer/gift/msg_to_db.go index 14d2b56..060ee35 100644 --- a/app/gift/rpc/msg_transfer/gift/msg_to_db.go +++ b/app/gift/rpc/msg_transfer/gift/msg_to_db.go @@ -11,6 +11,7 @@ import ( "live-service/app/gift/rpc/pb" pbMq "live-service/app/pb/mq" kfk "live-service/common/kafka" + "sync" ) type msgHandlerFunc func(data []byte, msgKey string) @@ -24,6 +25,7 @@ type MsgToDBHandler struct { ConsumerGroup *kafka.ConsumerGroup + mutex sync.Mutex logx.Logger } @@ -57,6 +59,8 @@ func (h *MsgToDBHandler) handleGift(data []byte, msgKey string) { h.Logger.Error("unmarshal msg err", err) return } + h.mutex.Lock() + defer h.mutex.Unlock() if _, err := h.svr.CollectGift(context.Background(), &pb.CollectGift{ Platform: msgFromMq.Platform, GiftId: msgFromMq.GiftId, diff --git a/app/gift/rpc/pb/gift.pb.go b/app/gift/rpc/pb/gift.pb.go index 4a0e0bc..ad69bb0 100644 --- a/app/gift/rpc/pb/gift.pb.go +++ b/app/gift/rpc/pb/gift.pb.go @@ -1,6 +1,6 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: -// protoc-gen-go v1.28.0 +// protoc-gen-go v1.27.1 // protoc v3.19.4 // source: gift.proto @@ -27,7 +27,7 @@ type CollectGift struct { unknownFields protoimpl.UnknownFields Platform string `protobuf:"bytes,1,opt,name=platform,proto3" json:"platform,omitempty"` - GiftId int32 `protobuf:"varint,2,opt,name=giftId,proto3" json:"giftId,omitempty"` + GiftId int64 `protobuf:"varint,2,opt,name=giftId,proto3" json:"giftId,omitempty"` GiftName string `protobuf:"bytes,3,opt,name=giftName,proto3" json:"giftName,omitempty"` TotalCoin int64 `protobuf:"varint,4,opt,name=totalCoin,proto3" json:"totalCoin,omitempty"` } @@ -71,7 +71,7 @@ func (x *CollectGift) GetPlatform() string { return "" } -func (x *CollectGift) GetGiftId() int32 { +func (x *CollectGift) GetGiftId() int64 { if x != nil { return x.GiftId } @@ -137,7 +137,7 @@ var file_gift_proto_rawDesc = []byte{ 0x22, 0x7b, 0x0a, 0x0b, 0x43, 0x6f, 0x6c, 0x6c, 0x65, 0x63, 0x74, 0x47, 0x69, 0x66, 0x74, 0x12, 0x1a, 0x0a, 0x08, 0x70, 0x6c, 0x61, 0x74, 0x66, 0x6f, 0x72, 0x6d, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x70, 0x6c, 0x61, 0x74, 0x66, 0x6f, 0x72, 0x6d, 0x12, 0x16, 0x0a, 0x06, 0x67, - 0x69, 0x66, 0x74, 0x49, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x67, 0x69, 0x66, + 0x69, 0x66, 0x74, 0x49, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x06, 0x67, 0x69, 0x66, 0x74, 0x49, 0x64, 0x12, 0x1a, 0x0a, 0x08, 0x67, 0x69, 0x66, 0x74, 0x4e, 0x61, 0x6d, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x67, 0x69, 0x66, 0x74, 0x4e, 0x61, 0x6d, 0x65, 0x12, 0x1c, 0x0a, 0x09, 0x74, 0x6f, 0x74, 0x61, 0x6c, 0x43, 0x6f, 0x69, 0x6e, 0x18, 0x04, 0x20, 0x01, diff --git a/app/gift/rpc/pb/gift.proto b/app/gift/rpc/pb/gift.proto index e5dd2e4..608552a 100644 --- a/app/gift/rpc/pb/gift.proto +++ b/app/gift/rpc/pb/gift.proto @@ -7,14 +7,13 @@ option go_package = "./pb"; // req message CollectGift { string platform = 1; - int32 giftId = 2; + int64 giftId = 2; string giftName = 3; int64 totalCoin = 4; } message Empty {} - service gift { // collectGift 收集礼物,仅作为收集使用 rpc collectGift(CollectGift) returns (Empty); diff --git a/app/pb/mq/mq.pb.go b/app/pb/mq/mq.pb.go index b515cf0..669a42e 100644 --- a/app/pb/mq/mq.pb.go +++ b/app/pb/mq/mq.pb.go @@ -1,6 +1,6 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: -// protoc-gen-go v1.28.0 +// protoc-gen-go v1.27.1 // protoc v3.19.4 // source: mq.proto @@ -168,7 +168,7 @@ type MqGift struct { LiveRoomId int64 `protobuf:"varint,2,opt,name=liveRoomId,proto3" json:"liveRoomId,omitempty"` Uid int64 `protobuf:"varint,3,opt,name=uid,proto3" json:"uid,omitempty"` Uname string `protobuf:"bytes,4,opt,name=uname,proto3" json:"uname,omitempty"` - GiftId int32 `protobuf:"varint,5,opt,name=giftId,proto3" json:"giftId,omitempty"` + GiftId int64 `protobuf:"varint,5,opt,name=giftId,proto3" json:"giftId,omitempty"` GiftName string `protobuf:"bytes,6,opt,name=giftName,proto3" json:"giftName,omitempty"` TotalCoin int64 `protobuf:"varint,7,opt,name=totalCoin,proto3" json:"totalCoin,omitempty"` SendTime int64 `protobuf:"varint,8,opt,name=sendTime,proto3" json:"sendTime,omitempty"` @@ -234,7 +234,7 @@ func (x *MqGift) GetUname() string { return "" } -func (x *MqGift) GetGiftId() int32 { +func (x *MqGift) GetGiftId() int64 { if x != nil { return x.GiftId } @@ -284,7 +284,7 @@ var file_mq_proto_rawDesc = []byte{ 0x03, 0x75, 0x69, 0x64, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52, 0x03, 0x75, 0x69, 0x64, 0x12, 0x14, 0x0a, 0x05, 0x75, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x75, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x67, 0x69, 0x66, 0x74, 0x49, 0x64, 0x18, - 0x05, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x67, 0x69, 0x66, 0x74, 0x49, 0x64, 0x12, 0x1a, 0x0a, + 0x05, 0x20, 0x01, 0x28, 0x03, 0x52, 0x06, 0x67, 0x69, 0x66, 0x74, 0x49, 0x64, 0x12, 0x1a, 0x0a, 0x08, 0x67, 0x69, 0x66, 0x74, 0x4e, 0x61, 0x6d, 0x65, 0x18, 0x06, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x67, 0x69, 0x66, 0x74, 0x4e, 0x61, 0x6d, 0x65, 0x12, 0x1c, 0x0a, 0x09, 0x74, 0x6f, 0x74, 0x61, 0x6c, 0x43, 0x6f, 0x69, 0x6e, 0x18, 0x07, 0x20, 0x01, 0x28, 0x03, 0x52, 0x09, 0x74, 0x6f, diff --git a/app/pb/mq/mq.proto b/app/pb/mq/mq.proto index 29822ed..8f3fd14 100644 --- a/app/pb/mq/mq.proto +++ b/app/pb/mq/mq.proto @@ -25,7 +25,7 @@ message MqGift { int64 liveRoomId = 2; int64 uid = 3; string uname = 4; - int32 giftId = 5; + int64 giftId = 5; string giftName = 6; int64 totalCoin = 7; int64 sendTime = 8; diff --git a/app/user_center/rpc/etc/user_center.yaml b/app/user_center/rpc/etc/user_center.yaml index 49efc1c..741aceb 100644 --- a/app/user_center/rpc/etc/user_center.yaml +++ b/app/user_center/rpc/etc/user_center.yaml @@ -5,7 +5,7 @@ Etcd: - 127.0.0.1:2379 Key: usercenter.rpc DB: - DataSource: root:root@tcp(192.168.1.100:3306)/dmgame?charset=utf8mb4&loc=Asia%2FShanghai&parseTime=true + DataSource: root:root@tcp(127.0.0.1:3306)/dmgame?charset=utf8mb4&loc=Asia%2FShanghai&parseTime=true Danmaku: Kafka: Addr: [ "127.0.0.1:9093" ] @@ -14,4 +14,8 @@ Danmaku: Command: Keys: [ "j","J","加入","加入游戏" ] User: - UpdateDuration: 72 # 72 hours \ No newline at end of file + UpdateDuration: 72 # 72 hours +Log: + Mode: file + KeepDays: 7 + Level: error \ No newline at end of file diff --git a/app/user_center/rpc/internal/config/config.go b/app/user_center/rpc/internal/config/config.go index b3fdae9..b621d73 100644 --- a/app/user_center/rpc/internal/config/config.go +++ b/app/user_center/rpc/internal/config/config.go @@ -1,6 +1,9 @@ package config -import "github.com/zeromicro/go-zero/zrpc" +import ( + "github.com/zeromicro/go-zero/core/logx" + "github.com/zeromicro/go-zero/zrpc" +) type ( Kafka struct { @@ -10,8 +13,8 @@ type ( } Config struct { zrpc.RpcServerConf - - DB struct { + Log logx.LogConf + DB struct { DataSource string } diff --git a/app/user_center/rpc/internal/logic/platform_user/platform_user_retrieve.go b/app/user_center/rpc/internal/logic/platform_user/platform_user_retrieve.go index 46a290f..8413fc4 100644 --- a/app/user_center/rpc/internal/logic/platform_user/platform_user_retrieve.go +++ b/app/user_center/rpc/internal/logic/platform_user/platform_user_retrieve.go @@ -2,7 +2,7 @@ package platform_user import ( "github.com/pkg/errors" - pbMq "live-service/app/user_center/pb/mq" + pbMq "live-service/app/pb/mq" ) // PlatformUser 仅提取需要的部分,其余的json后装到p_info diff --git a/app/user_center/rpc/msg_transfer/danmaku/cmd_manager.go b/app/user_center/rpc/msg_transfer/danmaku/cmd_manager.go index 051c79c..c292b06 100644 --- a/app/user_center/rpc/msg_transfer/danmaku/cmd_manager.go +++ b/app/user_center/rpc/msg_transfer/danmaku/cmd_manager.go @@ -1,6 +1,6 @@ package danmaku -import pbMq "live-service/app/user_center/pb/mq" +import pbMq "live-service/app/pb/mq" type HandlerFunc func(roomId int64, cmd string, dm *pbMq.MqDanmaku) diff --git a/app/user_center/rpc/msg_transfer/danmaku/msg_to_db.go b/app/user_center/rpc/msg_transfer/danmaku/msg_to_db.go index eebfc17..1fa001e 100644 --- a/app/user_center/rpc/msg_transfer/danmaku/msg_to_db.go +++ b/app/user_center/rpc/msg_transfer/danmaku/msg_to_db.go @@ -6,12 +6,13 @@ import ( "github.com/Shopify/sarama" "github.com/zeromicro/go-zero/core/logx" "google.golang.org/protobuf/proto" - pbMq "live-service/app/user_center/pb/mq" + pbMq "live-service/app/pb/mq" "live-service/app/user_center/rpc/internal/server" "live-service/app/user_center/rpc/internal/svc" "live-service/app/user_center/rpc/pb" kfk "live-service/common/kafka" "strconv" + "sync" ) type msgHandlerFunc func(data []byte, msgKey string) @@ -28,6 +29,7 @@ type MsgToDBHandler struct { ConsumerGroup *kafka.ConsumerGroup + mutex sync.Mutex logx.Logger } @@ -60,6 +62,8 @@ func (h *MsgToDBHandler) Init(context context.Context, ctx *svc.ServiceContext, } func (h *MsgToDBHandler) handleJoinGame(_ int64, _ string, danmaku *pbMq.MqDanmaku) { + h.mutex.Lock() + defer h.mutex.Unlock() _, err := h.svr.RetrievePlatformUser(h.context, &pb.PlatformUserReq{ Platform: danmaku.Platform, PUid: strconv.FormatInt(danmaku.Uid, 10),