You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
2.1 KiB

2 months ago
package rpc
import (
"context"
"fmt"
"time"
"github.com/TremblingV5/DouTok/kitex_gen/user"
"github.com/TremblingV5/DouTok/kitex_gen/user/userservice"
"github.com/TremblingV5/DouTok/pkg/dtviper"
"github.com/TremblingV5/DouTok/pkg/errno"
"github.com/TremblingV5/DouTok/pkg/middleware"
"github.com/cloudwego/kitex/client"
"github.com/cloudwego/kitex/pkg/retry"
"github.com/cloudwego/kitex/pkg/rpcinfo"
"github.com/kitex-contrib/obs-opentelemetry/tracing"
etcd "github.com/kitex-contrib/registry-etcd"
)
var userClient userservice.Client
// Comment RPC 客户端初始化
func initUserRpc(Config *dtviper.Config) {
EtcdAddress := fmt.Sprintf("%s:%d", Config.Viper.GetString("Etcd.Address"), Config.Viper.GetInt("Etcd.Port"))
r, err := etcd.NewEtcdResolver([]string{EtcdAddress})
if err != nil {
panic(err)
}
ServiceName := Config.Viper.GetString("Server.Name")
//p := provider.NewOpenTelemetryProvider(
// provider.WithServiceName(ServiceName),
// provider.WithExportEndpoint("localhost:4317"),
// provider.WithInsecure(),
//)
//defer p.Shutdown(context.Background())
c, err := userservice.NewClient(
ServiceName,
client.WithMiddleware(middleware.CommonMiddleware),
client.WithInstanceMW(middleware.ClientMiddleware),
client.WithMuxConnection(1), // mux
client.WithRPCTimeout(30*time.Second), // rpc timeout
client.WithConnectTimeout(30000*time.Millisecond), // conn timeout
client.WithFailureRetry(retry.NewFailurePolicy()), // retry
client.WithSuite(tracing.NewClientSuite()), // tracer
client.WithResolver(r), // resolver
// Please keep the same as provider.WithServiceName
client.WithClientBasicInfo(&rpcinfo.EndpointBasicInfo{ServiceName: ServiceName}),
)
if err != nil {
panic(err)
}
userClient = c
}
func GetUserListByIds(ctx context.Context, req *user.DouyinUserListRequest) (resp *user.DouyinUserListResponse, err error) {
resp, err = userClient.GetUserListByIds(ctx, req)
if err != nil {
return nil, err
}
if resp.StatusCode != 0 {
return nil, errno.NewErrNo(int(resp.StatusCode), resp.StatusMsg)
}
return resp, nil
}