阅读(2357) (2)

xingo 集群服务器入门教程

2017-01-05 16:29:46 更新

xingo集群服务器推荐目录结构:

.
├── admin_server                           管理服务,可以实现集群的GM管理操作,支持http方式访问
│   ├── test_admin_http.go              对外管理的http接口
│   └── test_admin_rpc.go               对内提供给集群其他节点访问的rpc接口
├── conf                                         集群服务器配置
│   ├── clusterconf.json                    分布式架构定义
│   └── server.json                           服务器配置
├── game_server                             游戏服务器逻辑
├── gate_server                               gate服务器逻辑
│   ├── gateserver.go                        这里可以绑定节点链接和断开处理函数
│   └── test_gate_rpc.go                   对内rpc接口
├── log                                            集群服务器日志文件/支持按服务器/日志大小/时间切割日志
│   ├── cluster.log
│   ├── gate1.log
│   ├── gate2.log
│   └── net.log
├── master.go                                  管理服务
├── net_server                                 对外的网关服务器负责于客户端通信
│   ├── core
│   │   ├── player.go
│   │   └── playermgr.go
│   ├── netserver.go
│   ├── test_net_api.go
│   └── test_net_rpc.go
├── pb
│   └── msg.pb.go
├── README.md
└── server.go                                    xingo server



master.go

package main

import (
       "path/filepath"
       "github.com/viphxin/xingo/sys_rpc"
       "github.com/viphxin/xingo/clusterserver"
)

func main() {
       dir, err := filepath.Abs(filepath.Dir("."))
       if err == nil{
              s := clusterserver.NewMaster(filepath.Join(dir, "conf", "clusterconf.json"))//关联集群配置
              s.AddRpcRouter(&sys_rpc.MasterRpc{})//添加rpc接口
              s.StartMaster()//开启服务
       }
}
server.go

package main

import (
       "github.com/viphxin/xingo/clusterserver"
       "github.com/viphxin/xingo/sys_rpc"
       "os"
       "path/filepath"
       "xingo_cluster/net_server"
       "xingo_cluster/gate_server"
       "xingo_cluster/admin_server"
        _ "net/http"
       _ "net/http/pprof"
)

func main() {
       //pprof
       //go func() {
       //     println(http.ListenAndServe("localhost:6060", nil))
       //}()

       //server code
       args := os.Args
       dir, err := filepath.Abs(filepath.Dir("."))
       if err == nil{
              s := clusterserver.NewClusterServer(args[1], filepath.Join(dir, "conf", "clusterconf.json"))
              s.AddRpcRouter(&sys_rpc.ChildRpc{})
              s.AddRpcRouter(&sys_rpc.RootRpc{})
              /*
              注册分布式服务器
              */
              //net server
              s.AddModule("net", &net_server.TestNetApi{}, &net_server.TestNetRpc{})
              //gate server
              s.AddModule("gate", nil, &gate_server.TestGateRpc{})
              //admin server
              s.AddModule("admin", &admin_server.TestAdminHttp{}, nil)

              s.StartClusterServer()
       }
}

启动:

go run master.go
go run server.go gate1
go run server.go gate2
go run server.go net1
go run server.go net2
go run server.go net3
go run server.go net4
go run server.go admin