现在的位置: 首页 > 综合 > 正文

[Erlang]使用Erlang application有什么好处?

2015年03月13日 ⁄ 综合 ⁄ 共 2171字 ⁄ 字号 评论关闭
文章目录


问题:

当我们把一个项目中所有的supervision tree通过一个简单的函数game: start(),会发现这个树结构特别复杂,只能有一个根节点,然后一直扩展。

QQ五笔截图未命名

 

那么有时,我们会需要:有些功能模块不启动,有些启动,如果再去改这颗树的结构,就会很麻烦。这里,这就是application出现的原因,设计一个可以随时开关的子块(application).比如:上图中的log app, db app ,game app, connect app ..

这样对这些应用的开关管理就非常方便啦,【试想你如果用supervisor,运行时还要手动去停进程树,然后还要移除监控树,还要做clean工作,下次启动还要做start工作…】,这些定义好application后,自然会把这个当成一个单元处理啦!这大概就是编程思想的体现吧。

如何构造一个典型的erlang application? 下面我们通过把[Erl_Question07] Erlang 做图形化编程的尝试:纯Erlang做2048游戏 的游戏改为application启动来做示范

原来的通过erl Script 启动是可以的,变成application有什么好处呢?

that can be started and stopped as a unit, and which can be re-used in other systems as well. 使用application方便随时只启动或关闭以application为单位的应用,其它application完全不受影响,这可以方便的管理一个大项目的各个功能,把这些功能都分成一个个小应用,又有层次又方便管理。

步骤:

1. 定义 .app文件,格式如下

复制代码
%% game2048.app
{application, game2048, [
    {description, "pure erlang game 2048 for fun"}, %%description应用说明,默认为""
    {vsn, "1"},    %% 版本号
    {modules, []},%%All modules introduced by this application,systools使用这个list来生成boot script and tar  file tar,这个module必须只能被一个且只能是一个application定义过
    {registerd,[]},%%All names of registered processes in the application. systools uses this list to detect name clashes between applications. Defaults to [].
    {applications, [
        kernel,
        stdlib
    ]},%%All applications which must be started before this application is started. systools uses this list to generate correct boot scripts. Defaults to [], but note that all applications have dependencies to at least kernel and stdlib.
    {mod, {game2048_app, []}},%% call game2048_app:start(normal, []) ,game2048_app:stop([])
    {env, []}
]}.
复制代码

2.给游戏加入监控进程:game2048_sup.erl

复制代码
init([]) ->
    RestartStrategy = one_for_one,
    MaxRestarts = 1000,
    MaxSecondsBetweenRestarts = 3600,

    SupFlags = {RestartStrategy, MaxRestarts, MaxSecondsBetweenRestarts},

    Restart = permanent,
    Shutdown = 2000,
    Type = worker,

    AChild = {'game2048', {'game2048', start, []}, %%监控的是game2048:start()
        Restart, Shutdown, Type, []},

    {ok, {SupFlags, [AChild]}}.
复制代码

3. 因原来的game2048: start()返回值改为{ok,PID}模式,这是supervisor规范要求的。

4. 重新编译代码,改造application工作完成。


你可以通过以下方式启动 application client.

1. 启动一个erlang shell :

erl -name test -pa "app所在目录" -pa "ebin目录"
    >application:start(game2048).
 
>application: stop(game2048).

2.当然你可以把application和其它的application共同使用,【不久我会把lager application也用来这里面来,大材小用学习下优秀代码也好:)】

变成application,好开心,居然看到和kernel并在一起,是不是高级点【使用observer:start().查看:

image

Tip: 你使用 erl 启动一个Shell时是不会启动 net_kernel模块的,导致分布式出错,如果加上 –name 指定节点名就会启动啦。

抱歉!评论已关闭.