首页 > 代码库 > nginx+tomcat8+memcached实现session共享具体操作

nginx+tomcat8+memcached实现session共享具体操作

本次试验用到的软件包的版本如下:

wKioL1PV_D-zah4yAABI9063cOU068.jpg

一、针对10.43.2.134的操作

1.安装jdk环境

tar zxf jdk-8u5-linux-x64.tar.gz 

mkdir /usr/java

mv jdk1.8.0_05/ /usr/java/

编辑/etc/profile

在文档的末尾追加如下5行内容:

JAVA_HOME=/usr/java/jdk1.8.0_05

JRE_HOME=/usr/java/jdk1.8.0_05/jre

PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin

CLASSPATH=.:${JAVA_HOME}/lib/:$JRE_HOME/lib

export JAVA_HOME JRE_HOME PATH CLASSPATH

[root@localhost jdk1.8.0_05]# source /etc/profile

查看Java的版本

[root@localhost ~]# java -version

java version "1.8.0_05"

Java(TM) SE Runtime Environment (build 1.8.0_05-b13)

Java HotSpot(TM) 64-Bit Server VM (build 25.5-b02, mixed mode)

2.安装Tomcat 

tar zxvf apache-tomcat-8.0.8.tar.gz

mv apache-tomcat-8.0.8 /usr/local/tomcat/

#默认tomcat是root身份运行的,这样不安全,我们设置来用普通用户

groupadd tomcat 

useradd -g tomcat tomcat 

passwd tomcat 

chown tomcat.tomcat -R /usr/local/tomcat 

su – tomcat /usr/local/tomcat/bin/startup.sh 

echo “su – tomcat /usr/local/tomcat/bin/startup.sh” >> /etc/rc.local  #开机启动

确认服务是否启动成功:http://localhost:8080

wKiom1PV89CjRPbyAAWR9Smwo48073.jpg

同样的方式再部署一个tomcat,端口号:8081

tar zxvf apache-tomcat-8.0.8.tar.gz

mv apache-tomcat-8.0.8 /usr/local/tomcat2

chown tomcat.tomcat -R /usr/local/tomcat2 

编辑 /usr/local/tomcat2/conf/server.xml

三处修改分别是:

wKioL1PV9LiT6lkwAADRbsNHMQI927.jpg

wKiom1PV857Cz4XlAACsizZfj1Y462.jpg

wKiom1PV856zrVwuAAB9rLSM5Cg903.jpg

su – tomcat /usr/local/tomcat2/bin/startup.sh 

echo “su – tomcat /usr/local/tomcat2/bin/startup.sh” >> /etc/rc.local

确认服务是否启动成功:http://localhost:8081

wKioL1PV9QjCfxyKAAT1IvNBoIo830.jpg

3.制作tomcat服务器测试页,并测试访问

分别在$CATALINA/webapps/ROOT/下建立测试页面t.jsp

# vim /usr/local/tomcat/webapps/ROOT/t.jsp

<html>

    <body bgcolor="green">      

    <center>     

    <%=  request.getSession().getId()  %>     

    <h1>10.43.2.134</h1> 

    <h1>port:8080</h1>  

    </center>

    </body>

</html>

# vim /usr/local/tomcat2/webapps/ROOT/t.jsp

<html>

    <body bgcolor="red">      

    <center>     

    <%=  request.getSession().getId()  %>     

    <h1>10.43.2.134</h1> 

    <h1>port:8081</h1>  

    </center>

    </body>

</html>

打开http://10.43.2.134:8080/t.jsp和http://10.43.2.134:8081/t.jsp可以看到不同的页面

wKioL1PV9SXSrIgyAABrUh0qEHU760.jpg

wKiom1PV9AuRkVGAAAByz33N1Ow224.jpg


二、针对10.43.2.135的操作

  1. nginx的安装

安装依赖包:yum -y install gcc openssl-devel pcre-devel zlib-devel 

安装nginx

useradd nginx -s /sbin/nologin

tar zxvf nginx-0.8.46.tar.gz  

cd nginx-0.8.46/  

./configure --user=nginx--group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module  && make  &&  make install

启动服务:/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf 

[root@localhost ~]# netstat -anpt|grep nginx 

tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      7920/nginx

测试nginx默认web服务器是否能正常运行

wKioL1PV9XiTjiYAAAEGMLVsjng785.jpg

设置nginx开机自动启动 

[root@rhel6u3-7 ~]# echo "/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf" >> /etc/rc.local  

[root@rhel6u3-7 ~]# cat /etc/rc.local | grep nginx 

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf 

nginx的启动脚本

#编写nginx启动、停止、重启的管理脚本,方便使用

cat /etc/init.d/nginx

#!/bin/sh 

# nginx - this script starts and stops the nginx daemon 

# chkconfig: - 85 15 

# description: Nginx is an HTTP(S) server, HTTP(S) reverse \ 

#   proxy and IMAP/POP3 proxy server 

# processname: nginx 

# config: /etc/nginx/nginx.conf 

# config: /etc/sysconfig/nginx 

# pidfile: /var/run/nginx.pid 

# Source function library. 

. /etc/rc.d/init.d/functions 

# Source networking configuration. 

. /etc/sysconfig/network 

# Check that networking is up. 

[ "$NETWORKING" = "no" ] && exit 0 

    nginx="/usr/local/nginx/sbin/nginx" 

    prog=$(basename $nginx) 

    NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf" 

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx 

    lockfile=/var/lock/subsys/nginx 

start() { 

    [ -x $nginx ] || exit 5 

    [ -f $NGINX_CONF_FILE ] || exit 6 

    echo -n $"Starting $prog: " 

    daemon $nginx -c $NGINX_CONF_FILE 

    retval=$? 

    echo 

[ $retval -eq 0 ] && touch $lockfile 

    return $retval 

stop() { 

    echo -n $"Stopping $prog: " 

    killproc $prog -QUIT 

    retval=$? 

    echo 

[ $retval -eq 0 ] && rm -f $lockfile 

    return $retval 

    killall -9 nginx 

restart() { 

    configtest || return $? 

    stop 

    sleep 1 

    start 

reload() { 

    configtest || return $? 

    echo -n $"Reloading $prog: " 

    killproc $nginx -HUP 

    RETVAL=$? 

    echo 

force_reload() { 

    restart 

configtest() { 

    $nginx -t -c $NGINX_CONF_FILE 

rh_status() { 

    status $prog 

rh_status_q() { 

    rh_status >/dev/null 2>&1 

case "$1" in 

    start) 

        rh_status_q && exit 0 

        $1 

    ;; 

    stop) 

        rh_status_q || exit 0 

        $1 

    ;; 

    restart|configtest) 

        $1 

    ;; 

    reload) 

        rh_status_q || exit 7 

        $1 

    ;; 

    force-reload) 

        force_reload 

    ;; 

    status) 

        rh_status 

    ;; 

    condrestart|try-restart) 

        rh_status_q || exit 0 

    ;; 

    *) 

        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" 

        exit 2 

esac 

[root@rhel6u3-7 init.d]# chmod 755 nginx  

[root@rhel6u3-7 init.d]# chkconfig --add nginx  

[root@rhel6u3-7 init.d]# chkconfig --level 35 nginx on   

[root@rhel6u3-7 init.d]# chkconfig --list | grep nginx 

nginx           0:off   1:off   2:off   3:on    4:off   5:on    6:off 

测试nginx脚本文件是否能够正常使用

[root@rhel6u3-7 init.d]# /etc/init.d/nginx restart 

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok 

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful 

Stopping nginx:                                            [  OK  ] 

Starting nginx:                                            [  OK  ] 

[root@rhel6u3-7 init.d]# /etc/init.d/nginx reload  

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok 

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful 

Reloading nginx:                                           [  OK  ] 

[root@rhel6u3-7 ~]# cat /usr/local/nginx/logs/nginx.pid  

15799 

[root@rhel6u3-7 ~]# kill -HUP `cat /usr/local/nginx/logs/nginx.pid`

 [root@rhel6u3-7 init.d]# /etc/init.d/nginx stop 

Stopping nginx:                                            [  OK  ] 

2.编辑nginx的主配置文件,实现对10.43.2.134上两个tomcat的代理

cat nginx.conf

user  nginx nginx;

worker_processes  1;

error_log  logs/error.log;

pid        logs/nginx.pid;

events {

    worker_connections  1024;

}


http {

    include       mime.types;

    default_type  application/octet-stream;


    log_format  main  ‘$remote_addr - $remote_user [$time_local] "$request" ‘

                      ‘$status $body_bytes_sent "$http_referer" ‘

                      ‘"$http_user_agent" "$http_x_forwarded_for"‘;

    #access_log  logs/access.log  main;

    sendfile        on;

    #tcp_nopush     on;

    #keepalive_timeout  0;

    keepalive_timeout  65;

    #gzip  on;

    upstream tomcatweb {

server 10.43.2.134:8080;

server 10.43.2.134:8081;

    }

    server {

        listen       80;

        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;


        location / {

            root   html;

            index  index.html index.htm;

   proxy_pass http://tomcatweb;

        }

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

    }

}

3.重新加载nginx服务并验证

重新加载:/etc/init.d/nginx reload 

验证:可以看到nginx已经成功代理10.43.2.134上的两个tomcat,访问http://10.43.2.135/t.jsp能够正常访问到10.43.2.134:8080/test.jsp和http://10.43.2.134:8081/test.jsp交替出现,并且session  id 刷新一次变化一次。

wKiom1PV7jqAhzZVAACslTGf15s392.jpg

wKioL1PV71SQsjeXAADEecTNV5c607.jpg

4.安装memcache

安装libevent

注:memcached是基于libevent进行事件处理的,所以我们得先安装libevent

[root@memcache src]# tar xf libevent-2.0.21-stable.tar.gz

[root@memcache src]# cd libevent-2.0.21-stable

[root@memcache libevent-2.0.21-stable]# ./configure --prefix=/usr/local/libevent

[root@memcache libevent-2.0.21-stable]# make && make install

安装memcached

[root@memcache src]# tar xf memcached-1.4.15.tar.gz

[root@memcache src]# cd memcached-1.4.15

[root@memcache memcached-1.4.15]# ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent

[root@memcache memcached-1.4.15]# make && make install

[root@memcache ~]# memcached -d -m 500 -u root -l 192.168.18.201 -c 256 -P /tmp/memcached.pid -vvv  

查看一下启动端口

root@memcache ~]# netstat -ntulp 

Active Internet connections (only servers)  

Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name

tcp        0      0 192.168.18.201:11211        0.0.0.0:*                   LISTEN      8086/memcached   

tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1026/sshd        

tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      1103/master      

tcp        0      0 127.0.0.1:6010              0.0.0.0:*                   LISTEN      1137/sshd        

tcp        0      0 127.0.0.1:6011              0.0.0.0:*                   LISTEN      8044/sshd        

tcp        0      0 :::22                       :::*                        LISTEN      1026/sshd        

tcp        0      0 ::1:25                      :::*                        LISTEN      1103/master      

tcp        0      0 ::1:6010                    :::*                        LISTEN      1137/sshd        

tcp        0      0 ::1:6011                    :::*                        LISTEN      8044/sshd        

udp        0      0 192.168.18.201:11211        0.0.0.0:*                               8086/memcached

提供SysV的startup脚本

[root@memcache ~]# vim /etc/init.d/memcached 

#!/bin/bash  

#  

# Init file for memcached  

#  

# chkconfig: - 86 14  

# description: Distributed memory caching daemon  

#  

# processname: memcached  

# config: /etc/sysconfig/memcached

. /etc/rc.d/init.d/functions

## Default variables 

PORT="11211"  

USER="root"  

MAXCONN="1024"  

CACHESIZE="64"  

OPTIONS=""

RETVAL=0 

prog="/usr/local/memcached/bin/memcached"  

desc="Distributed memory caching"  

lockfile="/var/lock/subsys/memcached"

start() { 

        echo -n $"Starting $desc (memcached): "  

        daemon $prog -d -p $PORT -u $USER -c $MAXCONN -m $CACHESIZE  $OPTIONS  

        RETVAL=$?  

        echo  

        [ $RETVAL -eq 0 ] && touch $lockfile  

        return $RETVAL  

}

stop() { 

        echo -n $"Shutting down $desc (memcached): "  

        killproc $prog  

        RETVAL=$?  

        echo  

        [ $RETVAL -eq 0 ] && rm -f $lockfile  

        return $RETVAL  

}

restart() { 

        stop  

        start  

}

reload() { 

        echo -n $"Reloading $desc ($prog): "  

        killproc $prog -HUP  

        RETVAL=$?  

        echo  

        return $RETVAL  

}

case "$1" in 

  start)  

        start  

        ;;  

  stop)  

        stop  

        ;;  

  restart)  

        restart  

        ;;  

  condrestart)  

        [ -e $lockfile ] && restart  

        RETVAL=$?  

        ;;    

  reload)  

        reload  

        ;;  

  status)  

        status $prog  

        RETVAL=$?  

        ;;  

   *)  

        echo $"Usage: $0 {start|stop|restart|condrestart|status}"  

        RETVAL=1  

esac

增加执行权限

[root@memcache ~]# chmod +x /etc/init.d/memcached

加入服务列表并设置开机自启动

[root@memcache ~]# chkconfig --add memcached 

[root@memcache ~]# chkconfig memcached on  

[root@memcache ~]# chkconfig memcached --list  

memcached          0:关闭    1:关闭    2:启用    3:启用    4:启用    5:启用    6:关闭


三 、如何实现memcached session共享

以下操作在每个tomcat上都需要执行

  1. 在$CATALINA/lib中添加如下jar包

wKioL1PV8j7jup8pAACAGsxDPKg546.jpg

jar包的下载地址:http://down.51cto.com/data/1634273

2.修改配置文件context.xml

context.xml中添加如下内容

<ManagerclassName="de.javakaffee.web.msm.MemcachedBackupSessionManager"

       memcachedNodes="n1:10.43.2.135:11211"

        sticky="false"

        sessionBackupAsync="false"

        lockingMode="none"

       requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"

       transcoderFactoryClass="de.javakaffee.web.msm.JavaSerializationTranscoderFactory"/>

3.重启tomcat服务并进行验证

再次http://10.43.2.135/t.jsp 可以看到10.43.2.134:8080/t.jsp和http://10.43.2.134:8081/t.jsp交替出现,端口变化,session  id保持不变

wKiom1PV8lmxfouRAABp8B79MzE908.jpg

wKioL1PV83PgUAVFAABriS2wm2U574.jpg





本文出自 “nagios故障” 博客,请务必保留此出处http://5250070.blog.51cto.com/5240070/1531488