设为首页 收藏本站
查看: 957|回复: 0

[经验分享] Nginx+Redis+Tomcat实现session共享的集群

[复制链接]

尚未签到

发表于 2018-11-5 11:22:47 | 显示全部楼层 |阅读模式
  Nginx 作为目前最流行的开源反向代理HTTP Server,用于实现资源缓存、web server负载均衡等功能,由于其轻量级、高性
能、高可靠等特点在互联网项目中有着非常普遍的应用,相关概念网上有丰富的介绍。分布式web server集群部署后需要实现session共享,针
对 tomcat 服务器的实现方案多种多样,比如 tomcat cluster session 广播、nginx IP hash策略、
nginx sticky module等方案,本文主要介绍了使用 redis 服务器进行 session 统一存储管理的共享方案。
  使 用Nginx作为Tomcat的负载平衡器,Tomcat的会话Session数据存储在Redis,能够实现0当机的7×24运营效果。因为
将会话存储 在Redis中,因此Nginx就不必配置成stick粘粘某个Tomcat方式,这样才能真正实现后台多个Tomcat负载平衡,用户请求
能够发往任何 一个tomcat主机,当我们需要部署新应用代码时,只要停止任何一台tomcat,所有当前在线用户都会导向到运行中的tomcat实
例,因为会话数据 被序列化到Redis,在线用户不会受到影响,一旦停掉的tomcat实例上线,另外其他重复部署过程。

  实验环境
IP            Roles               port  

  
node1 192.168.9.10  nginx,redis,tomcat  80,1111,8080
  

  
node2 192.168.9.70  tomcat              8080
  软件工具包
  apache-tomcat-7.0.68.tar
  redis-3.0.7.tar
  jdk-7u79-linux-x64.rpm
  nginx-1.0.15-12.el6.x86_64.rpm
  tomcat需要的jar包
  commons-logging-1.1.3.jar
  commons-pool2-2.2.jar
  jedis-2.5.2.jar
  tomcat-juli.jar
  tomcat-redis-session-manage-tomcat7.jar
  安装redis
  1. Redis 安装
  == Redis 源码安装
  — 参照源码 README
  — 默认端口 6379
  *** 安装redis之前先要确认系统已经安装了GCC和libc库 ***
wget http://download.redis.io/releases/redis-3.0.7.tar.gz  
tar xf redis-3.0.7.tar.gz
  
cd redis-3.0.7
  
make
  
# make test
  
make PREFIX=/usr/local/redis install
  == 可执行命令
redis-server    Redis服务系统  
redis-cli       Redis一个客户端管理工具
  
redis-benchmark 用来检测redis性能
  
redis-check-aof & redis-check-dump 用来处理损坏的数据文件
  
redis-sentinel -> redis-server   < soft link >
  

  
cd utils
  
./install_server.sh
  

  
^_^[15:50:34][root@master01 utils]#bash install_server.sh
  
Welcome to the redis service installer
  
This script will help you easily set up a running redis server
  

  
Please select the redis port for this instance: [6379] 1111
  
Please select the redis config file name [/etc/redis/1111.conf]
  
Selected default – /etc/redis/1111.conf
  
Please select the redis log file name [/var/log/redis_1111.log]
  
Selected default – /var/log/redis_1111.log
  
Please select the data directory for this instance [/var/lib/redis/1111]
  
Selected default – /var/lib/redis/1111
  
Please select the redis executable path [/usr/sbin/redis-server] /usr/local/redis/bin/redis-server
  
Selected config:
  
Port           : 1111
  
Config file    : /etc/redis/1111.conf
  
Log file       : /var/log/redis_1111.log
  
Data dir       : /var/lib/redis/1111
  
Executable     : /usr/local/redis/bin/redis-server
  
Cli Executable : /usr/bin/redis-cli
  
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
  
Copied /tmp/1111.conf => /etc/init.d/redis_1111
  
Installing service…
  
Successfully added to chkconfig!
  
Successfully added to runlevels 345!
  
Starting Redis server…
  
Installation successful!
  Redis 系统配置
echo "vm.overcommit_memory = 1" >> /etc/sysctl.conf  
sysctl vm.overcommit_memory=1
  
echo 511 > /proc/sys/net/core/somaxconn
  启动服务
  /etc/init.d/redis_1111 start
  配置文件可以用默认的配置,从服务器上配置文文件打开slavefo 加上主服务器的ip和端口,还有其他的参数如日志和dump.rdb保存路径可以适当修改
  FAQ:
  make时如果有类似如下报错:
  Selecting a non-default memory allocator when building Redis is done by setting
  the MALLOC environment variable. Redis is compiled and linked against libc
  malloc by default, with the exception of jemalloc being the default on Linux
  systems. This default was picked because jemalloc has proven to have fewer
  fragmentation problems than libc malloc.
  make MALLOC=libc
  安装 JDK and Tomcat
# wget -c --header "Cookie: oraclelicense=accept-securebackup-cookie" `curl -s http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html | awk -F'"' '/jdk\-7.*\-linux\-x64.rpm/{print $12;exit}'`  

  
# rpm -ivh jdk-7u79-linux-x64.rpm
  

  
# cat > /etc/profile.d/java.sh  /etc/profile.d/tomcat.sh  /etc/profile.d/gradle2.10.sh
  

  
#source /etc/profile.d/gradle2.10.sh
  安装 tomcat-redis-session-manager
git clone https://github.com/jcoleman/tomcat-redis-session-manager.git  

  
cd tomcat-redis-session-manager/
  

  
mv build.gradle build.gradle.bak
  

  
vim build.gradle
  

  
#diff -ruN build.gradle*
  

  
--- build.gradle 2016-01-04 07:07:59.000000000 +0800
  

  
+++ build.gradle.old 2016-03-18 10:48:15.620704187 +0800
  

  
@@ -44,13 +44,8 @@
  

  
   archives sourcesJar
  

  
}
  

  

  
-//signing {
  

  
-//  sign configurations.archives
  

  
-//}
  

  
-
  

  
-task copyJars(type: Copy) {
  

  
-  from configurations.runtime
  

  
-  into 'dist'
  

  
+signing {
  

  
+  sign configurations.archives
  

  
}
  

  

  
uploadArchives {
  

  
@@ -58,9 +53,9 @@
  

  
     mavenDeployer {
  

  
       beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }
  

  

  
-      //repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") {
  

  
-      //  authentication(userName: sonatypeUsername, password: sonatypePassword)
  

  
-      //}
  

  
+      repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") {
  

  
+        authentication(userName: sonatypeUsername, password: sonatypePassword)
  

  
+      }
  

  
       //repository(url: "https://oss.sonatype.org/content/repositories/snapshots") {
  

  
       //  authentication(userName: sonatypeUsername, password: sonatypePassword)
  

  
       //}
  

  
@@ -101,4 +96,4 @@
  

  
       }
  

  
     }
  

  
   }
  

  
-}
  

  
\ No newline at end of file
  

  
+}
  

  

  

  
# gradle build -x test copyJars
  
# mkdir /tmp/jar
  
# cp -a build/libs/*.jar /tmp/jar/ && cp -a dist/*.jar /tmp/jar/
  最后将 jar 包 分发到tomcat 项目中
  License:Attribution-NonCommercial-NoDerivatives 4.0 International
  本文出自 Suzf Blog。 如未注明,均为 SUZF.NET 原创。
  转载请注明出处:http://suzf.net/thread-0318-632.html



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-631050-1-1.html 上篇帖子: Linux+Nginx+Apache+Atlas+Mysql+Php+Redis 分部式部署详细版 下篇帖子: redis安装+redis集群配置+phpredis扩展安装
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表