继续使用之前的game_server做例子(加入了简易登陆的原型),具体参考:http://blog.youkuaiyun.com/huang1196/article/details/38401197
下载见最后。
参考:https://github.com/rebar/rebar/wiki/Release-handling
1. 因为我们的项目已经是按照otp设计原则建立,所以可以省去rebar create-app 这步。
2.
mark@localhost:~/eclipse-erlang/workspace/game_demo$ mkdir rel
mark@localhost:~/eclipse-erlang/workspace/game_demo$ cd rel
mark@localhost:~/eclipse-erlang/workspace/game_demo/rel$ ../rebar create-node nodeid=game_server
==> rel (create-node)
Writing reltool.config
Writing files/erl
Writing files/nodetool
Writing files/game
Writing files/app.config
Writing files/vm.args
3. 编辑reltool.config
注意因为使用到了deps里面的ranch,所以使用
{lib_dirs, ["../deps"]}
{sys, [ {lib_dirs, ["../deps"]}, {rel, "game_server", "1", [ kernel, stdlib, sasl, ranch, game_server ]}, {rel, "start_clean", "", [ kernel, stdlib ]}, {boot_rel, "game_server"}, {profile, embedded}, {excl_sys_filters, ["^bin/.*", "^erts.*/bin/(dialyzer|typer)"]}, {app, sasl, [{incl_cond, include}]}, {app, ranch, [{incl_cond, include}]}, {app, game_server, [{mod_cond, app}, {incl_cond, include}, {lib_dir, ".."}]} ]}. {target_dir, "game_server"}. {overlay, [ {mkdir, "log/sasl"}, {copy, "files/erl", "{{erts_vsn}}/bin/erl"}, {copy, "files/nodetool", "{{erts_vsn}}/bin/nodetool"}, {copy, "files/game_server", "bin/game_server"}, {copy, "files/app.config", "etc/app.config"}, {copy, "files/vm.args", "etc/vm.args"} ]}.
4. 修改game_demo目录下的rebar.config
添加
{sub_dirs, ["rel"]}.
5.
mark@localhost:~/eclipse-erlang/workspace/game_demo$ ./rebar compile generate
==> ranch (compile)
==> rel (compile)
==> game_demo (compile)
==> rel (generate)
6. 去运行吧!
mark@localhost:~/eclipse-erlang/workspace/game_demo$ cd rel/game_server/bin
mark@localhost:~/eclipse-erlang/workspace/game_demo/rel/game_server/bin$ ./game_server start
mark@localhost:~/eclipse-erlang/workspace/game_demo/rel/game_server/bin$ ./game_server attach
Attaching to /tmp//home/mark/eclipse-erlang/workspace/game_demo/rel/game_server/erlang.pipe.1 (^D to exit)
(game_server@127.0.0.1)1> application:which_applications().
[{game_server,"Ranch TCP reverse example.","1"},
{ranch,"Socket acceptor pool for TCP protocols.","1.0.0"},
{sasl,"SASL CXC 138 11","2.3.2"},
{stdlib,"ERTS CXC 138 10","1.19.2"},
{kernel,"ERTS CXC 138 10","2.16.2"}]
(game_server@127.0.0.1)2> client:send(<<>>).
#Port<0.754>
Data:<<1,0,1,6,77,97,114,107,95,104,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,112,97,115,115,119,111,114,100>>
{<<1,0,1,6,77,97,114,107,95,104,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,112,97,115,115,119,111,114,100>>,
{tcpstat,#Port<0.755>,ranch_tcp,<<>>,<<>>}}
{1,85080926805864,8097880544751088228,<<>>}
{1,<<"Mark_h">>,
<<0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,112,97,115,115,119,111,114,
100>>}
Client received = <<"Mark_h">>
ok
相对于使用demo中的start.sh启动,这种方法的一个好处就是可以很方便地使用./game_server attach调出console。
demo及其release已经打包,点击下载