首页 > 代码库 > tomcat-session-memcached

tomcat-session-memcached

tomcat-session-memcached

tomcatA-vm1:172.16.3.2/16
tomcatB-vm2:172.16.3.3/16
apache+mysql-vm3:172.16.3.10/16
memcached-vm4:172.16.3.20/16
测试机:172.16.255.109/16

拓扑图

一、tomcat-vm1:172.16.3.2/16
1、tomcat基础安装
    官网:tomcat.apache.org
包:
    jdk-7u9-linux-x64.rpm
    apache-tomcat-7.0.42.tar.gz
    (1)安装jdk环境
        # rpm -ivh jdk-7u9-linux-x64.rpm
        指定java路径(使用方便)
        # vim /etc/profile.d/java.sh

        JAVA_HOME=/usr/java/latest
        PATH=$JAVA_HOME/bin:$PATH
        export JAVA_HOME PATH
                ~                      
        # . /etc/profile.d/java.sh

        [root@hong jdk1.7.0_09]# java -version    运用下是否能正常使用
        java version "1.7.0_09"
        Java(TM) SE Runtime Environment (build 1.7.0_09-b05)  
        Java HotSpot(TM) 64-Bit Server VM (build 23.5-b02, mixed mode)   java模式是运行在mixed客户和服务器端

(2)安装tomcat
    # tar xf apache-tomcat-7.0.42.tar.gz -C /usr/local
    # cd /usr/local/apache-tomcat-7.0.42
    # ln -sv apache-tomcat-7.0.42/ tomcat   做个连接文件
    # vim /etc/profile.d/tomcat.sh 指定tomcat路径

    export CATALINA_HOME=/usr/local/tomcat
    export PATH=$CATALINA_HOME/bin:$PATH
    # . /etc/profile.d/tomcat.sh   
    # catalina.sh configtest    检测配置文件脚本等有没有错误。
    # catalina.sh start 启动tomcat

    测试:
http://172.16.3.2:8080 默认是监听在8080端口。
   (3)定义启动脚本 (复杂的脚本可以yum安装后查看脚本文件)
        # chkconfig --add tomcat
        # chkconfig --list tomcat
        tomcat             0:off    1:off    2:on    3:on    4:on    5:on    6:off   
        # service tomcat restart
2、修改配置文件
    # vim /usr/local/tomcat/conf/server.xml
     <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">    指定默认的站点位置

     <Context path="/test" docBase="/usr/local/tomcat/webapps/test" reloadable="true">   配置/test测试目录
       <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
        memcachedNodes="n1:172.16.3.20:11211"     指定memcached的地址和端口
        requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"   session缓存什么格式的网站
        transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"/>

    </Context>
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log." suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>
      <Host name="www.ning1.com"  appBase="/ning"   指定网站自定义的站点
            unpackWARs="true" autoDeploy="true">
        <Context path="" docBase="/ning"/>
      </Host>
3、提供测试/test下的jsp站点
    # mkdir -pv /usr/local/tomcat/webapps/test/WEB-INF/{classes,lib}
    # vim /usr/local/tomcat/webapps/test/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>
二、tomcatB-vm2:172.16.3.3/16
    1、tomcat安装和上面是一样的编译安装这里不再次说明
    2、修改配置文件:
    # vim /usr/local/tomcat/conf/server.xml
        <Host name="localhost"  appBase="webapps"
                unpackWARs="true" autoDeploy="true">
        <Context path="/test" docBase="/usr/local/tomcat/webapps/test" reloadable="true">
         <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
          memcachedNodes="n1:172.16.3.20:11211"
          requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
         transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"/>
        </Context>
            <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
                   prefix="localhost_access_log." suffix=".txt"
                   pattern="%h %l %u %t &quot;%r&quot; %s %b" />

          </Host>
           <Host name="www.ning3.com"  appBase="/ning"
                       unpackWARs="true" autoDeploy="true">
            <Context path="" docBase="/ning"/>
         </Host>
        </Engine>   
3、提供测试/test下的jsp站点
    # mkdir -pv /usr/local/tomcat/webapps/test/WEB-INF/{classes,lib}
    # vim /usr/local/tomcat/webapps/test/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>
三、apache+mysql-vm3:172.16.3.10/16
1、apache代理配置
    # yum install httpd mysql mysql-server
    # vim /etc/httpd/conf/httpd.conf (开启虚拟主机功能,再次不做声明)
     <Proxy balancer://tomcat1>                                指定代理组
        BalancerMember http://www.ning1.com:8080/ning loadfactor=1
        BalancerMember http://www.ning3.com:8080/ning loadfactor=1
        ProxySet lbmethod=byrequests               指定调度算法
    </Proxy>
    <VirtualHost *:80>                    指定第一个虚拟主机
    ServerName www.liang.com                 指定虚拟主机的server名称
    ProxyVia Off                           不在响应报文的首部添加via信息
    ProxyRequests Off                        关闭正向代理
    ProxyPreserveHost On                        开启支持tomcat虚拟主机功能
    ProxyPass / balancer://tomcat1/               指定虚拟主机的URL,(这里知道哪个的代理组)
    ProxyPassReverse /  balancer://tomcat1/
    <Proxy * >                          允许所有访问代理权限(在apache2.4后需要添加)
        Order Allow,Deny
        Allow from all
    </Proxy>
    </VirtualHost>

    <VirtualHost *:80>                   指定第二个虚拟主机
    ServerName www.hong.com
    ProxyVia Off
    ProxyRequests Off
    ProxyPreserveHost On
    ProxyPass / balancer://tomcat/
    ProxyPassReverse /  balancer://tomcat/
    <Proxy * >
        Order Allow,Deny
        Allow from all
    </Proxy>
    </VirtualHost>
    <Proxy balancer://tomcat>                       指定 代理组
        BalancerMember http://172.16.3.2:8080 loadfactor=1       指定后端tomcat和权重
        BalancerMember http://172.16.3.3:8080 loadfactor=1
        ProxySet lbmethod=byrequests          指定调度算法
    </Proxy>
2、mysql配置(主要是为了配置的网站虚拟主机www.liang.com中的网站提供)

    授权:
    > grant all on ning.* to ning@‘172.16.3.%‘ identified by ‘ning‘;
    > flush privilieges;
    > create database ning;
四:memcached-vm4:172.16.3.20/16
    # yum install memcached
    # service memcached start
    # ss -tnl (查看是否开启memcached的端口11211端口)
五、测试:
    1、http://www.liang.com

jcenter图1
    2、http://www.hong.com/test(图)

测试test图2

测试test图3

tomcat-session-memcached