erlang 使用rebar創建項目實例

1、mkdir mysample
2、cd mysample
3、拷貝rebar到當前目錄
4、./rebar create-app appid=mysample
5、添加mysample_server.erl:
-module(mysample_server).


-behaviour(gen_server).


-export([start_link/0, say_hello/0]).


-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
         terminate/2, code_change/3]).


start_link() ->
    gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).


init([]) ->
    {ok, []}.


say_hello() ->
    gen_server:call(?MODULE, hello).


%% callbacks
handle_call(hello, _From, State) ->
    io:format("Hello from server!~n", []),
    {reply, ok, State};


handle_call(_Request, _From, State) ->
    Reply = ok,
    {reply, Reply, State}.


handle_cast(_Msg, State) ->
    {noreply, State}.


handle_info(_Info, State) ->
    {noreply, State}.


terminate(_Reason, _State) ->
    ok.


code_change(_OldVsn, State, _Extra) ->
    {ok, State}.
6、修改mysample.app.src:
{application, mysample,
 [
  {description, ""},
  {vsn, "1"},
  {modules, [
             mysample_app,
             mysample_sup,
             mysample_server
            ]},
  {registered, []},
  {applications, [
                  kernel,
                  stdlib
                 ]},
  {mod, { mysample_app, []}},
  {env, []}
 ]}.
修改mysample_sup.erl:
-module(mysample_sup).


-behaviour(supervisor).


-export([start_link/0]).
-export([init/1]).


-define(CHILD(I, Type), {I, {I, start_link, []}, permanent, 5000, Type, 
[I]}).


start_link() ->
    supervisor:start_link({local, ?MODULE}, ?MODULE, []).


init([]) ->
    MySampleServer = ?CHILD(mysample_server, worker),
    {ok, { {one_for_one, 5, 10}, [MySampleServer]} }.
7、./rebar compile
8、mkdir rel
9、cd rel
10、../rebar create-node nodeid=mysample    可以按需要指定
11、修改rel/reltool.config
{sys, [
       {lib_dirs, ["../../"]},
……
12、創建rebar.config
{sub_dirs, ["rel"]}.
13、./rebar generate
14、cd rel/mysample/bin/
15、./mysample console
16、mysample_server:say_hello().
發佈了30 篇原創文章 · 獲贊 3 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章