(1)软件
(2)拓扑图
由于一台服务器,端口如下设计,如果是阿里云,记得把端口在安全组设置。
名称 | 监听端口 |
Apache | 80 |
tomcat1 | 8080 |
tomcat2 | 8081 |
说明,此次操作在阿里云操作。
推荐文章:
阿里云安装Apache:https://yq.aliyun.com/articles/284131
公司自己服务器安装推荐文章:https://www.cnblogs.com/JetpropelledSnake/p/9167662.html
(1)yum安装Apache,并且安装扩展模块
[root@localhost ~]# yum -y install httpd httpd-manual httpd-devel mod_ssl mod_perl mod_auth_mysql
如果显示以下内容说明已经完成安装
Installed:
httpd.x86_64 0:2.4.6-67.el7.centos.6 httpd-manual.noarch 0:2.4.6-67.el7.centos.6 mod_perl.x86_64 0:2.0.10-2.el7 mod_ssl.x86_64 1:2.4.6-67.el7.centos.6
Complete!
(2)安装路径说明
(3)操作Apache命令
(4)验证安装Apache成功与否
curl http://127.0.0.1
(1)安装JK依赖
因为JK-1.2.46 released安装依赖apxs
yum install httpd-devel
(2)JK下载与解压
官网http://tomcat.apache.org/connectors-doc/
wget https://www-eu.apache.org/dist/tomcat/tomcat-connectors/jk/tomcat-connectors-1.2.46-src.tar.gz
tar -zxvf tomcat-connectors-1.2.46-src.tar.gz
(3)JK编译与安装
到进入native目录
cd tomcat-connectors-1.2.46-src/native/
配置安装
./configure --with-apxs=/usr/bin/apxs
编译及安装
make && make install
(4)将mod_jk.so拷贝到Apache安装目录modules下
(1)下载
官网 https://tomcat.apache.org/download-80.cgi
wget https://www-eu.apache.org/dist/tomcat/tomcat-8/v8.5.42/bin/apache-tomcat-8.5.42.tar.gz
(2)解压
mkdir -p /opt/apps
tar -zxvf apache-tomcat-8.5.42.tar.gz -C /opt/apps
(3)重命名为tomca1,再复制一份为tomcat2
mv apache-tomcat-8.5.42 tomcat1
cp tomcat1 tomcat2
(4)测试Tomcat是否正常
因为现在tomcat1与Tomcat2是配置是一样,只要一个tomcat正常,证明Tomcat没有问题,
端口问题,后面集群配置一起修改。
(1)Tomcat配置修改
tomcat1:
打开 vim /opt/apps/tomcat1/conf/server.xml 文件,端口以及配置如下:
说明:
这个配置根据官网来说,支持Tomcat的所有虚拟主机中的群集,并共享消息传递组件。简单点老说,在虚拟主机或者局域网搭建tomcat集群可以实现session共享。
tomcat2:
打开 vim /opt/apps/tomcat2/conf/server.xml 文件,端口以及配置如下:
(2)Apache配置修改
打开Apache配置文件,在最下加上如下内容,vim /etc/httpd/conf/httpd.conf,
# Load mod_jk module
LoadModule jk_module modules/mod_jk.so
# Specify jk log file.
JkLogFile /var/log/mod_jk.log
# Specify jk log level [debug/error/info]
JkLogLevel info
# Specify workers.properties, this file tell jk:
# how many nodes and where they are.
JkWorkersFile conf/workers.properties
# Specify which requests should handled by which node.
JkMount /* controller
在 /etc/httpd/conf/目录下新建workers.properties文件,内容如下
#文件中配置的jvmRout的属性值是一致的!
worker.list = controller,tomcat1,tomcat2
#========tomcat1========
worker.tomcat1.port=6009 #ajp13 端口号,在tomcat下server.xml配置,默认8009
worker.tomcat1.host=localhost #tomcat的主机地址,如不为本机,请填写ip地址
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor = 1 #server的加权比重,值越高,分得的请求越多
#========tomcat2========
worker.tomcat2.port=7009 #ajp13 端口号,在tomcat下server.xml配置,默认8009
worker.tomcat2.host=localhost #tomcat的主机地址,如不为本机,请填写ip地址
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor = 1 #server的加权比重,值越高,分得的请求越多
#========controller,负载均衡控制器========
worker.controller.type=lb
worker.controller.balance_workers=tomcat1,tomcat2 #指定分担请求的tomcat,旧版本中的balanced_workers,已不再推荐使用!
worker.controller.sticky_session=false
#worker.controller.sticky_session_force=true
#sticky_session指定是否应将具有SESSION ID的请求路由回创建会话的同一Tomcat实例。当Tomcat使用会话管理器时,您可以将sticky_session设置为 false,该会话管理器可以跨Tomcat的多个实例共享会话数据 - 或者如果您的应用程序是无状态的。默认情况下,sticky_session设置为true
#sticky_session_force指定是否应拒绝具有用于处于错误状态的工作程序的SESSION ID的请求。如果sticky_session_force设置为true 且与该SESSION ID匹配的worker处于错误状态,则客户端将收到500(服务器错误)。如果设置为false,则会在其他工作程序上发出故障转移,并且会丢失客户端会话 仅当您设置sticky_session = true时才使用此指令
配置可以参考
https://tomcat.apache.org/connectors-doc/common_howto/loadbalancers.html
https://tomcat.apache.org/connectors-doc/reference/workers.html
(3)session共享测试
创建简单的maven项目
index.jsp内容
<%@ page contentType="text/html; charset=GBK"%>
<%@ page import="java.util.*"%>
Cluster App Test
Server Info:
<%
out.println(request.getLocalAddr() + " : " + request.getLocalPort() + "
");
%>
<%
out.println("
ID " + session.getId() + "
");
// 如果有新的 Session 属性设置
String dataName = request.getParameter("dataName");
if (dataName != null && dataName.length() > 0) {
String dataValue = request.getParameter("dataValue");
session.setAttribute(dataName, dataValue);
}
out.println("Session 列表
");
System.out.println("============================");
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);
}
%>
web.xml内容:必须加
(4)把apache-session-demo.war部署到tomcat1与tomcat2上
tomcat1:
tomcat2:
(5)启动服务
启动Apache
启动tomcat1
启动tomcat2
(6)访问
停止tomcat2
项目依旧能访问,证明集群生效。
到目前为止已经实现Apache+tomcat集群以及session共享。
(1)Redis安装
请参考https://blog.csdn.net/u012965203/article/details/90144745
(2)把redisson-all-3.6.3.jar与redisson-tomcat-8-3.6.3.jar放到tomcat8的lib目录下
Apache+Tomca8+Redis实现session共享tomcat8依赖jar.zip
tomcat8.zip
tomcat1
tomcat2
(3)把redisson.conf配置文件放到tomcat8应用conf目录下
redisson.conf内容:
单点redis配置
{
"singleServerConfig":{
"idleConnectionTimeout":10000,
"pingTimeout":1000,
"connectTimeout":10000,
"timeout":3000,
"retryAttempts":3,
"retryInterval":1500,
"reconnectionTimeout":3000,
"failedAttempts":3,
"password":null,
"subscriptionsPerConnection":5,
"clientName":null,
"address": "redis://127.0.0.1:6379",
"subscriptionConnectionMinimumIdleSize":1,
"subscriptionConnectionPoolSize":50,
"connectionMinimumIdleSize":32,
"connectionPoolSize":64,
"database":0,
"dnsMonitoring":false,
"dnsMonitoringInterval":5000
},
"threads":0,
"nettyThreads":0,
"codec":null,
"transportMode":"NIO"
}
集群redis配置(即你有多个redis服务)
{
"clusterServersConfig":{
"idleConnectionTimeout":10000,
"pingTimeout":1000,
"connectTimeout":10000,
"timeout":3000,
"retryAttempts":3,
"retryInterval":1500,
"reconnectionTimeout":3000,
"failedAttempts":3,
"password":null,
"subscriptionsPerConnection":5,
"clientName":null,
"loadBalancer":{
"class":"org.redisson.connection.balancer.RoundRobinLoadBalancer"
},
"slaveSubscriptionConnectionMinimumIdleSize":1,
"slaveSubscriptionConnectionPoolSize":50,
"slaveConnectionMinimumIdleSize":32,
"slaveConnectionPoolSize":64,
"masterConnectionMinimumIdleSize":32,
"masterConnectionPoolSize":64,
"readMode":"SLAVE",
"nodeAddresses":[
"redis://127.0.0.1:7004",
"redis://127.0.0.1:7001",
"redis://127.0.0.1:7000"
],
"scanInterval":1000
},
"threads":0,
"nettyThreads": 0,
"codec":null,
"transportMode":"NIO"
}
tomcat1
tomcat2
(4)修改tomcat应用conf下的context.xml文件内容
tomcat1
tomcat2
(5)启动各服务
redis启动
tomcat1启动
tomcat2启动
apche启动
(6)访问
redis客户端连接
参考 https://github.com/redisson/redisson/tree/master/redisson-tomcat