环境:ubuntu_server 1210
目的:构建web版hello world程序
 
1.使用rebar 构建一个项目的基础目录
 
首先获取rebar工具
$ 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 erlblog
1> application:which_applications().
 
最后使用浏览器访问测试
http://服务器IP:8080/ 返回hello, world表示能正确使用Cowboy

erlang 一个高性能web框架 Cowboy 的使用笔记的更多相关文章

  1. Netty高性能web框架

    框架背景: 前期为公司项目做全链路压测,发现公司跑到tomcat上的服务,即使是最简单的方法QPS也就到3000左右,后期查询发现可能和tomcat的业务逻辑有关. 因为以前在项目开发中用netty做 ...

  2. 彩虹女神跃长空,Go语言进阶之Go语言高性能Web框架Iris项目实战-项目入口与路由EP01

    书接上回,我们已经安装好Iris框架,并且构建好了Iris项目,同时配置了fresh自动监控项目的实时编译,万事俱备,只欠东风,彩虹女神蓄势待发.现在我们来看看Iris的基础功能,如何编写项目入口文件 ...

  3. NGINX高性能Web服务器详解(读书笔记)

    原文地址:NGINX高性能Web服务器详解(读书笔记) 作者:夏寥寥 第4章  Nginx服务器的高级配置 4.1 针对IPv4的内核7个参数的配置优化 说明:我们可以将这些内核参数的值追加到Linu ...

  4. python 高性能web框架 gunicorn+gevent

    参考链接: http://rfyiamcool.blog.51cto.com/1030776/1276364/ http://www.cnblogs.com/nanrou/p/7026789.html ...

  5. 急如闪电快如风,彩虹女神跃长空,Go语言高性能Web框架Iris项目实战-初始化项目ep00

    在Golang Web编程的世界里,君不言高性能则已,言高性能必称Iris.彩虹女神的名号响彻寰宇.名动江湖,单论一个快字,无人能出其右,就连以简洁轻量著称于世的Gin也难以望其项背,只见彩虹女神Ir ...

  6. dotweb——go语言的一个微型web框架(一)

    dotweb是16年正式托管到github的一个开源项目,go语言的web框架目前也有很多,出名的有bee和echo.它们都是很优秀的框架,但是我们喜欢更轻.更小的东西,经历一些之后我们更青睐微服务这 ...

  7. dotweb——go语言的一个微型web框架(二)启动dotweb

    以上的代码截图表示启动一个dotweb服务,在浏览器里输入127.0.0.1:8080,将会得到一个"index"的页面. app := dotweb.New() dotweb.N ...

  8. 运行第一个Go Web框架

    GO 语言的web框架很多,相对来说, Beego 框架,入门简单,文档齐全(中文),功能强大,本文以Beego 示例. Beego提供了详细的开发文档:http://beego.me/docs/in ...

  9. 选择一个 Python Web 框架:Django vs Flask vs Pyramid

    Pyramid, Django, 和 Flask都是优秀的框架,为项目选择其中的哪一个都是伤脑筋的事.我们将会用三种框架实现相同功能的应用来更容易的对比三者.也可以直接跳到框架实战(Framework ...

随机推荐

  1. SpringMVC Controller介绍及常用注解

    一.简介 在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层处理之后封装成一个Model ,然后再把该Mo ...

  2. Java Naming and Directory Interface (JNDI) Java 命名和目录接口

    https://www.oracle.com/technetwork/java/jndi/index.html Lesson: Overview of JNDI (The Java™ Tutorial ...

  3. ETL__pentaho__SPOON_PDI

    Pentaho Data Integration (PDI, also called Kettle),是pentaho的etl工具.虽然etl工具一般都用在数据仓库环境中,可是,PDI还是可以做以下事 ...

  4. Feed系统架构资料收集(转)

    add by zhj:有些链接已经失效,后续会修改. 原文:http://blog.csdn.net/zhangzhaokun/article/details/7834797 完全用nosql轻松打造 ...

  5. go-003-基础语法

    1.行分隔符 一行代表一个语句结束. 如果一行多个,使用“;”分割,不推荐使用,建议使用默认一行一个语句 2.标识符 标识符用来命名变量.类型等程序实体.一个标识符实际上就是一个或是多个字母(A~Z和 ...

  6. Python微信机器人

    Python微信机器人 本文目录 一 简介 二 登录微信 三 微信好友男女比例 四 微信好友地域分布 五 微信聊天机器人 一 简介 wxpy基于itchat,使用了 Web 微信的通讯协议,,通过大量 ...

  7. python之__setattr__常见问题

    #__setattr__ class Foo(object): def set(self,k,v): pass def __setattr__(self, key, value): print(key ...

  8. Flask系列(五)Flask实现分页

    一.flask分页组件 from urllib.parse import urlencode,quote,unquote class Pagination(object): ""& ...

  9. Spark Streaming里面使用文本分析模型

    功能:接收来自kafka的数据,数据是一篇文章,来判断文章的类型,把判断的结果一并保存到Hbase,并把文章建立索引(没有代码只有一个空壳,可以自己实现,以后有机会了可能会补上) import org ...

  10. cocos2dx 3.x 蒙板 遮罩 点击圆功能

    //注册触摸 EventListenerTouchOneByOne *listener = EventListenerTouchOneByOne::create(); listener->onT ...