-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
92 lines (83 loc) · 2.87 KB
/
user.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright 2021 CloudWeGo Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package rpc
import (
"context"
"github.com/Yra-A/Douyin_Simple_Demo/pkg/middleware"
"github.com/kitex-contrib/obs-opentelemetry/tracing"
"time"
"github.com/Yra-A/Douyin_Simple_Demo/kitex_gen/user"
"github.com/Yra-A/Douyin_Simple_Demo/kitex_gen/user/userservice"
"github.com/Yra-A/Douyin_Simple_Demo/pkg/constants"
"github.com/Yra-A/Douyin_Simple_Demo/pkg/errno"
"github.com/cloudwego/kitex/client"
"github.com/cloudwego/kitex/pkg/retry"
etcd "github.com/kitex-contrib/registry-etcd"
)
var userClient userservice.Client
func initUserRpc() {
r, err := etcd.NewEtcdResolver([]string{constants.EtcdAddress}) // 服务发现
if err != nil {
panic(err)
}
c, err := userservice.NewClient(
constants.UserServiceName,
client.WithMiddleware(middleware.CommonMiddleware),
client.WithInstanceMW(middleware.ClientMiddleware),
client.WithMuxConnection(1), // mux
client.WithRPCTimeout(3*time.Second), // rpc timeout
client.WithConnectTimeout(50*time.Millisecond), // conn timeout
client.WithFailureRetry(retry.NewFailurePolicy()), // retry
client.WithSuite(tracing.NewClientSuite()), // tracer
client.WithResolver(r), // resolver
)
if err != nil {
panic(err)
}
userClient = c
}
// UserRegister 用户注册【rpc 客户端】
func UserRegister(ctx context.Context, req *user.UserRegisterRequest) (*user.UserRegisterResponse, error) {
resp, err := userClient.UserRegister(ctx, req)
if err != nil {
return resp, err
}
if resp.StatusCode != 0 {
return resp, errno.NewErrNo(int64(resp.StatusCode), *resp.StatusMsg)
}
return resp, nil
}
// UserLogin 用户登录【rpc 客户端】
func UserLogin(ctx context.Context, req *user.UserLoginRequest) (*user.UserLoginResponse, error) {
resp, err := userClient.UserLogin(ctx, req)
if err != nil {
return resp, err
}
if resp.StatusCode != 0 {
return resp, errno.NewErrNo(int64(resp.StatusCode), *resp.StatusMsg)
}
return resp, nil
}
// UserInfo 用户信息【rpc 客户端】
func UserInfo(ctx context.Context, req *user.UserInfoRequest) (*user.UserInfoResponse, error) {
resp, err := userClient.UserInfo(ctx, req)
if err != nil {
return resp, err
}
if resp.StatusCode != 0 {
return resp, errno.NewErrNo(int64(resp.StatusCode), *resp.StatusMsg)
}
return resp, nil
}