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.

68 lines
2.2 KiB

2 months ago
package rpc
import (
"context"
"fmt"
"time"
"github.com/TremblingV5/DouTok/kitex_gen/message"
"github.com/TremblingV5/DouTok/kitex_gen/message/messageservice"
"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 messageClient messageservice.Client
// Comment RPC 客户端初始化
func initMessageRpc(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 := messageservice.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)
}
messageClient = c
}
// TODO 获取好友列表的最新一条消息
func GetFriendList(ctx context.Context, req *message.DouyinFriendListMessageRequest) (resp *message.DouyinFriendListMessageResponse, err error) {
resp, err = messageClient.MessageFriendList(ctx, req)
if err != nil {
return nil, err
}
if resp.StatusCode != 0 {
return nil, errno.NewErrNo(int(resp.StatusCode), resp.StatusMsg)
}
return resp, nil
}