openresty

一.前言

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

二.OpenResty运行原理

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

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

原理图如下:

三.OpenResty的优势

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

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

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

四.Nginx和lua的简介

  1. Nginx:

(1) Nginx的优点

轻量级同样起web 服务比apache占用更少内存及资源 

抗并发nginx 处理请求异步非阻塞而apache 则阻塞型高并发下nginx 能保持低资源低消耗高性能 

高度模块化设计编写模块相对简单 

社区活跃各种高性能模块出品迅速啊

(2) Nginx为什么性能高,占用内存少

众所周知,nginx性能高,而nginx的高性能与其架构是分不开的。在这里,我们简单粗略的介绍一下nginx的架构。

首先,nginx采用的是多多进程模式,好处是什么呢?首先,对于每个worker进程来说,独立的进程,不需要加锁,所以省掉了锁带来的开销,同时在编程以及问题查找时,也会方便很多。其次,采用独立的进程,可以让互相之间不会影响,一个进程退出后,其它进程还在工作,服务不会中断,master进程则很快启动新的worker进程。当然,worker进程的异常退出,肯定是程序有bug了,异常退出,会导致当前worker上的所有请求失败,不过不会影响到所有请求,所以降低风险。

Nginx是采用异步非阻塞的方式去处理请求的,什么是异步非阻塞呢?其实就是当一个线程调用出现等待的io之类的情况时,而不是阻塞在这里,而是去处理别的事情,等io准备好了,然后再去执行,具体的我就不在这里和大家描述了。
  1. lua:

(1) Lua 是一个小巧的脚本语言。作者是巴西人。该语言的设计目的是为了嵌入应用程序中,从而为应用程序提供灵活的扩展和定制功能

(2) Lua的特点:

Lua脚本可以很容易的被C/C++代码调用,也可以反过来调用C/C++的函数,这使得Lua在应用程序中可以被广泛应用。不仅仅作为扩展脚本,也可以作为普通的配置文件,代替XML,Ini等文件格式,并且更容易理解和维护。

Lua由标准C编写而成,代码简洁优美,几乎在所有操作系统和平台上都可以编译,运行。一个完整的Lua解释器不过200k,在目前所有脚本引擎中,Lua的速度是最快的。这一切都决定了Lua是作为嵌入式脚本的最佳选择。
openresty --help
--with需要加
--without不需要加,内置模块
nginx 同理





[root@server1 ~]# ls
memcache-4.0.5.2      oniguruma-6.8.2-1.el7.x86_64.rpm
memcache-4.0.5.2.tgz  oniguruma-devel-6.8.2-1.el7.x86_64.rpm
nginx-1.18.0          openresty-1.17.8.2.tar.gz
nginx-1.18.0.tar.gz   package.xml
nginx-1.19.1          php-7.4.6
nginx-1.19.1.tar.gz   php-7.4.6.tar.bz2
[root@server1 ~]# tar zxf openresty-1.17.8.2.tar.gz 
[root@server1 ~]# cd openresty-1.17.8.2
[root@server1 openresty-1.17.8.2]# ls
bundle     COPYRIGHT  README.markdown     util
configure  patches    README-windows.txt
[root@server1 openresty-1.17.8.2]# ll
total 104
drwxrwxr-x 46 nginx nginx  4096 Jul 14 01:56 bundle
-rwxrwxr-x  1 nginx nginx 52486 Jul 14 01:55 configure
-rw-rw-r--  1 nginx nginx 22924 Jul 14 01:55 COPYRIGHT
drwxrwxr-x  2 nginx nginx  4096 Jul 14 01:55 patches
-rw-rw-r--  1 nginx nginx  4689 Jul 14 01:55 README.markdown
-rw-rw-r--  1 nginx nginx  8972 Jul 14 01:55 README-windows.txt
drwxrwxr-x  2 nginx nginx    52 Jul 14 01:55 util
[root@server1 openresty-1.17.8.2]# ./configure 
[root@server1 openresty-1.17.8.2]# gmake install
[root@server1 openresty-1.17.8.2]# cd /usr/local/openresty/
[root@server1 openresty]# ls
bin  COPYRIGHT  luajit  lualib  nginx  pod  resty.index  site
[root@server1 openresty]# cd bin/
[root@server1 bin]# ls
md2pod.pl  nginx-xml2pod  openresty  opm  resty  restydoc  restydoc-index      
[root@server1 bin]# ll openresty 
lrwxrwxrwx 1 root root 37 Aug 14 09:59 openresty -> /usr/local/openresty/nginx/sbin/nginx
[root@server1 bin]# cd ../nginx/sbin/
[root@server1 sbin]# ls
nginx
[root@server1 sbin]# ./nginx -v
nginx version: openresty/1.17.8.2
[root@server1 sbin]# cd ..
[root@server1 nginx]# cd conf/
[root@server1 conf]# systemctl start memcached
[root@server1 conf]# netstat -antlp
Active Internet connections (servers and established)
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      13192/memcached     
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      3376/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      3600/master         
tcp        0      0 172.25.4.1:22           172.25.4.250:35876      ESTABLISHED 3873/sshd: root@pts 
tcp6       0      0 :::11211                :::*                    LISTEN      13192/memcached     
tcp6       0      0 :::22                   :::*                    LISTEN      3376/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      3600/master         

server3,server4
yum install -y memcached telnet
systemctl start memcached
netstat -antlp|grep memcached
tcp        0      0 0.0.0.0:11211           0.0.0.0:*               LISTEN      3556/memcached      
tcp6       0      0 :::11211                :::*                    LISTEN      3556/memcached      



[root@server1 conf]# pwd
/usr/local/openresty/nginx/conf
[root@server1 conf]# vim nginx.conf

http {

        upstream memcache {
        server 172.25.4.3:11211;
        server 172.25.4.4:11211;
        keepalive 512;
	}


        location /memc {
        internal;
        memc_connect_timeout 100ms;
        memc_send_timeout 100ms;
        memc_read_timeout 100ms;
        set $memc_key $query_string;
        set $memc_exptime 300;
        memc_pass memcache;
}


[root@server1 conf]# cat /etc/sysconfig/memcached
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS=""
[root@server1 conf]# free -m
              total        used        free      shared  buff/cache   available
Mem:           3789         103        3295          16         389        3424
Swap:          2047           0        2047


[root@server1 conf]# vim nginx.conf


 79         location ~ \.php$ {
 80             root           html;
 81             fastcgi_pass   127.0.0.1:9000;
 82             fastcgi_index  index.php;
 83             #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
 84             include        fastcgi.conf;
 85         }



[root@server1 conf]# /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
[root@server1 conf]# /usr/local/openresty/nginx/sbin/nginx
[root@server1 conf]# /usr/local/openresty/nginx/sbin/nginx -s reload
[root@server1 conf]# pwd
/usr/local/openresty/nginx/conf
[root@server1 conf]# cd ../html/
[root@server1 html]# vim index.php





[root@foundation4 ~]# ab -c10 -n 20000 http://172.25.4.1/index.php

Server Software:        openresty/1.17.8.2
Server Hostname:        172.25.4.1
Server Port:            80

Document Path:          /index.php
Document Length:        71521 bytes

Concurrency Level:      10
Time taken for tests:   12.913 seconds
Complete requests:      20000
Failed requests:        1996
   (Connect: 0, Receive: 0, Length: 1996, Exceptions: 0)
Total transferred:      1433777769 bytes
HTML transferred:       1430417769 bytes
Requests per second:    1548.83 [#/sec] (mean)
Time per request:       6.457 [ms] (mean)
Time per request:       0.646 [ms] (mean, across all concurrent requests)
Transfer rate:          108431.30 [Kbytes/sec] received





 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         }


[root@server1 conf]# /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
[root@server1 conf]# /usr/local/openresty/nginx/sbin/nginx -s reload

[root@server1 html]# /etc/init.d/php-fpm start
Starting php-fpm  done


[root@foundation4 ~]# ab -c10 -n 20000 http://172.25.4.1/index.php


Server Software:        openresty/1.17.8.2
Server Hostname:        172.25.4.1
Server Port:            80

Document Path:          /index.php
Document Length:        71521 bytes

Concurrency Level:      10
Time taken for tests:   7.394 seconds
Complete requests:      20000
Failed requests:        0
Total transferred:      1434240000 bytes
HTML transferred:       1430420000 bytes
Requests per second:    2704.91 [#/sec] (mean)
Time per request:       3.697 [ms] (mean)
Time per request:       0.370 [ms] (mean, across all concurrent requests)
Transfer rate:          189428.39 [Kbytes/sec] received








  440  cd /usr/local/openresty/nginx/html/
vim memcache.php 






 28 $MEMCACHE_SERVERS[] = '172.25.4.3:11211'; // add more as an array
 29 $MEMCACHE_SERVERS[] = '172.25.4.4:11211'; // add more as an array

==============
[root@server1 conf]# vim nginx.conf


 47         location / {
 48             root   html;
 49             index  index.html index.htm;
 50         }
 51 
 52         location /lua {
 53         default_type text/html;
 54         echo hello world;
 55         }



[root@server1 conf]# /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
[root@server1 conf]# /usr/local/openresty/nginx/sbin/nginx -s reload


网页测试:

http://172.25.4.1/lua
hello world 


[root@server1 conf]# vim nginx.conf

 52         location /lua {
 53         default_type text/html;
 54         content_by_lua 'ngx.say("hello world")';
 55         }


[root@server1 conf]# /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
[root@server1 conf]# /usr/local/openresty/nginx/sbin/nginx -s reload



http://172.25.4.1/lua

hello world 


[root@server1 conf]# vim nginx.conf

 52         location /lua {
 53         default_type text/html;
 54         content_by_lua "ngx.print(ngx.var['arg_a'],'\\n')";
 55         } 


[root@server1 conf]# /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
[root@server1 conf]# /usr/local/openresty/nginx/sbin/nginx -s reload



http://172.25.4.1/lua?a=helloworld
helloworld 


[root@server3 ~]# systemctl start httpd
[root@server3 ~]# cd /var/www/html/
[root@server3 html]# ls
images  index.html  index.php  upload  upload_file.php
[root@server3 html]# mkdir lua
[root@server3 html]# cd lua/
[root@server3 lua]# vim index.html

lua



[root@server1 conf]# vim nginx.conf

 82         location @client {
 83         proxy_pass http://172.25.4.3;
 84         }
 85 
 86         location ~\.php$ {
 87         default_type text/html;
 88         content_by_lua 'ngx.say("this is westos.org")';
 89         access_by_lua '
 90         if ngx.var.remote_addr == "172.25.4.250" then
 91         ngx.exec("@client")
 92         end
 93         ';
 94         }


[root@server1 conf]# /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
[root@server1 conf]# /usr/local/openresty/nginx/sbin/nginx -s reload


[root@server4 ROOT]# curl 172.25.4.1/index.php
this is westos.org
网页测试
http://172.25.4.1/index.php





















==========================================








[root@server3 ~]# ls
apache-tomcat-7.0.37.tar.gz
[root@server3 ~]# tar zxf apache-tomcat-7.0.37.tar.gz  -C /usr/local/
[root@server3 ~]# cd /usr/local/
[root@server3 local]# ls
apache-tomcat-7.0.37  bin  etc  games  include  lib  lib64  libexec  sbin  share  src
[root@server3 local]# ln -s apache-tomcat-7.0.37/ tomcat

[root@server3 local]# ll


lrwxrwxrwx  1 root root  21 Aug 14 14:38 tomcat -> apache-tomcat-7.0.37/



[root@server3 tomcat]# ls
bin  conf  lib  LICENSE  logs  NOTICE  RELEASE-NOTES  RUNNING.txt  temp  webapps  work
[root@server3 tomcat]# cd webapps/
[root@server3 webapps]# ls
docs  examples  host-manager  manager  ROOT
[root@server3 webapps]# cd ROOT/
[root@server3 ROOT]# ls
asf-logo.png       bg-middle.png    bg-upper.png  index.jsp          tomcat.gif        tomcat.svg
asf-logo-wide.gif  bg-nav-item.png  build.xml     RELEASE-NOTES.txt  tomcat.png        WEB-INF
bg-button.png      bg-nav.png       favicon.ico   tomcat.css         tomcat-power.gif


[root@server3 ~]# ls
apache-tomcat-7.0.37.tar.gz  jdk-8u121-linux-x64.rpm

[root@server3 ~]# yum install jdk-8u121-linux-x64.rpm -y
[root@server3 tomcat]# bin/startup.sh
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[root@server3 tomcat]# netstat -antlp

tcp6       0      0 :::8009                 :::*                    LISTEN      4876/java           
tcp6       0      0 :::11211                :::*                    LISTEN      3556/memcached      
tcp6       0      0 :::8080                 :::*                    LISTEN      4876/java                     
tcp6       0      0 ::1:25                  :::*                    LISTEN      3460/master         
tcp6       0      0 127.0.0.1:8005          :::*                    LISTEN      4876/java      


[root@server4 ~]# tar zxf apache-tomcat-7.0.37.tar.gz  -C /usr/local/
[root@server4 ~]# cd /usr/local/
[root@server4 local]# ln -s apache-tomcat-7.0.37/ tomcat
[root@server4 local]# cd tomcat/
[root@server4 tomcat]# bin/startup.sh
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[root@server4 tomcat]# netstat -antlp

tcp6       0      0 :::8009                 :::*                    LISTEN      4460/java           
tcp6       0      0 :::11211                :::*                    LISTEN      3492/memcached      
tcp6       0      0 :::8080                 :::*                    LISTEN      4460/java                    
tcp6       0      0 ::1:25                  :::*                    LISTEN      3403/master         
tcp6       0      0 127.0.0.1:8005          :::*                    LISTEN      4460/java           

[root@server3 tomcat]# cd /usr/local/tomcat/webapps/ROOT/   #test.jsp
[root@server3 ROOT]# ls
asf-logo.png       bg-nav-item.png  favicon.ico        tomcat.css        tomcat.svg
asf-logo-wide.gif  bg-nav.png       index.jsp          tomcat.gif        WEB-INF
bg-button.png      bg-upper.png     RELEASE-NOTES.txt  tomcat.png
bg-middle.png      build.xml        test.jsp           tomcat-power.gif
[root@server3 ROOT]# 




[root@server4 tomcat]# pwd
/usr/local/tomcat
[root@server4 tomcat]# cd webapps/ROOT/
[root@server4 ROOT]# ls
asf-logo.png       bg-nav-item.png  favicon.ico        tomcat.css        tomcat.svg
asf-logo-wide.gif  bg-nav.png       index.jsp          tomcat.gif        WEB-INF
bg-button.png      bg-upper.png     RELEASE-NOTES.txt  tomcat.png
bg-middle.png      build.xml        test.jsp           tomcat-power.gif




[root@server1 conf]# pwd
/usr/local/openresty/nginx/conf
[root@server1 conf]# vim nginx.conf


 17 http {
 18 
 19         upstream memcache {
 20         server 172.25.4.3:11211;
 21         server 172.25.4.4:11211;
 22         keepalive 512;
 23         }
 24 
 25         upstream tomcat {
 26         server 172.25.4.3:8080;
 27         server 172.25.4.4:8080;
 28         }


 86 
 87         location ~\.jsp$ {
 88         proxy_pass http://tomcat;
 89 }
 90 
 91         location @client {
 92         proxy_pass http://172.25.4.3;
 93         }




[root@server1 conf]# /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
[root@server1 conf]# /usr/local/openresty/nginx/sbin/nginx -s reload




网页测试:

http://172.25.4.1/


Welcome to OpenResty!

If you see this page, the OpenResty web platform is successfully installed and working. Further configuration is required.

For online documentation and support please refer to openresty.org.
Commercial support is available at openresty.com.

Thank you for flying OpenResty.


http://172.25.4.1/test.jsp



Server Info: 172.25.4.3 : 8080


Server Info: 172.25.4.4 : 8080


server3,server4负载均衡

=========================================
[root@server1 conf]# cd
[root@server1 ~]# ls
memcache-4.0.5.2                                       oniguruma-6.8.2-1.el7.x86_64.rpm
memcache-4.0.5.2.tgz                                   oniguruma-devel-6.8.2-1.el7.x86_64.rpm
nginx-1.18.0                                           openresty-1.17.8.2
nginx-1.18.0.tar.gz                                    openresty-1.17.8.2.tar.gz
nginx-1.19.1                                           package.xml
nginx-1.19.1.tar.gz                                    php-7.4.6
nginx-goodies-nginx-sticky-module-ng-08a395c66e42.zip  php-7.4.6.tar.bz2
[root@server1 ~]# yum install unzip -y
[root@server1 ~]# unzip nginx-goodies-nginx-sticky-module-ng-08a395c66e42.zip 
[root@server1 ~]# ls
nginx-goodies-nginx-sticky-module-ng-08a395c66e42
nginx-goodies-nginx-sticky-module-ng-08a395c66e42.zip
[root@server1 ~]# cd openresty-1.17.8.2
[root@server1 openresty-1.17.8.2]# ./configure --add-module=/root/nginx-goodies-nginx-sticky-module-ng-08a395c66e42

adding module in /root/nginx-goodies-nginx-sticky-module-ng-08a395c66e42
 + ngx_http_sticky_module was configured

[root@server1 openresty-1.17.8.2]# gmake

[root@server1 openresty-1.17.8.2]# find -name nginx
./bundle/nginx-1.17.8/docs/xml/nginx
./bundle/pod/nginx
./build/nginx-1.17.8/docs/xml/nginx
./build/nginx-1.17.8/objs/nginx
./build/pod/nginx
[root@server1 openresty-1.17.8.2]# cd build/nginx-1.17.8/objs/
[root@server1 objs]# ls
addon         Makefile  nginx.8            ngx_auto_headers.h  ngx_modules.o
autoconf.err  nginx     ngx_auto_config.h  ngx_modules.c       src
[root@server1 objs]# cd /usr/local/openresty/nginx/sbin/
[root@server1 sbin]# ls
nginx
[root@server1 sbin]# mv nginx nginx.old
[root@server1 sbin]# cd -
/root/openresty-1.17.8.2/build/nginx-1.17.8/objs
[root@server1 objs]# cp nginx /usr/local/openresty/nginx/sbin/



[root@server1 objs]# cd /usr/local/openresty/nginx/conf/
[root@server1 conf]# vim nginx.conf



 25         upstream tomcat {
 26         sticky;
 27         server 172.25.4.3:8080;
 28         server 172.25.4.4:8080;
 29         }
 30 

[root@server1 conf]# /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
[root@server1 conf]# /usr/local/openresty/nginx/sbin/nginx -s reload


[root@foundation4 ~]# ls
anaconda-ks.cfg              jdk-8u121-linux-x64.rpm                                oniguruma-6.8.2-1.el7.x86_64.rpm        patches            test.jsp
apache-tomcat-7.0.37.tar.gz  memcache-4.0.5.2.tgz                                   oniguruma-devel-6.8.2-1.el7.x86_64.rpm  php-7.4.6.tar.bz2  TEXT
foundation-config-post.log   nginx.conf                                             openresty-1.17.8.2.tar.gz               rht-ks-post.log    upload
jar                          nginx-goodies-nginx-sticky-module-ng-08a395c66e42.zip  original-ks.cfg                         rht-ks-pre.log
[root@foundation4 ~]# scp -r jar/ 172.25.4.3:
asm-3.2.jar                                                                                                                                   100%   42KB  50.2MB/s   00:00    
kryo-1.04.jar                                                                                                                                 100%   93KB  84.0MB/s   00:00    
kryo-serializers-0.10.jar                                                                                                                     100%   59KB  80.4MB/s   00:00    
memcached-session-manager-1.6.3.jar                                                                                                           100%  129KB  97.9MB/s   00:00    
memcached-session-manager-tc6-1.6.3.jar                                                                                                       100%   10KB  27.5MB/s   00:00    
memcached-session-manager-tc7-1.6.3.jar                                                                                                       100% 9258    24.9MB/s   00:00    
minlog-1.2.jar                                                                                                                                100% 4879    14.7MB/s   00:00    
msm-kryo-serializer-1.6.3.jar                                                                                                                 100%   23KB  49.2MB/s   00:00    
reflectasm-1.01.jar                                                                                                                           100%   11KB  30.6MB/s   00:00    
spymemcached-2.7.3.jar                                                                                                                        100%  398KB 119.4MB/s   00:00    
[root@foundation4 ~]# scp -r jar/ 172.25.4.4:
asm-3.2.jar                                                                                                                                   100%   42KB  49.0MB/s   00:00    
kryo-1.04.jar                                                                                                                                 100%   93KB  83.1MB/s   00:00    
kryo-serializers-0.10.jar                                                                                                                     100%   59KB  86.6MB/s   00:00    
memcached-session-manager-1.6.3.jar                                                                                                           100%  129KB 109.4MB/s   00:00    
memcached-session-manager-tc6-1.6.3.jar                                                                                                       100%   10KB  20.8MB/s   00:00    
memcached-session-manager-tc7-1.6.3.jar                                                                                                       100% 9258    27.2MB/s   00:00    
minlog-1.2.jar                                                                                                                                100% 4879    14.3MB/s   00:00    
msm-kryo-serializer-1.6.3.jar                                                                                                                 100%   23KB  50.1MB/s   00:00    
reflectasm-1.01.jar                                                                                                                           100%   11KB  30.5MB/s   00:00    
spymemcached-2.7.3.jar                                                                                                                        100%  398KB 140.1MB/s   00:00    








[root@server3 tomcat]# ls
bin  conf  lib  LICENSE  logs  NOTICE  RELEASE-NOTES  RUNNING.txt  temp  webapps  work
[root@server3 tomcat]# cd lib/
[root@server3 lib]# ls
annotations-api.jar  catalina.jar         el-api.jar     jsp-api.jar      tomcat-coyote.jar   tomcat-i18n-fr.jar  tomcat-util.jar
catalina-ant.jar     catalina-tribes.jar  jasper-el.jar  servlet-api.jar  tomcat-dbcp.jar     tomcat-i18n-ja.jar
catalina-ha.jar      ecj-4.2.1.jar        jasper.jar     tomcat-api.jar   tomcat-i18n-es.jar  tomcat-jdbc.jar
[root@server3 lib]# cd
[root@server3 ~]# ls
apache-tomcat-7.0.37.tar.gz  jdk-8u121-linux-x64.rpm
[root@server3 ~]# lftp ^C
[root@server3 ~]# ls
apache-tomcat-7.0.37.tar.gz  jar  jdk-8u121-linux-x64.rpm
[root@server3 ~]# cd jar/
[root@server3 jar]# ls
asm-3.2.jar                memcached-session-manager-1.6.3.jar      minlog-1.2.jar                 spymemcached-2.7.3.jar
kryo-1.04.jar              memcached-session-manager-tc6-1.6.3.jar  msm-kryo-serializer-1.6.3.jar
kryo-serializers-0.10.jar  memcached-session-manager-tc7-1.6.3.jar  reflectasm-1.01.jar
[root@server3 jar]# cp * /usr/local/tomcat/lib/
[root@server3 jar]# cd /usr/local/tomcat/lib/ls
-bash: cd: /usr/local/tomcat/lib/ls: No such file or directory
[root@server3 jar]# cd /usr/local/tomcat/lib/
[root@server3 lib]# ls
annotations-api.jar  ecj-4.2.1.jar  kryo-serializers-0.10.jar                reflectasm-1.01.jar     tomcat-i18n-es.jar
asm-3.2.jar          el-api.jar     memcached-session-manager-1.6.3.jar      servlet-api.jar         tomcat-i18n-fr.jar
catalina-ant.jar     jasper-el.jar  memcached-session-manager-tc6-1.6.3.jar  spymemcached-2.7.3.jar  tomcat-i18n-ja.jar
catalina-ha.jar      jasper.jar     memcached-session-manager-tc7-1.6.3.jar  tomcat-api.jar          tomcat-jdbc.jar
catalina.jar         jsp-api.jar    minlog-1.2.jar                           tomcat-coyote.jar       tomcat-util.jar
catalina-tribes.jar  kryo-1.04.jar  msm-kryo-serializer-1.6.3.jar            tomcat-dbcp.jar
[root@server3 lib]# rm -f memcached-session-manager-tc6-1.6.3.jar







[root@server4 tomcat]# ls
bin  conf  lib  LICENSE  logs  NOTICE  RELEASE-NOTES  RUNNING.txt  temp  webapps  work
[root@server4 tomcat]# cd conf/
[root@server4 conf]# ls
Catalina  catalina.policy  catalina.properties  context.xml  logging.properties  server.xml  tomcat-users.xml  web.xml
[root@server4 conf]# ls
Catalina  catalina.policy  catalina.properties  context.xml  logging.properties  server.xml  tomcat-users.xml  web.xml
[root@server4 conf]# vim context.xml 
[root@server4 conf]# cd ..
[root@server4 tomcat]# bin/shutdown.sh 
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[root@server4 tomcat]# ls
bin  conf  lib  LICENSE  logs  NOTICE  RELEASE-NOTES  RUNNING.txt  temp  webapps  work
[root@server4 tomcat]# cd lib/
[root@server4 lib]# ls
annotations-api.jar  catalina.jar         el-api.jar     jsp-api.jar      tomcat-coyote.jar   tomcat-i18n-fr.jar  tomcat-util.jar
catalina-ant.jar     catalina-tribes.jar  jasper-el.jar  servlet-api.jar  tomcat-dbcp.jar     tomcat-i18n-ja.jar
catalina-ha.jar      ecj-4.2.1.jar        jasper.jar     tomcat-api.jar   tomcat-i18n-es.jar  tomcat-jdbc.jar
[root@server4 lib]# cd
[root@server4 ~]# ls
apache-tomcat-7.0.37.tar.gz  jar  jdk-8u121-linux-x64.rpm
[root@server4 ~]# cd jar/
[root@server4 jar]# cp * /usr/local/tomcat/lib/
[root@server4 jar]# cd /usr/local/tomcat/lib/
[root@server4 lib]# ls
annotations-api.jar  catalina-tribes.jar  jsp-api.jar                              memcached-session-manager-tc7-1.6.3.jar  spymemcached-2.7.3.jar  tomcat-i18n-fr.jar
asm-3.2.jar          ecj-4.2.1.jar        kryo-1.04.jar                            minlog-1.2.jar                           tomcat-api.jar          tomcat-i18n-ja.jar
catalina-ant.jar     el-api.jar           kryo-serializers-0.10.jar                msm-kryo-serializer-1.6.3.jar            tomcat-coyote.jar       tomcat-jdbc.jar
catalina-ha.jar      jasper-el.jar        memcached-session-manager-1.6.3.jar      reflectasm-1.01.jar                      tomcat-dbcp.jar         tomcat-util.jar
catalina.jar         jasper.jar           memcached-session-manager-tc6-1.6.3.jar  servlet-api.jar                          tomcat-i18n-es.jar
[root@server4 lib]# rm -f memcached-session-manager-tc6-1.6.3.jar















[root@server3 conf]# pwd
/usr/local/tomcat/conf
[root@server3 conf]# ls
Catalina         catalina.properties  logging.properties  tomcat-users.xml
catalina.policy  context.xml          server.xml          web.xml
[root@server3 conf]# vim context.xml 




[root@server4 conf]# cd ..
[root@server4 tomcat]# bin/shutdown.sh
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[root@server4 tomcat]# bin/startup.sh
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar


[root@server4 ROOT]# cd /usr/local/tomcat/conf/
[root@server4 conf]# ls
Catalina         catalina.properties  logging.properties  tomcat-users.xml
catalina.policy  context.xml          server.xml          web.xml
[root@server4 conf]# vim context.xml 





[root@server4 conf]# cd ..
[root@server4 tomcat]# bin/shutdown.sh
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[root@server4 tomcat]# bin/startup.sh
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar


[root@server3 tomcat]# cat logs/catalina.out 


INFO: MemcachedSessionService finished initialization, sticky true, operation timeout 1000, with node ids [n2] and failover node ids [n1]

[root@server4 tomcat]# cat logs/catalina.out 


INFO: MemcachedSessionService finished initialization, sticky true, operation timeout 1000, with node ids [n1] and failover node ids [n2]


网页测试:
http://172.25.4.1/test.jsp



[root@server3 tomcat]# telnet localhost 11211
Trying ::1...
Connected to localhost.
Escape character is '^]'.
get 2A2273E5CF62AB1E1E89C85520D44526-n1
VALUE 2A2273E5CF62AB1E1E89C85520D44526-n1 2048 113
Ws�>^s�D�01s�D�`s�D�`#2A2273E5CF62AB1E1E89C85520D44526-n1key22222key44444
END                                    #网页继续输入
get 2A2273E5CF62AB1E1E89C85520D44526-n1
VALUE 2A2273E5CF62AB1E1E89C85520D44526-n1 2048 124
Ws�>^s�F�01s�F��s�F��#2A2273E5CF62AB1E1E89C85520D44526-n1key22222key5666key44444
END




 Server Info: 172.25.4.4 : 8080
 ID 2A2273E5CF62AB1E1E89C85520D44526-n1


[root@server4 tomcat]# bin/shutdown.sh 
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar



这时候网页还没有反应过来,继续写入提交就会变到server3上
之前写的东西还在

 Server Info: 172.25.4.3 : 8080

 ID 2A2273E5CF62AB1E1E89C85520D44526-n1



[root@server3 tomcat]# systemctl stop memcached

网页写入就会变成n2

Server Info: 172.25.4.3 : 8080

ID 2A2273E5CF62AB1E1E89C85520D44526-n2





[root@foundation4 ~]# telnet 172.25.4.4 11211
Trying 172.25.4.4...
Connected to 172.25.4.4.
Escape character is '^]'.
get 2A2273E5CF62AB1E1E89C85520D44526-n2
VALUE 2A2273E5CF62AB1E1E89C85520D44526-n2 2048 161
Ws�>^s�P)01s�P)as�P)b#2A2273E5CF62AB1E1E89C85520D44526-n2key11111key2key3333key9999key7777key88888key
END
^]
telnet> close
Connection closed.

[root@foundation4 ~]# telnet 172.25.4.3 11211
Trying 172.25.4.3...
Connected to 172.25.4.3.
Escape character is '^]'.
get 2A2273E5CF62AB1E1E89C85520D44526-n2
END




[root@server1 ~]# cd /usr/local/openresty/nginx/logs/
[root@server1 logs]# ls
access.log  error.log  nginx.pid
[root@server1 logs]# wc -l access.log 
100060 access.log






[root@server1 ~]# ls
goaccess-1.4.tar.gz
GeoIP-devel-1.5.0-13.el7.x86_64.rpm
[root@server1 ~]# tar zxf goaccess-1.4.tar.gz 
[root@server1 ~]# cd goaccess-1.4
[root@server1 goaccess-1.4]# ls
ABOUT-NLS   config        configure.ac  install-sh   NEWS       TODO
aclocal.m4  config.guess  COPYING       m4           po
AUTHORS     config.rpath  depcomp       Makefile.am  README
ChangeLog   config.sub    goaccess.1    Makefile.in  resources
compile     configure     INSTALL       missing      src
[root@server1 goaccess-1.4]# ./configure --enable-utf8 --enable-geoip=legacy

    *** Missing development files for the GeoIP library


[root@server1 ~]# yum install GeoIP-devel-1.5.0-13.el7.x86_64.rpm -y

[root@server1 ~]# cd goaccess-1.4
[root@server1 goaccess-1.4]# ./configure --enable-utf8 --enable-geoip=legacy
configure: error: *** Missing development libraries for ncursesw

[root@server1 goaccess-1.4]# yum install -y ncurses-devel

[root@server1 goaccess-1.4]# ./configure --enable-utf8 --enable-geoip=legacy

[root@server1 goaccess-1.4]# make 

[root@server1 goaccess-1.4]# make install

[root@server1 goaccess-1.4]# cd
[root@server1 ~]# which goaccess
/usr/local/bin/goaccess
[root@server1 ~]# cd /usr/local/
[root@server1 local]# cd openresty/
[root@server1 openresty]# cd nginx/logs/
[root@server1 logs]# ls
access.log  error.log  nginx.pid
[root@server1 logs]# goaccess access.log -c
[root@server1 logs]# goaccess  access.log -o ../html/report.html --log-format=COMBINED --real-time-html
Parsing... [96,531] [48,265/s]
WebSocket server ready to accept new client connections




网页测试:
http://172.25.4.1/report.html


[root@server1 ~]# vim /usr/local/openresty/nginx/conf/nginx.conf

 92         #location @client {
 93         #proxy_pass http://172.25.4.3;
 94         #}
 95 
 96         #location ~\.php$ {
 97         #default_type text/html;
 98         #content_by_lua 'ngx.say("this is westos.org")';
 99         #access_by_lua '
100         #if ngx.var.remote_addr == "172.25.4.250" then
101         #ngx.exec("@client")
102         #end
103         #';
104         #}


107         #location ~ \.php$ {
108         #    set $key $uri$args;
109         #    srcache_fetch GET /memc $key;
110         #    srcache_store PUT /memc $key;
111         #    root           html;
112         #    fastcgi_pass   127.0.0.1:9000;
113         #    fastcgi_index  index.php;
114             #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
115         #    include        fastcgi.conf;
116         #}



[root@server1 ~]# /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
[root@server1 ~]# /usr/local/openresty/nginx/sbin/nginx -s reload



[root@foundation4 ~]# ab -c10 -n 20000 http://172.25.4.1/index.php

网页测试:
数据变化

你可能感兴趣的:(openresty)