一.安装Tomcat
1.安装java环境
[root@tomcat1 ~]# yum install java-1.8.0-openjdk.x86_64 -y
2.安装并启动tomcat
[root@tomcat1 ~]# tar zxf apache-tomcat-9.0.93.tar.gz -C /usr/local/
[root@tomcat1 ~]# ln -s /usr/local/apache-tomcat-9.0.93/ /usr/local/tomcat
[root@tomcat1 local]# /usr/local/tomcat/bin/startup.sh
3.查看端口
[root@tomcat1 local]# netstat -antlupe | grep java
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 0 59554 32787/java
tcp6 0 0 :::8080 :::* LISTEN 0 59545 32787/java
4.测试:查看端口
二.生成tomcat的启动文件
1.
生成
tomcat
的主配置文件
[root@tomcat1 ~]# vim /usr/local/tomcat/conf/tomcat.conf
JAVA_HOME=/etc/alternatives/jre_openjdk
2.
生成启动文件
[root@tomcat1 ~]# useradd -s /sbin/nologin -M tomcat
[root@tomcat1 ~]# chown -R tomcat.tomcat /usr/local/apache-tomcat-9.0.93/
[root@tomcat1 ~]# vim /lib/systemd/system/tomcat.service
[Unit]
Description=Tomcat
#After=syslog.target network.target remote-fs.target nss-lookup.target
After=syslog.target network.target
[Service]
Type=forking
EnvironmentFile=/usr/local/tomcat/conf/tomcat.conf
ExecStart=/usr/local/tomcat/bin/startup.sh
ExecStop=/usr/local/tomcat/bin/shutdown.sh
PrivateTmp=true
User=tomcat
Group=tomcat
[Install]
WantedBy=multi-user.target
3.重启服务
[root@tomcat1 ~]# systemctl daemon-reload
[root@tomcat1 ~]# systemctl enable --now tomcat.service
三.实现tomcat中的负载均衡
1.将test.jsp文件放到 /usr/local/tomcat/webapps/ROOT/目录下
[root@tomcat1 ~]# ls
anaconda-ks.cfg apache-tomcat-9.0.93.tar.gz test.jsp
[root@tomcat1 ~]# cp test.jsp /usr/local/tomcat/webapps/ROOT/
2.nginx服务器编辑子配置文件并重启
[root@nginx ~]# vim /usr/local/nginx/conf.d/tomact.conf
upstream memcache {
server 127.0.0.1:11211;
keepalive 512;
}
upstream tomcat {
hash $cookie_JSESSIONID;
server 172.25.254.10:8080;
server 172.25.254.20:8080;
}
server {
listen *:80;
server_name www.rhel9.org;
root /data/web/html;
index index.html;
location ~ \.jsp$ {
proxy_pass http://tomcat;
}
}
3.浏览器测试www.rhel9.org/test.jsp
四.tomcat的session会话保持
1.安装tomcat服务器安装缓存软件
[root@tomcat1 ~]# yum install memcached telnet -y
2.编辑memcached文件并开启服务
[root@tomcat1 ~]# vim /etc/sysconfig/memcached
[root@tomcat1 ~]# systemctl start memcached.service
3.将spymemcached.jar、memcached-session-manage、kyro相关的jar文件都放到Tomcat的lib目录中
[root@tomcat1 ~]# ls
anaconda-ks.cfg apache-tomcat-9.0.93.tar.gz jar test.jsp
[root@tomcat1 ~]# cd jar/
[root@tomcat1 jar]# cp * /usr/local/tomcat/lib/
[root@tomcat1 jar]# ls
asm-5.2.jar minlog-1.3.1.jar
kryo-3.0.3.jar msm-kryo-serializer-2.3.2.jar
kryo-serializers-0.45.jar objenesis-2.6.jar
memcached-session-manager-2.3.2.jar reflectasm-1.11.9.jar
memcached-session-manager-tc9-2.3.2.jar spymemcached-2.12.3.jar
3.修改context.xml配置文件并重启memcacged服务
[root@tomcat1 ~]# vim /usr/local/tomcat/conf/context.xml
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:172.25.254.10:11211,n2:172.25.254.20:11211"
failoverNodes="n1" #所在主机的代号
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>
4.浏览器测试
没做缓存前当其中一台服务器挂机时,页面所保存的内容刷新后会消失,做缓存后当其中一台服务器挂机时,页面所保存的内容刷新后会一直存在。