TOMCAT+Memcached高可用实现session共享
案例环境:
必须注意软件版本号,版本号不同可能会导致实验失败
系统类型 |
IP地址 |
主机名 |
所需软件 |
Centos 7.4 1708 64bit |
192.168.100.101 |
Nginx |
nginx-1.12.2.tar.gz |
Centos 7.4 1708 64bit |
192.168.100.102 |
Tomcat1 |
apache-tomcat-7.0.54.tar.gz jdk-7u51-linux-x64.gz nfs-utils、rpcbind |
Centos 7.4 1708 64bit |
192.168.100.103 |
Tomcat2 |
apache-tomcat-7.0.54.tar.gz jdk-7u51-linux-x64.gz nfs-utils、rpcbind |
Centos 7.4 1708 64bit |
192.168.100.104 |
NFS |
nfs-utils、rpcbind、JAVA项目 |
Centos 7.4 1708 64bit |
192.168.100.105 |
MySQL |
MySQL 、项目smbms_db.sql |
Centos 7.4 1708 64bit |
192.168.100.106 |
Mem1 |
libevent-release-1.4.15-stable.tar.gz magent-0.5.tar.gz memcached-1.5.10.tar.gz keepalived |
Centos 7.4 1708 64bit |
192.168.100.107 |
Mem2 |
libevent-release-1.4.15-stable.tar.gz magent-0.5.tar.gz memcached-1.5.10.tar.gz keepalived |
Nginx反向代理服务器的配置192.168.100.101
[[email protected] ~]#yum -y install pcre-devel zlib-devel
[[email protected] ~]#useradd -M -s /sbin/nologin nginx
[[email protected] ~]#tar zxvf nginx-1.12.2.tar.gz -C /usr/src/
[[email protected] ~]# cd /usr/src/nginx-1.12.2/
[[email protected] nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
[[email protected] nginx-1.12.2]# make && make install
[[email protected] nginx-1.12.2]# cd
[[email protected] ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[[email protected] ~]#vim /usr/lib/systemd/system/nginx.service #nginx的启动脚本
[Unit]
Description=nginxapi
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=kill -s HUP $(cat /usr/local/nginx/logs/nginx.pid)
ExecStop=kill -s QUIT $(cat /usr/local/nginx/logs/nginx.pid)
PrivateTmp=Flase
[Install]
WantedBy=multi-user.target
[[email protected] ~]#yum -y remove httpd
[[email protected] ~]#systemctl start nginx
[[email protected] ~]#systemctl enable nginx
[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf
upstream tomcat{
server 192.168.100.102:8080;
server 192.168.100.103:8080;
}
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
location ~ \.(js|asp|aspx|php|jsp|do|css|jpg|png) {
proxy_pass http://tomcat;
}
[[email protected] ~]# systemctl stop nginx
[[email protected] ~]#systemctl start nginx
[[email protected] ~]# netstat -tunlp |grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4747/nginx: master
NFS服务器的部署192.168.100.104
[[email protected] ~]#yum -y install rpcbind nfs-utils
[[email protected] ~]# for I in rpcbind nfs; do systemctl enable $I; done #加入开机自启
[[email protected] ~]#vim /etc/exports
/opt/webapp 192.168.100.0/24(rw,sync,no_root_squash) #配置文件自己编写
[[email protected] ~]# mkdir /opt/webapp
[[email protected] ~]#chmod 777 /opt/webapp
[[email protected] ~]# cp -rf WebRoot/ /opt/webapp/
[[email protected] ~]#for I in rpcbind nfs; do systemctl start $I; done
[[email protected] ~]# netstat -tunlp | grep rpc
tcp 0 0 0.0.0.0:42315 0.0.0.0:* LISTEN 2998/rpc.statd
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 3296/rpcbind
tcp 0 0 0.0.0.0:20048 0.0.0.0:* LISTEN 3314/rpc.mountd
tcp6 0 0 :::111 :::* LISTEN 3296/rpcbind
tcp6 0 0 :::20048 :::* LISTEN 3314/rpc.mountd
tcp6 0 0 :::35578 :::* LISTEN 2998/rpc.statd
udp 0 0 0.0.0.0:49042 0.0.0.0:* 2998/rpc.statd
udp 0 0 127.0.0.1:659 0.0.0.0:* 2998/rpc.statd
udp 0 0 0.0.0.0:927 0.0.0.0:* 3296/rpcbind
udp 0 0 0.0.0.0:20048 0.0.0.0:* 3314/rpc.mountd
udp 0 0 0.0.0.0:111 0.0.0.0:* 3296/rpcbind
udp6 0 0 :::927 :::* 3296/rpcbind
udp6 0 0 :::49330 :::* 2998/rpc.statd
udp6 0 0 :::20048 :::* 3314/rpc.mountd
udp6 0 0 :::111 :::* 3296/rpcbind
[[email protected] ~]# showmount -e 192.168.100.104
Export list for 192.168.100.104:
/opt/webapp 192.168.100.0/24
[[email protected] ~]# vim /opt/webapp/WebRoot/WEB-INF/classes/database.properties
driver=com.mysql.jdbc.Driver
#在和mysql传递数据的过程中,使用unicode编码格式,并且字符集设置为utf-8
url=jdbc:mysql://192.168.100.105:3306/smbms?useUnicode=true&characterEncoding=utf-8
user=linux
password=123123
Memcached1服务器的部署 192.168.100.106
[[email protected] ~]# tar zxvf libevent-release-1.4.15-stable.tar.gz -C /usr/src/
[[email protected] ~]# cd /usr/src/libevent-release-1.4.15-stable/
[[email protected]mem1 libevent-release-1.4.15-stable]# ./autogen.sh
[[email protected]mem1 libevent-release-1.4.15-stable]# ./configure --prefix=/usr/local/libevent
[[email protected]mem1 libevent-release-1.4.15-stable]# make && make install
[[email protected]mem1 libevent-release-1.4.15-stable]# cd
[[email protected] ~]# ln -s /usr/local/libevent/lib/libevent* /usr/lib64/
[[email protected] ~]# tar zxvf memcached-1.5.10.tar.gz -C /usr/src/
[[email protected] ~]# cd /usr/src/memcached-1.5.10/
[[email protected]mem1 memcached-1.5.10 ~]# ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent/
[[email protected]mem1 memcached-1.5.10 ~]# make && make install
[[email protected]mem1 memcached-1.5.10 ~]# cd
[[email protected] ~]# ln -s /usr/local/memcached/bin/* /usr/local/bin/
部署master节点的magent程序,部署完成传送给slave节点;
[[email protected] ~]# ls magent-0.5.tar.gz
magent-0.5.tar.gz
[[email protected] ~]# mkdir magent
[[email protected] ~]# tar zxvf magent-0.5.tar.gz -C magent/
ketama.c
magent.c
ketama.h
Makefile
[[email protected] ~]# cd magent
[[email protected]mem1 magent]# ls
ketama.c ketama.h magent.c Makefile
[[email protected]mem1 magent]# vi ketama.h ##在文件开头添加
#ifndef SSIZE_MAX
#define SSIZE_MAX 32767
#endif
[[email protected]mem1 magent]# vi Makefile
1 LIBS = -levent -lm -L /usr/local/libevent/lib ##选项为小-L
2 INCLUDE= -I /usr/local/libevent/include ##选项为大-i
[[email protected]mem1 magent]# make
gcc -Wall -O2 -g -I /usr/local/libevent/include -c -o magent.o magent.c
gcc -Wall -O2 -g -I /usr/local/libevent/include -c -o ketama.o ketama.c
gcc -Wall -O2 -g -o magent magent.o ketama.o -levent -lm -L /usr/local/libevent/lib
[[email protected]mem1 magent]# ls
ketama.c ketama.h ketama.o magent magent.c magent.o Makefile
[[email protected]mem1 magent]# cp magent /usr/bin/
[[email protected]mem1 magent]# scp magent 192.168.100.107:/usr/bin/
[[email protected]mem1 magent]# cd
配置master节点的keepalived服务;
[[email protected] ~]# yum -y install keepalived
[[email protected] ~]# vi /etc/keepalived/keepalived.conf
global_defs {
router_id R1
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 1
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.100.250
}
}
[[email protected] ~]# vi /opt/check.sh
#!/bin/bash
while true;do
K=$(ip a|grep 192.168.100.250|wc -l)
if [ $K -ne 0 ];then
magent -u root -n 51200 -l 192.168.100.250 -p 12000 -s 192.168.100.106:11211 -b 192.168.100.107:11211
else
pkill -9 magent
fi
M=$(netstat -utpln |grep mem|wc -l)
if [ $M -eq 0 ];then
systemctl stop keepalived
fi
done
注解:magent参数详解:
-u ##指定运行用户
-n ##最大的连接数,默认为4096
-l ##小写L,magent监听的ip地址
-p ##magent监听的端口
-s ##设置memcached主缓存的ip地址和端口
-b ##设置memcached备缓存的ip地址和端口
[[email protected] ~]# chmod +x /opt/check.sh
[[email protected] ~]# memcached -u root -d -m 128m
[[email protected] ~]# netstat -utpln |grep memcached
tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN 7117/memcached
[[email protected] ~]# systemctl start keepalived
[[email protected] ~]# /opt/check.sh &> /dev/null &
[[email protected] ~]# jobs -l
[1]+ 16273 运行中 /opt/check.sh &
[[email protected] ~]# ip a|grep 192.168.100.250
inet 192.168.100.250/32 scope global eth0
[[email protected] ~]# netstat -utpln |grep magent
tcp 0 0 192.168.100.250:12000 0.0.0.0:* LISTEN 22834/magent
Memcached2服务器的部署 192.168.100.107
[[email protected] ~]# tar zxvf libevent-release-1.4.15-stable.tar.gz -C /usr/src/
[[email protected] ~]# cd /usr/src/libevent-release-1.4.15-stable/
[[email protected]mem2 libevent-release-1.4.15-stable]# ./autogen.sh
[[email protected]mem2 libevent-release-1.4.15-stable]# ./configure --prefix=/usr/local/libevent
[[email protected]mem2 libevent-release-1.4.15-stable]# make && make install
[[email protected]mem2 libevent-release-1.4.15-stable]# cd
[[email protected]2 ~]# ln -s /usr/local/libevent/lib/libevent* /usr/lib64/
[[email protected]2 ~]# tar zxvf memcached-1.5.10.tar.gz -C /usr/src/
[[email protected]2 ~]# cd /usr/src/memcached-1.5.10/
[[email protected]mem2 memcached-1.5.10 ~]# ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent/
[[email protected]mem2 memcached-1.5.10 ~]# make && make install
[[email protected]mem2 memcached-1.5.10 ~]# cd
[[email protected]2 ~]# ln -s /usr/local/memcached/bin/* /usr/local/bin/
配置slave节点的keepalived服务;
[[email protected]2 ~]# yum -y install keepalived
[[email protected]2 ~]# vi /etc/keepalived/keepalived.conf
global_defs {
router_id R2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 1
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.100.250
}
}
[[email protected]2 ~]# vi /opt/check.sh
#!/bin/bash
while true;do
K=$(ip a|grep 192.168.100.250|wc -l)
if [ $K -ne 0 ];then
memcached -u root -d -m 128m
magent -u root -n 51200 -l 192.168.100.250 -p 12000 -s 192.168.100.107:11211 -b 192.168.100.106:11211
else
pkill magent
fi
done
[[email protected]mem2 ~]# chmod +x /opt/check.sh
[[email protected]mem2 ~]# /opt/check.sh &
[1] 18066
[[email protected]mem2 ~]# jobs -l
[1]+ 18066 完成 /opt/check.sh
[[email protected]mem2 ~]# memcached -u root -d -m 128m
[[email protected]mem2 ~]# netstat -utpln |grep mem
tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN 17920/memcached
[[email protected]mem2 ~]# systemctl start keepalived
Tomcat1服务器的部署192.168.100.102
[[email protected] ~]#tar jdk-7u51-linux-x64.gz
[[email protected] ~]#mv jdk1.7.0_51/ /usr/local/java
[[email protected] ~]#cat <<END >>/etc/profile
export JAVA_HOME=/usr/local/java
export PATH=$PATH:/usr/local/java/bin
END
[[email protected] ~]#source /etc/profile
[[email protected] ~]# java -version
[[email protected] ~]#tar zxvf apache-tomcat-7.0.54.tar.gz
[[email protected] ~]# mv apache-tomcat-7.0.54 /usr/local/tomcat
kryo序列化tomcat配置
[[email protected] ~]# vim /usr/local/tomcat/conf/context.xml #在Context区域添加红色部分内容
<Context>
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->
<!-- Uncomment this to enable Comet connection tacking (provides events
on session expiration as well as webapp lifecycle) -->
<!--
<Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
-->
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:192.168.100.250:12000"
sticky="false"
requestUriIgnorePattern= ".*\.(png|gif|jpg|css|js)$"
sessionBackupAsync= "true"
sessionBackupTimeout= "1800000"
copyCollectionsForSerialization="false"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>
</Context>
在目录/usr/local/tomcat/lib/下添加
[[email protected] ~]#/usr/local/tomcat/bin/startup.sh
[[email protected] ~]#vim /usr/local/tomcat/conf/server.xml
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
[[email protected] ~]#/usr/local/tomcat/bin/shutdown.sh
[[email protected] ~]#/usr/local/tomcat/bin/startup.sh
[[email protected] ~]# netstat -tunlp | grep java
tcp6 0 0 :::8080 :::* LISTEN 2056/java
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 2056/java
tcp6 0 0 :::8009 :::* LISTEN 2056/java
[[email protected] ~]#yum -y install rpcbind nfs-utils
[[email protected] ~]#systemctl enable rpcbind
[[email protected] ~]# systemctl enable nfs
[[email protected] ~]# systemctl start rpcbind
[[email protected] ~]# systemctl start nfs
[[email protected] ~]#echo 3 >/proc/sys/vm/drop_caches
[[email protected] ~]# showmount -e 192.168.100.104
Export list for 192.168.100.104:
/opt/webapp 192.168.100.0/24
[[email protected] ~]# cat /etc/fstab | tail -1
192.168.100.104:/opt/webapp /usr/local/tomcat/wbeapps nfs defaults,_netdev 0 0 #进行挂载
[[email protected] ~]# mount -a
[[email protected] ~]# df -hT | tail -1
192.168.100.104:/opt/webapp nfs4 17G 5.7G 12G 34% /usr/local/tomcat/weapps
Tomcat2服务器的部署192.168.100.103
[[email protected] ~]#tar zxvf jdk-7u51-linux-x64.gz
[[email protected] ~]#mv jdk1.7.0_51/ /usr/local/java
[[email protected] ~]#cat <<END >>/etc/profile
export JAVA_HOME=/usr/local/java
export PATH=$PATH:/usr/local/java/bin
END
[[email protected] ~]#source /etc/profile
[[email protected] ~]# java -version
[[email protected] ~]#tar zxvf apache-tomcat-7.0.54.tar.gz
[[email protected] ~]#mv apache-tomcat-7.0.54 /usr/local/tomcat
kryo序列化tomcat配置
[[email protected] ~]# vim /usr/local/tomcat/conf/context.xml #在Context区域添加红色部分内容
<Context>
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->
<!-- Uncomment this to enable Comet connection tacking (provides events
on session expiration as well as webapp lifecycle) -->
<!--
<Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
-->
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:192.168.100.250:12000"
sticky="false"
requestUriIgnorePattern= ".*\.(png|gif|jpg|css|js)$"
sessionBackupAsync= "true"
sessionBackupTimeout= "1800000"
copyCollectionsForSerialization="false"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>
</Context>
在目录/usr/local/tomcat/lib/下添加 同tomcat1
[[email protected] ~]#/usr/local/tomcat/bin/startup.sh
[[email protected] ~]#vim /usr/local/tomcat/conf/server.xml
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
[[email protected] ~]#/usr/local/tomcat/bin/shutdown.sh
[[email protected] ~]#/usr/local/tomcat/bin/startup.sh
[[email protected] ~]# netstat -tunlp | grep java
tcp6 0 0 :::8080 :::* LISTEN 2056/java
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 2056/java
tcp6 0 0 :::8009 :::* LISTEN 2056/java
[[email protected] ~]#yum -y install rpcbind nfs-utils
[[email protected] ~]#systemctl enable rpcbind
[[email protected] ~]# systemctl enable nfs
[[email protected] ~]# systemctl start rpcbind
[[email protected] ~]# systemctl start nfs
[[email protected] ~]#echo 3 >/proc/sys/vm/drop_caches
[[email protected] ~]# showmount -e 192.168.100.104
Export list for 192.168.100.104:
/opt/webapp 192.168.100.0/24
[[email protected] ~]# cat /etc/fstab | tail -1
192.168.100.104:/opt/webapp /usr/local/tomcat/webapps nfs defaults,_netdev 0 0 #进行挂载
[[email protected] ~]# mount -a
[[email protected] ~]# df -hT | tail -1
192.168.100.104:/opt/webapp nfs4 17G 5.7G 12G 34% /usr/local/tomcat/weapps
MySQL服务器的部署
[[email protected] ~]# yum -y install mariadb-server mysql
[[email protected] ~]# systemctl start mariadb
[[email protected] ~]# mysqladmin -uroot password 123123
[[email protected] ~]# mysql -uroot -p123123 < smbms_db.sql
[[email protected] ~]# mysql -uroot -p123123
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| smbms |
| test |
+--------------------+
MariaDB [(none)]> grant all on smbms.* to 'linux'@'192.168.100.102' identified by '123123';
MariaDB [(none)]> grant all on smbms.* to 'linux'@'192.168.100.103' identified by '123123';
访问测试