探索 Golang 云原生游戏服务器开发,硬核实战之调试 NanoServer 生产级麻将游戏服务器

本文涉及的产品
RDS AI 助手,专业版
RDS MySQL DuckDB 分析主实例,基础系列 4核8GB
RDS MySQL DuckDB 分析主实例,集群系列 4核8GB
简介: 探索 Golang 云原生游戏服务器开发,硬核实战之调试 NanoServer 生产级麻将游戏服务器

介绍



这是一个系列


  1. 探索 Golang 云原生游戏服务器开发,5 分钟上手 Nano 游戏服务器框架 | https://juejin.im/post/6870388583019872270 
  2. 探索 Golang 云原生游戏服务器开发,根据官方示例实战Gorilla WebSocket的用法 | https://juejin.im/post/6872641375297339399
  3. 探索 Golang 云原生游戏服务器开发,Nano 内置分布式游戏服务器方案测试用例 | https://juejin.im/post/6877028133116706823
  4. 探索 Golang 云原生游戏服务器开发,Nano 分布式(集群)示例(Distributed Chat) | https://juejin.im/post/6878706308682350605


示例仓库



服务器


编写 Dockerfile.dev


FROM golang:1.14
WORKDIR /workspace
# 阿里云
RUN go env -w GO111MODULE=on
RUN go env -w GOPROXY=https://mirrors.aliyun.com/goproxy/,direct
# debug
RUN go get github.com/go-delve/delve/cmd/dlv
# live reload
RUN go get -u github.com/cosmtrek/air
# copy modules manifests
COPY go.mod go.mod
COPY go.sum go.sum
# cache modules
RUN go mod download


构建本地开发 Image


docker build -f Dockerfile.dev -t scmj-server:dev .


编写 mysql.cnf


[client]
port = 3306
socket = /var/run/mysqld/mysql.sock
default-character-set = utf8mb4
[mysql]
prompt="MySQL [\d]> "
no-auto-rehash
[mysqld]
port = 3306
socket = /var/run/mysqld/mysql.sock
basedir = /usr/local/mysql
datadir = /var/lib/mysql
pid-file = /var/run/mysqld/mysql.pid
user = mysql
bind-address = 0.0.0.0
server-id = 1
init-connect = 'SET NAMES utf8mb4'
character-set-server = utf8mb4
skip-name-resolve
#skip-networking
back_log = 300
max_connections = 497
max_connect_errors = 6000
open_files_limit = 65535
table_open_cache = 128
max_allowed_packet = 500M
binlog_cache_size = 1M
max_heap_table_size = 8M
tmp_table_size = 16M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
sort_buffer_size = 8M
join_buffer_size = 8M
key_buffer_size = 4M
thread_cache_size = 8
query_cache_type = 1
query_cache_size = 8M
query_cache_limit = 2M
ft_min_word_len = 4
log_bin = mysql-bin
binlog_format = mixed
expire_logs_days = 7
log_error = /var/lib/mysql/mysql-error.log
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /var/lib/mysql/mysql-slow.log
general_log = 1
general_log_file = /var/lib/mysql/mysql.log
performance_schema = 0
explicit_defaults_for_timestamp
#lower_case_table_names = 1
skip-external-locking
default_storage_engine = InnoDB
innodb_file_per_table = 1
innodb_open_files = 500
innodb_buffer_pool_size = 64M
innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_thread_concurrency = 0
innodb_purge_threads = 1
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 2M
innodb_log_file_size = 32M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
bulk_insert_buffer_size = 8M
myisam_sort_buffer_size = 8M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
interactive_timeout = 28800wait_timeout = 28800[mysqldump]
quick
max_allowed_packet = 500M
[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M


编写 docker-compose.mysql.yaml


version: "3.7"
services:
  db:
    image: mysql:5.7
    volumes:
      - db_data:/var/lib/mysql
      - ./mysql.cnf:/etc/mysql/my.cnf
    networks:
      - db_network
    ports:
      - "3306:3306"
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: 123456
      MYSQL_DATABASE: scmj
      MYSQL_USER: scmj
      MYSQL_PASSWORD: scmj
      TZ: Asia/Shanghai
  adminer:
    depends_on:
      - db
    image: adminer
    restart: always
    networks:
      - db_network
    ports:
      - 8086:8080
networks:
  db_network:
    driver: "bridge"
volumes:
    db_data: {}


一键启动 MySql 和 Adminer


docker-compose -f docker-compose.mysql.yaml up -d


登录 Adminer 管理界面


我们进入 http://localhost:8086,使用如下配置登录:


  • 系统:MySQL
  • 服务器:db
  • 用户名:root
  • 密码:123456
  • 数据库:scmj微信图片_20220610142044.png

微信图片_20220610142056.png

加入 launch.json


方便 VSCode 调试


{
  "version": "0.2.0",
  "configurations": [
      {
          "name": "nanoserver",
          "type": "go",
          "request": "launch",
          "mode": "remote",
          "remotePath":"/workspace/app",
          "port": 2345,
          "program": "${workspaceFolder}",
          "env": {
              "GO111MODULE":"on"
          },
          "args": [],
          "trace": "log",
          "showLog": true
      }
  ]
}


加入 .air.toml


# Config file for [Air](https://github.com/cosmtrek/air) in TOML format
# Working directory
# . or absolute path, please note that the directories following must be under root.
root = "."
tmp_dir = "tmp"
[build]
# Just plain old shell command. You could use `make` as well.
cmd = "go build -o ./tmp/main ."
# Binary file yields from `cmd`.
bin = "tmp/main"
# Customize binary.
full_bin = "APP_ENV=dev APP_USER=air ./tmp/main"
# Watch these filename extensions.
include_ext = ["go", "tpl", "tmpl", "html"]
# Ignore these filename extensions or directories.
exclude_dir = ["assets", "tmp", "vendor", "frontend/node_modules"]
# Watch these directories if you specified.
include_dir = []
# Exclude files.
exclude_file = []
# This log file places in your tmp_dir.
log = "air.log"
# It's not necessary to trigger build each time file changes if it's too frequent.
delay = 1000 # ms
# Stop running old binary when build errors occur.
stop_on_error = true
# Send Interrupt signal before killing process (windows does not support this feature)
send_interrupt = false
# Delay after sending Interrupt signal
kill_delay = 500 # ms
[log]
# Show log time
time = false
[color]
# Customize each part's color. If no color found, use the raw app log.
main = "magenta"
watcher = "cyan"
build = "yellow"
runner = "green"
[misc]
# Delete tmp directory on exit
clean_on_exit = true


编写 docker-compose.dev.yaml


version: "3.4"
services:
  scmj:
    image: scmj-server:dev
    command: >
      bash -c "cp ./go.mod ./go.sum app/
      && cd app/
      && ls -la
      && air -c .air.toml -d"
    volumes:
    - ./:/workspace/app
    networks:
      - db_network
    ports:
      - 12307:12307
      - 33251:33251
  scmj-debug:
    image: scmj-server:dev
    command: >
      bash -c "cp ./go.mod ./go.sum app/
      && cd app/
      && ls -la
      && dlv debug main.go --headless --log -l 0.0.0.0:2345 --api-version=2"
    volumes:
    - ./:/workspace/app
    networks:
      - db_network
    ports:
      - 12307:12307
      - 33251:33251
      - 2345:2345
    security_opt:
      - "seccomp:unconfined"
networks:
  db_network:
    driver: "bridge"


使用 docker-compose 调试


docker-compose -f docker-compose.dev.yaml up scmj-debug


使用 docker-compose 开发


docker-compose -f docker-compose.dev.yaml up scmj


因为 nanoserver 使用了 xorm,它会自动的根据定义的 model 生成数据库表 schema

微信图片_20220610142141.png

XORM 同步数据库

重新查看 Adminer,发现在 scmj 数据库中,xorm 已经为我们生成了表。微信图片_20220610142153.png

相关代码是:


....
func syncSchema() {
    database.StoreEngine("InnoDB").Sync2(
        new(model.Agent),
        new(model.CardConsume),
        new(model.Desk),
        new(model.History),
        new(model.Login),
        new(model.Online),
        new(model.Order),
        new(model.Recharge),
        new(model.Register),
        new(model.ThirdAccount),
        new(model.Trade),
        new(model.User),
        new(model.Uuid),
        new(model.Club),
        new(model.UserClub),
    )
}
...


客户端


这里我们直接使用 nanoserver 作者提供的 apk


安装安卓模拟器


这里我推荐网易的 MuMu模拟器。

微信图片_20220610142215.png

安装 APK


mahjong.apk,已经放到笔者修改过的项目中。这里我们使用多开助手,开4个空来血战。

image.gifimage.gif

客户端登录


我们点击微信登录。

微信图片_20220610142232.png微信图片_20220610142242.png

发现登录失败……


解决客户端登录失败问题


当然这问题,也好解决:


  1. 按作者所说那样,反编译 apk,找到 appConfig.luac,使用二进制编辑器改完服务器地址,然后重新打包。

微信图片_20220610142353.png

  1. 直接使用代理,如 Charles 进行请求地址转发。(本地调试服务器程序完全够了)


Charles 对客户端请求地址转发


使用 Map Remote 映射到你本机调试的地址就完全够了。

微信图片_20220610142411.pngimage.gif

加入 guest 测试渠道 konglaiimage.gif


重新登录进入游戏

微信图片_20220610142440.png

微信图片_20220610142455.png

完美,搞定。


测试并凑一局血战到底


创建房间

微信图片_20220610142514.png

加入房间


微信图片_20220610142528.png


开始游戏


微信图片_20220610142546.png


查看服务器日志微信图片_20220610142557.png


相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。   相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情: https://www.aliyun.com/product/rds/mysql 
相关文章
|
6月前
|
Linux Go iOS开发
Go语言100个实战案例-进阶与部署篇:使用Go打包生成可执行文件
本文详解Go语言打包与跨平台编译技巧,涵盖`go build`命令、多平台构建、二进制优化及资源嵌入(embed),助你将项目编译为无依赖的独立可执行文件,轻松实现高效分发与部署。
1054 162
|
6月前
|
存储 前端开发 JavaScript
Go语言实战案例-项目实战篇:编写一个轻量级在线聊天室
本文介绍如何用Go语言从零实现一个轻量级在线聊天室,基于WebSocket实现实时通信,支持多人消息广播。涵盖前后端开发、技术选型与功能扩展,助你掌握Go高并发与实时通信核心技术。
779 158
|
7月前
|
数据采集 数据挖掘 测试技术
Go与Python爬虫实战对比:从开发效率到性能瓶颈的深度解析
本文对比了Python与Go在爬虫开发中的特点。Python凭借Scrapy等框架在开发效率和易用性上占优,适合快速开发与中小型项目;而Go凭借高并发和高性能优势,适用于大规模、长期运行的爬虫服务。文章通过代码示例和性能测试,分析了两者在并发能力、错误处理、部署维护等方面的差异,并探讨了未来融合发展的趋势。
695 0
|
7月前
|
存储 人工智能 Go
Go-Zero全流程实战即时通讯
Go-Zero 是一个功能丰富的微服务框架,适用于开发高性能的即时通讯应用。它具备中间件、工具库和代码生成器,简化开发流程。本文介绍其环境搭建、项目初始化及即时通讯功能实现,涵盖用户认证、消息收发和实时推送,帮助开发者快速上手。
456 0
|
7月前
|
数据采集 Go API
Go语言实战案例:使用context控制协程取消
本文详解 Go 语言中 `context` 包的使用,通过实际案例演示如何利用 `context` 控制协程的生命周期,实现任务取消、超时控制及优雅退出,提升并发程序的稳定性与资源管理能力。
406 152
|
7月前
|
负载均衡 监控 Java
微服务稳定性三板斧:熔断、限流与负载均衡全面解析(附 Hystrix-Go 实战代码)
在微服务架构中,高可用与稳定性至关重要。本文详解熔断、限流与负载均衡三大关键技术,结合API网关与Hystrix-Go实战,帮助构建健壮、弹性的微服务系统。
740 1
微服务稳定性三板斧:熔断、限流与负载均衡全面解析(附 Hystrix-Go 实战代码)
|
7月前
|
安全 Go 开发者
Go语言实战案例:使用sync.Mutex实现资源加锁
在Go语言并发编程中,数据共享可能导致竞态条件,使用 `sync.Mutex` 可以有效避免这一问题。本文详细介绍了互斥锁的基本概念、加锁原理及实战应用,通过构建并发安全的计数器演示了加锁与未加锁的区别,并封装了一个线程安全的计数器结构。同时对比了Go中常见的同步机制,帮助开发者理解何时应使用 `Mutex` 及其注意事项。掌握 `Mutex` 是实现高效、安全并发编程的重要基础。
|
7月前
|
数据采集 Go API
Go语言实战案例:多协程并发下载网页内容
本文是《Go语言100个实战案例 · 网络与并发篇》第6篇,讲解如何使用 Goroutine 和 Channel 实现多协程并发抓取网页内容,提升网络请求效率。通过实战掌握高并发编程技巧,构建爬虫、内容聚合器等工具,涵盖 WaitGroup、超时控制、错误处理等核心知识点。
|
7月前
|
Go 开发者
Go语言实战案例:使用select监听多个channel
本文为《Go语言100个实战案例 · 网络与并发篇》第5篇,详解Go并发核心工具`select`的使用。通过实际案例讲解如何监听多个Channel、实现多任务处理、超时控制和非阻塞通信,帮助开发者掌握Go并发编程中的多路异步事件处理技巧。
|
7月前
|
数据采集 编解码 监控
Go语言实战案例:使用channel实现生产者消费者模型
本文是「Go语言100个实战案例 · 网络与并发篇」第4篇,通过实战案例详解使用 Channel 实现生产者-消费者模型,涵盖并发控制、任务调度及Go语言并发哲学,助你掌握优雅的并发编程技巧。

推荐镜像

更多