实战案例 : Tomcat8 利用memcached1.5实现会话Cluster (同一主机sticky 模式)

案头见蠹鱼,犹胜凡俦侣。这篇文章主要讲述实战案例 : Tomcat8 利用memcached1.5实现会话Cluster (同一主机sticky 模式)相关的知识,希望能为你提供帮助。
Tomcat 利用memcached实现会话Cluster复制和高可用性。Tomcat8 和 memcached 1.5.22安装在同一台主机(也可以安装在不同主机上,通过网络通信交互数据,考虑到实验的快捷,本次就安装在同一主机上),采用sticky 模式实现。
1.理解MSM sticky工作模式 sticky工作模式:在Tomcat本机上(t1)存一份session,在后端的session共享器 (memcached或者redis)m2上(一般为交叉节点)也存放一份副本,当m2故障了,新session会被写入到t1 和 m1 上去,同时m2故障发生同时还存活在t1上的session 还会被写到m1 上去。t2接收的session同样的方式被存放和容错。

The following description shows an example for a setup with sticky sessions, with two instances of tomcat and two instances of memcached installed.

Tomcat-1 (t1) will primarily store its sessions in memcached-2 (m2) which is running on another machine (m2 is a regular node for t1). Only if m2 is not available, t1 will store its sessions in memcached-1 (m1, m1 is the failoverNode for t1). With this configuration, sessions wont be lost when machine 1 (serving t1 and m1) crashes. The following really nice ASCII art shows this setup.

< t1> < t2>
. \\ / .
.X.
. / \\ .
< m1> < m2>

2.架构拓扑及主机
IP
主机名
角色
软件
192.168.250.8
proxy.shone.cn
反向代理调度
nginx
192.168.250.58
t58.shone.cn
【实战案例 : Tomcat8 利用memcached1.5实现会话Cluster (同一主机sticky 模式)】 Tomcat 主机58
JDK、Tomcat、Memcached
192.168.250.68
t68.shone.cn
Tomcat 主机68
JDK、Tomcatt、Memcached
环境准备:
时间同步,确保NTP或Chrony服务正常运行。
防火墙规则
禁用SELinux

3.Nginx 反向代理服务器配置
# 关闭防火墙、优化CentOS、修改主机名等
[root@CentOS84-IP08 ]#hostnamectl set-hostname proxy.shone.cn
[root@CentOS84-IP08 ]#exit
[root@proxy ]#hostname
proxy.shone.cn
[root@proxy ]#timedatectl set-timezone Asia/Shanghai
[root@proxy ]#sed -i s/pool 2.centos.pool.ntp.org iburst/server ntp1.aliyun.com iburst/g /etc/chrony.conf
[root@proxy ]#systemctl enable --nowchronyd.service
# 修改本地的主机名解析
[root@proxy ]#vim /etc/hosts
[root@proxy ]#cat /etc/hosts
192.168.250.8proxy.shone.cnproxy
192.168.250.58t58.shone.cnt58
192.168.250.68t68.shone.cnt68
[root@proxy ]#
# 安装Nginx
[root@proxy ]#yum -y install nginx

# 配置Nginx
[root@proxy ]#vim /etc/nginx/nginx.conf
[root@proxy ]#cat /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events
worker_connections 1024;


http
log_formatmain$remote_addr - $remote_user [$time_local] "$request"
$status $body_bytes_sent "$http_referer"
"$http_user_agent" "$http_x_forwarded_for";

access_log/var/log/nginx/access.logmain;

sendfileon;
tcp_nopushon;
tcp_nodelayon;
keepalive_timeout65;
types_hash_max_size 2048;

include/etc/nginx/mime.types;
default_typeapplication/octet-stream;


upstream tomcat-servers
#ip_hash;
#hash $cookie_JSESSIONID
server t58.shone.cn:8080;
server t68.shone.cn:8080;



server
location ~* \\.(jsp|do)$
proxy_pass http://tomcat-servers;





# 启动Nginx服务
[root@proxy ]#systemctl enable --now nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
[root@proxy ]#systemctl reload nginx
[root@proxy ]#
[root@proxy ]#
[root@proxy ]#ss -tln
StateRecv-QSend-QLocal Address:PortPeer Address:PortProcess
LISTEN0511[::]:80[::]:*
[root@proxy ]#

4.配置后端两台服务器4.1配置基础环境
4.1.1t68主机
#### t68.shone.cn 的配置过程

[root@CentOS84-IP68 ]#cd /data/
[root@CentOS84-IP68 ]#hostnamectl set-hostname t68.shone.cn
[root@CentOS84-IP68 ]#exit
[root@t68 ]#cd /data/


# 用脚本完成时间同步,主机名修改
[root@t68 ]#vim /data/xjzth.sh
#!/bin/bash
timedatectl set-timezone Asia/Shanghai
sed -i s/pool 2.centos.pool.ntp.org iburst/server ntp1.aliyun.com iburst/g /etc/chrony.conf
systemctl enable --nowchronyd.service
systemctl restartchronyd.service
date
[root@t68 ]#
[root@t68 ]#bash xjzth.sh

#### 准备好Tomcat和JDK安装包,用脚本一键完成安装
[root@t68 ]#ll
total 151880
-rw-r--r-- 1 root root10577344 Apr 15 23:59 apache-tomcat-8.5.78.tar.gz
-rw-r--r-- 1 root root3104 Apr 15 23:59 install_tomcat2022.sh
-rw-r--r-- 1 root root 144935989 Apr 15 23:59 jdk-8u291-linux-x64.tar.gz
-rw-r--r-- 1 root root637 Apr 15 23:59 xjzth.sh
# 安装tomcat 和jdk,注意脚本中的软件包的名称要和上面准备好的完全相同
[root@t68 ]#vim /data/install_tomcat2022.sh
#!/bin/bash
DIR=`pwd`
JDK_FILE="jdk-8u291-linux-x64.tar.gz"
TOMCAT_FILE="apache-tomcat-8.5.78.tar.gz"
JDK_DIR="/usr/local"
TOMCAT_DIR="/usr/local"

color ()
RES_COL=60
MOVE_TO_COL="echo -en \\\\033[$RES_COLG"
SETCOLOR_SUCCESS="echo -en \\\\033[1; 32m"
SETCOLOR_FAILURE="echo -en \\\\033[1; 31m"
SETCOLOR_WARNING="echo -en \\\\033[1; 33m"
SETCOLOR_NORMAL="echo -en \\E[0m"
echo -n "$2" & & $MOVE_TO_COL
echo -n "["
if [ $1 = "success" -o $1 = "0" ] ; then
$SETCOLOR_SUCCESS
echo -n $"OK"
elif [ $1 = "failure" -o $1 = "1"] ; then
$SETCOLOR_FAILURE
echo -n $"FAILED"
else
$SETCOLOR_WARNING
echo -n $"WARNING"
fi
$SETCOLOR_NORMAL
echo -n "]"
echo


install_jdk()
if ![-f "$DIR/$JDK_FILE" ]; then
color 1 "$JDK_FILE 文件不存在"
exit;
elif [ -d $JDK_DIR/jdk ]; then
color 1"JDK 已经安装"
exit
else
[ -d "$JDK_DIR" ] || mkdir -pv $JDK_DIR
fi
tar xvf $DIR/$JDK_FILE-C $JDK_DIR
cd$JDK_DIR & & ln -s jdk* jdk

cat > /etc/profile.d/jdk.sh < < EOF
export java_HOME=$JDK_DIR/jdk
export JRE_HOME=\\$JAVA_HOME/jre
export CLASSPATH=\\$JAVA_HOME/lib/:\\$JRE_HOME/lib/
export PATH=\\$PATH:\\$JAVA_HOME/bin
EOF
./etc/profile.d/jdk.sh
java -version & & color 0 "JDK 安装完成" ||color 1"JDK 安装失败" ; exit;



install_tomcat()
if ! [ -f "$DIR/$TOMCAT_FILE" ]; then
color 1 "$TOMCAT_FILE 文件不存在"
exit;
elif [ -d $TOMCAT_DIR/tomcat ]; then
color 1 "TOMCAT 已经安装"
exit
else
[ -d "$TOMCAT_DIR" ] || mkdir -pv $TOMCAT_DIR
fi
tar xf $DIR/$TOMCAT_FILE -C $TOMCAT_DIR
cd$TOMCAT_DIR & & ln -s apache-tomcat-*/tomcat
echo "PATH=$TOMCAT_DIR/tomcat/bin:"$PATH > /etc/profile.d/tomcat.sh
id tomcat & > /dev/null || useradd -r -s /sbin/nologin tomcat

cat > $TOMCAT_DIR/tomcat/conf/tomcat.conf < < EOF
JAVA_HOME=$JDK_DIR/jdk
EOF

chown -R tomcat.tomcat $TOMCAT_DIR/tomcat/

cat > /lib/systemd/system/tomcat.service< < EOF
[Unit]
Description=Tomcat
#After=syslog.target network.target remote-fs.target nss-lookup.target
After=syslog.target network.target

[Service]
Type=forking
EnvironmentFile=$TOMCAT_DIR/tomcat/conf/tomcat.conf
ExecStart=$TOMCAT_DIR/tomcat/bin/startup.sh
ExecStop=$TOMCAT_DIR/tomcat/bin/shutdown.sh
RestartSec=3
PrivateTmp=true
User=tomcat
Group=tomcat

[Install]

    推荐阅读