-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
53 lines (47 loc) · 1.65 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
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
}
// GetUser 此接口用于从 user_id 获取一个 user 的信息
func GetUser(ctx context.Context, req *user.UserInfoRequest) (*user.User, error) {
resp, err := userClient.UserInfo(ctx, req)
if err != nil {
return nil, err
}
if resp.StatusCode != 0 {
return nil, errno.NewErrNo(int64(resp.StatusCode), *resp.StatusMsg)
}
return resp.User, nil
}