Commit f415f471cc525c473107778fce764a52c59952bb
1 parent
101d1cc1
fix: roleID自增加上限制。etcd变成全局方法。
Showing
11 changed files
with
67 additions
and
28 deletions
Show diff stats
cmd/gameserver/action/RoleAction_test.go
@@ -12,7 +12,7 @@ import ( | @@ -12,7 +12,7 @@ import ( | ||
12 | func TestGetActionMap(t *testing.T) { | 12 | func TestGetActionMap(t *testing.T) { |
13 | rand.Seed(common.Timex()) | 13 | rand.Seed(common.Timex()) |
14 | 14 | ||
15 | - var redisConf = common.GlobalConf.GameConf.RedisConf | 15 | + var redisConf = common.GlobalSconf.RedisConf |
16 | if err := redisproxy.ConnectRedis(redisConf.DB, redisConf.Auth, redisConf.Address); err != nil { | 16 | if err := redisproxy.ConnectRedis(redisConf.DB, redisConf.Auth, redisConf.Address); err != nil { |
17 | logger.Error(err) | 17 | logger.Error(err) |
18 | return | 18 | return |
cmd/gameserver/main.go
@@ -19,17 +19,17 @@ func main() { | @@ -19,17 +19,17 @@ func main() { | ||
19 | userChan := make(chan os.Signal) | 19 | userChan := make(chan os.Signal) |
20 | signal.Notify(userChan, syscall.SIGUSR1, syscall.SIGUSR2) | 20 | signal.Notify(userChan, syscall.SIGUSR1, syscall.SIGUSR2) |
21 | 21 | ||
22 | - sconf := common.GlobalConf.GameConf | ||
23 | - s, err1 := service.NewGameServer(sconf) | 22 | + common.GlobalSconf = common.GlobalConf.GameConf |
23 | + s, err1 := service.NewGameServer() | ||
24 | if err1 != nil { | 24 | if err1 != nil { |
25 | logger.Error(err1) | 25 | logger.Error(err1) |
26 | return | 26 | return |
27 | } | 27 | } |
28 | go func() { | 28 | go func() { |
29 | - err <- http.ListenAndServe(fmt.Sprintf("localhost:%d", sconf.DebugPort), nil) | 29 | + err <- http.ListenAndServe(fmt.Sprintf("localhost:%d", common.GlobalSconf.DebugPort), nil) |
30 | }() | 30 | }() |
31 | 31 | ||
32 | - gm := service.NewGmServer(s, fmt.Sprintf(":%d", sconf.GMPort)) | 32 | + gm := service.NewGmServer(s, fmt.Sprintf(":%d", common.GlobalSconf.GMPort)) |
33 | go func() { | 33 | go func() { |
34 | err <- gm.Start() | 34 | err <- gm.Start() |
35 | }() | 35 | }() |
cmd/gameserver/service/game.go
@@ -18,10 +18,10 @@ import ( | @@ -18,10 +18,10 @@ import ( | ||
18 | 18 | ||
19 | type GameServer struct { | 19 | type GameServer struct { |
20 | components.IServer | 20 | components.IServer |
21 | - EtcdClient *etcd.EtcdClient | ||
22 | } | 21 | } |
23 | 22 | ||
24 | -func NewGameServer(sconf *common.SConf) (*GameServer, error) { | 23 | +func NewGameServer() (*GameServer, error) { |
24 | + sconf := common.GlobalSconf | ||
25 | s := &GameServer{} | 25 | s := &GameServer{} |
26 | 26 | ||
27 | options := []components.ServerOption{ | 27 | options := []components.ServerOption{ |
@@ -40,6 +40,7 @@ func NewGameServer(sconf *common.SConf) (*GameServer, error) { | @@ -40,6 +40,7 @@ func NewGameServer(sconf *common.SConf) (*GameServer, error) { | ||
40 | options = append(options, components.WithSplitter(components.NewPBSplitter(nil))) | 40 | options = append(options, components.WithSplitter(components.NewPBSplitter(nil))) |
41 | } | 41 | } |
42 | 42 | ||
43 | + //设置逻辑处理的handler | ||
43 | iserver := components.NewServer(sconf.Port, options...) | 44 | iserver := components.NewServer(sconf.Port, options...) |
44 | iserver.SetActions(action.GetActionMap()) | 45 | iserver.SetActions(action.GetActionMap()) |
45 | s.IServer = iserver | 46 | s.IServer = iserver |
@@ -56,11 +57,11 @@ func NewGameServer(sconf *common.SConf) (*GameServer, error) { | @@ -56,11 +57,11 @@ func NewGameServer(sconf *common.SConf) (*GameServer, error) { | ||
56 | } | 57 | } |
57 | 58 | ||
58 | //Etcd 初始化 | 59 | //Etcd 初始化 |
59 | - s.EtcdClient, err = etcd.NewEtcdClient(common.GlobalConf.Etcd) | 60 | + err = etcd.NewEtcdClient(common.GlobalConf.Etcd) |
60 | if err != nil { | 61 | if err != nil { |
61 | return nil, err | 62 | return nil, err |
62 | } | 63 | } |
63 | - 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) | 64 | + etcd.PutWithLeasePrefix(sconf.Name, fmt.Sprintf("%d", sconf.ID), fmt.Sprintf("%s:%d", sconf.IP, sconf.Port), 5) |
64 | 65 | ||
65 | //设置服务器ID & game | 66 | //设置服务器ID & game |
66 | models.NewDBSeed(sconf.ID).InitServerDatabase(models.GameModels()) | 67 | models.NewDBSeed(sconf.ID).InitServerDatabase(models.GameModels()) |
@@ -91,7 +92,7 @@ func (s *GameServer) Stop() { | @@ -91,7 +92,7 @@ func (s *GameServer) Stop() { | ||
91 | 92 | ||
92 | mongoproxy.CloseMongo() | 93 | mongoproxy.CloseMongo() |
93 | redisproxy.CloseRedis() | 94 | redisproxy.CloseRedis() |
94 | - s.EtcdClient.Close() | 95 | + etcd.CloseEtcd() |
95 | } | 96 | } |
96 | 97 | ||
97 | func (s *GameServer) OnConnection(conn components.IConnection) { | 98 | func (s *GameServer) OnConnection(conn components.IConnection) { |
cmd/httpserver/AccountAction.go
@@ -5,6 +5,7 @@ import ( | @@ -5,6 +5,7 @@ import ( | ||
5 | "github.com/gin-gonic/gin" | 5 | "github.com/gin-gonic/gin" |
6 | "pro2d/common" | 6 | "pro2d/common" |
7 | "pro2d/common/db/redisproxy" | 7 | "pro2d/common/db/redisproxy" |
8 | + "pro2d/common/etcd" | ||
8 | "pro2d/common/sms" | 9 | "pro2d/common/sms" |
9 | "pro2d/models" | 10 | "pro2d/models" |
10 | "pro2d/pb" | 11 | "pro2d/pb" |
@@ -65,7 +66,7 @@ func (h *AccountAction) Login(c *gin.Context) (int, interface{}) { | @@ -65,7 +66,7 @@ func (h *AccountAction) Login(c *gin.Context) (int, interface{}) { | ||
65 | } | 66 | } |
66 | 67 | ||
67 | var gs []*pb.ServiceInfo | 68 | var gs []*pb.ServiceInfo |
68 | - for k, v := range h.HttpServer.EtcdClient.GetByPrefix(common.GlobalConf.GameConf.Name) { | 69 | + for k, v := range etcd.GEtcdClient().GetByPrefix(common.GlobalSconf.Name) { |
69 | gs = append(gs, &pb.ServiceInfo{ | 70 | gs = append(gs, &pb.ServiceInfo{ |
70 | Id: k, | 71 | Id: k, |
71 | Name: common.GlobalConf.GameConf.Name, | 72 | Name: common.GlobalConf.GameConf.Name, |
cmd/httpserver/http.go
@@ -17,8 +17,7 @@ import ( | @@ -17,8 +17,7 @@ import ( | ||
17 | 17 | ||
18 | type AccountServer struct { | 18 | type AccountServer struct { |
19 | components.IHttp | 19 | components.IHttp |
20 | - EtcdClient *etcd.EtcdClient | ||
21 | - Sconf *common.SConf | 20 | + Sconf *common.SConf |
22 | } | 21 | } |
23 | 22 | ||
24 | func NewAccountServer(version string, port ...string) *AccountServer { | 23 | func NewAccountServer(version string, port ...string) *AccountServer { |
@@ -37,14 +36,14 @@ func (s *AccountServer) Init(sconf *common.SConf) error { | @@ -37,14 +36,14 @@ func (s *AccountServer) Init(sconf *common.SConf) error { | ||
37 | } | 36 | } |
38 | 37 | ||
39 | //Etcd 初始化 | 38 | //Etcd 初始化 |
40 | - s.EtcdClient, err = etcd.NewEtcdClient(common.GlobalConf.Etcd) | 39 | + err = etcd.NewEtcdClient(common.GlobalConf.Etcd) |
41 | if err != nil { | 40 | if err != nil { |
42 | return err | 41 | return err |
43 | } | 42 | } |
43 | + etcd.PutWithLeasePrefix(sconf.Name, fmt.Sprintf("%d", sconf.ID), fmt.Sprintf("%s:%d", sconf.IP, sconf.Port), 5) | ||
44 | + | ||
44 | models.NewDBSeed(sconf.ID).InitServerDatabase(models.AccountModels()) | 45 | models.NewDBSeed(sconf.ID).InitServerDatabase(models.AccountModels()) |
45 | models.DBSeedS().InitAutoIncreUidTable(models.AccountModels()) | 46 | models.DBSeedS().InitAutoIncreUidTable(models.AccountModels()) |
46 | - | ||
47 | - s.EtcdClient.PutWithLeasePrefix(sconf.Name, fmt.Sprintf("%d", sconf.ID), fmt.Sprintf("%s:%d", sconf.IP, sconf.Port), 5) | ||
48 | return nil | 47 | return nil |
49 | } | 48 | } |
50 | 49 | ||
@@ -65,12 +64,19 @@ func (s *AccountServer) Start() error { | @@ -65,12 +64,19 @@ func (s *AccountServer) Start() error { | ||
65 | return s.IHttp.Start() | 64 | return s.IHttp.Start() |
66 | } | 65 | } |
67 | 66 | ||
67 | +func (s *AccountServer) Stop() { | ||
68 | + s.IHttp.Stop() | ||
69 | + etcd.CloseEtcd() | ||
70 | +} | ||
71 | + | ||
68 | func main() { | 72 | func main() { |
69 | err := make(chan error) | 73 | err := make(chan error) |
70 | stopChan := make(chan os.Signal) | 74 | stopChan := make(chan os.Signal) |
71 | signal.Notify(stopChan, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL) | 75 | signal.Notify(stopChan, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL) |
72 | 76 | ||
73 | - web := NewAccountServer("v1", fmt.Sprintf(":%d", common.GlobalConf.AccountConf.Port)) | 77 | + common.GlobalSconf = common.GlobalConf.AccountConf |
78 | + | ||
79 | + web := NewAccountServer("v1", fmt.Sprintf(":%d", common.GlobalSconf.Port)) | ||
74 | web.BindHandler(&AccountAction{HttpServer: web}) | 80 | web.BindHandler(&AccountAction{HttpServer: web}) |
75 | go func() { | 81 | go func() { |
76 | err <- web.Start() | 82 | err <- web.Start() |
common/commonFunc.go
1 | package common | 1 | package common |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | + "errors" | ||
4 | "fmt" | 5 | "fmt" |
5 | "github.com/garyburd/redigo/redis" | 6 | "github.com/garyburd/redigo/redis" |
6 | "pro2d/common/db/redisproxy" | 7 | "pro2d/common/db/redisproxy" |
@@ -11,9 +12,18 @@ func GetNextRoleId() (string, error) { | @@ -11,9 +12,18 @@ func GetNextRoleId() (string, error) { | ||
11 | if err != nil { | 12 | if err != nil { |
12 | return "", err | 13 | return "", err |
13 | } | 14 | } |
15 | + ID, err := redis.Int64(relay, err) | ||
16 | + if err != nil { | ||
17 | + return "", err | ||
18 | + } | ||
19 | + | ||
20 | + //roleID的范围 [GlobalSconf.ID*MaxRoleNum, GlobalSconf.ID*MaxRoleNum + MaxRoleNum] | ||
21 | + if ID-GlobalSconf.ID*MaxRoleNum >= MaxCommNum-1 { | ||
22 | + return "", errors.New("DB_FULL") | ||
23 | + } | ||
14 | 24 | ||
15 | relay, err = redisproxy.HINCRBY(AutoIncrement, "role", 1) | 25 | relay, err = redisproxy.HINCRBY(AutoIncrement, "role", 1) |
16 | - ID, err := redis.Int64(relay, err) | 26 | + ID, err = redis.Int64(relay, err) |
17 | if err != nil { | 27 | if err != nil { |
18 | return "", err | 28 | return "", err |
19 | } | 29 | } |
common/components/http.go
@@ -74,3 +74,6 @@ func (h *HttpServer) Start() error { | @@ -74,3 +74,6 @@ func (h *HttpServer) Start() error { | ||
74 | } | 74 | } |
75 | return r.Run(h.port...) // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080") | 75 | return r.Run(h.port...) // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080") |
76 | } | 76 | } |
77 | + | ||
78 | +func (h *HttpServer) Stop() { | ||
79 | +} |
common/conf.go
@@ -94,8 +94,9 @@ type ServerConf struct { | @@ -94,8 +94,9 @@ type ServerConf struct { | ||
94 | } | 94 | } |
95 | 95 | ||
96 | var ( | 96 | var ( |
97 | - GlobalConf ServerConf | ||
98 | - SnowFlack *snow.Snowflake | 97 | + GlobalConf ServerConf |
98 | + GlobalSconf *SConf | ||
99 | + SnowFlack *snow.Snowflake | ||
99 | ) | 100 | ) |
100 | 101 | ||
101 | func init() { | 102 | func init() { |
common/const.go
common/etcd/etcd.go
@@ -14,19 +14,27 @@ type EtcdClient struct { | @@ -14,19 +14,27 @@ type EtcdClient struct { | ||
14 | lease clientv3.Lease | 14 | lease clientv3.Lease |
15 | } | 15 | } |
16 | 16 | ||
17 | -func NewEtcdClient(conf *common.Etcd) (*EtcdClient, error) { | 17 | +var GlobalEtcd *EtcdClient |
18 | + | ||
19 | +func NewEtcdClient(conf *common.Etcd) error { | ||
18 | cli, err := clientv3.New(clientv3.Config{ | 20 | cli, err := clientv3.New(clientv3.Config{ |
19 | Endpoints: conf.Endpoints, | 21 | Endpoints: conf.Endpoints, |
20 | DialTimeout: time.Duration(conf.DialTimeout) * time.Second, | 22 | DialTimeout: time.Duration(conf.DialTimeout) * time.Second, |
21 | }) | 23 | }) |
22 | if err != nil { | 24 | if err != nil { |
23 | logger.Error("etcd init err: %v", err) | 25 | logger.Error("etcd init err: %v", err) |
24 | - return nil, err | 26 | + return err |
25 | } | 27 | } |
26 | - return &EtcdClient{ | 28 | + |
29 | + GlobalEtcd = &EtcdClient{ | ||
27 | etcd: cli, | 30 | etcd: cli, |
28 | lease: clientv3.NewLease(cli), | 31 | lease: clientv3.NewLease(cli), |
29 | - }, nil | 32 | + } |
33 | + return nil | ||
34 | +} | ||
35 | + | ||
36 | +func GEtcdClient() *EtcdClient { | ||
37 | + return GlobalEtcd | ||
30 | } | 38 | } |
31 | 39 | ||
32 | func (e *EtcdClient) PutWithPrefix(prefix, key, val string) { | 40 | func (e *EtcdClient) PutWithPrefix(prefix, key, val string) { |
@@ -131,3 +139,11 @@ func (e *EtcdClient) GetByPrefix(prefix string) map[string]string { | @@ -131,3 +139,11 @@ func (e *EtcdClient) GetByPrefix(prefix string) map[string]string { | ||
131 | func (e *EtcdClient) Close() { | 139 | func (e *EtcdClient) Close() { |
132 | e.etcd.Close() | 140 | e.etcd.Close() |
133 | } | 141 | } |
142 | + | ||
143 | +func PutWithLeasePrefix(prefix, key, val string, ttl int64) error { | ||
144 | + return GEtcdClient().PutWithLeasePrefix(prefix, key, val, ttl) | ||
145 | +} | ||
146 | + | ||
147 | +func CloseEtcd() { | ||
148 | + GEtcdClient().Close() | ||
149 | +} |
common/etcd/etcd_test.go
@@ -11,17 +11,17 @@ import ( | @@ -11,17 +11,17 @@ import ( | ||
11 | ) | 11 | ) |
12 | 12 | ||
13 | func TestEtcdClient_GetByPrefix(t *testing.T) { | 13 | func TestEtcdClient_GetByPrefix(t *testing.T) { |
14 | - etcd, err := NewEtcdClient(common.GlobalConf.Etcd) | 14 | + err := NewEtcdClient(common.GlobalConf.Etcd) |
15 | if err != nil { | 15 | if err != nil { |
16 | logger.Error(err) | 16 | logger.Error(err) |
17 | return | 17 | return |
18 | } | 18 | } |
19 | - gameInfo := etcd.GetByPrefix(common.GlobalConf.AccountConf.Name) | 19 | + gameInfo := GEtcdClient().GetByPrefix(common.GlobalConf.AccountConf.Name) |
20 | for k, v := range gameInfo { | 20 | for k, v := range gameInfo { |
21 | logger.Debug("game info key: %v val: %v", k, v) | 21 | logger.Debug("game info key: %v val: %v", k, v) |
22 | } | 22 | } |
23 | 23 | ||
24 | - rch := etcd.etcd.Watch(context.Background(), fmt.Sprintf("/%s/", common.GlobalConf.AccountConf.Name), clientv3.WithPrefix()) | 24 | + rch := GEtcdClient().etcd.Watch(context.Background(), fmt.Sprintf("/%s/", common.GlobalConf.AccountConf.Name), clientv3.WithPrefix()) |
25 | go func() { | 25 | go func() { |
26 | for wresp := range rch { | 26 | for wresp := range rch { |
27 | for _, ev := range wresp.Events { | 27 | for _, ev := range wresp.Events { |
@@ -35,7 +35,7 @@ func TestEtcdClient_GetByPrefix(t *testing.T) { | @@ -35,7 +35,7 @@ func TestEtcdClient_GetByPrefix(t *testing.T) { | ||
35 | } | 35 | } |
36 | }() | 36 | }() |
37 | 37 | ||
38 | - game := etcd.etcd.Watch(context.Background(), fmt.Sprintf("/%s/", common.GlobalConf.GameConf.Name), clientv3.WithPrefix()) | 38 | + game := GEtcdClient().etcd.Watch(context.Background(), fmt.Sprintf("/%s/", common.GlobalConf.GameConf.Name), clientv3.WithPrefix()) |
39 | for wresp := range game { | 39 | for wresp := range game { |
40 | for _, ev := range wresp.Events { | 40 | for _, ev := range wresp.Events { |
41 | switch ev.Type { | 41 | switch ev.Type { |