diff --git a/discover/common/define.go b/discover/common/define.go index 1254f89..ed41b96 100644 --- a/discover/common/define.go +++ b/discover/common/define.go @@ -30,13 +30,13 @@ var ( // ServiceProvider 服务提供者 type ServiceProvider struct { Target string - SID int64 + SID string Addr string } // InstanceProvider 副本提供者 type InstanceProvider struct { - InstanceID int // 副本ID - UniqueNo int64 // 副本唯一编号 + InstanceID int // 副本ID + UniqueNo string // 副本唯一编号 SID string } diff --git a/discover/instance.go b/discover/instance.go index 6af42a3..d0f4824 100644 --- a/discover/instance.go +++ b/discover/instance.go @@ -6,15 +6,14 @@ import ( "git.hlsq.asia/mmorpg/service-common/discover/common" "git.hlsq.asia/mmorpg/service-common/log" clientv3 "go.etcd.io/etcd/client/v3" - "strconv" "sync" ) // 大量读少量写的情况下,读写锁比同步Map更高效 var ( instanceMU = sync.RWMutex{} - instanceM = make(map[int64]string) // [uniqueNo]sid - instanceLeaseM = make(map[int64]clientv3.LeaseID) // [uniqueNo] + instanceM = make(map[string]string) // [uniqueNo]sid + instanceLeaseM = make(map[string]clientv3.LeaseID) // [uniqueNo] ) func init() { @@ -23,7 +22,7 @@ func init() { } // FindInstanceByUniqueNo 根据唯一标识查询副本 -func FindInstanceByUniqueNo(uniqueNO int64) (sid string) { +func FindInstanceByUniqueNo(uniqueNO string) (sid string) { instanceMU.RLock() defer instanceMU.RUnlock() if c, ok := instanceM[uniqueNO]; ok { @@ -33,7 +32,7 @@ func FindInstanceByUniqueNo(uniqueNO int64) (sid string) { } // RegisterInstance 注册副本 -func RegisterInstance(sid int64, instanceID int32, uniqueNo, ttl int64) error { +func RegisterInstance(sid string, instanceID int32, uniqueNo string, ttl int64) error { serverMU.Lock() defer serverMU.Unlock() leaseID, err := common.NewLeaseAndKeepAlive(ttl) @@ -41,7 +40,7 @@ func RegisterInstance(sid int64, instanceID int32, uniqueNo, ttl int64) error { return err } key := fmt.Sprintf("%v/%v/%v", common.KeyDiscoverInstance, instanceID, uniqueNo) - _, err = etcd.GetClient().Put(key, strconv.Itoa(int(sid)), clientv3.WithLease(leaseID)) + _, err = etcd.GetClient().Put(key, sid, clientv3.WithLease(leaseID)) if err != nil { return err } @@ -50,7 +49,7 @@ func RegisterInstance(sid int64, instanceID int32, uniqueNo, ttl int64) error { } // UnRegisterInstance 解注册副本 -func UnRegisterInstance(uniqueNo int64) { +func UnRegisterInstance(uniqueNo string) { serverMU.Lock() defer serverMU.Unlock() if leaseID, ok := instanceLeaseM[uniqueNo]; ok { diff --git a/discover/listener.go b/discover/listener.go index c7d6e68..8016e97 100644 --- a/discover/listener.go +++ b/discover/listener.go @@ -6,7 +6,6 @@ import ( "git.hlsq.asia/mmorpg/service-common/db/etcd" "git.hlsq.asia/mmorpg/service-common/discover/common" "git.hlsq.asia/mmorpg/service-common/log" - "git.hlsq.asia/mmorpg/service-common/utils" "go.etcd.io/etcd/api/v3/mvccpb" clientv3 "go.etcd.io/etcd/client/v3" "strconv" @@ -88,13 +87,13 @@ func onServerChange(t mvccpb.Event_EventType, key, value string) { case clientv3.EventTypePut: onCBByType(common.ListenerTypeNewServer, &common.ServiceProvider{ Target: common.KeyDiscoverService + "/" + split[2], - SID: utils.StringToInt64(split[3]), + SID: split[3], Addr: value, }) case clientv3.EventTypeDelete: onCBByType(common.ListenerTypeCloseServer, &common.ServiceProvider{ Target: common.KeyDiscoverService + "/" + split[2], - SID: utils.StringToInt64(split[3]), + SID: split[3], }) } } @@ -110,13 +109,13 @@ func onInstanceChange(t mvccpb.Event_EventType, key, value string, preKv *mvccpb case clientv3.EventTypePut: onCBByType(common.ListenerTypeNewInstance, &common.InstanceProvider{ InstanceID: instanceID, - UniqueNo: utils.StringToInt64(split[3]), + UniqueNo: split[3], SID: value, }) case clientv3.EventTypeDelete: onCBByType(common.ListenerTypeCloseInstance, &common.InstanceProvider{ InstanceID: instanceID, - UniqueNo: utils.StringToInt64(split[3]), + UniqueNo: split[3], SID: string(preKv.Value), }) } diff --git a/discover/server.go b/discover/server.go index d8b1bde..462ba98 100644 --- a/discover/server.go +++ b/discover/server.go @@ -15,7 +15,7 @@ import ( var ( serverMU = sync.RWMutex{} conn = make(map[string]*grpc_conn.GrpcConnectionMgr) - serverLeaseM = make(map[int64]clientv3.LeaseID) + serverLeaseM = make(map[string]clientv3.LeaseID) ) func init() { @@ -24,7 +24,7 @@ func init() { } // FindServer 根据SID或随机查找服务 -func FindServer(target string, sid ...int64) (*grpc.ClientConn, error) { +func FindServer(target string, sid ...string) (*grpc.ClientConn, error) { serverMU.RLock() defer serverMU.RUnlock() if v, ok := conn[target]; ok { @@ -33,17 +33,17 @@ func FindServer(target string, sid ...int64) (*grpc.ClientConn, error) { return nil, fmt.Errorf("cannot find server") } -func FindServerAll(target string) map[int64]*grpc.ClientConn { +func FindServerAll(target string) map[string]*grpc.ClientConn { serverMU.RLock() defer serverMU.RUnlock() if v, ok := conn[target]; ok { return v.LoadAll() } - return make(map[int64]*grpc.ClientConn) + return make(map[string]*grpc.ClientConn) } // RegisterGrpcServer 注册服务提供者 -func RegisterGrpcServer(target string, sid int64, addr string, ttl int64) error { +func RegisterGrpcServer(target string, sid string, addr string, ttl int64) error { serverMU.Lock() defer serverMU.Unlock() leaseID, err := common.NewLeaseAndKeepAlive(ttl) @@ -59,7 +59,7 @@ func RegisterGrpcServer(target string, sid int64, addr string, ttl int64) error } // UnRegisterGrpcServer 解注册服务提供者 -func UnRegisterGrpcServer(sid int64) { +func UnRegisterGrpcServer(sid string) { serverMU.Lock() defer serverMU.Unlock() if leaseID, ok := serverLeaseM[sid]; ok { diff --git a/net/grpc/grpc_conn/conn.go b/net/grpc/grpc_conn/conn.go index 7b0da0f..471957c 100644 --- a/net/grpc/grpc_conn/conn.go +++ b/net/grpc/grpc_conn/conn.go @@ -9,11 +9,11 @@ import ( ) type GrpcConnection struct { - sid int64 + sid string conn *grpc.ClientConn } -func NewGrpcConnection(sid int64, address string) (*GrpcConnection, error) { +func NewGrpcConnection(sid string, address string) (*GrpcConnection, error) { p := &GrpcConnection{ sid: sid, } diff --git a/net/grpc/grpc_conn/conn_mgr.go b/net/grpc/grpc_conn/conn_mgr.go index 6743b8e..4127c42 100644 --- a/net/grpc/grpc_conn/conn_mgr.go +++ b/net/grpc/grpc_conn/conn_mgr.go @@ -8,18 +8,18 @@ import ( ) type GrpcConnectionMgr struct { - poolM map[int64]*GrpcConnection + poolM map[string]*GrpcConnection poolS []*GrpcConnection } func NewGrpcConnectionMgr() *GrpcConnectionMgr { return &GrpcConnectionMgr{ - poolM: make(map[int64]*GrpcConnection), + poolM: make(map[string]*GrpcConnection), poolS: make([]*GrpcConnection, 0), } } -func (p *GrpcConnectionMgr) Store(sid int64, addr string) { +func (p *GrpcConnectionMgr) Store(sid string, addr string) { pool, err := NewGrpcConnection(sid, addr) if err != nil { log.Errorf("create grpc err: %v, sid: %v, addr: %v", err, sid, addr) @@ -29,7 +29,7 @@ func (p *GrpcConnectionMgr) Store(sid int64, addr string) { p.poolS = append(p.poolS, pool) } -func (p *GrpcConnectionMgr) Delete(sid int64) int { +func (p *GrpcConnectionMgr) Delete(sid string) int { delete(p.poolM, sid) for i, pool := range p.poolS { if pool.sid == sid { @@ -40,9 +40,9 @@ func (p *GrpcConnectionMgr) Delete(sid int64) int { return len(p.poolS) } -func (p *GrpcConnectionMgr) Load(sid ...int64) (*grpc.ClientConn, error) { +func (p *GrpcConnectionMgr) Load(sid ...string) (*grpc.ClientConn, error) { var pool *GrpcConnection - if len(sid) > 0 && sid[0] > 0 { + if len(sid) > 0 && sid[0] != "" { pool = p.poolM[sid[0]] } else { pool = p.poolS[rand.Intn(len(p.poolS))] @@ -53,8 +53,8 @@ func (p *GrpcConnectionMgr) Load(sid ...int64) (*grpc.ClientConn, error) { return pool.GetConnection(), nil } -func (p *GrpcConnectionMgr) LoadAll() map[int64]*grpc.ClientConn { - sidM := make(map[int64]*grpc.ClientConn) +func (p *GrpcConnectionMgr) LoadAll() map[string]*grpc.ClientConn { + sidM := make(map[string]*grpc.ClientConn) for sid, pool := range p.poolM { sidM[sid] = pool.GetConnection() } diff --git a/net/grpc/service/client_gateway.go b/net/grpc/service/client_gateway.go index 1dea5af..8bf7a3e 100644 --- a/net/grpc/service/client_gateway.go +++ b/net/grpc/service/client_gateway.go @@ -6,7 +6,7 @@ import ( "git.hlsq.asia/mmorpg/service-common/proto/ss/grpc_pb" ) -func GatewayNewClient(sid ...int64) (grpc_pb.GatewayClient, error) { +func GatewayNewClient(sid ...string) (grpc_pb.GatewayClient, error) { c, err := discover.FindServer(common.KeyDiscoverGateway, sid...) if err != nil { return nil, err @@ -14,8 +14,8 @@ func GatewayNewClient(sid ...int64) (grpc_pb.GatewayClient, error) { return grpc_pb.NewGatewayClient(c), nil } -func GatewayNewBroadcastClient() map[int64]grpc_pb.GatewayClient { - clientM := make(map[int64]grpc_pb.GatewayClient) +func GatewayNewBroadcastClient() map[string]grpc_pb.GatewayClient { + clientM := make(map[string]grpc_pb.GatewayClient) connM := discover.FindServerAll(common.KeyDiscoverGateway) for sid, conn := range connM { clientM[sid] = grpc_pb.NewGatewayClient(conn) diff --git a/net/grpc/service/client_scene.go b/net/grpc/service/client_scene.go index 906f5d5..09ba00c 100644 --- a/net/grpc/service/client_scene.go +++ b/net/grpc/service/client_scene.go @@ -7,7 +7,7 @@ import ( "git.hlsq.asia/mmorpg/service-common/proto/ss/grpc_pb" ) -func SceneNewClient(sid ...int64) (grpc_pb.SceneClient, error) { +func SceneNewClient(sid ...string) (grpc_pb.SceneClient, error) { c, err := discover.FindServer(common.KeyDiscoverScene, sid...) if err != nil { return nil, err @@ -23,8 +23,8 @@ func SceneNewClientLB() (grpc_pb.SceneClient, error) { return grpc_pb.NewSceneClient(c), nil } -func SceneNewBroadcastClient() map[int64]grpc_pb.SceneClient { - clientM := make(map[int64]grpc_pb.SceneClient) +func SceneNewBroadcastClient() map[string]grpc_pb.SceneClient { + clientM := make(map[string]grpc_pb.SceneClient) connM := discover.FindServerAll(common.KeyDiscoverScene) for sid, conn := range connM { clientM[sid] = grpc_pb.NewSceneClient(conn) diff --git a/net/grpc/service/client_user.go b/net/grpc/service/client_user.go index ad6e65f..e71eebe 100644 --- a/net/grpc/service/client_user.go +++ b/net/grpc/service/client_user.go @@ -7,7 +7,7 @@ import ( "git.hlsq.asia/mmorpg/service-common/proto/ss/grpc_pb" ) -func UserNewClient(sid ...int64) (grpc_pb.UserClient, error) { +func UserNewClient(sid ...string) (grpc_pb.UserClient, error) { c, err := discover.FindServer(common.KeyDiscoverUser, sid...) if err != nil { return nil, err diff --git a/net/grpc/service/service.go b/net/grpc/service/service.go index c3b8dd9..d469482 100644 --- a/net/grpc/service/service.go +++ b/net/grpc/service/service.go @@ -22,7 +22,7 @@ type IService interface { type Base struct { Target string - SID int64 + SID string Serve *grpc.Server EtcdTTL int64 OnInit func(serve *grpc.Server) @@ -34,7 +34,7 @@ type Base struct { func (s *Base) Init(addr string, port int32) { s.wg = &sync.WaitGroup{} s.wg.Add(1) - s.SID = utils.SnowflakeInstance().Generate().Int64() + s.SID = utils.SnowflakeInstance().Generate().String() go func() { defer s.wg.Done() defer s.OnClose() diff --git a/utils/jwt.go b/utils/jwt.go index e358b4a..4ff01c6 100644 --- a/utils/jwt.go +++ b/utils/jwt.go @@ -5,16 +5,15 @@ import ( "errors" "github.com/golang-jwt/jwt/v5" "google.golang.org/grpc/metadata" - "strconv" "time" ) type Claims struct { - USN int64 `json:"usn"` + USN string `json:"usn"` jwt.RegisteredClaims } -func GenToken(usn int64, secret string, expires time.Duration) (string, error) { +func GenToken(usn string, secret string, expires time.Duration) (string, error) { token := jwt.NewWithClaims(jwt.SigningMethodHS256, Claims{ USN: usn, RegisteredClaims: jwt.RegisteredClaims{ @@ -41,16 +40,13 @@ func ParseToken(tokenString string, secret string) (*Claims, error) { return claims, nil } -func ShouldBindUsn(ctx context.Context, usn *int64) bool { +func ShouldBindUsn(ctx context.Context, usn *string) bool { if md, ok := metadata.FromIncomingContext(ctx); ok { usnArr := md.Get("X-Usn") if len(usnArr) == 0 || usnArr[0] == "" { return false } - s, _ := strconv.Atoi(usnArr[0]) - if s > 0 { - *usn = int64(s) - } + *usn = usnArr[0] } - return *usn > 0 + return *usn != "" }