nginx+tomcat+redis(session)

session实现的方式有3种

1.利用nginx或者haproxy等负载均衡器实现源地址hash等

2.tomcat自身的session集群

3.独立的session服务器来存放

 

msm(memcached session manager)提供将Tomcat的session保持到memcached或redis的程序,可以实现高可用。
项目链接:https://github.com/magro/memcached-session-manager/wiki/SetupAndConfiguration
 
 
 

本次着重讲述redis的哨兵或集群的配置

 
 
nginx 192.168.10.30
tomcat1 192.168.10.31
tomcat2 192.168.10.32
redis1 192.168.10.33
redis2 192.168.10.34
redis3

192.168.10.35

    
 
 --》前提部署
 
所有节点时间同步,关闭防火墙等

 

 
---》安装nginx
复制代码
[root@nginx ~]# yum install nginx -y ;systemctl enable --now nginx
[root@nginx conf.d]# cat /etc/nginx/conf.d/test.conf

upstream tomcat-server {
server 192.168.10.31:8080;
server 192.168.10.32:8080;
}
server {
listen 80;
location ~* \.(jsp|do)$ {
proxy_pass http://tomcat-server;
}
}

[root@nginx ~]# nginx -s reload

复制代码

 

 
 ---》安装tomcat(2个节点都一样)
复制代码
[root@tomcat1 ~]# yum install tomcat1 -y ;systemctl enable --now tomcat1
 [root@tomcat1 ROOT]# cat index.jsp         替代默认的jsp页面,即是webapps/ROOT/index.jsp

<%@ page import="java.util.*" %>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>tomcat test</title>
</head>
<body>
<h1> tomcat website </h1>
<div>On <%=request.getServerName() %></div>
<div><%=request.getLocalAddr() + ":" + request.getLocalPort() %></div>
<div>SessionID = <span style="color:blue"><%=session.getId() %></span></div>
<%=new Date()%>
</body>
</html>

[root@tomcat1 ~]#systemctl restart tomcat

复制代码

 

---》安装redis哨兵(3个节点一样,redis3作为主节点,redis1/2作为从节点)
复制代码
[root@redis1 ~]#yum -y install redis
[root@redis1 ~]#systemctl enable --now redis redis-sentinel.service
[root@redis1 ~]#sed -i -e 's/bind 127.0.0.1/bind 0.0.0.0/'  -e " /# requirepass/a requirepass 123456"  -e "/# masterauth/a masterauth 123456"  /etc/redis.conf
[root@redis1 ~]#echo  "replicaof 192.168.10.35 6379"  >> /erc/redis.conf     在1/2节点配置,3不需要
[root@redis1 ~]#echo "sentinel monitor mymaster 192.168.10.35 6379 2" >> /etc/redis.sentinel.conf
[root@redis1 ~]#echo "sentinel auth-pass mymaster 123456"  >> /etc/redis.sentinel.conf
[root@redis1 ~]#systemctl restart  redis redis-sentinel.service


一定要确保哨兵没问题;如下

[root@redis1 etc]# redis-cli -p 26379 info |tail -n1
master0:name=mymaster,status=ok,address=192.168.10.35:6379,slaves=2,sentinels=3   这样主节点没问题,哨兵数量为3

复制代码

 

 
 
---》配置session(tomcat1/2一样)
复制代码
[root@tomcat1 ~]#wget  https://github.com/ran-jit/tomcat-cluster-redis-session-manager/releases/download/4.0/tomcat-cluster-redis-session-manager.zip
[root@tomcat1 ~]#unzip  tomcat-cluster-redis-session-manager.zip
[root@tomcat1 ~]#mv  tomcat-cluster-redis-session-manager/lib/*   /usr/local/tomcat/lib(路径会有不同)
[root@tomcat1 ~]#env "catalina.base=/usrl/local/tomcat" bash 添加系统变量
[root@tomcat1 ~]#chown tomcat.tomcat  /root/tomcat-cluster-redis-session-manager/conf/redis-data-cache.properties
[root@tomcat1 ~]#mv  /root/tomcat-cluster-redis-session-manager/conf/redis-data-cache.properties   usr/local/tomcat/conf/

[root@tomcat1 conf]# grep -Ev '^$|#' redis-data-cache.properties
redis.hosts=192.168.10.33:26379,192.168.10.34:26379,192.168.10.35:26379    哨兵,也可以写集群(端口就为6379了)
redis.password=123456      这个密码是指redis的密码
redis.cluster.enabled=false   如果部署的是集群那么开启这里  
redis.sentinel.enabled=true   哨兵
redis.sentinel.master=mymaster     默认的,如果redis的配置修改了就需要改
lb.sticky-session.enabled=false
session.persistent.policies=DEFAULT
redis.sso.timeout=0

[root@tomcat1 ~]##vim /usr/local/tomcat/conf/context.xml
<Context>
....................

<Valve className="tomcat.request.session.redis.SessionHandlerValve" />    这两行是追加进去的
<Manager className="tomcat.request.session.redis.SessionManager" />

......................

</Context>

[root@tomcat1 ~]#systemctl restart tomcat
复制代码

 

 
 
--》检验session

 

 

 

 

 
-------------------------------------------------------------------------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
posted @   好像认识你很久了  阅读(129)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示