diff --git a/cmd/gameserver/action/RoleAction_test.go b/cmd/gameserver/action/RoleAction_test.go index 8716bb8..c460b29 100644 --- a/cmd/gameserver/action/RoleAction_test.go +++ b/cmd/gameserver/action/RoleAction_test.go @@ -12,7 +12,7 @@ import ( func TestGetActionMap(t *testing.T) { rand.Seed(common.Timex()) - var redisConf = common.GlobalConf.GameConf.RedisConf + var redisConf = common.GlobalSconf.RedisConf if err := redisproxy.ConnectRedis(redisConf.DB, redisConf.Auth, redisConf.Address); err != nil { logger.Error(err) return diff --git a/cmd/gameserver/main.go b/cmd/gameserver/main.go index f43331b..9d682a7 100644 --- a/cmd/gameserver/main.go +++ b/cmd/gameserver/main.go @@ -19,17 +19,17 @@ func main() { userChan := make(chan os.Signal) signal.Notify(userChan, syscall.SIGUSR1, syscall.SIGUSR2) - sconf := common.GlobalConf.GameConf - s, err1 := service.NewGameServer(sconf) + common.GlobalSconf = common.GlobalConf.GameConf + s, err1 := service.NewGameServer() if err1 != nil { logger.Error(err1) return } go func() { - err <- http.ListenAndServe(fmt.Sprintf("localhost:%d", sconf.DebugPort), nil) + err <- http.ListenAndServe(fmt.Sprintf("localhost:%d", common.GlobalSconf.DebugPort), nil) }() - gm := service.NewGmServer(s, fmt.Sprintf(":%d", sconf.GMPort)) + gm := service.NewGmServer(s, fmt.Sprintf(":%d", common.GlobalSconf.GMPort)) go func() { err <- gm.Start() }() diff --git a/cmd/gameserver/service/game.go b/cmd/gameserver/service/game.go index 6fa80e2..25766fe 100644 --- a/cmd/gameserver/service/game.go +++ b/cmd/gameserver/service/game.go @@ -18,10 +18,10 @@ import ( type GameServer struct { components.IServer - EtcdClient *etcd.EtcdClient } -func NewGameServer(sconf *common.SConf) (*GameServer, error) { +func NewGameServer() (*GameServer, error) { + sconf := common.GlobalSconf s := &GameServer{} options := []components.ServerOption{ @@ -40,6 +40,7 @@ func NewGameServer(sconf *common.SConf) (*GameServer, error) { options = append(options, components.WithSplitter(components.NewPBSplitter(nil))) } + //设置逻辑处理的handler iserver := components.NewServer(sconf.Port, options...) iserver.SetActions(action.GetActionMap()) s.IServer = iserver @@ -56,11 +57,11 @@ func NewGameServer(sconf *common.SConf) (*GameServer, error) { } //Etcd 初始化 - s.EtcdClient, err = etcd.NewEtcdClient(common.GlobalConf.Etcd) + err = etcd.NewEtcdClient(common.GlobalConf.Etcd) if err != nil { return nil, err } - s.EtcdClient.PutWithLeasePrefix(common.GlobalConf.GameConf.Name, fmt.Sprintf("%d", common.GlobalConf.GameConf.ID), fmt.Sprintf("%s:%d", common.GlobalConf.GameConf.IP, common.GlobalConf.GameConf.Port), 5) + etcd.PutWithLeasePrefix(sconf.Name, fmt.Sprintf("%d", sconf.ID), fmt.Sprintf("%s:%d", sconf.IP, sconf.Port), 5) //设置服务器ID & game models.NewDBSeed(sconf.ID).InitServerDatabase(models.GameModels()) @@ -91,7 +92,7 @@ func (s *GameServer) Stop() { mongoproxy.CloseMongo() redisproxy.CloseRedis() - s.EtcdClient.Close() + etcd.CloseEtcd() } func (s *GameServer) OnConnection(conn components.IConnection) { diff --git a/cmd/httpserver/AccountAction.go b/cmd/httpserver/AccountAction.go index 106c8ef..7671dcb 100644 --- a/cmd/httpserver/AccountAction.go +++ b/cmd/httpserver/AccountAction.go @@ -5,6 +5,7 @@ import ( "github.com/gin-gonic/gin" "pro2d/common" "pro2d/common/db/redisproxy" + "pro2d/common/etcd" "pro2d/common/sms" "pro2d/models" "pro2d/pb" @@ -65,7 +66,7 @@ func (h *AccountAction) Login(c *gin.Context) (int, interface{}) { } var gs []*pb.ServiceInfo - for k, v := range h.HttpServer.EtcdClient.GetByPrefix(common.GlobalConf.GameConf.Name) { + for k, v := range etcd.GEtcdClient().GetByPrefix(common.GlobalSconf.Name) { gs = append(gs, &pb.ServiceInfo{ Id: k, Name: common.GlobalConf.GameConf.Name, diff --git a/cmd/httpserver/http.go b/cmd/httpserver/http.go index 872f5c0..1ccec00 100644 --- a/cmd/httpserver/http.go +++ b/cmd/httpserver/http.go @@ -17,8 +17,7 @@ import ( type AccountServer struct { components.IHttp - EtcdClient *etcd.EtcdClient - Sconf *common.SConf + Sconf *common.SConf } func NewAccountServer(version string, port ...string) *AccountServer { @@ -37,14 +36,14 @@ func (s *AccountServer) Init(sconf *common.SConf) error { } //Etcd 初始化 - s.EtcdClient, err = etcd.NewEtcdClient(common.GlobalConf.Etcd) + err = etcd.NewEtcdClient(common.GlobalConf.Etcd) if err != nil { return err } + etcd.PutWithLeasePrefix(sconf.Name, fmt.Sprintf("%d", sconf.ID), fmt.Sprintf("%s:%d", sconf.IP, sconf.Port), 5) + models.NewDBSeed(sconf.ID).InitServerDatabase(models.AccountModels()) models.DBSeedS().InitAutoIncreUidTable(models.AccountModels()) - - s.EtcdClient.PutWithLeasePrefix(sconf.Name, fmt.Sprintf("%d", sconf.ID), fmt.Sprintf("%s:%d", sconf.IP, sconf.Port), 5) return nil } @@ -65,12 +64,19 @@ func (s *AccountServer) Start() error { return s.IHttp.Start() } +func (s *AccountServer) Stop() { + s.IHttp.Stop() + etcd.CloseEtcd() +} + func main() { err := make(chan error) stopChan := make(chan os.Signal) signal.Notify(stopChan, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL) - web := NewAccountServer("v1", fmt.Sprintf(":%d", common.GlobalConf.AccountConf.Port)) + common.GlobalSconf = common.GlobalConf.AccountConf + + web := NewAccountServer("v1", fmt.Sprintf(":%d", common.GlobalSconf.Port)) web.BindHandler(&AccountAction{HttpServer: web}) go func() { err <- web.Start() diff --git a/common/commonFunc.go b/common/commonFunc.go index f62220c..3026044 100644 --- a/common/commonFunc.go +++ b/common/commonFunc.go @@ -1,6 +1,7 @@ package common import ( + "errors" "fmt" "github.com/garyburd/redigo/redis" "pro2d/common/db/redisproxy" @@ -11,9 +12,18 @@ func GetNextRoleId() (string, error) { if err != nil { return "", err } + ID, err := redis.Int64(relay, err) + if err != nil { + return "", err + } + + //roleID的范围 [GlobalSconf.ID*MaxRoleNum, GlobalSconf.ID*MaxRoleNum + MaxRoleNum] + if ID-GlobalSconf.ID*MaxRoleNum >= MaxCommNum-1 { + return "", errors.New("DB_FULL") + } relay, err = redisproxy.HINCRBY(AutoIncrement, "role", 1) - ID, err := redis.Int64(relay, err) + ID, err = redis.Int64(relay, err) if err != nil { return "", err } diff --git a/common/components/http.go b/common/components/http.go index 28df49e..1e10981 100644 --- a/common/components/http.go +++ b/common/components/http.go @@ -74,3 +74,6 @@ func (h *HttpServer) Start() error { } return r.Run(h.port...) // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080") } + +func (h *HttpServer) Stop() { +} diff --git a/common/conf.go b/common/conf.go index c490848..e674b81 100644 --- a/common/conf.go +++ b/common/conf.go @@ -94,8 +94,9 @@ type ServerConf struct { } var ( - GlobalConf ServerConf - SnowFlack *snow.Snowflake + GlobalConf ServerConf + GlobalSconf *SConf + SnowFlack *snow.Snowflake ) func init() { diff --git a/common/const.go b/common/const.go index c40f118..ea4b5ba 100644 --- a/common/const.go +++ b/common/const.go @@ -14,6 +14,7 @@ const ( //自增id相关 MaxCommNum = 1000000 + MaxRoleNum = MaxCommNum MaxUidNum = 90000 AutoIncrement = "pro2d_autoincrement_set" diff --git a/common/etcd/etcd.go b/common/etcd/etcd.go index 4b1639a..7d08982 100644 --- a/common/etcd/etcd.go +++ b/common/etcd/etcd.go @@ -14,19 +14,27 @@ type EtcdClient struct { lease clientv3.Lease } -func NewEtcdClient(conf *common.Etcd) (*EtcdClient, error) { +var GlobalEtcd *EtcdClient + +func NewEtcdClient(conf *common.Etcd) error { cli, err := clientv3.New(clientv3.Config{ Endpoints: conf.Endpoints, DialTimeout: time.Duration(conf.DialTimeout) * time.Second, }) if err != nil { logger.Error("etcd init err: %v", err) - return nil, err + return err } - return &EtcdClient{ + + GlobalEtcd = &EtcdClient{ etcd: cli, lease: clientv3.NewLease(cli), - }, nil + } + return nil +} + +func GEtcdClient() *EtcdClient { + return GlobalEtcd } func (e *EtcdClient) PutWithPrefix(prefix, key, val string) { @@ -131,3 +139,11 @@ func (e *EtcdClient) GetByPrefix(prefix string) map[string]string { func (e *EtcdClient) Close() { e.etcd.Close() } + +func PutWithLeasePrefix(prefix, key, val string, ttl int64) error { + return GEtcdClient().PutWithLeasePrefix(prefix, key, val, ttl) +} + +func CloseEtcd() { + GEtcdClient().Close() +} diff --git a/common/etcd/etcd_test.go b/common/etcd/etcd_test.go index 9abfa10..2a1a627 100644 --- a/common/etcd/etcd_test.go +++ b/common/etcd/etcd_test.go @@ -11,17 +11,17 @@ import ( ) func TestEtcdClient_GetByPrefix(t *testing.T) { - etcd, err := NewEtcdClient(common.GlobalConf.Etcd) + err := NewEtcdClient(common.GlobalConf.Etcd) if err != nil { logger.Error(err) return } - gameInfo := etcd.GetByPrefix(common.GlobalConf.AccountConf.Name) + gameInfo := GEtcdClient().GetByPrefix(common.GlobalConf.AccountConf.Name) for k, v := range gameInfo { logger.Debug("game info key: %v val: %v", k, v) } - rch := etcd.etcd.Watch(context.Background(), fmt.Sprintf("/%s/", common.GlobalConf.AccountConf.Name), clientv3.WithPrefix()) + rch := GEtcdClient().etcd.Watch(context.Background(), fmt.Sprintf("/%s/", common.GlobalConf.AccountConf.Name), clientv3.WithPrefix()) go func() { for wresp := range rch { for _, ev := range wresp.Events { @@ -35,7 +35,7 @@ func TestEtcdClient_GetByPrefix(t *testing.T) { } }() - game := etcd.etcd.Watch(context.Background(), fmt.Sprintf("/%s/", common.GlobalConf.GameConf.Name), clientv3.WithPrefix()) + game := GEtcdClient().etcd.Watch(context.Background(), fmt.Sprintf("/%s/", common.GlobalConf.GameConf.Name), clientv3.WithPrefix()) for wresp := range game { for _, ev := range wresp.Events { switch ev.Type { -- libgit2 0.21.2