tomcat的安装部署
安装jdk和tomcat
tar zxf jdk-7u79-linux-x64.tar.gz -C /usr/local/
tar zxf apache-tomcat-7.0.37.tar.gz -C /usr/local/
做好软连接便于访问
cd /usr/local
ln -s jdk1.7.0_79/ java
ln -s apache-tomcat-7.0.37/ tomcat
配置环境变量
vim /etc/profile
export JAVA_HOME=/usr/local/java
export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
export PATH=$PATH:$JAVA_HOME/binsource /etc/profile # 使文件生效
编写测试网页并测试
vim test.java
public class test
{public static void main(String[] args){System.out.println("Hello World1");}
}
javac test.java
java test
开启tommcat
cd /usr/local/tomcat
bin/startup.sh
打开浏览器测试
打开浏览器测试
cd /usr/local/tomcat/webapps/ROOT/
vim test.jsp
server1-The time is: <%=new java.util.Date() %>
测试
设置nginx对tomcat的负载均衡
cd /usr/local/lnmp/nginx
vim nginx.conf需要注释掉之前的php设定,并删除index.phpupstream tomcat {server 172.25.11.4:8080;server 172.25.11.5:8080;}location / {root /usr/local/tomcat/webapps/ROOT;index index.html index.htm;}location ~ \.jsp$ {proxy_pass http://tomcat;}nginx -t
nginx -s reload # 重新加载一下nginx
打开浏览器刷新测试:
再次刷新会发现时间在变
sticky(粘滞)模块
重新编写两台tomcat服务器的网页
vim /usr/local/tomcat/webapps/ROOT/test.jsp
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.util.*" %>
<html><head><title>Cluster App Test</title></head>
<body>
Server Info:
<%
out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"<br>");%>
<%
out.println("<br> ID " + session.getId()+"<br>");
String dataName = request.getParameter("dataName");
if (dataName != null && dataName.length() > 0) {
String dataValue = request.getParameter("dataValue");
session.setAttribute(dataName, dataValue);
}
out.print("<b>Session list</b>");
Enumeration e = session.getAttributeNames();
while (e.hasMoreElements()) {
String name = (String)e.nextElement();
String value = session.getAttribute(name).toString();
out.println( name + " = " + value+"<br>");
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>
进行测试
在nginx配置文件中增加tomcat模块,为了实现session共享,需要支持sticky(粘滞)模块,nginx-1.14不支持sticky,所以使用nginx-1.10版本,重新编译nginx
备份之前版本nginx的配置文件到/mnt
并删除nginx目录
cd /usr/local/lnmp/nginx/conf
cp nginx.conf /mnt/
nginx -s stop
cd /usr/local/lnmp/
rm -fr nginx/*
解压编译安装
tar zxf nginx-1.10.1.tar.gz
tar zxf nginx-sticky-module-ng.tar.gz
cd nginx-1.10.1/
./configure --prefix=/usr/local/lnmp/nginx --with-http_ssl_module --add-module=/lnmp安装包/nginx-sticky-module-ng
make && make install
切换到新编译安装好的nginx
从/mnt恢复原nginx.conf
cd /usr/local/lnmp/nginx/
cd conf/
cp /mnt/nginx.conf .
vim nginx.conf
upstream tomcat {sticky;server 172.25.0.1:8080;server 172.25.0.2:8080;}
gzip on; #开启gzip
location / {root /usr/local/tomcat/webapps/ROOT;index index.html index.htm;}location ~ \.jsp$ {proxy_pass http://tomcat;}nginx -t # 检测文件语法有问题没
nginx
测试:
再次刷新页面,访问的ID号不会变,实现了sticky
session共享(交叉存储)
在设置之前
关闭一个服务器的tomca查看网页
cd /usr/localtomcat
bin/shutdown.sh
刷新网页发现立即切换到了另外一台服务器,并且id发生变化
这样极为的不安全,且服务器发生故障时,会丢失数据,所以要设置交叉存储的方式
设置步骤:(两台服务器相同设置)
进入/usr/local/tomcat/lib目录(两台服务器都做)
将以下jar包拷贝到该目录:
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
cd /lnmp安装包/jar
cp * /usr/local/tomcat/lib
cd /usr/local/tomcat/lib/
rm -fr memcached-session-manager-tc6-1.6.3.jar
编辑/usr/local/tomcat/conf/context.xml文件,加入session共享配置
vim /usr/local/tomcat/conf/context.xml<Manager
className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:172.25.11.4:11211,n2:172.25.11.4:11211"
failoverNodes="n1" (在172.25.11.5上"n1"改为"n2")
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>
</Context>
在两台服务器安装memcached并开启
yum install memcached -y
systemctl start memcached
设置完毕测试:
关闭server4的tomcat
cd /usr/local/tomcat
bin/shutdown.sh
刷新页面:发现ID没有变
开启server4的tomcat同时关闭server5的tomcat
再次刷新页面
关闭server5的memcached
刷新页面
开启server5的memcached同时关闭server4的memcached
刷新页面
发现ID始终没有改变,即达到了交叉存储的目的