Tomcat是Apache 软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache、Sun 和其他一些公司及个人共同开发而成。由于有了Sun 的参与和支持,最新的Servlet 和JSP 规范总是能在Tomcat 中得到体现,Tomcat 5支持最新的Servlet 2.4 和JSP 2.0 规范。因为Tomcat 技术先进、性能稳定,而且免费,因而深受Java 爱好者的喜爱并得到了部分软件开发商的认可,成为目前比较流行的Web 应用服务器。
Tomcat 服务器是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP 程序的首选。实际上Tomcat是Apache 服务器的扩展,但运行时它是独立运行的,所以当你运行tomcat 时,它实际上作为一个与Apache 独立的进程单独运行的。
当配置正确时,Apache 为HTML页面服务,而Tomcat 实际上运行JSP 页面和Servlet。另外,Tomcat和IIS等Web服务器一样,具有处理HTML页面的功能,另外它还是一个Servlet和JSP容器,独立的Servlet容器是Tomcat的默认模式。不过,Tomcat处理静态HTML的能力不如Apache服务器。
我们可以通过官网查看其信息。
原理:Nginx实现对Tomcat的调度,通过sticky保持访问同一台Tomcat服务器,通过配置Tomcat实现相互使用Memcahed,并且当Memcached挂掉后,能够使用自己的Memcached,达到session保持的效果。
java serialization
msm-kryo-serializer
msm-javolution-serializer
msm-xstream-serializer
其中性能最好的序列化方案是 Kryo,此实验我们采用 kryo 方式。
操作系统:redhat6.5
selinux iptables off
百度网盘链接: https://pan.baidu.com/s/1sZ3WJhRHFrTPYP-o8-VGDA 密码: n4vy
主机名 | IP | 安装服务 | 功能说明 |
---|---|---|---|
server1 | 10.10.10.1 | Nginx、Tomcat、Memcached | 实现调度以及session保持 |
server2 | 10.10.10.2 | Tomcat、Memcached | 实现session保持 |
[root@server1 ~]# wget http://download.oracle.com/otn-pub/java/jdk/8u181-b13/96a7b8442fe848ef90c96a2fad6ed6d1/jdk-8u171-linux-x64.tar.gz
[root@server1 ~]# tar xf jdk-8u171-linux-x64.tar.gz
[root@server1 ~]# mv jdk1.8.0_171/ /usr/local/java
[root@server1 ~]# vim /etc/profile
export JAVA_HOME=/usr/local/java
export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
export PATH=$PATH:$JAVA_HOME/bin
[root@server1 ~]# . /etc/profile
[root@server1 ~]# java -version
java version "1.8.0_171"
Java(TM) SE Runtime Environment (build 1.8.0_171-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.171-b11, mixed mode)
[root@server1 ~]# scp -r /usr/local/java/ [email protected]:/usr/local/
[root@server1 ~]# scp /etc/profile [email protected]:/etc/
[root@server2 ~]# source /etc/profile
官网下载地址:https://tomcat.apache.org/download-80.cgi
[root@server1 ~]# wget http://mirrors.hust.edu.cn/apache/tomcat/tomcat-7/v7.0.90/bin/apache-tomcat-7.0.90.tar.gz
[root@server1 ~]# tar xf apache-tomcat-7.0.90.tar.gz
[root@server1 ~]# mv apache-tomcat-7.0.90 /usr/local/tomcat
[root@server1 ~]# /usr/local/tomcat/bin/startup.sh
[root@server1 tomcat]# /usr/local/tomcat/bin/shutdown.sh ###关闭
[root@server1 ~]# netstat -lntup|grep 8080 ###默认开启端口8080
tcp 0 0 :::8080 :::* LISTEN 1185/java
http://10.10.10.1:8080
[root@server1 ~]# vim /usr/local/tomcat/webapps/ROOT/test.jsp
Time: <%=new java.util.Date() %>
http://10.10.10.1:8080/test.jsp
把如下软件包放置到/usr/local/tomcat/lib目录中:
asm-3.2.jar
kryo-1.04.jar
kryo-serializers-0.10.jar
memcached-session-manager-1.6.3.jar
memcached-session-manager-tc7-1.6.3.jar
minlog-1.2.jar
msm-kryo-serializer-1.6.3.jar
reflectasm-1.01.jar
spymemcached-2.7.3.jar
[root@server1 ~]# scp -r /usr/local/tomcat/ [email protected]:/usr/local/
[root@server2 ~]# /usr/local/tomcat/bin/startup.sh
[root@server1 ~]# wget http://nginx.org/download/nginx-1.14.0.tar.gz
[root@server1 ~]# wget https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/get/master.tar.gz
[root@server1 ~]# tar xf nginx-1.14.0.tar.gz
[root@server1 ~]# tar xf master.tar.gz
[root@server1 ~]# mv nginx-goodies-nginx-sticky-module-ng-08a395c66e42/ nginx-sticky-module-ng
[root@server1 ~]# yum install -y pcre-devel gcc openssl-devel
[root@server1 ~]# useradd -s /sbin/nologin nginx
[root@server1 ~]# cd nginx-1.14.0
[root@server1 nginx-1.14.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --add-module=/root/nginx-sticky-module-ng
[root@server1 nginx-1.14.0]# make && make install
[root@server1 nginx-1.14.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@server1 ~]# vim /usr/local/nginx/conf/nginx.conf
[root@server1 ~]# nginx
http://10.10.10.1
[root@server1 ~]# vim /usr/local/tomcat/webapps/ROOT/test.jsp
server1 Time: <%=new java.util.Date() %>
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.util.*" %>
<html><head><title>Cluster App Testtitle>head>
<body>
Server Info:
<%
out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"
");%>
<%
out.println("
ID " + session.getId()+"
");
String dataName = request.getParameter("dataName");
if (dataName != null && dataName.length() > 0) {
String dataValue = request.getParameter("dataValue");
session.setAttribute(dataName, dataValue);
}
out.print("Session list");
Enumeration e = session.getAttributeNames();
while (e.hasMoreElements()) {
String name = (String)e.nextElement();
String value = session.getAttribute(name).toString();
out.println( name + " = " + value+"
");
System.out.println( name + " = " + value);
}
%>
<form action="test.jsp" method="POST">
name:<input type=text size=20 name="dataName">
<br>
key:<input type=text size=20 name="dataValue">
<br>
<input type=submit>
form>
body>
html>
[root@server2 ~]# vim /usr/local/tomcat/webapps/ROOT/test.jsp
server2 Time: <%=new java.util.Date() %>
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.util.*" %>
<html><head><title>Cluster App Testtitle>head>
<body>
Server Info:
<%
out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"
");%>
<%
out.println("
ID " + session.getId()+"
");
String dataName = request.getParameter("dataName");
if (dataName != null && dataName.length() > 0) {
String dataValue = request.getParameter("dataValue");
session.setAttribute(dataName, dataValue);
}
out.print("Session list");
Enumeration e = session.getAttributeNames();
while (e.hasMoreElements()) {
String name = (String)e.nextElement();
String value = session.getAttribute(name).toString();
out.println( name + " = " + value+"
");
System.out.println( name + " = " + value);
}
%>
<form action="test.jsp" method="POST">
name:<input type=text size=20 name="dataName">
<br>
key:<input type=text size=20 name="dataValue">
<br>
<input type=submit>
form>
body>
html>
如果删除sticky,会访问不同的服务器,加入后会话保持成功!!!
[root@server1 ~]# yum install -y memcached
[root@server1 ~]# /etc/init.d/memcached restart
[root@server1 ~]# netstat -lntup|grep 11211
tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN 7063/memcached
tcp 0 0 :::11211 :::* LISTEN 7063/memcached
udp 0 0 0.0.0.0:11211 0.0.0.0:* 7063/memcached
udp 0 0 :::11211 :::* 7063/memcached
实现功能:把Tomcat和Memcached联系起来!!!
[root@server1 ~]# vim /usr/local/tomcat/conf/context.xml ###写在context中
...
"de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="server1:10.10.10.1:11211,server2:10.10.10.2:11211"
failoverNodes="server1"
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>
[root@server2 ~]# vim /usr/local/tomcat/conf/context.xml
...
"de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="server1:10.10.10.1:11211,server2:10.10.10.2:11211"
failoverNodes="server2"
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>
[root@server1 ~]# /usr/local/tomcat/bin/shutdown.sh
[root@server1 ~]# /usr/local/tomcat/bin/startup.sh
可以发现Tomcat和Memcached已经连接起来!!!
[root@server2 ~]# cat /usr/local/tomcat/logs/catalina.out
我们可以发现server1可以使用server2和Memcached,当停止server2的Memcahed时,session不会中断,server1会自动连接到自己的Memcached!!!
[root@server2 ~]# /etc/init.d/memcached stop
[root@server1 ~]# cat /usr/local/tomcat/logs/catalina.out
14-Aug-2018 23:46:36.723 SEVERE [localhost-startStop-1] org.apache.catalina.core.StandardContext.startInternal The session manager failed to start
org.apache.catalina.LifecycleException: Failed to start component [de.javakaffee.web.msm.MemcachedBackupSessionManager[/docs]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:167)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5273)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:754)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:730)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1140)
at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1875)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.AbstractMethodError: de.javakaffee.web.msm.MemcachedBackupSessionManager.getContainer()Lorg/apache/catalina/Container;
使用tomcat8、9上出现的,一直无法解决,后来用tomcat7可以工作!!!