内容简介:
本文介绍了,centos7+nginx+tomcat集群 环境的搭建,其中都是在vm虚拟机中进行的
1.安装nginx1.8.1
1) 下载、放置
http://nginx.org/en/download.html
- 新建一个data文件夹:mkdir /data
- 获取该文件夹的777权限:chmod -R 777 /data
- 将下载的安装包拷贝进data:mv /home/aishang/桌面/nginx-1.8.1.tar.gz /data/
- 进入目录、解压:cd /data/
tar zxvf nginx-1.8.1.tar.gz
2) 进行配置
- 安装依赖插件:
yum -y install pcre-devel
yum -y install openssl-devel
yum -y install gcc
- 配置:cd /data/nginx-1.8.1
./configure
3)编译、安装
make
make install
4)检测是否安装成功
cd /usr/local/nginx/sbin
./nginx -t
5) 启动,访问测试
./nginx
- 打开浏览器,访问 http://localhost:80
即安装成功。
6)相关命令:
cd /usr/local/nginx/sbin
pkill -9 nginx 停止
./nginx -s reload 重启
./nginx 启动
2.安装jdk
1)查看Linux自带的JDK是否已安装
rpm -qa|grep java
如果有,则用 yum -y remove 版本 卸载
3)安装 jdk1.7:
yum -y install java-1.7.0-openjdk*
3.安装tomcat7
1) 下载安装包
2) 将包放到 data目录下,直接解压
tar zvxf apache-tomcat-7.0.56.tar.gz
3)启动tomcat
sh /data/apache-tomcat-7.0.56/bin/startup.sh
4)测试
http://localhost:8080/
即安装成功。
5)相关命令:
sh /data/tomcat/bin/startup.sh 开启
sh /data/tomcat/bin/shutdown.sh 关闭
4.nginx+单个tomcat
1) 新建web项目
项目名称test,在WebContent目录下新建一个t.html,然后把项目放到tomcat下面,启动,进行访问
t.html 的内容为
<span style="font-size:14px;"><!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
这是tomcat1
</body>
</html></span>
2) 将tomcat和nginx关联起来
其实就是访问nginx,然后nginx再转发给tomcat.
这就要修改下nginx的配置文件.(注意不是nginx安装目录下的配置文件)
vim /usr/local/nginx/conf/nginx.conf
在下图标红的区域加入该语句:
proxy_pass http://localhost:8080;
保存、退出 ESC后 :wq!
3)验证是否配置成功
重启nginx,访问 http://localhost/test/t.html (注意这里端口是默认80),成功显示,如下图:
5.nginx+2个tomcat集群
1) 准备工作
首先把tomcat停掉,然后复制一份出来,改个名字,做区分,目录分别是tomcat1,tomcat2
2) 修改tomcat2 web内容
将tomcat2下面的 t.html内容修改一下,改为tomcat2
3)修改tomcat2 使用的端口
修改tomcat2占用的端口为7080;(8005,8009端口也要修改)
4) 修改ngnix的配置文件
用来做指向2个tomcat,如下图
vim /usr/local/nginx/conf/nginx.conf
增加 upstream 节点, 将2个tomcat包进去。下面的 proxy_pass 指向 upstream节点, 所以2处名称要保持一致。
nginx的负载均衡有好几种方式,常见的是轮询和ip_hash,默认的是轮询。所以我们在upstream中没有指定,就用默认的。
5)验证是否配置成功
启动2个tomcat,分别访问。
http://localhost:8080/test/t.html
http://localhost:7080/test/t.html
页面显示对应的tomcat名称。
重启nginx,访问http://localhost/test/t.html
多次刷新页面,发现 tomcat1和tomcat2 字样轮流显示, OK. 集群成功!
可以测试停掉tomcat1,或者tomcat2, nginx还能正常访问,这样就可以部署的时候不影响用户访问了.
如图,端口为8080时访问的是tomcat1,端口是7080时访问的是tomcat2,和配置的预期结果一致。
当访问80端口时(80端口为默认端口,浏览器不显示),刷新会进入不同的tomcat,因为使用的是nginx默认的轮询方式,默认权重比为2:2。
6. session复制的实现
1)、Tomat配置部分
在tomcat的server.xml配置参数据中增加session同步复制的设置
vi server.xml (增加下列代码,增加Engine后面的cluster中去)
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
channelSendOptions="8">
<Manager className="org.apache.catalina.ha.session.DeltaManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"/>
<Channel className="org.apache.catalina.tribes.group.GroupChannel">
<Membership className="org.apache.catalina.tribes.membership.McastService"
address="228.0.0.4"
port="45564"
frequency="500"
dropTime="3000"/>
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="192.168.1.104"
port="4000"
autoBind="100"
selectorTimeout="5000"
maxThreads="6"/>
<Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
<Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
</Sender>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
</Channel>
<Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
filter=""/>
<Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
<Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
tempDir="/tmp/war-temp/"
deployDir="/tmp/war-deploy/"
watchDir="/tmp/war-listen/"
watchEnabled="false"/>
<ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>
两个tomcat直接用上面代码覆盖这里即可。(注意jvmRoute 命名为 tomcat1和tomcat2)
2)、Tomat web.xml配置部分
同时需要修改tomcat中应用中的web.xml配置参数才能真正实现session同步复制的设置web.xml;
<!--....--!>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<distributable /> <!--在倒数第二行增加这个代码才能实现session同步复制功能-->
</web-app>
3)、测试页面
在webapp中建立一个test文件夹,在文件夹中建立index.jsp页面,并且建立WEB-INF和web.xml(可以拷贝conf中的,注意配置),来模拟一个应用。
index.jsp页面:
<%@page language="java"%>
<html>
<body>
<h1><font color="red">Session serviced by tomcat</font></h1>
<table aligh="center" border="1">
<tr>
<td>Session ID</td>
<td><%=session.getId() %></td>
<% session.setAttribute("abc","abc");%>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
<html>
测试时将两台tomcat都启动,访问以下index.jsp页面出现一个session ID,关闭其中一台tomcat,如果session保持一致就说明成功了。
7.最后注意:
1)关于centos防火墙
很重要的一点,防火墙会禁止session共享,
因为是内部测试,不考虑安全等因素,所以,可直接禁用防火墙:
service iptables stop #停止
chkconfig iptables off #禁用
2.)关于vm的网络共享设置
虚拟机的话,这里需要勾选,不然tomcat开启报错。