TOMCAT实验
- tomcat的部署
- 步骤
- tomcat的反向代理及负载均衡
- [tomcat 的session会话保持](#tomcat 的session会话保持)
tomcat的部署
准备rhel9环境
nginx 172.25.254.100
tomcat-node1 172.25.254.10
tomcat-node2 172.25.254.20
提示:下载安装包https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.93/bin/apache-tomcat-9.0.93.tar.gz xftp上传node1和node2里 要实现tomcat需要安装java环境
步骤
1.下载java环境
c
[root@tomcat-node1 ~]# dnf install java-1.8.0-openjdk.x86_64 -y
[root@tomcat-node2 ~]# dnf install java-1.8.0-openjdk.x86_64 -y
2.安装tomcat
c
[root@tomcat-node1 ~]# tar zxf apache-tomcat-9.0.93.tar.gz -C /usr/local/
[root@tomcat-node1 ~]# cd /usr/local/
[root@tomcat-node1 local]# ls
[root@tomcat-node1 local]# ln -s apache-tomcat-9.0.93 tomcat
[root@tomcat-node1 local]# ls
[root@tomcat-node1 local]# cd tomcat/
3.启动:(脚本启动)
c
[root@tomcat-node1 tomcat]# cd bin/
[root@tomcat-node1 bin]# ./startup.sh
[root@tomcat-node1 bin]# netstat -anltupe | grep java
8080
打开浏览器:172.25.254.10:端口------>172.25.254.10:8080
目录结构
目录 说明
bin 服务启动、停止等相关程序和文件
conf 配置文件
lib 库目录
logs 日志目录
webapps 应用程序,应用部署目录,相当于nginx的默认发布目录
work jsp 编译后的结果文件,建议提前预热访问
4.生成主配置文件
c
[root@tomcat-node1 tomcat]# cd /usr/local/tomcat/conf/
[root@tomcat-node1 conf]# vim tomcat.conf
> JAVA_HOME=/etc/alternatives/jre_openjkd
[root@tomcat-node1 conf]# 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
[root@tomcat-node1 conf]# vim /usr/bin/tomcat
#!/bin/bash
case $1 in
start)
/usr/local/tomcat/bin/startup.sh
;;
stop)
/usr/local/tomcat/bin/shutdown.sh
esac
把配置文件里启动命令的路径换一下
[root@tomcat-node1 conf]# vim /lib/systemd/system/tomcat.service
> ExecStart=/usr/bin/tomcat start
> ExecStop=/usr/bin/tomcat stop
[root@tomcat-node1 conf]# useradd -s /sbin/nologin -M tomcat
[root@tomcat-node1 conf]# systemctl daemon-reload
[root@tomcat-node1 conf]# ps aux | grep tomcat
[root@tomcat-node1 ~]# ls /usr/local/tomcat/ -ld
[root@tomcat-node1 ~]# chown -R tomcat.tomcat /usr/local/tomcat/
[root@tomcat-node1 ~]# systemctl enable --now tomcat
[root@tomcat-node1 ~]# ps aux | grep tomcat
继续访问浏览器172.25.254.10:8080还在
web1完成
web2copy
tomcat的反向代理及负载均衡
把test.jsp 下载到web1/root下
c
[root@tomcat-node1 ~]# cp test.jsp /usr/local/tomcat/webapps/ROOT/
在浏览器下访问:172.25.254.10:8080/test.jsp(模拟网页会话机制)
步骤
通过nginx单机反向代理
c
[root@nginx ~]# cd /usr/local/nginx/conf.d/
[root@nginx conf.d]# vim vhost.conf
upstream memcache{
server 127.0.0.1:11211;
keepalive 512;
}
server{
listen *:80;
server_name www.hhhoo.org;
root /data/web/html;
index index.html;
location /memc {
internal;
memc_connect_timeout 100s;
memc_send_timeout 100ms;
memc_read_timeout 100ms;
set $memc_key $query_string;
set $memc_exptime 300;
memc_pass memcache;
}
location ~ \.php$ {
root /data/web/php;
set $key $uri$args;
srcache_fetch GET /memc $key;
srcache_store PUT /memc $key;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~ \.jsp$ {
proxy_pass http://172.25.254.10:8080;
}
}
[root@nginx conf.d]# nginx -s reload
打开浏览器访问:http://www.hhhoo.org/test.jsp
c
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.hhhoo.org;
root /data/web/html;
index index.html;
location ~ \.jsp$ {
proxy_pass http://tomcat;
}
}
如果tomcat没了,会话内容会丢失;
tomcat 的session会话保持
tomcat可以把session复制到需要的地方(memcached);
memcached的安装与启动
memcached没有客户端
在web1和web2上装
c
[root@tomcat-node1 ~]# dnf install memcached -y
[root@tomcat-node1 ~]# vim /etc/sysconfig/memcached
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS="-l 0.0.0.0,::1"
[root@tomcat-node1 ~]# systemctl start memcached.service
[root@tomcat-node1 ~]# netstat -antlupe | grep memcached
[root@tomcat-node1 ~]# dnf install telnet -y
[root@tomcat-node1 ~]# telnet 172.25.254.10 11211
> Trying 172.25.254.10...
> Connected to 172.25.254.10.
> Escape character is '^]'.
> add key1 0 20 2
> hh
> STORED
> get key1
> VALUE key1 0 2
> hh
> quit
把jar包上传到node1、2的/root里
c
[root@tomcat-node1 jar]# cp *.jar /usr/local/tomcat/lib/
[root@tomcat-node1 jar]# cd /usr/local/tomcat/conf/
[root@tomcat-node1 conf]# ls
[root@tomcat-node1 conf]# vim context.xml
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" memcachedNodes="m1:172.25.254.10:11211,m2:172.25.254.20:11211" failoverNodes="m1" #出错了找memcached主机 requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$" transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory" />
[root@tomcat-node1 conf]# systemctl restart tomcat.service