环境: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. 170705、springboot编程之自定义properties

    spring boot使用application.properties默认了很多配置.但需要自己添加一些配置的时候,可以这样用,如下! 在application.properties文件中增加信息 1 ...

  2. RedisDesktopManager 打开报0xc000007b程序错误

    RedisDesktopManager 是一个管理redis的工具,很好用,我的电脑可以安装0.8.3版的,最新版到0.9.4了,其中经典版本是0.8.8,可惜0.8.3版之后,我的电脑安装软件后,打 ...

  3. 程序入口函数和glibc及C++全局构造和析构

    分类: CRT Machnasim 2011-06-15 17:45 144人阅读 评论(0) 收藏 举报 c++汇编linuxlist语言编译器 1,程序入口函数和初始化 操作系统在装载可执行文件后 ...

  4. Silverlight中ListBox的数据绑定

    在Silverlight中ListBox是一个非常强大的控件.总结下ListBox的绑定数据的方式. 首先,新建一个Book类, public class Book { public string B ...

  5. IntelliJ IDEA 插件

    alibaba java  coding guidelines 阿里巴巴Java编码指南插件支持. Mybatis-log-plugin 把 mybatis 输出的sql日志还原成完整的sql语句. ...

  6. mapxtreme java学习之路(1)——.dwg转.tab再转.gst详细教程

    [背景] 因为项目的需要,需要在java web 项目中使用到地图,厂家提供的是dwg格式的地图,而我们采用的是mapxtreme java技术,所以先要把dwg格式的地图转成mapxtreme ja ...

  7. mysql 数据操作 单表查询 having 过滤 练习

    1. 查询各岗位内包含的员工个数小于2的岗位名.岗位内包含员工名字.个数 mysql> select post,group_concat(name),count(id) from employe ...

  8. SSL/TSL握手过程详解

    1. Client Hello 握手第一步是客户端向服务端发送 Client Hello 消息,这个消息里包含了一个客户端生成的随机数 Random1.客户端支持的加密套件(Support Ciphe ...

  9. ppt 文字按笔画进入。

    1:在word里面写一个字(艺术字或非艺术字都可),字体格式设置为  楷体_gb23212, 没有则下载安装,http://www.cnblogs.com/liyafei/p/8747992.html ...

  10. PHP接收json格式的POST数据

    /** * 获取 post 参数; 在 content_type 为 application/json 时,自动解析 json * @return array */ private function ...