feat 初始化
This commit is contained in:
24
net/grpc/grpcclient/client_admin.go
Normal file
24
net/grpc/grpcclient/client_admin.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package grpcclient
|
||||
|
||||
import (
|
||||
"git.hlsq.asia/alex/proto/proto/rs/rspb"
|
||||
"git.hlsq.asia/alex/service-common/discover"
|
||||
"git.hlsq.asia/alex/service-common/discover/common"
|
||||
"git.hlsq.asia/alex/service-common/net/grpc/resolver"
|
||||
)
|
||||
|
||||
func AdminNewClient(sid ...int64) (rspb.AdminClient, error) {
|
||||
c, err := discover.FindServer(common.KeyDiscoverAdmin, sid...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rspb.NewAdminClient(c), nil
|
||||
}
|
||||
|
||||
func AdminNewClientLB() (rspb.AdminClient, error) {
|
||||
c, err := resolver.GetGrpcClientConn("etcd:///" + common.KeyDiscoverServiceNameAdmin)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rspb.NewAdminClient(c), nil
|
||||
}
|
||||
24
net/grpc/grpcclient/client_announcement.go
Normal file
24
net/grpc/grpcclient/client_announcement.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package grpcclient
|
||||
|
||||
import (
|
||||
"git.hlsq.asia/alex/proto/proto/rs/rspb"
|
||||
"git.hlsq.asia/alex/service-common/discover"
|
||||
"git.hlsq.asia/alex/service-common/discover/common"
|
||||
"git.hlsq.asia/alex/service-common/net/grpc/resolver"
|
||||
)
|
||||
|
||||
func AnnouncementNewClient(sid ...int64) (rspb.AnnouncementClient, error) {
|
||||
c, err := discover.FindServer(common.KeyDiscoverAnnouncement, sid...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rspb.NewAnnouncementClient(c), nil
|
||||
}
|
||||
|
||||
func AnnouncementNewClientLB() (rspb.AnnouncementClient, error) {
|
||||
c, err := resolver.GetGrpcClientConn("etcd:///" + common.KeyDiscoverServiceNameAnnouncement)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rspb.NewAnnouncementClient(c), nil
|
||||
}
|
||||
24
net/grpc/grpcclient/client_court-physical.go
Normal file
24
net/grpc/grpcclient/client_court-physical.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package grpcclient
|
||||
|
||||
import (
|
||||
"git.hlsq.asia/alex/proto/proto/rs/rspb"
|
||||
"git.hlsq.asia/alex/service-common/discover"
|
||||
"git.hlsq.asia/alex/service-common/discover/common"
|
||||
"git.hlsq.asia/alex/service-common/net/grpc/resolver"
|
||||
)
|
||||
|
||||
func CourtPhysicalNewClient(sid ...int64) (rspb.CourtPhysicalClient, error) {
|
||||
c, err := discover.FindServer(common.KeyDiscoverCourtPhysical, sid...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rspb.NewCourtPhysicalClient(c), nil
|
||||
}
|
||||
|
||||
func CourtPhysicalNewClientLB() (rspb.CourtPhysicalClient, error) {
|
||||
c, err := resolver.GetGrpcClientConn("etcd:///" + common.KeyDiscoverServiceNameCourtPhysical)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rspb.NewCourtPhysicalClient(c), nil
|
||||
}
|
||||
24
net/grpc/grpcclient/client_court.go
Normal file
24
net/grpc/grpcclient/client_court.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package grpcclient
|
||||
|
||||
import (
|
||||
"git.hlsq.asia/alex/proto/proto/rs/rspb"
|
||||
"git.hlsq.asia/alex/service-common/discover"
|
||||
"git.hlsq.asia/alex/service-common/discover/common"
|
||||
"git.hlsq.asia/alex/service-common/net/grpc/resolver"
|
||||
)
|
||||
|
||||
func CourtNewClient(sid ...int64) (rspb.CourtClient, error) {
|
||||
c, err := discover.FindServer(common.KeyDiscoverCourt, sid...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rspb.NewCourtClient(c), nil
|
||||
}
|
||||
|
||||
func CourtNewClientLB() (rspb.CourtClient, error) {
|
||||
c, err := resolver.GetGrpcClientConn("etcd:///" + common.KeyDiscoverServiceNameCourt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rspb.NewCourtClient(c), nil
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
package grpcclient
|
||||
|
||||
import (
|
||||
"git.hlsq.asia/alex/proto/proto/rs/rspb"
|
||||
"git.hlsq.asia/alex/service-common/discover"
|
||||
"git.hlsq.asia/alex/service-common/discover/common"
|
||||
"git.hlsq.asia/alex/service-common/proto/rs/rspb"
|
||||
)
|
||||
|
||||
func GatewayNewClient(sid ...int64) (rspb.GatewayClient, error) {
|
||||
|
||||
24
net/grpc/grpcclient/client_post.go
Normal file
24
net/grpc/grpcclient/client_post.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package grpcclient
|
||||
|
||||
import (
|
||||
"git.hlsq.asia/alex/proto/proto/rs/rspb"
|
||||
"git.hlsq.asia/alex/service-common/discover"
|
||||
"git.hlsq.asia/alex/service-common/discover/common"
|
||||
"git.hlsq.asia/alex/service-common/net/grpc/resolver"
|
||||
)
|
||||
|
||||
func PostNewClient(sid ...int64) (rspb.PostClient, error) {
|
||||
c, err := discover.FindServer(common.KeyDiscoverPost, sid...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rspb.NewPostClient(c), nil
|
||||
}
|
||||
|
||||
func PostNewClientLB() (rspb.PostClient, error) {
|
||||
c, err := resolver.GetGrpcClientConn("etcd:///" + common.KeyDiscoverServiceNamePost)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rspb.NewPostClient(c), nil
|
||||
}
|
||||
@@ -1,12 +1,14 @@
|
||||
package grpcclient
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.hlsq.asia/alex/proto/proto/rs/rspb"
|
||||
"git.hlsq.asia/alex/service-common/discover"
|
||||
"git.hlsq.asia/alex/service-common/discover/common"
|
||||
"git.hlsq.asia/alex/service-common/net/grpc/resolver"
|
||||
"git.hlsq.asia/alex/service-common/proto/rs/rspb"
|
||||
)
|
||||
|
||||
// SceneNewClient 根据SID查找场景服,一般不用这个函数
|
||||
func SceneNewClient(sid ...int64) (rspb.SceneClient, error) {
|
||||
c, err := discover.FindServer(common.KeyDiscoverScene, sid...)
|
||||
if err != nil {
|
||||
@@ -15,6 +17,15 @@ func SceneNewClient(sid ...int64) (rspb.SceneClient, error) {
|
||||
return rspb.NewSceneClient(c), nil
|
||||
}
|
||||
|
||||
// SceneInstanceNewClient 根据副本唯一编号查找场景服
|
||||
func SceneInstanceNewClient(uniqueNO int64) (rspb.SceneClient, error) {
|
||||
sid := discover.FindInstanceByUniqueNo(uniqueNO)
|
||||
if sid == 0 {
|
||||
return nil, fmt.Errorf("instance not found: %v", uniqueNO)
|
||||
}
|
||||
return SceneNewClient(sid)
|
||||
}
|
||||
|
||||
func SceneNewClientLB() (rspb.SceneClient, error) {
|
||||
c, err := resolver.GetGrpcClientConn("etcd:///" + common.KeyDiscoverServiceNameScene)
|
||||
if err != nil {
|
||||
|
||||
24
net/grpc/grpcclient/client_third-party.go
Normal file
24
net/grpc/grpcclient/client_third-party.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package grpcclient
|
||||
|
||||
import (
|
||||
"git.hlsq.asia/alex/proto/proto/rs/rspb"
|
||||
"git.hlsq.asia/alex/service-common/discover"
|
||||
"git.hlsq.asia/alex/service-common/discover/common"
|
||||
"git.hlsq.asia/alex/service-common/net/grpc/resolver"
|
||||
)
|
||||
|
||||
func ThirdPartyNewClient(sid ...int64) (rspb.ThirdPartyClient, error) {
|
||||
c, err := discover.FindServer(common.KeyDiscoverThirdParty, sid...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rspb.NewThirdPartyClient(c), nil
|
||||
}
|
||||
|
||||
func ThirdPartyNewClientLB() (rspb.ThirdPartyClient, error) {
|
||||
c, err := resolver.GetGrpcClientConn("etcd:///" + common.KeyDiscoverServiceNameThirdParty)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rspb.NewThirdPartyClient(c), nil
|
||||
}
|
||||
@@ -1,10 +1,10 @@
|
||||
package grpcclient
|
||||
|
||||
import (
|
||||
"git.hlsq.asia/alex/proto/proto/rs/rspb"
|
||||
"git.hlsq.asia/alex/service-common/discover"
|
||||
"git.hlsq.asia/alex/service-common/discover/common"
|
||||
"git.hlsq.asia/alex/service-common/net/grpc/resolver"
|
||||
"git.hlsq.asia/alex/service-common/proto/rs/rspb"
|
||||
)
|
||||
|
||||
func UserNewClient(sid ...int64) (rspb.UserClient, error) {
|
||||
|
||||
@@ -3,7 +3,11 @@ package grpcclient
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"git.hlsq.asia/alex/proto/proto/rs/rspb"
|
||||
"git.hlsq.asia/alex/service-common/discover"
|
||||
"git.hlsq.asia/alex/service-common/discover/common"
|
||||
"git.hlsq.asia/alex/service-common/log"
|
||||
"git.hlsq.asia/alex/service-common/utils/locator"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/protobuf/proto"
|
||||
"sync"
|
||||
@@ -13,6 +17,8 @@ type GatewayFun int
|
||||
|
||||
const (
|
||||
FunToClient GatewayFun = iota
|
||||
FunBroadcastClient
|
||||
FunBroadcastChannel
|
||||
)
|
||||
|
||||
var gatewayServer sync.Map // map[string]*gatewayStream
|
||||
@@ -37,7 +43,11 @@ func findGatewayBySID(sid int64, fun GatewayFun) (*gatewayStream, error) {
|
||||
var stream grpc.ClientStream
|
||||
switch fun {
|
||||
case FunToClient:
|
||||
stream, err = client.ToClient(context.Background())
|
||||
stream, err = client.InnerToClient(context.Background())
|
||||
case FunBroadcastClient:
|
||||
stream, err = client.InnerBroadcastClient(context.Background())
|
||||
case FunBroadcastChannel:
|
||||
stream, err = client.InnerBroadcastChannel(context.Background())
|
||||
}
|
||||
if err != nil {
|
||||
log.Errorf("findGatewayBySID %v err: %v, sid: %v", fun, err, sid)
|
||||
@@ -53,7 +63,7 @@ func findGatewayBySID(sid int64, fun GatewayFun) (*gatewayStream, error) {
|
||||
return ss, nil
|
||||
}
|
||||
|
||||
func SendMessageToGateway(sid int64, fun GatewayFun, msg proto.Message, re ...bool) error {
|
||||
func sendMessageToGateway(sid int64, fun GatewayFun, msg proto.Message, re ...bool) error {
|
||||
ss, err := findGatewayBySID(sid, fun)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -71,13 +81,46 @@ func SendMessageToGateway(sid int64, fun GatewayFun, msg proto.Message, re ...bo
|
||||
}
|
||||
// 如果没有标识本次是重试的,就重试一次(默认重试)
|
||||
if re == nil || !re[0] {
|
||||
return SendMessageToGateway(sid, fun, msg, true)
|
||||
return sendMessageToGateway(sid, fun, msg, true)
|
||||
}
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SendMessageToClient 单播消息给客户端
|
||||
func SendMessageToClient(ctx context.Context, usn int64, msg proto.Message, platform ...rspb.PlatformType) error {
|
||||
if platform == nil || len(platform) == 0 {
|
||||
return nil
|
||||
}
|
||||
for _, pf := range platform {
|
||||
if gatewaySID := locator.NewLocator(ctx, usn, int32(pf)).LocateGateway(); gatewaySID > 0 {
|
||||
return sendMessageToGateway(gatewaySID, FunToClient, msg)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// BroadcastMessageToClient 广播消息给客户端
|
||||
func BroadcastMessageToClient(msg *rspb.InnerBroadcastClientReq) error {
|
||||
return broadcastMessageToGateway(FunBroadcastClient, msg)
|
||||
}
|
||||
|
||||
// BroadcastMessageToChannel 广播消息到频道(加入频道的能收到,不区分平台)
|
||||
func BroadcastMessageToChannel(msg *rspb.InnerBroadcastChannelReq) error {
|
||||
return broadcastMessageToGateway(FunBroadcastChannel, msg)
|
||||
}
|
||||
|
||||
func broadcastMessageToGateway(fun GatewayFun, msg proto.Message) error {
|
||||
sidArr := discover.FindServerAllSid(common.KeyDiscoverGateway)
|
||||
for _, sid := range sidArr {
|
||||
if err := sendMessageToGateway(sid, fun, msg); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func gatewayKey(sid int64, fun GatewayFun) string {
|
||||
return fmt.Sprintf("%v-%v", sid, fun)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user