topfans/backend/services/socialService/client/notification_client.go
2026-06-16 21:30:58 +08:00

54 lines
1.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package client
import (
"context"
"fmt"
"dubbo.apache.org/dubbo-go/v3/client"
_ "dubbo.apache.org/dubbo-go/v3/imports"
notifPb "github.com/topfans/backend/pkg/proto/notification"
"go.uber.org/zap"
)
// NotificationClient Notification Service RPC 客户端
// 用于点赞等业务场景向 notification service 投递通知
type NotificationClient struct {
client notifPb.NotificationService
logger *zap.Logger
}
// NewNotificationClient 创建 Notification Service RPC 客户端
// serviceURL 格式tri://host:port
func NewNotificationClient(serviceURL string, logger *zap.Logger) (*NotificationClient, error) {
cli, err := client.NewClient(
client.WithClientURL(serviceURL),
)
if err != nil {
return nil, fmt.Errorf("failed to create dubbo client: %w", err)
}
notifClient, err := notifPb.NewNotificationService(cli)
if err != nil {
return nil, fmt.Errorf("failed to create notification service client: %w", err)
}
return &NotificationClient{
client: notifClient,
logger: logger,
}, nil
}
// CreateNotification 创建通知
// 调用方需要自行处理错误:失败时通常只记日志,不影响主业务路径
func (c *NotificationClient) CreateNotification(ctx context.Context, req *notifPb.CreateNotificationRequest) (*notifPb.CreateNotificationResponse, error) {
resp, err := c.client.CreateNotification(ctx, req)
if err != nil {
c.logger.Error("Failed to create notification",
zap.Error(err),
zap.Int64("user_id", req.GetUserId()),
zap.String("type", req.GetType()),
)
return nil, err
}
return resp, nil
}