微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

企业级运维——使用OpenResty实现LNMP架构的缓存前移

文章目录

OpenResty简介

OpenResty(又称:ngx_openresty) 是一个基于 Nginx 的可伸缩的 Web 平台,由中国人章亦春发起,提供了很多高质量的第三方模块。

OpenResty 是一个强大的 Web 应用服务器,Web 开发人员可以使用 Lua 脚本语言调动 Nginx 支持的各种 C 以及 Lua 模块,更主要的是在性能方面,OpenResty可以 快速构造出足以胜任 10K 以上并发连接响应的超高性能 Web 应用系统

模拟缓存前移

Nginx -s stop
  • 2.官网下载OpenResty的原码包解压并编译
    注意:OpenResty的编译安装使用的是gmake
 tar zxf openresty-1.13.6.1.tar.gz
 cd openresty-1.13.6.1
 ./configure
 gmake && gmake install
  • 3.将memcache用于测试的俩个PHP文件复制到OpenResty的Nginx认发布目录下
cp /usr/local/lnmp/Nginx/html/example.PHP /usr/local/openresty/Nginx/html
cp /usr/local/lnmp/Nginx/html/memcache.PHP /usr/local/openresty/Nginx/html
vim /usr/local/openresty/Nginx/conf/Nginx.conf

写入:

http {
        upstream memcache {   ##所有的请求,通过转发给memcache缓存服务
           server localhost:11211;   ##memcache服务在本机的11211端口
           keepalive 512;
       }
 location /memc { ##设置memcache的访问及其存储 
         internal; ##只允许内部 
         memc_connect_timeout 100ms; #定义连接发送及其超时时间 
         memc_send_timeout 100ms;
         memc_read_timeout 100ms;
         set $memc_key $query_string; #memcache以键值对的形式存储 
         set $memc_exptime 300;
         memc_pass memcache;
         }
     location ~ \.PHP$ {
           set $key $uri$args;
           srcache_fetch GET /memc $key;
           srcache_store PUT /memc $key;
           root           html;
           fastcgi_pass   127.0.0.1:9000;
           fastcgi_index  index.PHP;
           #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
           include        fastcgi.conf;         ##定义NginxPHP连接的时识别的文件
       }

配置文件相关参数详解如下:

  • upstream模块接口

从本质上说,upstream属于handler,只是他不产生自己的内容,而是通过请求后端服务器得到内容,所以才称为upstream(上游)。请求并取得响应内容的整个过程已经被封装到Nginx内部,所以upstream模块只需要开发若干回调函数,完成构造请求和解析响应等具体的工作。

所有请求都是通过location来操作memcache,memc-Nginx-module存取memcache是基于http method语义的

使用http的GET方法表示get,PUT方法表示set,设置memcache的缓存方式为internal只接受内部访问

memc_key表示以什么作为key,文件中使用的是query_string来作为key,$memc_exptime表示缓存失效时间,单位为秒

[root@LNMPserver1 html]# /usr/local/openresty/Nginx/sbin/Nginx -t
Nginx: the configuration file /usr/local/openresty/Nginx/conf/Nginx.conf Syntax is ok
Nginx: configuration file /usr/local/openresty/Nginx/conf/Nginx.conf test is successful
  • 6.开启服务,查看端口
[root@LNMPserver1 html]# /usr/local/openresty/Nginx/sbin/Nginx 
[root@LNMPserver1 html]# netstat -tnlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
tcp        0      0 0.0.0.0:11211               0.0.0.0:*                   LISTEN      6843/memcached      
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      15320/Nginx         
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      5089/sshd           
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      962/master          
tcp        0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN      6748/PHP-fpm        
tcp        0      0 :::11211                    :::*                        LISTEN      6843/memcached      
tcp        0      0 :::22                       :::*                        LISTEN      5089/sshd           
tcp        0      0 ::1:25                      :::*                        LISTEN      962/master          
tcp        0      0 :::3306                     :::*                        LISTEN      31925/MysqLd        
  • 7.浏览器查看

    在这里插入图片描述


    至此,结合OpenResty的缓存前移已经做好了,我们来总结分析一下。

分析

我们这里主要干了一件事,就是在OpenResty的Nginx配置文件中,指定了upstream memcache,这也是我们使用OpenResty的原因,因为它集成了memcache等模块,我们可以清晰的从其配置文件中理解,所有的请求,会经由本地memcache的11211端口,而且

location ~ \.PHP$ {
           set $key $uri$args;
           srcache_fetch GET /memc $key;
           srcache_store PUT /memc $key;

这里指定了所有访问.PHP结尾的页面,会从 /memc 中取,没有的话就存进去。所以,我们相当于让memcache前移到Nginx接受请求的位置。

压力测试

我们再次在真实主机上测试:

ab -c 10 -n 2000 http://172.25.66.1/index.PHP
ab -c 10 -n 2000 http://172.25.66.1/example.PHP

可以发现,两个页面的响应时间和正确率都有质的提高。而有两级的memcache的example.PHP页面速度更快了。

在这里插入图片描述


在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐