WEB应用服务器–Tomcat
1.安装java环境
安装 Tomcat
yum install java-1.8.0-openjdk.x86_64 -y
2.安装并启动tomcat
推荐从Apache官网下载源码包直接安装启动
wget -c https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.93/bin/apache-tomcat-9.0.93.tar.gz
[root@tomcat ~]# tar zxf apache-tomcat-9.0.91.tar.gz -C /usr/local/
[root@tomcat ~]# ln -s /usr/local/apache-tomcat-9.0.91/ /usr/local/tomcat
[root@tomcat ~]# /usr/local/tomcat/bin/startup.sh
3.查看端口
[root@tomcat ~]# netstat -antlupe | grep java
4.访问tomcat
生成tomcat的启动文件
1.生成tomcat的主配置文件
[root@tomcat ~]# vim /usr/local/tomcat/conf/tomcat.conf
JAVA_HOME=/etc/alternatives/jre_openjdk
2.生成启动文件
增加用户
[root@tomcat ~]# useradd -s /sbin/nologin -M tomcat
[root@tomcat ~]# chown -R tomcat:tomcat /usr/local/tomcat/
[root@tomcat ~]# ll -Zd /usr/local/tomcat/
drwxr-xr-x 9 tomcat tomcat ? 4096 Aug 21 11:46 /usr/local/tomcat/
[root@tomcat ~]# 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 ~]# systemctl daemon-reload
[root@tomcat ~]# systemctl enable --now tomcat
[root@tomcat ~]# netstat -launpt | grep java
或者自定义脚本
[root@tomcat ~]# vim /usr/bin/tomcat
#!/bin/bash
case $1 in
start)
/usr/local/tomcat/bin/startup.sh
;;
stop)
/usr/local/tomcat/bin/shutdown.sh
esac
chmod +x /usr/bin/tomcat
[root@tomcat ~]# 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 start
ExecStop=/usr/local/tomcat stop
PrivateTmp=true
User=tomcat
Group=tomcat
[Install]
WantedBy=multi-user.target
tomcat负载均衡实现
将test.jsp上传
[root@tomcat-node2 ~]# cp test.jsp /usr/local/tomcat/webapps/ROOT/
upstream tomcat {
server 192.168.136.10:8080;
server 192.168.136.20:8080;
}
server {
listen *:80;
server_name www.jieyu.org;
root /data/web/html;
index index.html;
location ~ \.jsp$ {
proxy_pass http://tomcat;
}
}
cookie hash
用Edge浏览器
用chrome浏览器
memcached的安装与启动
简介: Memcached 只支持能序列化的数据类型,不支持持久化,基于Key-Value的内存缓存系统memcached虽然没有像redis所具备的数据持久化功能,比如RDB和AOF都没有,但是可以通过做集群同步的方式,
让各memcached服务器的数据进行同步,从而实现数据的一致性,即保证各memcached的数据是一样的,即使有任何一台 memcached 发生故障,只要集群中有一台 memcached 可用就不会出现数据丢失,当其他memcached 重新加入到集群的时候,可以自动从有数据的memcached 当中自动获取数据并提供服务。
[root@tomcat-node1 ~]# yum install memcached
[root@tomcat-node1 ~]# vim /etc/sysconfig/memcached
[root@tomcat-node1 ~]# systemctl start memcached.service
[root@tomcat-node1 ~]# ss -lunpt | grep memcached
tcp LISTEN 0 1024 0.0.0.0:11211 0.0.0.0:* users:(("memcached",pid=2562,fd=26))
tcp LISTEN 0 1024 [::1]:11211 [::]:* users:(("memcached",pid=2562,fd=27))
session 共享服务器
将jar包导入
[root@tomcat-node1 ~]# cd jar/
[root@tomcat-node1 jar]# ls
asm-5.2.jar memcached-session-manager-tc9-2.3.2.jar reflectasm-1.11.9.jar
kryo-3.0.3.jar minlog-1.3.1.jar spymemcached-2.12.3.jar
kryo-serializers-0.45.jar msm-kryo-serializer-2.3.2.jar
memcached-session-manager-2.3.2.jar objenesis-2.6.jar
[root@tomcat-node1 jar]# cp *.jar /usr/local/tomcat/lib/
在tomcat1上的配置
[root@tomcat-node1 ~]# vim /usr/local/tomcat/conf/context.xml
<Context>
...省略...
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:192.168.136.10:11211,n2:192.168.136.20:11211"
failoverNodes="n1"
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>
</Context>
在tomcat2上的配置 反一下
systemctl restart tomcat
测试
当把tomcat2停了之后,再次访问,跳到了tomcat1上