tomcat msm

׼������

׼����̨�����, ��ΪCentOS-7-x86_64��С����װ, iptables��SELinux���ڹر�״̬, ���ú�yumԴ(base��epel). ���ÿ���, �Ա�ÿ��ʵ�����ٻָ�.


HostA
OS: CentOS-7-x86_64
hostname: 80e54d87.twoyang.com
eno16777736: 172.18.71.101/16
gateway: 172.18.0.1

HostB
OS: CentOS-7-x86_64
hostname: b9cf468b.twoyang.com
eno16777736: 172.18.71.102/16
gateway: 172.18.0.1

HostC
OS: CentOS-7-x86_64
hostname: 1f5fafa6.twoyang.com
eno16777736: 172.18.71.103/16
gateway: 172.18.0.1

�Ȳ���Memcached��MSM, ����nginx���ؾ��tomcat����ü�Ⱥ.

��Ⱥ��ʼ��

������������, ʱ��ͬ��, ͳһ����yumԴ. ��һ����ʵ���ĸ��ڵ�����������, ������ѡ��HostA.

# д�˸��ű������������
[root@80e54d87 ~]# wget https://raw.githubusercontent.com/wqiang0917/LearnInMagedu/master/shell/sshtrust.sh
# ����Ⱥ�����нڵ�IP��ַ(��������)��������������б�����
[root@80e54d87 ~]# vim sshtrust.sh
HOSTS=("172.18.71.101" "172.18.71.102" "172.18.71.103")
# �ַ���Կ, ������������.
[root@80e54d87 ~]# bash sshtrust.sh --key
# ͬ��ʱ��
[root@80e54d87 ~]# bash sshtrust.sh "ntpdate 172.18.0.1"
[root@80e54d87 ~]# bash sshtrust.sh "hwclock --systohc"
[root@80e54d87 ~]# bash sshtrust.sh date
Tue Jun  7 21:54:22 CST 2016
Tue Jun  7 21:54:22 CST 2016
Tue Jun  7 21:54:22 CST 2016

HostA

# ��װ`OpenJDK`��`Tomcat`
[root@80e54d87 ~]# yum install -y java-1.7.0-openjdk java-1.7.0-openjdk-devel tomcat tomcat-lib tomcat-admin-webapps tomcat-webapps
[root@80e54d87 ~]# echo "export JAVA_HOME=/usr" > /etc/profile.d/java.sh
[root@80e54d87 ~]# exec bash
# ����sessappӦ��, ���ṩsession������ҳ.
[root@80e54d87 ~]# mkdir -p /var/lib/tomcat/webapps/sessapp/{classes,lib,WEB-INF,META-INF}
[root@80e54d87 ~]# cat << EOF > /var/lib/tomcat/webapps/sessapp/index.jsp
<%@ page language="java" %>
<html>
  <head><title>TomcatA</title></head>
  <body>
    <h1><font color="red">TomcatA.magedu.com</font></h1>
    <table align="centre" border="1">
      <tr>
        <td>Session ID</td>
    <% session.setAttribute("magedu.com","magedu.com"); %>
        <td><%= session.getId() %></td>
      </tr>
      <tr>
        <td>Created on</td>
        <td><%= session.getCreationTime() %></td>
     </tr>
    </table>
  </body>
</html>
EOF
# ��������, ��������˿�.
[root@80e54d87 ~]# systemctl start tomcat.service 
[root@80e54d87 ~]# ss -tnl | grep "\(4000\|8009\|8080\)"
LISTEN     0      100         :::8009                    :::*                  
LISTEN     0      100         :::8080                    :::*                  
LISTEN     0      50        ::ffff:172.18.71.101:4000                    :::*

HostB

# ��װ`OpenJDK`��`Tomcat`
[root@b9cf468b ~]# yum install -y java-1.7.0-openjdk java-1.7.0-openjdk-devel tomcat tomcat-lib tomcat-admin-webapps tomcat-webapps
[root@b9cf468b ~]# echo "export JAVA_HOME=/usr" > /etc/profile.d/java.sh
[root@b9cf468b ~]# exec bash
[root@b9cf468b ~]# mkdir -p /var/lib/tomcat/webapps/sessapp/{classes,lib,WEB-INF,META-INF}
# ����sessappӦ��, ���ṩsession������ҳ.
[root@b9cf468b ~]# cat << EOF > /var/lib/tomcat/webapps/sessapp/index.jsp
<%@ page language="java" %>
<html>
  <head><title>TomcatB</title></head>
  <body>
    <h1><font color="blue">TomcatB.magedu.com</font></h1>
    <table align="centre" border="1">
      <tr>
        <td>Session ID</td>
    <% session.setAttribute("magedu.com","magedu.com"); %>
        <td><%= session.getId() %></td>
      </tr>
      <tr>
        <td>Created on</td>
        <td><%= session.getCreationTime() %></td>
     </tr>
    </table>
  </body>
</html>
EOF
# ��������, ��������˿�.
[root@b9cf468b ~]# systemctl start tomcat.service 
[root@b9cf468b ~]# ss -tnl | grep "\(4000\|8009\|8080\)"
LISTEN     0      100         :::8009                    :::*                  
LISTEN     0      100         :::8080                    :::*                  
LISTEN     0      50        ::ffff:172.18.71.101:4000                    :::*

HostC

# ���Ȳ���ʹ��curlֱ�ӷ���HostA��HostB��sessapp.
[root@1f5fafa6 ~]# curl http://172.18.71.101:8080/sessapp/

<html>
  <head><title>TomcatA</title></head>
  <body>
    <h1><font color="red">TomcatA.magedu.com</font></h1>
    <table align="centre" border="1">
      <tr>
        <td>Session ID</td>
    
        <td>4D5C16AFE6FDA767E506729B0781A0B7</td>
      </tr>
      <tr>
        <td>Created on</td>
        <td>1465284908776</td>
     </tr>
    </table>
  </body>
</html>
[root@1f5fafa6 ~]# curl http://172.18.71.102:8080/sessapp/

<html>
  <head><title>TomcatB</title></head>
  <body>
    <h1><font color="blue">TomcatB.magedu.com</font></h1>
    <table align="centre" border="1">
      <tr>
        <td>Session ID</td>
    
        <td>185E2BDA227CB166CA75B7FDC1A07BE4</td>
      </tr>
      <tr>
        <td>Created on</td>
        <td>1465284924384</td>
     </tr>
    </table>
  </body>
</html>
# ��װnginx��Ϊǰ�˵���������.
[root@1f5fafa6 ~]# yum install -y nginx
[root@1f5fafa6 ~]# vim /etc/nginx/nginx.conf
    ...
    upstream tcsrvs {
        server 172.18.71.101:8080 weight=1;
        server 172.18.71.102:8080 weight=2;
    }

    server {
        ...
        location / {
            proxy_pass http://tcsrvs;
        }
    }
    ...
# �����﷨
[root@1f5fafa6 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# ��������
[root@1f5fafa6 ~]# systemctl start nginx.service 
[root@1f5fafa6 ~]# ss -tnl | grep 80
LISTEN     0      128          *:80                       *:*                  
LISTEN     0      128         :::80                      :::*

ʹ�����������http://172.18.71.103/sessapp/, ����ˢ��ҳ��. ���Կ���ͨ��ǰ�˵�����nginx�ڰ���Ȩ���ֵ���˷�����, ����SessionIDһֱ�ڱ仯. ��ʱʹ��Memcached��MSM�洢�Ự.
ע��: ��Ҫʹ��curl������, curl����daemon���, ִ������˳���, û�а취����Ự.

Memcached��MSM

��HostA��HostB�������tomcat�����Ͼ�Ҫִ�����²���, ��HostAΪ��.

# ��װmemcached
[root@80e54d87 ~]# yum install -y memcached
[root@80e54d87 ~]# systemctl start memcached.service
[root@80e54d87 ~]# ss -tnl | grep 11211
LISTEN     0      128          *:11211                    *:*                  
LISTEN     0      128         :::11211                   :::*
# ����MSM����������tomcat�Ĺ������Ŀ¼��
[root@80e54d87 ~]# ls msm/
javolution-5.4.3.1.jar               memcached-session-manager-tc7-1.8.3.jar  spymemcached-2.11.1.jar
memcached-session-manager-1.8.3.jar  msm-javolution-serializer-1.8.3.jar
[root@80e54d87 ~]# cp msm/*.jar /usr/share/java/tomcat/
# ��������
[root@80e54d87 ~]# vim /etc/tomcat/server.xml
...
<Host ...>
    <Context path="/sessapp" docBase="/var/lib/tomcat/webapps/sessapp" reloadable="true">
      <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
        memcachedNodes="n1:172.18.71.101:11211,n2:172.18.71.102:11211"
        # ʹ��non-sticky sessionsģʽ, ����ղο������й��ڷ�ճ��session�Ľ���.
        sticky="false"
        # session����ʹ��ͬ��ģʽ
        sessionBackupAsync="false"
        requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
        transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"
      />
    </Context>
</Host>
...
# ��������
[root@80e54d87 ~]# systemctl restart tomcat.service

������֤

  • ʹ�����������http://172.18.71.103/sessapp/, ����ˢ��ҳ��.
    tomcat msm_第1张图片

���Թ۲쵽������һֱ�ڰ���Ȩ���ֵ���˷�����, ����SessionIDһֱû�б仯. ��ʱʹ�õ��DZ�ʶΪn2��memcached��Ϊ��session server. ˵��session����ȫ�ֹ���.
ע��: ��Ҫʹ��curl������, curl����daemon���, ִ������˳���, û�а취����Ự.

  • �رյ�ǰ��Ϊ��session server��HostB�ϵ�memcached, ����ˢ��ҳ��.

[root@b9cf468b ~]# systemctl stop memcached.service

tomcat msm_第2张图片
���Թ۲쵽SessionIDû�б仯, ��session server�Ѿ��л������DZ�ʶΪn1��memcached. ˵������session server�������, �ỰҲ���ᶪʧ.

���ij��� ����ǿ�IJ����� ���ͣ�����ر����˳���http://wqiang.blog.51cto.com/6074114/1795869

你可能感兴趣的:(tomcat msm)