Commit f415f471cc525c473107778fce764a52c59952bb

Authored by zhangqijia
1 parent 101d1cc1

fix: roleID自增加上限制。etcd变成全局方法。

cmd/gameserver/action/RoleAction_test.go
... ... @@ -12,7 +12,7 @@ import (
12 12 func TestGetActionMap(t *testing.T) {
13 13 rand.Seed(common.Timex())
14 14  
15   - var redisConf = common.GlobalConf.GameConf.RedisConf
  15 + var redisConf = common.GlobalSconf.RedisConf
16 16 if err := redisproxy.ConnectRedis(redisConf.DB, redisConf.Auth, redisConf.Address); err != nil {
17 17 logger.Error(err)
18 18 return
... ...
cmd/gameserver/main.go
... ... @@ -19,17 +19,17 @@ func main() {
19 19 userChan := make(chan os.Signal)
20 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 24 if err1 != nil {
25 25 logger.Error(err1)
26 26 return
27 27 }
28 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 33 go func() {
34 34 err <- gm.Start()
35 35 }()
... ...
cmd/gameserver/service/game.go
... ... @@ -18,10 +18,10 @@ import (
18 18  
19 19 type GameServer struct {
20 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 25 s := &GameServer{}
26 26  
27 27 options := []components.ServerOption{
... ... @@ -40,6 +40,7 @@ func NewGameServer(sconf *common.SConf) (*GameServer, error) {
40 40 options = append(options, components.WithSplitter(components.NewPBSplitter(nil)))
41 41 }
42 42  
  43 + //设置逻辑处理的handler
43 44 iserver := components.NewServer(sconf.Port, options...)
44 45 iserver.SetActions(action.GetActionMap())
45 46 s.IServer = iserver
... ... @@ -56,11 +57,11 @@ func NewGameServer(sconf *common.SConf) (*GameServer, error) {
56 57 }
57 58  
58 59 //Etcd 初始化
59   - s.EtcdClient, err = etcd.NewEtcdClient(common.GlobalConf.Etcd)
  60 + err = etcd.NewEtcdClient(common.GlobalConf.Etcd)
60 61 if err != nil {
61 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 66 //设置服务器ID & game
66 67 models.NewDBSeed(sconf.ID).InitServerDatabase(models.GameModels())
... ... @@ -91,7 +92,7 @@ func (s *GameServer) Stop() {
91 92  
92 93 mongoproxy.CloseMongo()
93 94 redisproxy.CloseRedis()
94   - s.EtcdClient.Close()
  95 + etcd.CloseEtcd()
95 96 }
96 97  
97 98 func (s *GameServer) OnConnection(conn components.IConnection) {
... ...
cmd/httpserver/AccountAction.go
... ... @@ -5,6 +5,7 @@ import (
5 5 "github.com/gin-gonic/gin"
6 6 "pro2d/common"
7 7 "pro2d/common/db/redisproxy"
  8 + "pro2d/common/etcd"
8 9 "pro2d/common/sms"
9 10 "pro2d/models"
10 11 "pro2d/pb"
... ... @@ -65,7 +66,7 @@ func (h *AccountAction) Login(c *gin.Context) (int, interface{}) {
65 66 }
66 67  
67 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 70 gs = append(gs, &pb.ServiceInfo{
70 71 Id: k,
71 72 Name: common.GlobalConf.GameConf.Name,
... ...
cmd/httpserver/http.go
... ... @@ -17,8 +17,7 @@ import (
17 17  
18 18 type AccountServer struct {
19 19 components.IHttp
20   - EtcdClient *etcd.EtcdClient
21   - Sconf *common.SConf
  20 + Sconf *common.SConf
22 21 }
23 22  
24 23 func NewAccountServer(version string, port ...string) *AccountServer {
... ... @@ -37,14 +36,14 @@ func (s *AccountServer) Init(sconf *common.SConf) error {
37 36 }
38 37  
39 38 //Etcd 初始化
40   - s.EtcdClient, err = etcd.NewEtcdClient(common.GlobalConf.Etcd)
  39 + err = etcd.NewEtcdClient(common.GlobalConf.Etcd)
41 40 if err != nil {
42 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 45 models.NewDBSeed(sconf.ID).InitServerDatabase(models.AccountModels())
45 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 47 return nil
49 48 }
50 49  
... ... @@ -65,12 +64,19 @@ func (s *AccountServer) Start() error {
65 64 return s.IHttp.Start()
66 65 }
67 66  
  67 +func (s *AccountServer) Stop() {
  68 + s.IHttp.Stop()
  69 + etcd.CloseEtcd()
  70 +}
  71 +
68 72 func main() {
69 73 err := make(chan error)
70 74 stopChan := make(chan os.Signal)
71 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 80 web.BindHandler(&AccountAction{HttpServer: web})
75 81 go func() {
76 82 err <- web.Start()
... ...
common/commonFunc.go
1 1 package common
2 2  
3 3 import (
  4 + "errors"
4 5 "fmt"
5 6 "github.com/garyburd/redigo/redis"
6 7 "pro2d/common/db/redisproxy"
... ... @@ -11,9 +12,18 @@ func GetNextRoleId() (string, error) {
11 12 if err != nil {
12 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 25 relay, err = redisproxy.HINCRBY(AutoIncrement, "role", 1)
16   - ID, err := redis.Int64(relay, err)
  26 + ID, err = redis.Int64(relay, err)
17 27 if err != nil {
18 28 return "", err
19 29 }
... ...
common/components/http.go
... ... @@ -74,3 +74,6 @@ func (h *HttpServer) Start() error {
74 74 }
75 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 94 }
95 95  
96 96 var (
97   - GlobalConf ServerConf
98   - SnowFlack *snow.Snowflake
  97 + GlobalConf ServerConf
  98 + GlobalSconf *SConf
  99 + SnowFlack *snow.Snowflake
99 100 )
100 101  
101 102 func init() {
... ...
common/const.go
... ... @@ -14,6 +14,7 @@ const (
14 14  
15 15 //自增id相关
16 16 MaxCommNum = 1000000
  17 + MaxRoleNum = MaxCommNum
17 18 MaxUidNum = 90000
18 19  
19 20 AutoIncrement = "pro2d_autoincrement_set"
... ...
common/etcd/etcd.go
... ... @@ -14,19 +14,27 @@ type EtcdClient struct {
14 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 20 cli, err := clientv3.New(clientv3.Config{
19 21 Endpoints: conf.Endpoints,
20 22 DialTimeout: time.Duration(conf.DialTimeout) * time.Second,
21 23 })
22 24 if err != nil {
23 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 30 etcd: cli,
28 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 40 func (e *EtcdClient) PutWithPrefix(prefix, key, val string) {
... ... @@ -131,3 +139,11 @@ func (e *EtcdClient) GetByPrefix(prefix string) map[string]string {
131 139 func (e *EtcdClient) Close() {
132 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 11 )
12 12  
13 13 func TestEtcdClient_GetByPrefix(t *testing.T) {
14   - etcd, err := NewEtcdClient(common.GlobalConf.Etcd)
  14 + err := NewEtcdClient(common.GlobalConf.Etcd)
15 15 if err != nil {
16 16 logger.Error(err)
17 17 return
18 18 }
19   - gameInfo := etcd.GetByPrefix(common.GlobalConf.AccountConf.Name)
  19 + gameInfo := GEtcdClient().GetByPrefix(common.GlobalConf.AccountConf.Name)
20 20 for k, v := range gameInfo {
21 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 25 go func() {
26 26 for wresp := range rch {
27 27 for _, ev := range wresp.Events {
... ... @@ -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 39 for wresp := range game {
40 40 for _, ev := range wresp.Events {
41 41 switch ev.Type {
... ...