分布式登录

master
shenzhuan 2 years ago
parent 57f74a1f33
commit 4117638ad4

@ -58,8 +58,9 @@ func main() {
micro.WrapClient(roundrobin.NewClientWrapper()),
)
client := proto.NewAddCartService("shop-cart", rpcServer.Client())
clientA := proto.NewShowProductDetailService("shop-product", rpcServer.Client())
clientB := proto.NewShowDetailSkuService("shop-product", rpcServer.Client())
ShowProductDetailClient := proto.NewShowProductDetailService("shop-product", rpcServer.Client())
ShowDetailSkuClient := proto.NewShowDetailSkuService("shop-product", rpcServer.Client())
GetUserTokenClient := proto.NewGetUserTokenService("shop-user", rpcServer.Client())
//分页查询商品列表
router.GET("/increase", func(c *gin.Context) {
// "number": 0,
@ -75,11 +76,28 @@ func main() {
ProductId: int32(productId),
ProductSkuId: int32(productSkuId),
}
resp, err := client.AddCart(context.TODO(), req)
//Token校验
//拼接请求信息
tokenReq := &proto.TokenReq{
Uuid: c.Request.Header["Uuid"][0],
}
//响应
tokenResp, err := GetUserTokenClient.GetUserToken(context.TODO(), tokenReq)
if err != nil || tokenResp.IsLogin == false {
log.Println("GetUserToken err : ", err)
common.RespFail(c.Writer, resp, "未登录!")
return
}
log.Println("GetUserToken success : ", tokenResp)
//商品详情
reqDetail := &proto.ProductDetailReq{
Id: int32(productId),
}
respDetail, err := clientA.ShowProductDetail(context.TODO(), reqDetail)
respDetail, err := ShowProductDetailClient.ShowProductDetail(context.TODO(), reqDetail)
if respDetail != nil {
req.ProductName = respDetail.ProductDetail[0].Name
req.ProductMainPicture = respDetail.ProductDetail[0].MainPicture
@ -88,7 +106,7 @@ func main() {
log.Println(" /ShowProductDetail resp :", respDetail)
//SKU详情
reqDetail.Id = req.ProductSkuId
respSkuDetail, err := clientB.ShowDetailSku(context.TODO(), reqDetail)
respSkuDetail, err := ShowDetailSkuClient.ShowDetailSku(context.TODO(), reqDetail)
log.Println(" /ShowDetailSku resp :", respSkuDetail)
@ -100,7 +118,6 @@ func main() {
return
}
resp, err := client.AddCart(context.TODO(), req)
resp.ProductSkuSimple = respSkuDetail.ProductSku[0]
resp.ProductSimple = respDetail.ProductDetail[0]

@ -1028,6 +1028,110 @@ func (x *ProductSkuResp) GetProductSku() []*ProductSku {
return nil
}
// 获取 分布式 token
type TokenReq struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Uuid string `protobuf:"bytes,1,opt,name=uuid,proto3" json:"uuid,omitempty"`
}
func (x *TokenReq) Reset() {
*x = TokenReq{}
if protoimpl.UnsafeEnabled {
mi := &file_cart_proto_msgTypes[12]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *TokenReq) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*TokenReq) ProtoMessage() {}
func (x *TokenReq) ProtoReflect() protoreflect.Message {
mi := &file_cart_proto_msgTypes[12]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use TokenReq.ProtoReflect.Descriptor instead.
func (*TokenReq) Descriptor() ([]byte, []int) {
return file_cart_proto_rawDescGZIP(), []int{12}
}
func (x *TokenReq) GetUuid() string {
if x != nil {
return x.Uuid
}
return ""
}
// 响应 resp struct
type TokenResp struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Token string `protobuf:"bytes,1,opt,name=token,proto3" json:"token,omitempty"`
IsLogin bool `protobuf:"varint,2,opt,name=isLogin,proto3" json:"isLogin,omitempty"`
}
func (x *TokenResp) Reset() {
*x = TokenResp{}
if protoimpl.UnsafeEnabled {
mi := &file_cart_proto_msgTypes[13]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *TokenResp) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*TokenResp) ProtoMessage() {}
func (x *TokenResp) ProtoReflect() protoreflect.Message {
mi := &file_cart_proto_msgTypes[13]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use TokenResp.ProtoReflect.Descriptor instead.
func (*TokenResp) Descriptor() ([]byte, []int) {
return file_cart_proto_rawDescGZIP(), []int{13}
}
func (x *TokenResp) GetToken() string {
if x != nil {
return x.Token
}
return ""
}
func (x *TokenResp) GetIsLogin() bool {
if x != nil {
return x.IsLogin
}
return false
}
var File_cart_proto protoreflect.FileDescriptor
var file_cart_proto_rawDesc = []byte{
@ -1171,32 +1275,42 @@ var file_cart_proto_rawDesc = []byte{
0x75, 0x52, 0x65, 0x73, 0x70, 0x12, 0x31, 0x0a, 0x0a, 0x70, 0x72, 0x6f, 0x64, 0x75, 0x63, 0x74,
0x53, 0x6b, 0x75, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x11, 0x2e, 0x70, 0x72, 0x6f, 0x74,
0x6f, 0x2e, 0x50, 0x72, 0x6f, 0x64, 0x75, 0x63, 0x74, 0x53, 0x6b, 0x75, 0x52, 0x0a, 0x70, 0x72,
0x6f, 0x64, 0x75, 0x63, 0x74, 0x53, 0x6b, 0x75, 0x32, 0x3d, 0x0a, 0x07, 0x41, 0x64, 0x64, 0x43,
0x61, 0x72, 0x74, 0x12, 0x32, 0x0a, 0x07, 0x41, 0x64, 0x64, 0x43, 0x61, 0x72, 0x74, 0x12, 0x11,
0x6f, 0x64, 0x75, 0x63, 0x74, 0x53, 0x6b, 0x75, 0x22, 0x1e, 0x0a, 0x08, 0x54, 0x6f, 0x6b, 0x65,
0x6e, 0x52, 0x65, 0x71, 0x12, 0x12, 0x0a, 0x04, 0x75, 0x75, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01,
0x28, 0x09, 0x52, 0x04, 0x75, 0x75, 0x69, 0x64, 0x22, 0x3b, 0x0a, 0x09, 0x54, 0x6f, 0x6b, 0x65,
0x6e, 0x52, 0x65, 0x73, 0x70, 0x12, 0x14, 0x0a, 0x05, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x18, 0x01,
0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x12, 0x18, 0x0a, 0x07, 0x69,
0x73, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x18, 0x02, 0x20, 0x01, 0x28, 0x08, 0x52, 0x07, 0x69, 0x73,
0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x32, 0x3d, 0x0a, 0x07, 0x41, 0x64, 0x64, 0x43, 0x61, 0x72, 0x74,
0x12, 0x32, 0x0a, 0x07, 0x41, 0x64, 0x64, 0x43, 0x61, 0x72, 0x74, 0x12, 0x11, 0x2e, 0x70, 0x72,
0x6f, 0x74, 0x6f, 0x2e, 0x41, 0x64, 0x64, 0x43, 0x61, 0x72, 0x74, 0x52, 0x65, 0x71, 0x1a, 0x12,
0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x41, 0x64, 0x64, 0x43, 0x61, 0x72, 0x74, 0x52, 0x65,
0x71, 0x1a, 0x12, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x41, 0x64, 0x64, 0x43, 0x61, 0x72,
0x74, 0x52, 0x65, 0x73, 0x70, 0x22, 0x00, 0x32, 0x31, 0x0a, 0x04, 0x50, 0x61, 0x67, 0x65, 0x12,
0x29, 0x0a, 0x04, 0x50, 0x61, 0x67, 0x65, 0x12, 0x0e, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e,
0x50, 0x61, 0x67, 0x65, 0x52, 0x65, 0x71, 0x1a, 0x0f, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e,
0x50, 0x61, 0x67, 0x65, 0x52, 0x65, 0x73, 0x70, 0x22, 0x00, 0x32, 0x5d, 0x0a, 0x11, 0x53, 0x68,
0x6f, 0x77, 0x50, 0x72, 0x6f, 0x64, 0x75, 0x63, 0x74, 0x44, 0x65, 0x74, 0x61, 0x69, 0x6c, 0x12,
0x48, 0x0a, 0x11, 0x53, 0x68, 0x6f, 0x77, 0x50, 0x72, 0x6f, 0x64, 0x75, 0x63, 0x74, 0x44, 0x65,
0x74, 0x61, 0x69, 0x6c, 0x12, 0x17, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x50, 0x72, 0x6f,
0x64, 0x75, 0x63, 0x74, 0x44, 0x65, 0x74, 0x61, 0x69, 0x6c, 0x52, 0x65, 0x71, 0x1a, 0x18, 0x2e,
0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x50, 0x72, 0x6f, 0x64, 0x75, 0x63, 0x74, 0x44, 0x65, 0x74,
0x61, 0x69, 0x6c, 0x52, 0x65, 0x73, 0x70, 0x22, 0x00, 0x32, 0x51, 0x0a, 0x0e, 0x53, 0x68, 0x6f,
0x77, 0x50, 0x72, 0x6f, 0x64, 0x75, 0x63, 0x74, 0x53, 0x6b, 0x75, 0x12, 0x3f, 0x0a, 0x0e, 0x53,
0x68, 0x6f, 0x77, 0x50, 0x72, 0x6f, 0x64, 0x75, 0x63, 0x74, 0x53, 0x6b, 0x75, 0x12, 0x14, 0x2e,
0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x50, 0x72, 0x6f, 0x64, 0x75, 0x63, 0x74, 0x53, 0x6b, 0x75,
0x52, 0x65, 0x71, 0x1a, 0x15, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x50, 0x72, 0x6f, 0x64,
0x75, 0x63, 0x74, 0x53, 0x6b, 0x75, 0x52, 0x65, 0x73, 0x70, 0x22, 0x00, 0x32, 0x52, 0x0a, 0x0d,
0x53, 0x68, 0x6f, 0x77, 0x44, 0x65, 0x74, 0x61, 0x69, 0x6c, 0x53, 0x6b, 0x75, 0x12, 0x41, 0x0a,
0x0d, 0x53, 0x68, 0x6f, 0x77, 0x44, 0x65, 0x74, 0x61, 0x69, 0x6c, 0x53, 0x6b, 0x75, 0x12, 0x17,
0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x50, 0x72, 0x6f, 0x64, 0x75, 0x63, 0x74, 0x44, 0x65,
0x74, 0x61, 0x69, 0x6c, 0x52, 0x65, 0x71, 0x1a, 0x15, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e,
0x50, 0x72, 0x6f, 0x64, 0x75, 0x63, 0x74, 0x53, 0x6b, 0x75, 0x52, 0x65, 0x73, 0x70, 0x22, 0x00,
0x42, 0x0a, 0x5a, 0x08, 0x2e, 0x2f, 0x3b, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x06, 0x70, 0x72,
0x6f, 0x74, 0x6f, 0x33,
0x73, 0x70, 0x22, 0x00, 0x32, 0x31, 0x0a, 0x04, 0x50, 0x61, 0x67, 0x65, 0x12, 0x29, 0x0a, 0x04,
0x50, 0x61, 0x67, 0x65, 0x12, 0x0e, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x50, 0x61, 0x67,
0x65, 0x52, 0x65, 0x71, 0x1a, 0x0f, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x50, 0x61, 0x67,
0x65, 0x52, 0x65, 0x73, 0x70, 0x22, 0x00, 0x32, 0x5d, 0x0a, 0x11, 0x53, 0x68, 0x6f, 0x77, 0x50,
0x72, 0x6f, 0x64, 0x75, 0x63, 0x74, 0x44, 0x65, 0x74, 0x61, 0x69, 0x6c, 0x12, 0x48, 0x0a, 0x11,
0x53, 0x68, 0x6f, 0x77, 0x50, 0x72, 0x6f, 0x64, 0x75, 0x63, 0x74, 0x44, 0x65, 0x74, 0x61, 0x69,
0x6c, 0x12, 0x17, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x50, 0x72, 0x6f, 0x64, 0x75, 0x63,
0x74, 0x44, 0x65, 0x74, 0x61, 0x69, 0x6c, 0x52, 0x65, 0x71, 0x1a, 0x18, 0x2e, 0x70, 0x72, 0x6f,
0x74, 0x6f, 0x2e, 0x50, 0x72, 0x6f, 0x64, 0x75, 0x63, 0x74, 0x44, 0x65, 0x74, 0x61, 0x69, 0x6c,
0x52, 0x65, 0x73, 0x70, 0x22, 0x00, 0x32, 0x51, 0x0a, 0x0e, 0x53, 0x68, 0x6f, 0x77, 0x50, 0x72,
0x6f, 0x64, 0x75, 0x63, 0x74, 0x53, 0x6b, 0x75, 0x12, 0x3f, 0x0a, 0x0e, 0x53, 0x68, 0x6f, 0x77,
0x50, 0x72, 0x6f, 0x64, 0x75, 0x63, 0x74, 0x53, 0x6b, 0x75, 0x12, 0x14, 0x2e, 0x70, 0x72, 0x6f,
0x74, 0x6f, 0x2e, 0x50, 0x72, 0x6f, 0x64, 0x75, 0x63, 0x74, 0x53, 0x6b, 0x75, 0x52, 0x65, 0x71,
0x1a, 0x15, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x50, 0x72, 0x6f, 0x64, 0x75, 0x63, 0x74,
0x53, 0x6b, 0x75, 0x52, 0x65, 0x73, 0x70, 0x22, 0x00, 0x32, 0x52, 0x0a, 0x0d, 0x53, 0x68, 0x6f,
0x77, 0x44, 0x65, 0x74, 0x61, 0x69, 0x6c, 0x53, 0x6b, 0x75, 0x12, 0x41, 0x0a, 0x0d, 0x53, 0x68,
0x6f, 0x77, 0x44, 0x65, 0x74, 0x61, 0x69, 0x6c, 0x53, 0x6b, 0x75, 0x12, 0x17, 0x2e, 0x70, 0x72,
0x6f, 0x74, 0x6f, 0x2e, 0x50, 0x72, 0x6f, 0x64, 0x75, 0x63, 0x74, 0x44, 0x65, 0x74, 0x61, 0x69,
0x6c, 0x52, 0x65, 0x71, 0x1a, 0x15, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x50, 0x72, 0x6f,
0x64, 0x75, 0x63, 0x74, 0x53, 0x6b, 0x75, 0x52, 0x65, 0x73, 0x70, 0x22, 0x00, 0x32, 0x43, 0x0a,
0x0c, 0x47, 0x65, 0x74, 0x55, 0x73, 0x65, 0x72, 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x12, 0x33, 0x0a,
0x0c, 0x47, 0x65, 0x74, 0x55, 0x73, 0x65, 0x72, 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x12, 0x0f, 0x2e,
0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x52, 0x65, 0x71, 0x1a, 0x10,
0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x52, 0x65, 0x73, 0x70,
0x22, 0x00, 0x42, 0x0a, 0x5a, 0x08, 0x2e, 0x2f, 0x3b, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x06,
0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (
@ -1211,7 +1325,7 @@ func file_cart_proto_rawDescGZIP() []byte {
return file_cart_proto_rawDescData
}
var file_cart_proto_msgTypes = make([]protoimpl.MessageInfo, 13)
var file_cart_proto_msgTypes = make([]protoimpl.MessageInfo, 15)
var file_cart_proto_goTypes = []interface{}{
(*ShoppingCart)(nil), // 0: proto.ShoppingCart
(*AddCartReq)(nil), // 1: proto.AddCartReq
@ -1225,12 +1339,14 @@ var file_cart_proto_goTypes = []interface{}{
(*ProductSku)(nil), // 9: proto.ProductSku
(*ProductSkuReq)(nil), // 10: proto.ProductSkuReq
(*ProductSkuResp)(nil), // 11: proto.ProductSkuResp
nil, // 12: proto.Product.LabelListEntry
(*TokenReq)(nil), // 12: proto.TokenReq
(*TokenResp)(nil), // 13: proto.TokenResp
nil, // 14: proto.Product.LabelListEntry
}
var file_cart_proto_depIdxs = []int32{
6, // 0: proto.AddCartResp.productSimple:type_name -> proto.ProductDetail
9, // 1: proto.AddCartResp.productSkuSimple:type_name -> proto.ProductSku
12, // 2: proto.Product.labelList:type_name -> proto.Product.LabelListEntry
14, // 2: proto.Product.labelList:type_name -> proto.Product.LabelListEntry
3, // 3: proto.PageResp.product:type_name -> proto.Product
6, // 4: proto.ProductDetailResp.productDetail:type_name -> proto.ProductDetail
9, // 5: proto.ProductSkuResp.productSku:type_name -> proto.ProductSku
@ -1239,13 +1355,15 @@ var file_cart_proto_depIdxs = []int32{
7, // 8: proto.ShowProductDetail.ShowProductDetail:input_type -> proto.ProductDetailReq
10, // 9: proto.ShowProductSku.ShowProductSku:input_type -> proto.ProductSkuReq
7, // 10: proto.ShowDetailSku.ShowDetailSku:input_type -> proto.ProductDetailReq
2, // 11: proto.AddCart.AddCart:output_type -> proto.AddCartResp
5, // 12: proto.Page.Page:output_type -> proto.PageResp
8, // 13: proto.ShowProductDetail.ShowProductDetail:output_type -> proto.ProductDetailResp
11, // 14: proto.ShowProductSku.ShowProductSku:output_type -> proto.ProductSkuResp
11, // 15: proto.ShowDetailSku.ShowDetailSku:output_type -> proto.ProductSkuResp
11, // [11:16] is the sub-list for method output_type
6, // [6:11] is the sub-list for method input_type
12, // 11: proto.GetUserToken.GetUserToken:input_type -> proto.TokenReq
2, // 12: proto.AddCart.AddCart:output_type -> proto.AddCartResp
5, // 13: proto.Page.Page:output_type -> proto.PageResp
8, // 14: proto.ShowProductDetail.ShowProductDetail:output_type -> proto.ProductDetailResp
11, // 15: proto.ShowProductSku.ShowProductSku:output_type -> proto.ProductSkuResp
11, // 16: proto.ShowDetailSku.ShowDetailSku:output_type -> proto.ProductSkuResp
13, // 17: proto.GetUserToken.GetUserToken:output_type -> proto.TokenResp
12, // [12:18] is the sub-list for method output_type
6, // [6:12] is the sub-list for method input_type
6, // [6:6] is the sub-list for extension type_name
6, // [6:6] is the sub-list for extension extendee
0, // [0:6] is the sub-list for field type_name
@ -1401,6 +1519,30 @@ func file_cart_proto_init() {
return nil
}
}
file_cart_proto_msgTypes[12].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*TokenReq); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_cart_proto_msgTypes[13].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*TokenResp); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
}
type x struct{}
out := protoimpl.TypeBuilder{
@ -1408,9 +1550,9 @@ func file_cart_proto_init() {
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_cart_proto_rawDesc,
NumEnums: 0,
NumMessages: 13,
NumMessages: 15,
NumExtensions: 0,
NumServices: 5,
NumServices: 6,
},
GoTypes: file_cart_proto_goTypes,
DependencyIndexes: file_cart_proto_depIdxs,

@ -331,3 +331,64 @@ type showDetailSkuHandler struct {
func (h *showDetailSkuHandler) ShowDetailSku(ctx context.Context, in *ProductDetailReq, out *ProductSkuResp) error {
return h.ShowDetailSkuHandler.ShowDetailSku(ctx, in, out)
}
// Api Endpoints for GetUserToken service
func NewGetUserTokenEndpoints() []*api.Endpoint {
return []*api.Endpoint{}
}
// Client API for GetUserToken service
type GetUserTokenService interface {
// rpc 服务
GetUserToken(ctx context.Context, in *TokenReq, opts ...client.CallOption) (*TokenResp, error)
}
type getUserTokenService struct {
c client.Client
name string
}
func NewGetUserTokenService(name string, c client.Client) GetUserTokenService {
return &getUserTokenService{
c: c,
name: name,
}
}
func (c *getUserTokenService) GetUserToken(ctx context.Context, in *TokenReq, opts ...client.CallOption) (*TokenResp, error) {
req := c.c.NewRequest(c.name, "GetUserToken.GetUserToken", in)
out := new(TokenResp)
err := c.c.Call(ctx, req, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// Server API for GetUserToken service
type GetUserTokenHandler interface {
// rpc 服务
GetUserToken(context.Context, *TokenReq, *TokenResp) error
}
func RegisterGetUserTokenHandler(s server.Server, hdlr GetUserTokenHandler, opts ...server.HandlerOption) error {
type getUserToken interface {
GetUserToken(ctx context.Context, in *TokenReq, out *TokenResp) error
}
type GetUserToken struct {
getUserToken
}
h := &getUserTokenHandler{hdlr}
return s.Handle(s.NewHandler(&GetUserToken{h}, opts...))
}
type getUserTokenHandler struct {
GetUserTokenHandler
}
func (h *getUserTokenHandler) GetUserToken(ctx context.Context, in *TokenReq, out *TokenResp) error {
return h.GetUserTokenHandler.GetUserToken(ctx, in, out)
}

@ -214,3 +214,18 @@ service ShowDetailSku {
//rpc
rpc ShowDetailSku (ProductDetailReq) returns (ProductSkuResp){}
}
// token
message TokenReq {
string uuid = 1;
}
// resp struct
message TokenResp{
string token = 1;
bool isLogin=2;
}
//RPC
service GetUserToken {
//rpc
rpc GetUserToken (TokenReq) returns (TokenResp){}
}

@ -22,8 +22,8 @@ func GetConsulConfig(url string, fileKey string) (*viper.Viper, error) {
err := conf.ReadRemoteConfig()
if err != nil {
log.Println("viper conf err :", err)
} else {
log.Println("viper conf :", conf)
//}else {
//log.Println("viper conf :", conf)
}
return conf, nil
}

@ -2,6 +2,7 @@ package common
import (
"math"
"strconv"
)
/*
@ -52,3 +53,13 @@ func ToInt(intput string) int {
}
return sum
}
// int 转 二进制的字符串
func ConverToBinary(n int) string {
res := ""
for ; n > 0; n /= 2 {
lsb := n % 2
res = strconv.Itoa(lsb) + res
}
return res
}

@ -2,12 +2,12 @@ package repository
import (
"errors"
"fmt"
"github.com/go-redis/redis/v8"
"gorm.io/gorm"
"gouser/common"
"gouser/domain/model"
"time"
"gorm.io/gorm"
)
/**
@ -52,7 +52,12 @@ func (u *UserRepository) Login(clientId int32, phone string, systemId int32, ver
}
}
func (u *UserRepository) SetUserToken(key string, val []byte, timeTTL time.Duration) {
common.SetUserToken(u.red, key, val, timeTTL)
intKey := common.ToInt(key)
binKey := common.ConverToBinary(intKey)
fmt.Println(">>>>>>>>>>>>>>>>>>>>>>>", binKey)
common.SetUserToken(u.red, binKey, val, timeTTL)
}
func (u *UserRepository) GetUserToken(key string) string {

@ -49,12 +49,15 @@ func UserForResp(userModel *model.User, resp *proto.LoginResp) *proto.LoginResp
return resp
}
//func (u *User) GetUserToken(ctx context.Context, loginRequest *proto.LoginRequest, loginResp *proto.LoginResp) bool {
// userInfo, err := u.UserDataService.GetUserToken()
// if err != nil {
// return err
// }
// fmt.Println(">>>>>>>>>>>>> login success :", userInfo)
// UserForResp(userInfo, loginResp)
// return nil
//}
func (u *User) GetUserToken(ctx context.Context, req *proto.TokenReq, resp *proto.TokenResp) error {
res := u.UserDataService.GetUserToken(req.GetUuid())
if res != "" {
resp.IsLogin = true
resp.Token = res
fmt.Println(">>>>>>>>>>>>> GetUserToken success :", res)
} else {
resp.IsLogin = false
resp.Token = ""
}
return nil
}

@ -54,7 +54,7 @@ func main() {
userDataService := service.NewUserDataService(repository.NewUserRepository(db, red))
//4.注册handler
proto.RegisterLoginHandler(repcService.Server(), &handler.User{userDataService})
proto.RegisterGetUserTokenHandler(repcService.Server(), &handler.User{userDataService})
//5.启动服务
if err := repcService.Run(); err != nil {
log.Println("start user service err :", err)

@ -11,6 +11,10 @@
package proto
import (
context "context"
grpc "google.golang.org/grpc"
codes "google.golang.org/grpc/codes"
status "google.golang.org/grpc/status"
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
reflect "reflect"
@ -283,6 +287,110 @@ func (x *LoginResp) GetUser() *User {
return nil
}
// 获取 分布式 token
type TokenReq struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Uuid string `protobuf:"bytes,1,opt,name=uuid,proto3" json:"uuid,omitempty"`
}
func (x *TokenReq) Reset() {
*x = TokenReq{}
if protoimpl.UnsafeEnabled {
mi := &file_user_proto_msgTypes[3]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *TokenReq) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*TokenReq) ProtoMessage() {}
func (x *TokenReq) ProtoReflect() protoreflect.Message {
mi := &file_user_proto_msgTypes[3]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use TokenReq.ProtoReflect.Descriptor instead.
func (*TokenReq) Descriptor() ([]byte, []int) {
return file_user_proto_rawDescGZIP(), []int{3}
}
func (x *TokenReq) GetUuid() string {
if x != nil {
return x.Uuid
}
return ""
}
// 响应 resp struct
type TokenResp struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Token string `protobuf:"bytes,1,opt,name=token,proto3" json:"token,omitempty"`
IsLogin bool `protobuf:"varint,2,opt,name=isLogin,proto3" json:"isLogin,omitempty"`
}
func (x *TokenResp) Reset() {
*x = TokenResp{}
if protoimpl.UnsafeEnabled {
mi := &file_user_proto_msgTypes[4]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *TokenResp) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*TokenResp) ProtoMessage() {}
func (x *TokenResp) ProtoReflect() protoreflect.Message {
mi := &file_user_proto_msgTypes[4]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use TokenResp.ProtoReflect.Descriptor instead.
func (*TokenResp) Descriptor() ([]byte, []int) {
return file_user_proto_rawDescGZIP(), []int{4}
}
func (x *TokenResp) GetToken() string {
if x != nil {
return x.Token
}
return ""
}
func (x *TokenResp) GetIsLogin() bool {
if x != nil {
return x.IsLogin
}
return false
}
var File_user_proto protoreflect.FileDescriptor
var file_user_proto_rawDesc = []byte{
@ -317,12 +425,22 @@ var file_user_proto_rawDesc = []byte{
0x73, 0x70, 0x12, 0x14, 0x0a, 0x05, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x18, 0x01, 0x20, 0x01, 0x28,
0x09, 0x52, 0x05, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x12, 0x1f, 0x0a, 0x04, 0x75, 0x73, 0x65, 0x72,
0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0b, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x55,
0x73, 0x65, 0x72, 0x52, 0x04, 0x75, 0x73, 0x65, 0x72, 0x32, 0x39, 0x0a, 0x05, 0x4c, 0x6f, 0x67,
0x69, 0x6e, 0x12, 0x30, 0x0a, 0x05, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x12, 0x13, 0x2e, 0x70, 0x72,
0x6f, 0x74, 0x6f, 0x2e, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74,
0x1a, 0x10, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x52, 0x65,
0x73, 0x70, 0x22, 0x00, 0x42, 0x0a, 0x5a, 0x08, 0x2e, 0x2f, 0x3b, 0x70, 0x72, 0x6f, 0x74, 0x6f,
0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
0x73, 0x65, 0x72, 0x52, 0x04, 0x75, 0x73, 0x65, 0x72, 0x22, 0x1e, 0x0a, 0x08, 0x54, 0x6f, 0x6b,
0x65, 0x6e, 0x52, 0x65, 0x71, 0x12, 0x12, 0x0a, 0x04, 0x75, 0x75, 0x69, 0x64, 0x18, 0x01, 0x20,
0x01, 0x28, 0x09, 0x52, 0x04, 0x75, 0x75, 0x69, 0x64, 0x22, 0x3b, 0x0a, 0x09, 0x54, 0x6f, 0x6b,
0x65, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x12, 0x14, 0x0a, 0x05, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x18,
0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x12, 0x18, 0x0a, 0x07,
0x69, 0x73, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x18, 0x02, 0x20, 0x01, 0x28, 0x08, 0x52, 0x07, 0x69,
0x73, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x32, 0x39, 0x0a, 0x05, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x12,
0x30, 0x0a, 0x05, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x12, 0x13, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f,
0x2e, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x10, 0x2e,
0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x22,
0x00, 0x32, 0x43, 0x0a, 0x0c, 0x47, 0x65, 0x74, 0x55, 0x73, 0x65, 0x72, 0x54, 0x6f, 0x6b, 0x65,
0x6e, 0x12, 0x33, 0x0a, 0x0c, 0x47, 0x65, 0x74, 0x55, 0x73, 0x65, 0x72, 0x54, 0x6f, 0x6b, 0x65,
0x6e, 0x12, 0x0f, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x52,
0x65, 0x71, 0x1a, 0x10, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x54, 0x6f, 0x6b, 0x65, 0x6e,
0x52, 0x65, 0x73, 0x70, 0x22, 0x00, 0x42, 0x0a, 0x5a, 0x08, 0x2e, 0x2f, 0x3b, 0x70, 0x72, 0x6f,
0x74, 0x6f, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (
@ -337,18 +455,22 @@ func file_user_proto_rawDescGZIP() []byte {
return file_user_proto_rawDescData
}
var file_user_proto_msgTypes = make([]protoimpl.MessageInfo, 3)
var file_user_proto_msgTypes = make([]protoimpl.MessageInfo, 5)
var file_user_proto_goTypes = []interface{}{
(*User)(nil), // 0: proto.User
(*LoginRequest)(nil), // 1: proto.LoginRequest
(*LoginResp)(nil), // 2: proto.LoginResp
(*TokenReq)(nil), // 3: proto.TokenReq
(*TokenResp)(nil), // 4: proto.TokenResp
}
var file_user_proto_depIdxs = []int32{
0, // 0: proto.LoginResp.user:type_name -> proto.User
1, // 1: proto.Login.Login:input_type -> proto.LoginRequest
2, // 2: proto.Login.Login:output_type -> proto.LoginResp
2, // [2:3] is the sub-list for method output_type
1, // [1:2] is the sub-list for method input_type
3, // 2: proto.GetUserToken.GetUserToken:input_type -> proto.TokenReq
2, // 3: proto.Login.Login:output_type -> proto.LoginResp
4, // 4: proto.GetUserToken.GetUserToken:output_type -> proto.TokenResp
3, // [3:5] is the sub-list for method output_type
1, // [1:3] is the sub-list for method input_type
1, // [1:1] is the sub-list for extension type_name
1, // [1:1] is the sub-list for extension extendee
0, // [0:1] is the sub-list for field type_name
@ -396,6 +518,30 @@ func file_user_proto_init() {
return nil
}
}
file_user_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*TokenReq); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_user_proto_msgTypes[4].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*TokenResp); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
}
type x struct{}
out := protoimpl.TypeBuilder{
@ -403,9 +549,9 @@ func file_user_proto_init() {
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_user_proto_rawDesc,
NumEnums: 0,
NumMessages: 3,
NumMessages: 5,
NumExtensions: 0,
NumServices: 1,
NumServices: 2,
},
GoTypes: file_user_proto_goTypes,
DependencyIndexes: file_user_proto_depIdxs,
@ -416,3 +562,159 @@ func file_user_proto_init() {
file_user_proto_goTypes = nil
file_user_proto_depIdxs = nil
}
// Reference imports to suppress errors if they are not otherwise used.
var _ context.Context
var _ grpc.ClientConnInterface
// This is a compile-time assertion to ensure that this generated file
// is compatible with the grpc package it is being compiled against.
const _ = grpc.SupportPackageIsVersion6
// LoginClient is the client API for Login service.
//
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream.
type LoginClient interface {
// rpc 服务
Login(ctx context.Context, in *LoginRequest, opts ...grpc.CallOption) (*LoginResp, error)
}
type loginClient struct {
cc grpc.ClientConnInterface
}
func NewLoginClient(cc grpc.ClientConnInterface) LoginClient {
return &loginClient{cc}
}
func (c *loginClient) Login(ctx context.Context, in *LoginRequest, opts ...grpc.CallOption) (*LoginResp, error) {
out := new(LoginResp)
err := c.cc.Invoke(ctx, "/proto.Login/Login", in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// LoginServer is the server API for Login service.
type LoginServer interface {
// rpc 服务
Login(context.Context, *LoginRequest) (*LoginResp, error)
}
// UnimplementedLoginServer can be embedded to have forward compatible implementations.
type UnimplementedLoginServer struct {
}
func (*UnimplementedLoginServer) Login(context.Context, *LoginRequest) (*LoginResp, error) {
return nil, status.Errorf(codes.Unimplemented, "method Login not implemented")
}
func RegisterLoginServer(s *grpc.Server, srv LoginServer) {
s.RegisterService(&_Login_serviceDesc, srv)
}
func _Login_Login_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(LoginRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(LoginServer).Login(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/proto.Login/Login",
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(LoginServer).Login(ctx, req.(*LoginRequest))
}
return interceptor(ctx, in, info, handler)
}
var _Login_serviceDesc = grpc.ServiceDesc{
ServiceName: "proto.Login",
HandlerType: (*LoginServer)(nil),
Methods: []grpc.MethodDesc{
{
MethodName: "Login",
Handler: _Login_Login_Handler,
},
},
Streams: []grpc.StreamDesc{},
Metadata: "user.proto",
}
// GetUserTokenClient is the client API for GetUserToken service.
//
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream.
type GetUserTokenClient interface {
// rpc 服务
GetUserToken(ctx context.Context, in *TokenReq, opts ...grpc.CallOption) (*TokenResp, error)
}
type getUserTokenClient struct {
cc grpc.ClientConnInterface
}
func NewGetUserTokenClient(cc grpc.ClientConnInterface) GetUserTokenClient {
return &getUserTokenClient{cc}
}
func (c *getUserTokenClient) GetUserToken(ctx context.Context, in *TokenReq, opts ...grpc.CallOption) (*TokenResp, error) {
out := new(TokenResp)
err := c.cc.Invoke(ctx, "/proto.GetUserToken/GetUserToken", in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// GetUserTokenServer is the server API for GetUserToken service.
type GetUserTokenServer interface {
// rpc 服务
GetUserToken(context.Context, *TokenReq) (*TokenResp, error)
}
// UnimplementedGetUserTokenServer can be embedded to have forward compatible implementations.
type UnimplementedGetUserTokenServer struct {
}
func (*UnimplementedGetUserTokenServer) GetUserToken(context.Context, *TokenReq) (*TokenResp, error) {
return nil, status.Errorf(codes.Unimplemented, "method GetUserToken not implemented")
}
func RegisterGetUserTokenServer(s *grpc.Server, srv GetUserTokenServer) {
s.RegisterService(&_GetUserToken_serviceDesc, srv)
}
func _GetUserToken_GetUserToken_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(TokenReq)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(GetUserTokenServer).GetUserToken(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/proto.GetUserToken/GetUserToken",
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(GetUserTokenServer).GetUserToken(ctx, req.(*TokenReq))
}
return interceptor(ctx, in, info, handler)
}
var _GetUserToken_serviceDesc = grpc.ServiceDesc{
ServiceName: "proto.GetUserToken",
HandlerType: (*GetUserTokenServer)(nil),
Methods: []grpc.MethodDesc{
{
MethodName: "GetUserToken",
Handler: _GetUserToken_GetUserToken_Handler,
},
},
Streams: []grpc.StreamDesc{},
Metadata: "user.proto",
}

@ -87,3 +87,64 @@ type loginHandler struct {
func (h *loginHandler) Login(ctx context.Context, in *LoginRequest, out *LoginResp) error {
return h.LoginHandler.Login(ctx, in, out)
}
// Api Endpoints for GetUserToken service
func NewGetUserTokenEndpoints() []*api.Endpoint {
return []*api.Endpoint{}
}
// Client API for GetUserToken service
type GetUserTokenService interface {
// rpc 服务
GetUserToken(ctx context.Context, in *TokenReq, opts ...client.CallOption) (*TokenResp, error)
}
type getUserTokenService struct {
c client.Client
name string
}
func NewGetUserTokenService(name string, c client.Client) GetUserTokenService {
return &getUserTokenService{
c: c,
name: name,
}
}
func (c *getUserTokenService) GetUserToken(ctx context.Context, in *TokenReq, opts ...client.CallOption) (*TokenResp, error) {
req := c.c.NewRequest(c.name, "GetUserToken.GetUserToken", in)
out := new(TokenResp)
err := c.c.Call(ctx, req, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// Server API for GetUserToken service
type GetUserTokenHandler interface {
// rpc 服务
GetUserToken(context.Context, *TokenReq, *TokenResp) error
}
func RegisterGetUserTokenHandler(s server.Server, hdlr GetUserTokenHandler, opts ...server.HandlerOption) error {
type getUserToken interface {
GetUserToken(ctx context.Context, in *TokenReq, out *TokenResp) error
}
type GetUserToken struct {
getUserToken
}
h := &getUserTokenHandler{hdlr}
return s.Handle(s.NewHandler(&GetUserToken{h}, opts...))
}
type getUserTokenHandler struct {
GetUserTokenHandler
}
func (h *getUserTokenHandler) GetUserToken(ctx context.Context, in *TokenReq, out *TokenResp) error {
return h.GetUserTokenHandler.GetUserToken(ctx, in, out)
}

@ -57,3 +57,17 @@ service Login {
rpc Login (LoginRequest) returns (LoginResp){}
}
// token
message TokenReq {
string uuid = 1;
}
// resp struct
message TokenResp{
string token = 1;
bool isLogin=2;
}
//RPC
service GetUserToken {
//rpc
rpc GetUserToken (TokenReq) returns (TokenResp){}
}
Loading…
Cancel
Save