erlang 一個高效能web架構 Cowboy 的使用筆記

來源:互聯網
上載者:User

標籤:des   blog   http   tar   ext   com   

環境:ubuntu_server 1210目的:構建web版hello world程式參考連結:http://roberto-aloi.com/blog/2013/07/13/create-deploy-erlang-cowboy-application-heroku/ 1.使用rebar 構建一個項目的基礎目錄 首先擷取rebar工具$ git clone https://github.com/rebar/rebar.git$ cd rebar$ ./bootstrap$ cd ..Initialize a new git repository and use rebar to create the skeleton for a new Erlang app. I decided to call my application erlblog. Call your application differently, replacing every occurrence of erlblog with your favourite application name in the instructions below. Please note that this is not optional, since two applications cannot have the same name on Heroku and you don’t dare to clash with my own application. 建立一個目錄erlblog將rebar編譯好並產生的可執行檔rebar複製到erlblog目錄,執行以下命令產生項目基礎目錄$ ./rebar create-app appid=erlblog產生rebar需要的設定檔,名字必須為rebar.config$ cat rebar.config設定檔內容如下:{deps, [        {cowboy, "0.8.4", {git, "https://github.com/extend/cowboy.git", {tag, "0.8.4"}}}       ]}.Add cowboy to the list of applications in your .app.src file. Also, set the http_port environment variable to 8080 (see next paragraphs). 在erlblog 目錄的src目錄下產生erlblog設定檔,erlang虛擬機器根據此檔案啟動應用程式$ cat src/erlblog.app.src檔案內容:{application, erlblog, [  {description, ""},  {vsn, "1"},  {registered, []},  {applications, [                  kernel,                  stdlib,                  cowboy                 ]},  {mod, { erlblog_app, []}},  {env, [{http_port, 8080}]} ]}.修改erlblog_app.erl檔案的start/2函數,當erlblog應用程式啟動時Cowboy才能啟動一個進程池,接收使用者串連配置Cowboy路由指派器為一個單一的路徑,所有的請求都路由到根路徑"/",並使用erlblog_handler處理使用者請求修改模組內容:$ cat src/erlblog_app.erl -module(erlblog_app). -behaviour(application). %% Application callbacks-export([start/2, stop/1]). -define(C_ACCEPTORS,  100).%% ===================================================================%% Application callbacks%% =================================================================== start(_StartType, _StartArgs) ->    Routes    = routes(),    Dispatch  = cowboy_router:compile(Routes),    Port      = port(),    TransOpts = [{port, Port}],    ProtoOpts = [{env, [{dispatch, Dispatch}]}],    {ok, _}   = cowboy:start_http(http, ?C_ACCEPTORS, TransOpts, ProtoOpts),    erlblog_sup:start_link(). stop(_State) ->    ok. %% ===================================================================%% Internal functions%% ===================================================================routes() ->    [     {‘_‘, [            {"/", erlblog_handler, []}           ]}    ]. port() ->    case os:getenv("PORT") of        false ->            {ok, Port} = application:get_env(http_port),            Port;        Other ->            list_to_integer(Other)    end. 添加請求處理模組$ cat src/erlblog_handler.erl -module(erlblog_handler). -export([init/3]).-export([handle/2]).-export([terminate/3]). init(_Transport, Req, []) ->    {ok, Req, undefined}. handle(Req, State) ->    {ok, Req2} = cowboy_req:reply(200, [], <<"Hello world!">>, Req),    {ok, Req2, State}. terminate(_Reason, _Req, _State) ->    ok.Finally, let’s create an interface module which will be responsible for starting your erlblog application together with all its dependencies. erlblog模組內容:$ cat src/erlblog.erl -module(erlblog). -export([start/0]). start() ->    ok = application:start(crypto),    ok = application:start(ranch),    ok = application:start(cowboy),    ok = application:start(erlblog). 使用rebar 獲得Cowboy程式以及依賴程式並編譯$ ./rebar get-deps compile 啟動程式$ erl -pa ebin deps/*/ebin -s erlblog1> application:which_applications(). 最後使用瀏覽器訪問測試http://伺服器IP:8080/ 返回hello, world表示能正確使用Cowboy

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.