1、rebar安装(最好把rebar路径写到~/bashrc文件)
2、创建项目
1、mkdir myapp
2、cd myapp
3、rebar create-app appid=myapp
4、编译rebar compile
3、发布版本
1、mkdir apps
2、mkdir apps/myapp
3、将第二步得到的src文件cp到apps/myapp/src
4、mkdir rel
5、cd rel
6、rebar create-node nodeid=myapp (创建节点)
7、创建rebar.config文件,内容如下:
{sub_dirs, ["apps/myapp","rel"]}.
{cover_enabled, true}.
{deps_dir,["deps"]}. // 引入source code
{deps,[
{lager,".*",{git,"git://github.com/basho/lager.git",{branch,"master"}}}
]}.
8、修改reltool.config文件
{sys, [
{lib_dirs, ["../apps", "../deps"]}, //lib所在的目录,source code保存在deps中,见上一步
{rel, "myapp", "1", // “1” 表示版本号
[
kernel,
stdlib,
sasl,
myapp
]},
{rel, "start_clean", "",
[
kernel,
stdlib
]},
{boot_rel, "myapp"},
{profile, embedded},
{excl_archive_filters, [".*"]}, %% Do not archive built libs
{excl_sys_filters, ["^bin/.*",
"^erts.*/bin/(dialyzer|typer)"]},
%% Including HiPE can cause issues generating your first upgrade.
%% If you plan to distribute HiPE in your release remove the
%% following line.
{app, lager, [{incl_cond, include}]}, // 引入的source code 必须要在此进行声明,不然不能识别
{app, myapp, [{incl_cond, include}]}
]}.
{target_dir, "myapp"}.
{overlay, [
{mkdir, "log/sasl"}, // 程序启动时候出现的错误,会记录在此
{copy, "files/erl", "\{\{erts_vsn\}\}/bin/erl"},
{copy, "files/nodetool", "\{\{erts_vsn\}\}/bin/nodetool"},
{copy, "files/myapp", "bin/myapp"},
{copy, "files/sys.config", "releases/\{\{rel_vsn\}\}/sys.config"},
{copy, "files/myapp.cmd", "bin/myapp.cmd"},
{copy, "files/start_erl.cmd", "bin/start_erl.cmd"},
{copy, "files/vm.args", "releases/\{\{rel_vsn\}\}/vm.args"}
]}.
4、编译并生成release
其实这个时候,来个Makefile更好
.PHONY: deps
.PHONY: rel
all:deps compile
deps:
./rebar get-deps //这个命令会根据rebar.config中的配置,获得指定的source code并保存在指定的目录下,如deps
clean:
./rebar clean
compile: deps clean
./rebar compile // 没错,这个是编译的
rel:compile
./rebar generate //生成release
5、启动
经过编译和生成release之后,可执行文件会在./rel/myapp/bin/myapp ,启动时,应该指定启动的模式。
6、热更
BTW:热更之前备份。
1、修改版本号,有两个文件,一个是myapp.app.src 另一个是reltool.config
2、编译,rebar compile
3、rebar generate
4、rebar generate-appups previous_release=myapp_1 指定之前的版本是1
5、rebar generate-upgrade previous_release=myapp_1
6、mv rel/myapp_2.tar.gz rel/myapp_1/releases/
7、
([email protected])> release_handler:unpack_release("myapp_2").
{ok,"2"}
([email protected])> release_handler:install_release("2").
{ok,"1",[]}
([email protected])> release_handler:make_permanent("2").
ok
主要参见:http://blog.sina.com.cn/s/blog_6530ad590100wmkn.html
1、引入source code
rebar提供了source 引入功能。
1、一般用本身用rebar组织的source 项目,需要注意三个地方,第一,指定source code保存的目录,一般是deps;第二,指定source code 的地址,一般都是git 或者是hg;最后一点,在reltool.config文件要进行声明。例子可见3.8 。
2、本身并不是用rebar组织的,如 rabbitmq erlang client 这种形式的,需要把rabbitmq erlang client的lib 和ebin放在 apps目录下(也就是和myapp是同一级目录),并在使用到该lib的代码中指定hrl所在的位置。-include_lib("apps/myapp ……").
2、sys.config文件 (系统配置文件)
这个文件中,定义了如sasl snmp 等的启动参数
[
%% SASL config
{sasl, [
{sasl_error_logger, {file, "log/sasl-error.log"}},
{errlog_type, error},
{error_logger_mf_dir, "log/sasl"}, % Log directory
{error_logger_mf_maxbytes, 10485760}, % 10 MB max file size
{error_logger_mf_maxfiles, 5} % 5 files max
]},
{snmp,
[{manager,
[{config, [{dir, "../manager/conf/"},
{db_dir, "../manager/db/"}]}]}]}
].
3、vm.args (虚拟机启动参数)
这个文件,定义了Erlang虚拟机启动的参数
## Name of the node
-name [email protected]
## Cookie for distributed erlang
-setcookie myapp
## Heartbeat management; auto-restarts VM if it dies or becomes unresponsive
## (Disabled by default..use with caution!)
##-heart
## Enable kernel poll and a few async threads
##+K true
##+A 5
## Increase number of concurrent ports/sockets
##-env ERL_MAX_PORTS 4096
## Tweak GC to run more often
##-env ERL_FULLSWEEP_AFTER 10