介绍下 openresty(转载),


什么是 openresty

openresty 的主力作者是 @agentzh 它的网页在 这里,上面有介绍。按我的理解,他是介于客户端浏览器 js 和数据库之间的一层。

在 ajex 还没有盛行的时代,数据库的数据需要展现在浏览器的时候,一般都是使用 php/jsp 之类读取数据,然后拼表格/图表这些。在客户端机器越来越牛逼之后,把部分运算放在浏览器里面开始盛行,ajex 也越来越流行。这个时候通常还需要有个服务器端的程序来配合从数据库获取并提供数据,应该也有不少类似的程序来提供这个数据。

老版本的 openresty 是基于 perl 做的,可以上 cpan 上面 搜到 (不知道为啥这页面我打不开了)。agentzh 还专门为他写了一个 admin site,纯 js + oprensty 来实现的,可以直接在上面配置接口,很方便。目前老版本应该没人用了。

新版本的 openresty 基本上等于是 nginx 和一些 nginx 模块的集合,大部分模块都是 agentzh 和 chaoslawful 完成的,目前 agentzh 离职在家全职开发 openresty 相关,chaoslawful 还在淘宝 量子统计 

这大概就是我了解的 openresty 的起源和目前的情况。写的比较简单,里面的曲折就不多说了,可以找上面提到的大牛聊天。

怎么使用 openresty

我下面用一个简单的例子来描述下,我是怎么使用 openresty 的,从中应该能看出来 openresty 能干啥,怎么用。

需求

在 postgresql 数据库有张网站日访问流量表,包含两个字段 thedate 和 pv。需要把里面的数据展现出来,画出来流量曲线。

注意
下面的代码大都从现有程序里面扒出来的,所以不一定直接就能用,只是个示意而已。

安装 openresty

首先需要安装 openresty。从 openresty.org 下载当前的 stable 版本 ngx_openresty-1.0.6.22.tar.gz。

$ tar zxvf ngx_openresty-1.0.6.22.tar.gz  $ cd ngx_openresty-1.0.6.22  $ ./configure --with-http_drizzle_module --with-http_postgres_module --with-pg_config=/opt/pg90/bin/pg_config --prefix=/usr/local/openresty --with-libdrizzle=/usr/local/libdrizzle/ --with-luajit --with-http_iconv_module # 这是我用到的参数,按照需要加减  $ make  # make install

configure 的时候 postgres_module 是必须的,其他的 drizzle_module 是用来支持从 mysql 获取数据的,iconv_module 是用来做编码转换的,luajit 据说可以提升不少性能。

不出问题的话,在 /usr/local/openresty 目录下面就安装好了。其实更合理的方式应该是提供一个 rpm 或者 deb 包的。

启动 nginx

openresty 给提供了简单可用的 nginx.conf,所以现在可以先尝试启动下 /usr/local/openresty/nginx/sbin/nginx 了,如果启动没问题,那就 ok 了。

配置文件在 /usr/local/openresty/nginx/conf/nginx.conf。

配置 nginx

主要就是配置 /usr/local/openresty/nginx/conf/nginx.conf,以后很多事情都会在这里面来完成,说是 nginx.conf 编程也不为过,呵呵。

增加下面的配置

# http 级别增加下面的,设置好里面的 ip 用户名密码什么的         upstream pgsql {          postgres_server server_ip:5432 dbname=test password=123 user=test;          #postgres_keepalive  max=2 mode=single overflow=reject;          postgres_keepalive off;      }     # server 里面增加一个 location            location /=/pv {              postgres_query 'select thedate, pv from pv';              postgres_pass pgsql;                 rds_json on;              rds_json_format compact;                 xss_get on;              xss_callback_arg '_c';          }

这样配置好之后,重启下。结果应该很清晰了,请求 http://your\_ip/=/pv 应该就可以得到数据库里面的数据了,可以使用 curl 看看结果,应该类似下面的

[['2011-10-09', 245],['2011-10-10', 456]]

js 画图

挑一个画图程序,比如我用过的 highcharts, amcharts 这些都不错,amcharts 是使用 flash 画图,兼容各种浏览器,highcharts 号称也支持,不过我弄出来的图在 chrome/firefox 下面没问题,ie 不支持,他用的是 svg 标签。

就写几行代码来示意下吧

$(document).ready( function () {      $.ajax({          url : 'http://your_ip/=/pv',          success: function (data) {              renderPvCharts(data);          }      });         function renderPvCharts(data) {          $('body').append('
');          var result = Utils.getSplineChartSeries( data ); # 将 nginx 返回的 json 格式数据转化为 highcharts 需要的格式          var options = {     chart: {                  zoomType: 'xy',      renderTo: 'pv', # div 的 id      defaultSeriesType: 'spline'     },     title: {                  text: '每日 pv'              },     xAxis: {                  type: 'datetime'     },     tooltip: {      formatter: function() {             return '<strong>'+ this.series.name +'</strong>  '+        Highcharts.dateFormat('%e. %b', this.x) +': '+ this.y;      }     },     legend: {      layout: 'vertical',      align: 'right',      verticalAlign: 'top',      x: -10,      y: 100,      borderWidth: 0     },              series : result.y          };             var chart = new Highcharts.Chart( options );      };     } );

简单解释下

这样就完事了,数据就展现出来了。

其他

从上面可以看到整个数据流是怎么回事。openresty 可以做的事情远比上面描述的复杂,上面只是个最简单的应用了。

本篇只能算是一个入门而已,openresty 在淘宝量子统计的应用非常广泛。另外在 去哪网 也有不少应用,比如我知道的安全过滤模块,和一些数据报表,都是基于 openresty 的。

附一个 highcarts 画的图

相关内容

    暂无相关文章