tomcat+memcached配置

nginx:192.168.152.140        #前端反向代理http请求至后端
node1(apache+tomcat):192.168.152.141    #http通过ajp模块反代至tomcat
node2(apache+tomcat):192.168.152.142    #同理
memcached1:192.168.152.143        #用于保存会话,实现访问不同主机,会话统一。
memcached2:192.168.152.144

nginx

yum -y install nginx        #安装前端反代服务

vim /etc/nginx/nginx.conf

http {                
    upstream tomcatservers {        #配置后端高可用节点
        server 192.168.152.141:80 weight=1;
        server 192.168.152.142:80 weight=1;
    }

    location / {
                proxy_pass http://tomcatservers;
         }
}

node1

yum -y install java-1.7.0-openjdk java-1.7.0-openjdk-devel        #安装JDK(java development kit)

vim /etc/profile.d/java.sh        #设置JAVA环境变量

export JAVA_HOME=/usr

. /etc/profile.d/java.sh

yum -y install httpd            #安装httpd服务,通过httpd代理前端请求到tomcat

vim /etc/httpd/conf.d/proxy-ajp.conf        #配置http与tomcat连接器ajp

<VirtualHost *:80>
    ServerName test.com
    ProxyVia On
    ProxyRequests Off
    ProxyPreserveHost On
    <Proxy *>
        Require all granted
    </Proxy>
    ProxyPass / ajp://192.168.152.141:8009/          
    ProxyPa***everse / ajp://192.168.152.141:8009/
    <Location />
        Require all granted
    </Location>
</VirtualHost>

yum -y install tomcat tomcat-lib tomcat-webapps tomcat-admin-webapps            #安装tomcat

cd /var/lib/tomcat/webapps

mkdir test

cd test

mkdir classes lib WEB-INF META-INF

vim index.jsp        #配置测试页

<%@ page language="java" %>
    <html>
        <head><title>node1</title></head>
        <body>
            <h1><font color="red">node1:192.168.152.141</font></h1>
            <table align="centre" border="1">
                <tr>
                    <td>Session ID</td>
                <% session.setAttribute("test","test"); %>
                    <td><%= session.getId() %></td>
                </tr>
                <tr>
                    <td>Created on</td>
                    <td><%= session.getCreationTime() %></td>
                </tr>
            </table>
        </body>
    </html>

把实现memcached-session-manager相关jar文件拷贝到/usr/share/tomcat/lib下

javolution-5.5.1.jar
memcached-session-manager-1.9.2.jar
memcached-session-manager-tc7-1.9.2.jar
msm-javolution-serializer-1.9.2.jar
spymemcached-2.12.0.jar

cd /etc/tomcat/

vim server.xml        #配置后端会话保存至后端memcached-server

 <Engine name="Catalina" defaultHost="localhost" jvmRoute="node1">
 <Host>

    <Context path="/t" docBase="test">
                <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
                memcachedNodes="n1:192.168.152.143:11211,n2:192.168.152.144:11211"
                failoverNodes="n1"
                requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
                transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"
                />
    </Context>
 </Host>

systemctl start httpd.service

systemctl start tomcat.service

node2

yum -y install java-1.7.0-openjdk java-1.7.0-openjdk-devel

vim /etc/profile.d/java.sh

export JAVA_HOME=/usr

. /etc/profile.d/java.sh

yum -y install httpd

vim /etc/httpd/conf.d/proxy-ajp.conf

<VirtualHost *:80>
    ServerName test.com
    ProxyVia On
    ProxyRequests Off
    ProxyPreserveHost On
    <Proxy *>
        Require all granted
    </Proxy>
    ProxyPass / ajp://192.168.152.142:8009/          
    ProxyPa***everse / ajp://192.168.152.141:8009/
    <Location />
        Require all granted
    </Location>
</VirtualHost>

yum -y install tomcat tomcat-lib tomcat-webapps tomcat-admin-webapps

cd /var/lib/tomcat/webapps

mkdir test

cd test

mkdir classes lib WEB-INF META-INF

vim index.jsp

<%@ page language="java" %>
    <html>
        <head><title>node2</title></head>
        <body>
            <h1><font color="green">node2:192.168.152.142</font></h1>
            <table align="centre" border="1">
                <tr>
                    <td>Session ID</td>
                <% session.setAttribute("test","test"); %>
                    <td><%= session.getId() %></td>
                </tr>
                <tr>
                    <td>Created on</td>
                    <td><%= session.getCreationTime() %></td>
                </tr>
            </table>
        </body>
    </html>

把实现memcached-session-manager相关jar文件拷贝到/usr/share/tomcat/lib下

javolution-5.5.1.jar
memcached-session-manager-1.9.2.jar
memcached-session-manager-tc7-1.9.2.jar
msm-javolution-serializer-1.9.2.jar
spymemcached-2.12.0.jar

cd /etc/tomcat/

vim server.xml

 <Engine name="Catalina" defaultHost="localhost" jvmRoute="node2">
 <Host>

    <Context path="/t" docBase="test">
                <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
                memcachedNodes="n1:192.168.152.143:11211,n2:192.168.152.144:11211"
                failoverNodes="n1"
                requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
                transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"
                />
    </Context>

 </Host>

systemctl start httpd.service

systemctl start tomcat.service

memcached1,memcached2

yum -y install memcached

systemctl start memcached.service

确认进程监听在11211端口即可

可尝试关闭node1或node2的httpd服务,查看是否切换到另外一个节点,session-id是否变化,没有变化说明session-server配置成功,会话被保存在后端memcached服务器。关闭memcached2,查看会话是否切换到memcached1上。