随机(16)单节点架构

1. 脚本创建数据库表:updata.bat,按照changes的php脚本更新数据库结构

2.工具生成服务端代码:tool.bat, 生成路由、映射数据库

  • 1)根据协议生成路由文件:router_lib.erl/router_encode.erl/router_decode.erl/
  • 2)根据数据库生成数据库映射文件:database.hrl包含了数据库表结构(recordpk_player表和player表)/返回码等表的常量

3.编译build.bat

@echo off
title build
cd ../ebin
erl -noshell -s make all -s init stop
pause

4.ebin/目录下应有EmakeFilegame.app以及game.beam

ebin/目录的EmakeFile,设置需要编译的所有.erl文件路径

{
	[
		'../src/core/gen_server2/gen_server2.erl',
		'../src/core/cowboy/cowboy_stream.erl',
		'../src/core/cowboy/cowboy_middleware.erl',
		'../src/core/cowboy/cowboy_sub_protocol.erl',
		'../src/core/websocket_client/websocket_client_handler.erl',
		'../src/core/*',
		'../src/core/*/*',
		'../src/core/*/*/*',
		'../src/api/*',
		'../src/api/*/*',
		'../src/api/*/*/*',
		'../src/mod/*',
		'../src/worker/*',
		'../src/worker/*/*',
		'../src/worker/*/*/*',
		'../src/gen/*',
		'../src/gen/*/*',
		'../src/gen/*/*/*',
		'../src/*'
	],
	[
		debug_info,
		{d,debug},
		{outdir,"./"},
		{i,"../include"}
	]
}.

5. start.bat启动,默认端口号8000

@echo off
title logic1
cls
erl                                                 ^
+A               10                                 ^
+P               1000000                            ^
+IOp             10                                 ^
+IOt             10                                 ^
-boot            start_sasl                         ^
-pa              ../ebin                            ^
-s               game start                         ^
-name            logic@127.0.0.1                   ^
-kernel 		 logger_sasl_compatible true        ^
-setcookie       the_cookie                         ^
-connect_all     false                              ^
-config          ../conf/logic                      ^
-env             ERL_MAX_ETS_TABLES 65535           ^
-game                                               ^
 server_port     '8000'                             ^
 mysql_host      '127.0.0.1'                        ^
 mysql_port      '3306'                             ^
 mysql_username	 'root'                             ^
 mysql_password	 'mjmjmj'                           ^
 mysql_database	 'egamedb'                       ^
 crt_file        '"../cert/debug.crt"'              ^
 key_file        '"../cert/debug.key"'              ^
 code_db_enable  'true'                             ^
 is_prof         'false'                            ^
 is_printf_req   'false'                            ^
 is_debug        'true'                             ^
pause
  • 1)erl -s game start 【erlang虚拟机ebin/目录下】 调用game:start()方法
  • 2)game:start()方法中的application:start(game) 启动一个game应用,【此时erlang虚拟机】在当前目录下搜索game.app这个而文件,不存在报错
  • 3)game.app文件中参数{mod, {game,[]}}指示,调用game:start(_Type, Arg)
-module(game).

-behaviour(supervisor).
-behaviour(application).

start () ->
	application:start(?APP_NAME).
    
stop () ->
    application:stop(?APP_NAME).

restart () ->
    stop(),
    start().

start (_Type, _Args) ->	
    Result = supervisor:start_link({local,?APP_NAME},?APP_NAME,[]),
	% cluster:init(),
	start_child({glog,{glog,start_link,[]},worker}),
	start_child({prof,{prof,start_link,[]},worker}),
	start_child({reloader,{reloader,start_link,[]},worker}),
	main:start(lib_misc:get_env_int(tid,0)),
	Result.

stop (_State) -> 
	ok.
	
prep_stop (_State) ->
	main:stop(?TID),
	ok.
	
init ([]) ->
    {ok,{{one_for_one,10,10},[]}}.
	
start_child ({Id, {M, F, A}, Type}) ->
	Spec = {Id,{M,F,A},permanent,16#FFFFFFFF,Type,[M]},
	{ok,_} = supervisor:start_child(?APP_NAME,Spec).
	
stop_child (Id) ->
	supervisor:terminate_child(?APP_NAME,Id).
	
restart_child (Id) ->
	stop_child(Id),
	supervisor:restart_child(?APP_NAME,Id).
	
delete_child (Id) ->
	stop_child(Id),
	supervisor:delete_child(?APP_NAME,Id).
==game.app==
{
	application,
	game, 
	[
		{description,"The Game Server"},
		{mod,{game,[]}},
		{env,[
			{version,2019021501},						%%-----版本
			{code_db_dir,"../ebin"},					%%-----code_db文件编译目录(默认./ebin)
			{code_db_prefix,"db_"},						%%-----db文件前缀(默认db_)
			{code_logic_prefix,"logic_"},				%%-----logic文件前缀(默认logic_)
			{code_module,mod_codedb},					%%-----codedb逻辑模块(默认mod_codedb)
			{uets_module,mod_uets},						%%-----uets逻辑模块(默认mod_uets)
			{log_dir,"../log"},							%%-----错误日志目录(默认./log)
			{data_dir,"../data"}						%%-----数据库日志目录(默认./data)
		]}
	]
}. 

6. cowboy(依赖cowlib、ranch库)

  • 1)服务端ranch创建ws工作进程(监听池)、http工作进程(监听池)
  • 2)客户端tcp创建socket连接服务端,发送数据
  • 3)ws或http监听池,监听到连接请求创建玩家进程,由玩家进程解析数据
  • 4)cowboy,调用方法,解析web格式的数据在这里插入图片描述

7. ets(增删改查、读写分离)

  • 1)开源工具将数据库所有表结构生成对应记录
  • 2)将所有数据载入ets(记录)
  • 3)读写ets,异步通知进程,对数据库持久化
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值