跳到主要内容

Zebra 游戏服务器 Systemd 服务使用指南

一、服务介绍

本项目包含以下 10 个服务,按启动顺序排列:

服务名描述工作目录依赖关系
SuperServer主控服务器/home/s1/ztgame/MySQL
LoginServer登录服务器/home/s1/Server/MySQL
UserServer用户服务器/home/s1/Server/MySQL
MiniServer小游戏服务器/home/s1/ztgame/-
SessionServer会话服务器/home/s1/ztgame/MiniServer
ScenesServer场景服务器/home/s1/ztgame/SessionServer
GatewayServer网关服务器/home/s1/ztgame/ScenesServer
FLServer账号验证服务器/home/s1/Server/-
BillServer计费服务器/home/s1/ztgame/-
RecordServer记录服务器/home/s1/ztgame/-

二、快速部署(一区)

2.1 复制服务文件

# 将服务文件复制到 systemd 目录
sudo cp systemd/*.service /etc/systemd/system/

# 重新加载 systemd 配置
sudo systemctl daemon-reload

2.2 启动服务

# 按顺序启动服务(必须按依赖顺序)
sudo systemctl start SuperServer
sudo systemctl start LoginServer
sudo systemctl start UserServer
sudo systemctl start MiniServer
sudo systemctl start SessionServer
sudo systemctl start ScenesServer
sudo systemctl start GatewayServer
sudo systemctl start FLServer
sudo systemctl start BillServer
sudo systemctl start RecordServer

2.3 设置开机自启

# 启用开机自启(按顺序)
sudo systemctl enable SuperServer
sudo systemctl enable LoginServer
sudo systemctl enable UserServer
sudo systemctl enable MiniServer
sudo systemctl enable SessionServer
sudo systemctl enable ScenesServer
sudo systemctl enable GatewayServer
sudo systemctl enable FLServer
sudo systemctl enable BillServer
sudo systemctl enable RecordServer

三、常用管理命令

# 查看服务状态
sudo systemctl status SuperServer

# 查看所有服务状态
sudo systemctl list-units --type=service | grep -E "(Super|Login|User|Mini|Session|Scenes|Gateway|FL|Bill|Record)Server"

# 重启服务
sudo systemctl restart SuperServer

# 停止服务(按反向顺序)
sudo systemctl stop RecordServer
sudo systemctl stop BillServer
sudo systemctl stop FLServer
sudo systemctl stop GatewayServer
sudo systemctl stop ScenesServer
sudo systemctl stop SessionServer
sudo systemctl stop MiniServer
sudo systemctl stop UserServer
sudo systemctl stop LoginServer
sudo systemctl stop SuperServer

# 查看服务日志
sudo journalctl -u SuperServer -f
sudo journalctl -u SuperServer --since "1 hour ago"

# 一键重启所有服务脚本

#!/bin/bash
for svc in RecordServer BillServer FLServer GatewayServer ScenesServer SessionServer MiniServer UserServer LoginServer SuperServer; do
sudo systemctl restart $svc
done

四、部署二区 / 三区 示范

4.1 二区部署示例

假设二区安装目录为 /home/s2/ztgame//home/s2/Server/

4.1.1 创建二区服务文件

# 复制并重命名服务文件
sudo cp /etc/systemd/system/SuperServer.service /etc/systemd/system/SuperServer2.service
sudo cp /etc/systemd/system/ScenesServer.service /etc/systemd/system/ScenesServer2.service
sudo cp /etc/systemd/system/GatewayServer.service /etc/systemd/system/GatewayServer2.service
# ... 其他服务同理

4.1.2 修改 SuperServer2.service

[Unit]
Description=SuperServer2 Service (二区)
After=mysql.service

[Service]
Type=simple
Restart=always
User=root
WorkingDirectory=/home/s2/ztgame/
ExecStart=/home/s2/ztgame/SuperServer
RestartSec=1
StandardOutput=journal
StandardError=journal

[Install]
WantedBy=multi-user.target

关键修改点:

  • Description → 添加 "2" 或 "二区"
  • WorkingDirectory/home/s2/ztgame/
  • ExecStart/home/s2/ztgame/SuperServer

4.1.3 修改 LoginServer2.service

[Unit]
Description=LoginServer2 Service (二区)
After=mysql.service

[Service]
Type=simple
Restart=always
User=root
WorkingDirectory=/home/s2/Server/
ExecStart=/home/s2/Server/LoginServer
RestartSec=1
StandardOutput=journal
StandardError=journal

[Install]
WantedBy=multi-user.target

关键修改点:

  • WorkingDirectory/home/s2/Server/
  • ExecStart/home/s2/Server/LoginServer

4.1.4 修改 ScenesServer2.service

[Unit]
Description=ScenesServer2 Service (二区)
After=SessionServer2.service

[Service]
Type=simple
Restart=always
User=root
WorkingDirectory=/home/s2/ztgame/
ExecStart=/home/s2/ztgame/ScenesServer
RestartSec=1
StandardOutput=journal
StandardError=journal

[Install]
WantedBy=multi-user.target

注意: After= 依赖也要改为对应的二区服务名

4.1.5 部署二区服务

# 重新加载并启动二区服务
sudo systemctl daemon-reload
sudo systemctl enable SuperServer2 ScenesServer2 GatewayServer2 LoginServer2 UserServer2 MiniServer2 SessionServer2 FLServer2 BillServer2 RecordServer2
sudo systemctl start SuperServer2 ScenesServer2 GatewayServer2 LoginServer2 UserServer2 MiniServer2 SessionServer2 FLServer2 BillServer2 RecordServer2

4.2 三区部署示例

假设三区安装目录为 /home/s3/ztgame//home/s3/Server/

4.2.1 修改三区服务文件

GatewayServer3.service 为例:

[Unit]
Description=GatewayServer3 Service (三区)
After=ScenesServer3.service

[Service]
Type=simple
Restart=always
User=root
WorkingDirectory=/home/s3/ztgame/
ExecStart=/home/s3/ztgame/GatewayServer
RestartSec=1
StandardOutput=journal
StandardError=journal

[Install]
WantedBy=multi-user.target

关键修改点:

  • Description → "GatewayServer3"
  • After=ScenesServer3.service
  • WorkingDirectory/home/s3/ztgame/
  • ExecStart/home/s3/ztgame/GatewayServer

4.2.2 批量修改脚本

#!/bin/bash
# 创建三区服务文件

ZONE_NUM=3
ZONE_PATH="/home/s3"

for svc in SuperServer ScenesServer GatewayServer SessionServer MiniServer FLServer BillServer RecordServer; do
# 创建 ztgame 目录的服务
if [ "$svc" == "SuperServer" ] || [ "$svc" == "ScenesServer" ] || [ "$svc" == "GatewayServer" ] || [ "$svc" == "SessionServer" ] || [ "$svc" == "MiniServer" ] || [ "$svc" == "FLServer" ] || [ "$svc" == "BillServer" ] || [ "$svc" == "RecordServer" ]; then
sed -e "s/Description=${svc} service/Description=${svc}${ZONE_NUM} Service (三区)/" \
-e "s|/home/s1/ztgame/|/home/s${ZONE_NUM}/ztgame/|g" \
-e "s/${svc} /${svc}${ZONE_NUM} /g" \
-e "s/${svc}$/${svc}${ZONE_NUM}/g" \
/etc/systemd/system/${svc}.service > /etc/systemd/system/${svc}${ZONE_NUM}.service
fi
done

# 创建 Server 目录的服务
for svc in LoginServer UserServer; do
sed -e "s/Description=${svc} service/Description=${svc}${ZONE_NUM} Service (三区)/" \
-e "s|/home/s1/Server/|/home/s${ZONE_NUM}/Server/|g" \
-e "s/${svc} /${svc}${ZONE_NUM} /g" \
-e "s/${svc}$/${svc}${ZONE_NUM}/g" \
/etc/systemd/system/${svc}.service > /etc/systemd/system/${svc}${ZONE_NUM}.service
done

# 重新加载
sudo systemctl daemon-reload

echo "三区服务文件已创建,请修改配置文件中的端口号后启动"

五、注意事项

5.1 端口冲突

部署多区时,需要修改各服务的配置文件,避免端口冲突:

服务默认端口需要修改的配置
SuperServer9000config.xml
LoginServer9001login_server.xml
UserServer9002config.xml
GatewayServer9003config.xml
ScenesServer9004config.xml

5.2 数据库配置

确保各区的 config.xml 中数据库连接指向正确的实例或库名:

<!-- 一区 -->
<db name="game">
<host>localhost</host>
<database>zebra_s1</database>
</db>

<!-- 二区 -->
<db name="game">
<host>localhost</host>
<database>zebra_s2</database>
</db>

5.3 防火墙设置

# 开放游戏端口
sudo firewall-cmd --permanent --add-port=9000-9100/tcp

# 重载防火墙
sudo firewall-cmd --reload

5.4 服务启动顺序图

MySQL (数据库)

SuperServer (主控)

┌──────────────────────────────────┐
│ LoginServer(登陆器注册商城旧版本) + UserServer (登录) │
└──────────────────────────────────┘

MiniServer (小游戏)

SessionServer (会话)

ScenesServer (场景)

GatewayServer (网关)

┌──────────────────────────────────┐
│ FLServer(账号验证) + BillServer(计费) + RecordServer(档案) │
└──────────────────────────────────┘