From d1ebb9882177e437c3701fb0c0876a16a6a4637b Mon Sep 17 00:00:00 2001 From: xiabin Date: Tue, 11 Feb 2025 17:27:59 +0800 Subject: [PATCH] 1 --- .../internal/logic/get_ranking_list_logic.go | 3 +- .../internal/svc/service_context.go | 28 +++- .../internal/logic/get_user_by_id_logic.go | 44 ++++++ .../internal/server/user_management_server.go | 11 +- app/user_management/user_management.proto | 14 +- .../user_management/user_management.pb.go | 128 ++++++++++++++---- .../user_management_grpc.pb.go | 50 ++++++- .../user_management_client/user_management.go | 19 ++- 8 files changed, 253 insertions(+), 44 deletions(-) create mode 100644 app/user_management/internal/logic/get_user_by_id_logic.go diff --git a/app/ranking_management/internal/logic/get_ranking_list_logic.go b/app/ranking_management/internal/logic/get_ranking_list_logic.go index 8850ea2..a15d2ba 100644 --- a/app/ranking_management/internal/logic/get_ranking_list_logic.go +++ b/app/ranking_management/internal/logic/get_ranking_list_logic.go @@ -6,6 +6,7 @@ import ( "gitea.youtukeji.com.cn/xiabin/youtu_grpc/app/ranking_management/internal/logic/rankings" "gitea.youtukeji.com.cn/xiabin/youtu_grpc/app/ranking_management/internal/svc" ranking_management2 "gitea.youtukeji.com.cn/xiabin/youtu_grpc/app/ranking_management/ranking_management" + "gitea.youtukeji.com.cn/xiabin/youtu_grpc/app/user_management/user_management" "gorm.io/gorm" "strconv" @@ -43,7 +44,7 @@ func (l *GetRankingListLogic) GetRankingList(in *ranking_management2.GetRankingL continue } //查询用户数据,FindOne带缓存 - user, err := l.svcCtx.Query.AppUser.Where(l.svcCtx.Query.AppUser.ID.Eq(uint64(userId))).Take() + user, err := l.svcCtx.UserManagerClient.GetUserById(l.ctx, &user_management.UserId{UserId: uint64(userId)}) if err != nil { return nil, err } diff --git a/app/ranking_management/internal/svc/service_context.go b/app/ranking_management/internal/svc/service_context.go index 1903061..00a1add 100644 --- a/app/ranking_management/internal/svc/service_context.go +++ b/app/ranking_management/internal/svc/service_context.go @@ -4,17 +4,24 @@ import ( "context" "gitea.youtukeji.com.cn/xiabin/youtu_grpc/app/ranking_management/internal/gen/dao/query" "gitea.youtukeji.com.cn/xiabin/youtu_grpc/app/ranking_management/internal/logic/rankings" + "gitea.youtukeji.com.cn/xiabin/youtu_grpc/app/user_management/user_management" + "gitea.youtukeji.com.cn/xiabin/youtu_grpc/app/user_management/user_management_client" "gitea.youtukeji.com.cn/xiabin/youtu_grpc/pkg/config" "gitea.youtukeji.com.cn/xiabin/youtu_grpc/pkg/my_gorm" "github.com/redis/go-redis/v9" + "github.com/spf13/viper" + "github.com/zeromicro/go-zero/core/conf" + "github.com/zeromicro/go-zero/core/discov" + "github.com/zeromicro/go-zero/zrpc" "gorm.io/driver/mysql" "gorm.io/gorm" ) type ServiceContext struct { - Config config.Config - Query *query.Query - RedisRanking *rankings.Ranking + Config config.Config + Query *query.Query + RedisRanking *rankings.Ranking + UserManagerClient user_management_client.UserManagement } func NewServiceContext(c config.Config) *ServiceContext { @@ -34,6 +41,21 @@ func NewServiceContext(c config.Config) *ServiceContext { svc.Query = query.Use(db) + // 初始化用户客户端 + clientConf := zrpc.RpcClientConf{} + err = conf.FillDefault(&clientConf) // 填充默认值,比如 trace 透传等,参考服务配置说明 + if err != nil { + panic(err) + } + clientConf.Token = "user_management.rpc.key" + clientConf.App = "user_management.rpc" + clientConf.Etcd = discov.EtcdConf{ // 通过 etcd 服务发现 + Hosts: []string{viper.GetString(config.EtcdAddrKey)}, + Key: "user_management.rpc", + } + + svc.UserManagerClient = user_management.NewUserManagementClient(zrpc.MustNewClient(clientConf).Conn()) + //初始化排行榜对象 svc.InitRankings(redisClient) diff --git a/app/user_management/internal/logic/get_user_by_id_logic.go b/app/user_management/internal/logic/get_user_by_id_logic.go new file mode 100644 index 0000000..acbf3a3 --- /dev/null +++ b/app/user_management/internal/logic/get_user_by_id_logic.go @@ -0,0 +1,44 @@ +package logic + +import ( + "context" + "errors" + + "gitea.youtukeji.com.cn/xiabin/youtu_grpc/app/user_management/internal/svc" + "gitea.youtukeji.com.cn/xiabin/youtu_grpc/app/user_management/user_management" + + "github.com/zeromicro/go-zero/core/logx" +) + +type GetUserByIdLogic struct { + ctx context.Context + svcCtx *svc.ServiceContext + logx.Logger +} + +func NewGetUserByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserByIdLogic { + return &GetUserByIdLogic{ + ctx: ctx, + svcCtx: svcCtx, + Logger: logx.WithContext(ctx), + } +} + +// GetUserById 通过 ID 获取用户 +func (l *GetUserByIdLogic) GetUserById(in *user_management.UserId) (*user_management.User, error) { + + if in.GetUserId() == 0 { + return &user_management.User{}, errors.New("userId is empty") + } + + user, err := l.svcCtx.Query.User.WithContext(l.ctx).Where(l.svcCtx.Query.User.ID.Eq(in.UserId)).First() + if err != nil { + return &user_management.User{}, err + } + + return &user_management.User{ + ID: user.ID, + Nickname: user.Nickname, + Avatar: user.Avatar, + }, nil +} diff --git a/app/user_management/internal/server/user_management_server.go b/app/user_management/internal/server/user_management_server.go index e50980d..6cc4a5e 100644 --- a/app/user_management/internal/server/user_management_server.go +++ b/app/user_management/internal/server/user_management_server.go @@ -28,19 +28,26 @@ func (s *UserManagementServer) Ping(ctx context.Context, in *user_management.Req return l.Ping(in) } -// 设置用户信息 +// SetUser 设置用户信息 func (s *UserManagementServer) SetUser(ctx context.Context, in *user_management.SetUserRequest) (*user_management.SetUserResponse, error) { l := logic.NewSetUserLogic(ctx, s.svcCtx) return l.SetUser(in) } -// 获取用户或者创建用户 +// CreateUser 获取用户或者创建用户 func (s *UserManagementServer) CreateUser(ctx context.Context, in *user_management.CreateUserRequest) (*user_management.UserId, error) { l := logic.NewCreateUserLogic(ctx, s.svcCtx) return l.CreateUser(in) } +// CreateEmptyUser 创建空用户,用于关联游戏账号的空数据 func (s *UserManagementServer) CreateEmptyUser(ctx context.Context, in *user_management.Empty) (*user_management.UserId, error) { l := logic.NewCreateEmptyUserLogic(ctx, s.svcCtx) return l.CreateEmptyUser(in) } + +// GetUserById 通过 ID 获取用户 +func (s *UserManagementServer) GetUserById(ctx context.Context, in *user_management.UserId) (*user_management.User, error) { + l := logic.NewGetUserByIdLogic(ctx, s.svcCtx) + return l.GetUserById(in) +} diff --git a/app/user_management/user_management.proto b/app/user_management/user_management.proto index 89c54d2..5cf4309 100644 --- a/app/user_management/user_management.proto +++ b/app/user_management/user_management.proto @@ -32,14 +32,24 @@ message UserId{ message Empty {}//空结构体 +message User { + uint64 ID = 1; + string Nickname = 2; + string Avatar = 3; +} + service user_management { rpc Ping(Request) returns(Response); - //设置用户信息 + //SetUser 设置用户信息 rpc SetUser (SetUserRequest) returns(SetUserResponse); - //获取用户或者创建用户 + //CreateUser 获取用户或者创建用户 rpc CreateUser(CreateUserRequest) returns(UserId); + //CreateEmptyUser 创建空用户,用于关联游戏账号的空数据 rpc CreateEmptyUser(Empty) returns(UserId); + + //GetUserById 通过 ID 获取用户 + rpc GetUserById(UserId) returns(User); } diff --git a/app/user_management/user_management/user_management.pb.go b/app/user_management/user_management/user_management.pb.go index 65afca9..ba7c405 100644 --- a/app/user_management/user_management/user_management.pb.go +++ b/app/user_management/user_management/user_management.pb.go @@ -345,6 +345,66 @@ func (*Empty) Descriptor() ([]byte, []int) { return file_user_management_proto_rawDescGZIP(), []int{6} } +type User struct { + state protoimpl.MessageState `protogen:"open.v1"` + ID uint64 `protobuf:"varint,1,opt,name=ID,proto3" json:"ID,omitempty"` + Nickname string `protobuf:"bytes,2,opt,name=Nickname,proto3" json:"Nickname,omitempty"` + Avatar string `protobuf:"bytes,3,opt,name=Avatar,proto3" json:"Avatar,omitempty"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *User) Reset() { + *x = User{} + mi := &file_user_management_proto_msgTypes[7] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *User) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*User) ProtoMessage() {} + +func (x *User) ProtoReflect() protoreflect.Message { + mi := &file_user_management_proto_msgTypes[7] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use User.ProtoReflect.Descriptor instead. +func (*User) Descriptor() ([]byte, []int) { + return file_user_management_proto_rawDescGZIP(), []int{7} +} + +func (x *User) GetID() uint64 { + if x != nil { + return x.ID + } + return 0 +} + +func (x *User) GetNickname() string { + if x != nil { + return x.Nickname + } + return "" +} + +func (x *User) GetAvatar() string { + if x != nil { + return x.Avatar + } + return "" +} + var File_user_management_proto protoreflect.FileDescriptor var file_user_management_proto_rawDesc = string([]byte{ @@ -371,27 +431,36 @@ var file_user_management_proto_rawDesc = string([]byte{ 0x76, 0x61, 0x74, 0x61, 0x72, 0x22, 0x20, 0x0a, 0x06, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x04, 0x52, 0x06, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x22, 0x07, 0x0a, 0x05, 0x45, 0x6d, 0x70, 0x74, 0x79, - 0x32, 0xab, 0x02, 0x0a, 0x0f, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x6d, 0x61, 0x6e, 0x61, 0x67, 0x65, - 0x6d, 0x65, 0x6e, 0x74, 0x12, 0x3b, 0x0a, 0x04, 0x50, 0x69, 0x6e, 0x67, 0x12, 0x18, 0x2e, 0x75, - 0x73, 0x65, 0x72, 0x5f, 0x6d, 0x61, 0x6e, 0x61, 0x67, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x2e, 0x52, - 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x19, 0x2e, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x6d, 0x61, - 0x6e, 0x61, 0x67, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x2e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, - 0x65, 0x12, 0x4c, 0x0a, 0x07, 0x53, 0x65, 0x74, 0x55, 0x73, 0x65, 0x72, 0x12, 0x1f, 0x2e, 0x75, - 0x73, 0x65, 0x72, 0x5f, 0x6d, 0x61, 0x6e, 0x61, 0x67, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x2e, 0x53, - 0x65, 0x74, 0x55, 0x73, 0x65, 0x72, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x20, 0x2e, + 0x22, 0x4a, 0x0a, 0x04, 0x55, 0x73, 0x65, 0x72, 0x12, 0x0e, 0x0a, 0x02, 0x49, 0x44, 0x18, 0x01, + 0x20, 0x01, 0x28, 0x04, 0x52, 0x02, 0x49, 0x44, 0x12, 0x1a, 0x0a, 0x08, 0x4e, 0x69, 0x63, 0x6b, + 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x4e, 0x69, 0x63, 0x6b, + 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x41, 0x76, 0x61, 0x74, 0x61, 0x72, 0x18, 0x03, + 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x41, 0x76, 0x61, 0x74, 0x61, 0x72, 0x32, 0xea, 0x02, 0x0a, + 0x0f, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x6d, 0x61, 0x6e, 0x61, 0x67, 0x65, 0x6d, 0x65, 0x6e, 0x74, + 0x12, 0x3b, 0x0a, 0x04, 0x50, 0x69, 0x6e, 0x67, 0x12, 0x18, 0x2e, 0x75, 0x73, 0x65, 0x72, 0x5f, + 0x6d, 0x61, 0x6e, 0x61, 0x67, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x2e, 0x52, 0x65, 0x71, 0x75, 0x65, + 0x73, 0x74, 0x1a, 0x19, 0x2e, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x6d, 0x61, 0x6e, 0x61, 0x67, 0x65, + 0x6d, 0x65, 0x6e, 0x74, 0x2e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x4c, 0x0a, + 0x07, 0x53, 0x65, 0x74, 0x55, 0x73, 0x65, 0x72, 0x12, 0x1f, 0x2e, 0x75, 0x73, 0x65, 0x72, 0x5f, + 0x6d, 0x61, 0x6e, 0x61, 0x67, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x2e, 0x53, 0x65, 0x74, 0x55, 0x73, + 0x65, 0x72, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x20, 0x2e, 0x75, 0x73, 0x65, 0x72, + 0x5f, 0x6d, 0x61, 0x6e, 0x61, 0x67, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x2e, 0x53, 0x65, 0x74, 0x55, + 0x73, 0x65, 0x72, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x49, 0x0a, 0x0a, 0x43, + 0x72, 0x65, 0x61, 0x74, 0x65, 0x55, 0x73, 0x65, 0x72, 0x12, 0x22, 0x2e, 0x75, 0x73, 0x65, 0x72, + 0x5f, 0x6d, 0x61, 0x6e, 0x61, 0x67, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x2e, 0x43, 0x72, 0x65, 0x61, + 0x74, 0x65, 0x55, 0x73, 0x65, 0x72, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x17, 0x2e, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x6d, 0x61, 0x6e, 0x61, 0x67, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x2e, - 0x53, 0x65, 0x74, 0x55, 0x73, 0x65, 0x72, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, - 0x49, 0x0a, 0x0a, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x55, 0x73, 0x65, 0x72, 0x12, 0x22, 0x2e, - 0x75, 0x73, 0x65, 0x72, 0x5f, 0x6d, 0x61, 0x6e, 0x61, 0x67, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x2e, - 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x55, 0x73, 0x65, 0x72, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, - 0x74, 0x1a, 0x17, 0x2e, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x6d, 0x61, 0x6e, 0x61, 0x67, 0x65, 0x6d, - 0x65, 0x6e, 0x74, 0x2e, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x12, 0x42, 0x0a, 0x0f, 0x43, 0x72, - 0x65, 0x61, 0x74, 0x65, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x55, 0x73, 0x65, 0x72, 0x12, 0x16, 0x2e, - 0x75, 0x73, 0x65, 0x72, 0x5f, 0x6d, 0x61, 0x6e, 0x61, 0x67, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x2e, - 0x45, 0x6d, 0x70, 0x74, 0x79, 0x1a, 0x17, 0x2e, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x6d, 0x61, 0x6e, - 0x61, 0x67, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x2e, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x42, 0x13, - 0x5a, 0x11, 0x2e, 0x2f, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x6d, 0x61, 0x6e, 0x61, 0x67, 0x65, 0x6d, - 0x65, 0x6e, 0x74, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x12, 0x42, 0x0a, 0x0f, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, + 0x45, 0x6d, 0x70, 0x74, 0x79, 0x55, 0x73, 0x65, 0x72, 0x12, 0x16, 0x2e, 0x75, 0x73, 0x65, 0x72, + 0x5f, 0x6d, 0x61, 0x6e, 0x61, 0x67, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x2e, 0x45, 0x6d, 0x70, 0x74, + 0x79, 0x1a, 0x17, 0x2e, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x6d, 0x61, 0x6e, 0x61, 0x67, 0x65, 0x6d, + 0x65, 0x6e, 0x74, 0x2e, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x12, 0x3d, 0x0a, 0x0b, 0x47, 0x65, + 0x74, 0x55, 0x73, 0x65, 0x72, 0x42, 0x79, 0x49, 0x64, 0x12, 0x17, 0x2e, 0x75, 0x73, 0x65, 0x72, + 0x5f, 0x6d, 0x61, 0x6e, 0x61, 0x67, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x2e, 0x55, 0x73, 0x65, 0x72, + 0x49, 0x64, 0x1a, 0x15, 0x2e, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x6d, 0x61, 0x6e, 0x61, 0x67, 0x65, + 0x6d, 0x65, 0x6e, 0x74, 0x2e, 0x55, 0x73, 0x65, 0x72, 0x42, 0x13, 0x5a, 0x11, 0x2e, 0x2f, 0x75, + 0x73, 0x65, 0x72, 0x5f, 0x6d, 0x61, 0x6e, 0x61, 0x67, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x62, 0x06, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, }) var ( @@ -406,7 +475,7 @@ func file_user_management_proto_rawDescGZIP() []byte { return file_user_management_proto_rawDescData } -var file_user_management_proto_msgTypes = make([]protoimpl.MessageInfo, 7) +var file_user_management_proto_msgTypes = make([]protoimpl.MessageInfo, 8) var file_user_management_proto_goTypes = []any{ (*Request)(nil), // 0: user_management.Request (*Response)(nil), // 1: user_management.Response @@ -415,18 +484,21 @@ var file_user_management_proto_goTypes = []any{ (*CreateUserRequest)(nil), // 4: user_management.CreateUserRequest (*UserId)(nil), // 5: user_management.UserId (*Empty)(nil), // 6: user_management.Empty + (*User)(nil), // 7: user_management.User } var file_user_management_proto_depIdxs = []int32{ 0, // 0: user_management.user_management.Ping:input_type -> user_management.Request 2, // 1: user_management.user_management.SetUser:input_type -> user_management.SetUserRequest 4, // 2: user_management.user_management.CreateUser:input_type -> user_management.CreateUserRequest 6, // 3: user_management.user_management.CreateEmptyUser:input_type -> user_management.Empty - 1, // 4: user_management.user_management.Ping:output_type -> user_management.Response - 3, // 5: user_management.user_management.SetUser:output_type -> user_management.SetUserResponse - 5, // 6: user_management.user_management.CreateUser:output_type -> user_management.UserId - 5, // 7: user_management.user_management.CreateEmptyUser:output_type -> user_management.UserId - 4, // [4:8] is the sub-list for method output_type - 0, // [0:4] is the sub-list for method input_type + 5, // 4: user_management.user_management.GetUserById:input_type -> user_management.UserId + 1, // 5: user_management.user_management.Ping:output_type -> user_management.Response + 3, // 6: user_management.user_management.SetUser:output_type -> user_management.SetUserResponse + 5, // 7: user_management.user_management.CreateUser:output_type -> user_management.UserId + 5, // 8: user_management.user_management.CreateEmptyUser:output_type -> user_management.UserId + 7, // 9: user_management.user_management.GetUserById:output_type -> user_management.User + 5, // [5:10] is the sub-list for method output_type + 0, // [0:5] is the sub-list for method input_type 0, // [0:0] is the sub-list for extension type_name 0, // [0:0] is the sub-list for extension extendee 0, // [0:0] is the sub-list for field type_name @@ -443,7 +515,7 @@ func file_user_management_proto_init() { GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: unsafe.Slice(unsafe.StringData(file_user_management_proto_rawDesc), len(file_user_management_proto_rawDesc)), NumEnums: 0, - NumMessages: 7, + NumMessages: 8, NumExtensions: 0, NumServices: 1, }, diff --git a/app/user_management/user_management/user_management_grpc.pb.go b/app/user_management/user_management/user_management_grpc.pb.go index ccb5676..72626c4 100644 --- a/app/user_management/user_management/user_management_grpc.pb.go +++ b/app/user_management/user_management/user_management_grpc.pb.go @@ -23,6 +23,7 @@ const ( UserManagement_SetUser_FullMethodName = "/user_management.user_management/SetUser" UserManagement_CreateUser_FullMethodName = "/user_management.user_management/CreateUser" UserManagement_CreateEmptyUser_FullMethodName = "/user_management.user_management/CreateEmptyUser" + UserManagement_GetUserById_FullMethodName = "/user_management.user_management/GetUserById" ) // UserManagementClient is the client API for UserManagement service. @@ -30,11 +31,14 @@ const ( // For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. type UserManagementClient interface { Ping(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Response, error) - // 设置用户信息 + // SetUser 设置用户信息 SetUser(ctx context.Context, in *SetUserRequest, opts ...grpc.CallOption) (*SetUserResponse, error) - // 获取用户或者创建用户 + // CreateUser 获取用户或者创建用户 CreateUser(ctx context.Context, in *CreateUserRequest, opts ...grpc.CallOption) (*UserId, error) + // CreateEmptyUser 创建空用户,用于关联游戏账号的空数据 CreateEmptyUser(ctx context.Context, in *Empty, opts ...grpc.CallOption) (*UserId, error) + // GetUserById 通过 ID 获取用户 + GetUserById(ctx context.Context, in *UserId, opts ...grpc.CallOption) (*User, error) } type userManagementClient struct { @@ -85,16 +89,29 @@ func (c *userManagementClient) CreateEmptyUser(ctx context.Context, in *Empty, o return out, nil } +func (c *userManagementClient) GetUserById(ctx context.Context, in *UserId, opts ...grpc.CallOption) (*User, error) { + cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) + out := new(User) + err := c.cc.Invoke(ctx, UserManagement_GetUserById_FullMethodName, in, out, cOpts...) + if err != nil { + return nil, err + } + return out, nil +} + // UserManagementServer is the server API for UserManagement service. // All implementations must embed UnimplementedUserManagementServer // for forward compatibility. type UserManagementServer interface { Ping(context.Context, *Request) (*Response, error) - // 设置用户信息 + // SetUser 设置用户信息 SetUser(context.Context, *SetUserRequest) (*SetUserResponse, error) - // 获取用户或者创建用户 + // CreateUser 获取用户或者创建用户 CreateUser(context.Context, *CreateUserRequest) (*UserId, error) + // CreateEmptyUser 创建空用户,用于关联游戏账号的空数据 CreateEmptyUser(context.Context, *Empty) (*UserId, error) + // GetUserById 通过 ID 获取用户 + GetUserById(context.Context, *UserId) (*User, error) mustEmbedUnimplementedUserManagementServer() } @@ -117,6 +134,9 @@ func (UnimplementedUserManagementServer) CreateUser(context.Context, *CreateUser func (UnimplementedUserManagementServer) CreateEmptyUser(context.Context, *Empty) (*UserId, error) { return nil, status.Errorf(codes.Unimplemented, "method CreateEmptyUser not implemented") } +func (UnimplementedUserManagementServer) GetUserById(context.Context, *UserId) (*User, error) { + return nil, status.Errorf(codes.Unimplemented, "method GetUserById not implemented") +} func (UnimplementedUserManagementServer) mustEmbedUnimplementedUserManagementServer() {} func (UnimplementedUserManagementServer) testEmbeddedByValue() {} @@ -210,6 +230,24 @@ func _UserManagement_CreateEmptyUser_Handler(srv interface{}, ctx context.Contex return interceptor(ctx, in, info, handler) } +func _UserManagement_GetUserById_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(UserId) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(UserManagementServer).GetUserById(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: UserManagement_GetUserById_FullMethodName, + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(UserManagementServer).GetUserById(ctx, req.(*UserId)) + } + return interceptor(ctx, in, info, handler) +} + // UserManagement_ServiceDesc is the grpc.ServiceDesc for UserManagement service. // It's only intended for direct use with grpc.RegisterService, // and not to be introspected or modified (even as a copy) @@ -233,6 +271,10 @@ var UserManagement_ServiceDesc = grpc.ServiceDesc{ MethodName: "CreateEmptyUser", Handler: _UserManagement_CreateEmptyUser_Handler, }, + { + MethodName: "GetUserById", + Handler: _UserManagement_GetUserById_Handler, + }, }, Streams: []grpc.StreamDesc{}, Metadata: "user_management.proto", diff --git a/app/user_management/user_management_client/user_management.go b/app/user_management/user_management_client/user_management.go index 00f6e31..7039889 100644 --- a/app/user_management/user_management_client/user_management.go +++ b/app/user_management/user_management_client/user_management.go @@ -20,15 +20,19 @@ type ( Response = user_management.Response SetUserRequest = user_management.SetUserRequest SetUserResponse = user_management.SetUserResponse + User = user_management.User UserId = user_management.UserId UserManagement interface { Ping(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Response, error) - // 设置用户信息 + // SetUser 设置用户信息 SetUser(ctx context.Context, in *SetUserRequest, opts ...grpc.CallOption) (*SetUserResponse, error) - // 获取用户或者创建用户 + // CreateUser 获取用户或者创建用户 CreateUser(ctx context.Context, in *CreateUserRequest, opts ...grpc.CallOption) (*UserId, error) + // CreateEmptyUser 创建空用户,用于关联游戏账号的空数据 CreateEmptyUser(ctx context.Context, in *Empty, opts ...grpc.CallOption) (*UserId, error) + // GetUserById 通过 ID 获取用户 + GetUserById(ctx context.Context, in *UserId, opts ...grpc.CallOption) (*User, error) } defaultUserManagement struct { @@ -47,19 +51,26 @@ func (m *defaultUserManagement) Ping(ctx context.Context, in *Request, opts ...g return client.Ping(ctx, in, opts...) } -// 设置用户信息 +// SetUser 设置用户信息 func (m *defaultUserManagement) SetUser(ctx context.Context, in *SetUserRequest, opts ...grpc.CallOption) (*SetUserResponse, error) { client := user_management.NewUserManagementClient(m.cli.Conn()) return client.SetUser(ctx, in, opts...) } -// 获取用户或者创建用户 +// CreateUser 获取用户或者创建用户 func (m *defaultUserManagement) CreateUser(ctx context.Context, in *CreateUserRequest, opts ...grpc.CallOption) (*UserId, error) { client := user_management.NewUserManagementClient(m.cli.Conn()) return client.CreateUser(ctx, in, opts...) } +// CreateEmptyUser 创建空用户,用于关联游戏账号的空数据 func (m *defaultUserManagement) CreateEmptyUser(ctx context.Context, in *Empty, opts ...grpc.CallOption) (*UserId, error) { client := user_management.NewUserManagementClient(m.cli.Conn()) return client.CreateEmptyUser(ctx, in, opts...) } + +// GetUserById 通过 ID 获取用户 +func (m *defaultUserManagement) GetUserById(ctx context.Context, in *UserId, opts ...grpc.CallOption) (*User, error) { + client := user_management.NewUserManagementClient(m.cli.Conn()) + return client.GetUserById(ctx, in, opts...) +}