Skip to content
oyx319 edited this page Sep 3, 2024 · 2 revisions

GGS websocket configuration

背景

ggs服务新增网页版websocket访问的功能, 需要ggs在调用worker节点时,默认使用wss访问worker节点。

通过域名+端口调用ggs服务

环境信息

环境 ggs服务 ggs节点内外网IP 域名 WebSocket端口
生产环境 ControlServer 172.30.0.14 ggs-wss.keepwork.com 9000
生产环境 WorkerServer 172.30.0.14 ggs-wss.keepwork.com 9001
生产环境 WorkerServer 172.16.0.5 ggs-wss.keepwork.com 9002
生产环境 WorkerServer 172.16.0.16 ggs-wss.keepwork.com 9003

变更步骤

一、修改Nginx配置,配置域名转发ggs服务

新增ggs.conf配置

## GGS control节点
server {
    listen 9000 ssl;
    server_name ggs-wss.keepwork.com;

    ssl_certificate /etc/nginx/ssl/keepwork_com/nginx/keepwork.com.pem;
    ssl_certificate_key /etc/nginx/ssl/keepwork_com/nginx/keepwork.com.key;
    access_log /var/log/nginx/access/ggs-wss_keepwork_com.log;
    ssl_protocols TLSv1.2 TLSv1.3;

    location / {
        proxy_pass http://172.30.0.14:9000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        
        # 添加 CORS 头部支持跨域
        add_header 'Access-Control-Allow-Origin' '*'; # 允许任何来源
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Max-Age' 1728000;
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
        
        # 处理预检请求
        if ($request_method = 'OPTIONS') {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Max-Age' 1728000;
            add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
            add_header 'Content-Type' 'text/plain charset=UTF-8';
            add_header 'Content-Length' 0;
            return 204;
        }
    }
}

## 当GGS worker节点与Controller节点不在同一台机器上时,需要配置两个nginx配置文件,域名均为ggs-wss.keepwork.com,端口不一样,可以设置为9001,、9002等等
## GGS worker节点172.30.0.14
server {
    listen 9001 ssl;
    server_name ggs-wss.keepwork.com;

    ssl_certificate /etc/nginx/ssl/keepwork_com/nginx/keepwork.com.pem;
    ssl_certificate_key /etc/nginx/ssl/keepwork_com/nginx/keepwork.com.key;
    access_log /var/log/nginx/access/ggs-wss1_keepwork_com.log;
    ssl_protocols TLSv1.2 TLSv1.3;

    location / {
        proxy_pass http://172.30.0.14:9000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        
        # 添加 CORS 头部支持跨域
        add_header 'Access-Control-Allow-Origin' '*'; # 允许任何来源
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Max-Age' 1728000;
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
        
        # 处理预检请求
        if ($request_method = 'OPTIONS') {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Max-Age' 1728000;
            add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
            add_header 'Content-Type' 'text/plain charset=UTF-8';
            add_header 'Content-Length' 0;
            return 204;
        }
    }
}

## GGS worker节点172.16.0.5
server {
    listen 9002 ssl;
    server_name ggs-wss.keepwork.com;

    ssl_certificate /etc/nginx/ssl/keepwork_com/nginx/keepwork.com.pem;
    ssl_certificate_key /etc/nginx/ssl/keepwork_com/nginx/keepwork.com.key;
    access_log /var/log/nginx/access/ggs-wss2_keepwork_com.log;
    ssl_protocols TLSv1.2 TLSv1.3;

    location / {
        proxy_pass http://172.16.0.5:9000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        
        # 添加 CORS 头部支持跨域
        add_header 'Access-Control-Allow-Origin' '*'; # 允许任何来源
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Max-Age' 1728000;
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
        
        # 处理预检请求
        if ($request_method = 'OPTIONS') {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Max-Age' 1728000;
            add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
            add_header 'Content-Type' 'text/plain charset=UTF-8';
            add_header 'Content-Length' 0;
            return 204;
        }
    }
}

## GGS worker节点172.16.0.16
server {
    listen 9003 ssl;
    server_name ggs-wss.keepwork.com;

    ssl_certificate /etc/nginx/ssl/keepwork_com/nginx/keepwork.com.pem;
    ssl_certificate_key /etc/nginx/ssl/keepwork_com/nginx/keepwork.com.key;
    access_log /var/log/nginx/access/ggs-wss3_keepwork_com.log;
    ssl_protocols TLSv1.2 TLSv1.3;

    location / {
        proxy_pass http://172.16.0.16:9000;
        proxy_http_version 1.1;

        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;

        # 添加 CORS 头部支持跨域
        add_header 'Access-Control-Allow-Origin' '*'; # 允许任何来源
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Max-Age' 1728000;
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';

        # 处理预检请求
        if ($request_method = 'OPTIONS') {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Max-Age' 1728000;
            add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
            add_header 'Content-Type' 'text/plain charset=UTF-8';
            add_header 'Content-Length' 0;
            return 204;
        }
    }
}

二、Nginx配置生效后,放开nginx节点安全组的入网规则

来源 协议端口 策略 备注
0.0.0.0/0 TCP:9000,9001,9002,9003 允许 ggs的websocket端口

三、修改ggs配置文件config.xml

在每个ggs WorkerServer节点上修改该节点websocket的访问地址和端口(各个worker节点的端口据上面nginx配置的9001~9003填入)

新增两行配置:

websocketIp="ggs-wss.keepwork.com"
websocketPort=9001
<!-- GGS 后端配置 -->
<GeneralGameServer>
  <!--服务器配置
    threadCount 工作线程数量 可设置cpu数
    threadMaxClientCount 每个线程维护客户端数量 500
    listenIp 服务器监听的IP
    listenPort 服务器监听的端口
    isControlServer 是否为控制节点
    isWorkerServer 是否为工作节点
    maxClientCount 可接受的最大客户端数 建议: threadCount * threadMaxClientCount
    maxWorldCount 可接受的最大世界数
  -->
  <Server 
    threadCount=8
    threadMaxClientCount=500
    listenIp="0.0.0.0" 
    websocketIp="ggs-wss.keepwork.com"
    websocketPort=9001
    isControlServer=true
    isWorkerServer=true
    maxClientCount=8000
    maxWorldCount=200>
    .
    .
    .
    .
    省略

四、重新发布ggs服务使配置生效

通过下面CI流水线发布

开发环境CI: https://ci.kp-para.cn/job/ggs/

生产环境CI:    https://cd-online.keepwork.com/job/ggs/

五、验证

线上环境, 打开游览器,并登录

https://webparacraft.keepwork.com/

在世界中,打开命令行输入:

/ggs connect -serverIp=ggs-wss.keepwork.com

Clone this wiki locally