大佬教程收集整理的这篇文章主要介绍了02 . Tomcat集群会话共享,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
https://www.cnblogs.com/you-men/tag/redis/
主机 | 操作系统 | IP地址 | 硬件/网络 |
---|---|---|---|
Nginx | CentOS7.3 | 39.108.140.0 | 1C2G / 公有云 |
tomcat-1 | CentOS7.3 | 121.36.43.2 | 1C2G / 公有云 |
tomcat-2 | CentOS7.3 | 49.233.69.195 | 1C2G / 公有云 |
redis | CentOS7.3 | 116.196.83.113 | 1C2G / 公有云 |
CentOS7.3 | 116.196.83.113 | 1C2G / 公有云 |
部署Nginx
#!/usr/bin/env bash
# Author: ZhouJian
# Mail: 18621048481@163.com
# Time: 2019-9-3
# Describe: CentOS 7 Install Nginx source Code Script
version="Nginx-1.14.2.tar.gz"
user="Nginx"
Nginx=${version%.tar*}
path=/usr/local/src/$Nginx
echo $path
if ! ping -c2 www.baidu.com &>/dev/null
then
echo "网络不通,无法安装"
exit
fi
yum install -y gcc gcc-c++ openssl-devel pcre-devel make zlib-devel wget psmisc
if [ ! -e $version ];then
wget http://Nginx.org/download/$version
fi
if ! id $user &>/dev/null
then
useradd $user -M -s /sbin/nologin
fi
if [ ! -d /var/tmp/Nginx ];then
mkdir -p /var/tmp/Nginx/{Client,proxy,fastcgi,uwsgi,scgi}
fi
tar xf $version -C /usr/local/src
cd $path
./configure \
--prefix=/usr/local/Nginx \
--user=Nginx \
--group=Nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_sub_module \
--with-http_gzip_static_module \
--with-http_auth_request_module \
--with-http_random_index_module \
--with-http_realip_module \
--http-client-body-temp-path=/var/tmp/Nginx/client \
--http-proxy-temp-path=/var/tmp/Nginx/proxy \
--http-fastcgi-temp-path=/var/tmp/Nginx/fastcgi \
--http-uwsgi-temp-path=/var/tmp/Nginx/uwsgi \
--http-scgi-temp-path=/var/tmp/Nginx/scgi \
--with-pcre \
--with-file-aio \
--with-http_secure_link_module && make && make install
if [ $? -ne 0 ];then
echo "Nginx未安装成功"
exit
fi
killall Nginx
/usr/local/Nginx/sbin/Nginx
#echo "/usr/local/Nginx/sbin/Nginx" >> /etc/rc.local
#chmod +x /etc/rc.local
#systemctl start rc-local
#systemctl enable rc-local
ss -antp |grep Nginx
vim /usr/local/Nginx/conf/Nginx.conf
worker_processes 4;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forWARDed_for"';
#blog lb by oldboy at 201303
upstream BACkend_tomcat {
#ip_hash;
server 192.168.6.241:8080 weight=1 max_fails=2 fail_timeout=10s;
server 192.168.6.242:8080 weight=1 max_fails=2 fail_timeout=10s;
#server 192.168.6.243:8080 weight=1 max_fails=2 fail_timeout=10s;
}
server {
listen 80;
server_name www.98yz.cn;
charset utf-8;
LOCATIOn / {
root html;
index index.jsp index.html index.htm;
}
LOCATIOn ~* \.(jsp|do)$ {
proxy_pass http://BACkend_tomcat;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-ForWARDed-For $proxy_add_x_forWARDed_for;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
}
}
}
安装JDK,tomcat 程序
tar xvf jdk-8u151-linux-x64.tar.gz -C /usr/local/
wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.55/bin/apache-tomcat-8.5.55.tar.gz
tar xf apache-tomcat-8.5.55.tar.gz -C /usr/local/
cd /usr/local/
mv apache-tomcat-8.5.55/ tomcat
mv jdk1.8.0_151/ jdk
按照相同方法在tomcat-2也安装
vim conf/server.xml
// 设置默认虚拟主机,并增加jvmRoute
<ENGIne name="Catalina" defaultHost="localhost" jvmRoute="tomcat-1">
// 修改默认虚拟主机,并将网站文件路径指向/web/webapp1,在host段增加context段
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Context docBase="/web/webapp1" path="" reloadable="true"/>
</Host>
// 增加文档目录与测试文件
mkdir -p /web/webapp1
cd /web/webapp1
cat index.jsp
<%@page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>tomcat-1</title>
</head>
<body>
<h1><font color="red">Session serviced by tomcat</font></h1>
<table aligh="center" border="1">
<tr>
<td>Session ID</td>
<td><%=session.getId() %></td>
<% session.setAttribute("abc","abc");%>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
tomcat-1
</body>
<html>
// 接下来我们将tomcat和Nginx都启动起来,可以发现用户访问index.jsp会一会跳转tomcat1,一会tomcat2,session还不一致
yum -y install gcc
wget http://download.redis.io/releases/redis-4.0.14.tar.gz
tar xvf redis-4.0.14.tar.gz -C /opt/
cd /opt/redis-4.0.14
编译安装
# redis的编译,只将命令文件编译,将会在当前目录生成bin目录
make && make install PREFIX=/usr/local/redis
cd ..
mv redis-4.0.14/* /usr/local/redis/
# 创建环境变量
echo 'PATH=$PATH:/usr/local/redis/src/' >> /etc/profile
source /etc/profile
# 此时在任何目录位置都可以是用redis-server等相关命令
[root@redis1 ~]# redis-
redis-benchmark redis-check-rdb redis-senTinel redis-trib.rb
redis-check-aof redis-cli redis-server
配置redis
# 设置后台启动
# 由于redis默认是前台启动,不建议使用.可以修改为后台
daemonize yes
# 禁止protected-mode yes/no(保护模式,是否只允许本地访问)
protected-mode
# 设置远程访问
# redis默认只允许本机访问,把bind修改为bind 0.0.0.0 此设置会变成允许所有远程访问,如果指定限制访问,可设置对应IP。
# bind指定是redis所在服务器网卡的IP,不指定本机网卡IP,可能导致你的redis实例无法启动
# 如果想限制IP访问,内网的话通过网络接口(网卡限定),让客户端访问固定网卡链接redis
# 如果是公网,通过iptables指定某个IP允许访问
bind 0.0.0.0
# 配置redis日志记录
# 找到logfile,默认为logfile "",改为自定义日志格式
logfile /var/log/redis_6379.log
# 把requirepass修改为123456,修改之后重启下服务
requirepass "123456"
# 不重启redis设置密码
# 在配置文件中配置requirepass的密码(当redis重启时密码依然生效)
127.0.0.1:6379> config set requirepass test123
# 查询密码
127.0.0.1:6379> config get requirepass
1) "requirepass"
2) "test123"
# 密码验证
127.0.0.1:6379> auth test123
OK
127.0.0.1:6379> set name flying
OK
127.0.0.1:6379> get name
"flying"
# 远程主机连接
# redis-cli -h redis_ip -p redis_port -a password
启动测试
# 放到后台输出,redis自带日志了,可以输出到黑洞
nohup redis-server /usr/local/redis/redis.conf &> /usr/local/redis/redis.log &
# 关闭命令
redis-cli -h 127.0.0.1 -p 6379 -a 123456 shutdown
# 注意:不建议使用 kill -9,这种方式不但不会做持久化操作,还会造成缓冲区等资源不能优雅关闭。极端情况下造成 AOF 和 复制丢失数据 的情况。
# shutdown 还有一个参数,代表是否在关闭 redis 前,生成 持久化文件,命令为 redis-cli shutdown nosave|save。
# 设置开机自启动
echo "redis-server /usr/local/redis.conf" >> /etc/rc.local
cd /usr/local/tomcat/lib/
wget https://github.com/ran-jit/tomcat-cluster-redis-session-manager/releases/download/2.0.4/tomcat-cluster-redis-session-manager.zip
unzip tomcat-cluster-redis-session-manager.zip
cp tomcat-cluster-redis-session-manager/lib/* ./
cp tomcat-cluster-redis-session-manager/conf/redis-data-cache.properties ../conf/
cat ../conf/redis-data-cache.properties
#-- redis data-cache configuration
//远端redis数据库的地址和端口
#- redis hosts ex: 127.0.0.1:6379,127.0.0.2:6379,127.0.0.2:6380,....
redis.hosts=192.168.6.244:6379
//远端redis数据库的连接密码
#- redis password (for stand-alone modE)
redis.password=pwd@123
//是否支持集群,默认的是关闭
#- set true to enable redis cluster mode
redis.cluster.enabled=false
//连接redis的那个库
#- redis database (default 0)
#redis.database=0
//连接超时时间
#- redis connection timeout (default 2000)
#redis.timeout=2000
//在这个<Context>标签里面配置
vim ../conf/context.xml
<Valve className="tomcat.request.session.redis.SessionHandlerValve" />
<Manager className="tomcat.request.session.redis.SessionManager" />
配置会话到期时间在../conf/web.xml
<session-config>
<session-timeout>60</session-timeout>
</session-config>
启动tomcat服务
[root@linux-node2 lib]# ../bin/startup.sh
以上是大佬教程为你收集整理的02 . Tomcat集群会话共享全部内容,希望文章能够帮你解决02 . Tomcat集群会话共享所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。