您的位置:首页 > 运维架构 > Nginx

LNMP架构——OpenResty实现缓存前移(到达Nginx前端层面)

2019-05-08 16:52 302 查看

前言

我们都知道Nginx有很多的特性和好处,但是在Nginx上开发成了一个难题,Nginx模块需要用C开发,而且必须符合一系列复杂的规则,最重要的用C开发模块必须要熟悉Nginx的源代码,使得开发者对其望而生畏。为了开发人员方便,所以接下来我们要介绍一种整合了Nginx和lua的框架,那就是OpenResty,它帮我们实现了可以用lua的规范开发,实现各种业务,并且帮我们弄清楚各个模块的编译顺序。关于OpenResty,我想大家应该不再陌生,随着系统架构的不断升级、优化,OpenResty在被广泛的应用。

1.OpenResty运行原理

Nginx 采用的是 master-worker 模型,一个 master 进程管理多个 worker 进程,基本的事件处理都是放在 woker 中,master 负责一些全局初始化,以及对 worker 的管理。在OpenResty中,每个 woker 使用一个 LuaVM,当请求被分配到 woker 时,将在这个 LuaVM 里创建一个 coroutine(协程)。协程之间数据隔离,每个协程具有独立的全局变量_G。

ps. 协程和多线程下的线程类似:有自己的堆栈,自己的局部变量,有自己的指令指针,但是和其他协程程序共享全局变量等信息。线程和协程的主要不同在于:多处理器的情况下,概念上来说多线程是同时运行多个线程,而协程是通过代码来完成协程的切换,任何时刻只有一个协程程序在运行。并且这个在运行的协程只有明确被要求挂起时才会被挂起。

2.OpenResty的优势

OpenResty 的目标是让你的Web服务直接跑在 Nginx 服务内部,充分利用 Nginx 的非阻塞 I/O 模型,不仅仅对 HTTP 客户端请求,甚至于对远程后端诸如 MySQL、PostgreSQL、Memcached 以及 Redis 等都进行一致的高性能响应。

首先我们选择使用OpenResty,其是由Nginx核心加很多第三方模块组成,其最大的亮点是默认集成了Lua开发环境,使得Nginx可以作为一个Web Server使用。

借助于Nginx的事件驱动模型和非阻塞IO,可以实现高性能的Web应用程序。

而且OpenResty提供了大量组件如Mysql、Redis、Memcached等等,使在Nginx上开发Web应用更方便更简单。目前在京东如实时价格、秒杀、动态服务、单品页、列表页等都在使用Nginx+Lua架构,其他公司如淘宝、去哪儿网等。

一、配置缓存前移

(注:下文配置时用到的部分相关资源在前面LNMP架构系列博客中都有介绍,这里不再细说)

1.关闭之前配置的nginx服务

[root@server1 ~]# nginx -s stop


2.下载一个openresty安装包,解压

[root@server1 ~]# tar  zxf openresty-1.13.6.1.tar.gz
[root@server1 ~]# ls

3.编译

[root@server1 openresty]# ls
openresty-1.13.6.1  openresty-1.13.6.1.tar.gz
[root@server1 openresty]# cd openresty-1.13.6.1
[root@server1 openresty-1.13.6.1]# ls
bundle  configure  COPYRIGHT  patches  README.markdown  README-win32.txt  util
[root@server1 openresty-1.13.6.1]# ./configure

编译完成后会提示安装时用gmake

4.安装

[root@server1 openresty-1.13.6.1]# gmake && gmake install

安装完成如下:

5.将 /usr/local/lnmp/nginx/html 下的两个配置文件复制到openresty的默认发布目录下

[root@server1 openresty-1.13.6.1]# cd /usr/local/openresty/
[root@server1 openresty]# ls
bin  COPYRIGHT  luajit  lualib  nginx  pod  resty.index  site
[root@server1 openresty]# cd nginx/
[root@server1 nginx]# ls
conf  html  logs  sbin
[root@server1 nginx]# cd html/
[root@server1 html]# ls
50x.html  index.html
[root@server1 html]# cp /usr/local/lnmp/nginx/html/index.php .
[root@server1 html]# cp /usr/local/lnmp/nginx/html/example.php .
[root@server1 html]# ls
50x.html  example.php  index.html  index.php
[root@server1 html]#

6.编辑openresty的配置文件

[root@server1 html]# cd ../conf/
[root@server1 conf]# ls
[root@server1 conf]# vim nginx.conf

修改内容如下:

17 http {
18         upstream memcache {
19         server localhost:11211;
20         keepalive 512;
21         }
22
23     include       mime.types;
24     default_type  application/octet-stream;

69         location /memc {
70                 internal;
71                 memc_connect_timeout 100ms;
72                 memc_send_timeout 100ms;
73                 memc_read_timeout 100ms;
74                 set $memc_key $query_string;
75                 set $memc_exptime 300;
76                 memc_pass memcache;
77         }
78
79         location ~ \.php$ {
80             set $key $uri$args;
81             srcache_fetch GET /memc $key;
82             srcache_store PUT /memc $key;
83             root           html;
84             fastcgi_pass   127.0.0.1:9000;
85             fastcgi_index  index.php;
86         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
87             include        fastcgi.conf;
88         }


7.检查没有语法错误之后开启服务,查看进程

[root@server1 conf]# ../sbin/nginx -t
[root@server1 conf]# ../sbin/nginx
[root@server1 conf]# ps ax

测试:浏览器访问成功

(1)http://172.25.6.1/index.php

(2)http://172.25.6.1/example.php

(3)http://172.25.6.1/

重启服务:

[root@server1 conf]# ../sbin/nginx -s reload

8.模拟5000请求量测试命中率和访问时间

(1)测试一:

[root@server1 conf]# ab -c 10 -n 5000 http://172.25.6.1/index.php

发现访问index.php也没有失败,而且两个的访问时间都比memcache短

下图是前面实验的结果:当处理5000访问量时,失败520,用时3.903秒;上图中用时0.424秒

(2)测试二:

[root@server1 conf]# ab -c 10 -n 5000 http://172.25.6.1/example.php

下图是前面的实验结果:当处理5000访问量时,用时1.466秒;上图中用时0.295秒

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: