我学xingo golang服务器之-xingo的单进程,集群服务器的配置和初始化过程
上一节
学习了xingo_demo的启动过程,顺便学习了,启动tcp,rpc和cluster集群服务器的过程,
这三个服务器的启动方式类似,但是配置信息不同,这地方还是要注意,所以这一节我提取出来介绍这三个不同类型服务器的配置使用方式;
xingo_demo 单进程服务器-初始化过程:
先来看个./server启动后的log信息
再看看xingo.go代码:
这里log顺序只是个演示不必太在意!总之从xingo.go开始
代码调用utils.GlobalObject,这变量什么时候初始化的?
现在我们看看xingo/utils/globalobj.go:
golang规定包内init()在main()之前运行
具体说明 @L_502_0@
从而可以确定,xingo的配置初始化就在xingo/utils/globalobj.go init()函数进行初始化,写好server.json 就可以启动服务器了:
配置参考:
XINGO单进程服务器入门教程:
http://www.runingman.net/index.php/2017/06/12/xingo_dan_jin_cheng_fu_wu_qi_ru_men_jiao_cheng/
{
"WriteList": ["10.211.55.4"],//telnet ip list 用于单机模式
"DebugPort": 8881,//telnet port 用于单机模式
"TcpPort": 8999,//监听端口
"StepPerMs": 2000,//demo逻辑配置,非通用配置项
"PoolSize": 5,//工作队列数
"IsUsePool": true,//是否使用工作队列
"LogLevel": 1,//日志级别 0 ALL 1 DEBUG 2 INFO 3 WARN 4 ERROR 5 FATAL 6 OFF
"MaxConn": 2000 //支持最大连接数
}
了解了配置和配置启动过程,我做了个流程图(参考):
通过分析可以知道,单进程tcp服务器,只需配置好server.json即可;
现在我们分解一下集群服务器的配置和初始化过程:
参考例子xingo_cluster:
https://github.com/viphxin/xingo_cluster
集群服务器用到两个配置:clusterconf.json,server.json
xingo_cluster/conf/clusterconf.json
{
"master":{"host": "127.0.0.1","rootport":9999,"debugport":8881,"writelist": ["127.0.0.1"]},"servers":{ "gate1":{"host": "127.0.0.1","rootport":10000,"name":"gate1","module": "gate","log": "gate.log"},"gate2":{"host": "127.0.0.1","rootport":10001,"name":"gate2","net1":{"host": "127.0.0.1","netport":11009,"debugport":11007,"name":"net1","remotes":["gate1","gate2"],"module": "net","log": "net.log"},"net2":{"host": "127.0.0.1","netport":11010,"name":"net2","net3":{"host": "127.0.0.1","netport":11011,"name":"net3","net4":{"host": "127.0.0.1","netport":11012,"name":"net4","admin1":{"host": "127.0.0.1","remotes":["gate2","gate1"],"name":"admin1","module": "admin","http": [8888,"/static"]},"game1":{"host": "127.0.0.1","name":"game1","module": "game"} } }
xingo_cluster/conf/server.json:
{
"TcpPort": 8999,"StepPerMs": 2000,"PoolSize": 1,"LogLevel": 2,"MaxConn": 20000,"LogPath": "/Users/huangxin/workspace/src/xingo_cluster/log","LogName": "cluster.log" }
可以看到xingo_cluster/conf/server.json配置很基础,主要是tcp配置;
现在给个我做得参考流程图:
从流程上可以看到集群服务器要启动多个配置来配置每个小server,最后连接到master服务器,构成了服务器集群;
集群服务器相对比较复杂了;
下一节开始,做单进程服务器的小游戏;
学习交流,非官方文档,感谢xingo大神的倾力指导!
官方交流QQ群:535378240