Nginx+Tomcat8+Memcached实现负载均衡及session共享

简易拓扑图:

Nginx+Tomcat8+Memcached实现负载均衡及session共享

2> 部署Tomcat

[root@node01 ~]# ll -h ~ |egrep 'jdk|tomcat'
-rw-r--r-- 1 root root 9.3M May 4 17:40 apache-tomcat-8.5.41.tar.gz
-rw-r--r-- 1 root root 183M Jul 15 2018 jdk-8u171-linux-x64.tar.gz
[root@node01 ~]# tar zxf jdk-8u171-linux-x64.tar.gz
[root@node01 ~]# mv jdk1.8.0_171 /usr/local/
[root@node01 ~]# vim /etc/profile.d/Java.sh

JAVA_HOME=/usr/local/jdk1.8.0_171 JAVA_BIN=/usr/local/jdk1.8.0_171/bin JRE_HOME=/usr/local/jdk1.8.0_171/jre PATH=$PATH:/usr/local/jdk1.8.0_171/bin:/usr/local/jdk1.8.0_171/jre/bin CLASSPATH=/usr/local/jdk1.8.0_171/jre/lib:/usr/local/jdk1.8.0_171/lib:/usr/local/jdk1.8.0_171/jre/lib/charsets.jar

[root@node01 ~]# . /etc/profile.d/java.sh
[root@node01 ~]# java -version
[root@node01 ~]# tar zxf apache-tomcat-8.5.41.tar.gz
[root@node01 ~]# mv apache-tomcat-8.5.41 /opt/tomcat8
[root@node01 ~]# cd /opt/tomcat8/
[root@node01 ~]# > webapps/ROOT/index.jsp
[root@node01 ~]# vim webapps/ROOT/index.jsp

<html> <head> <title>web session test</title> </head> <body> <h1>10.100.1.121:8080</h1> <h2>session: <%=session.getId()%> </h2> </body> </html>

[root@node01 ~]# ./bin/startup.sh

Nginx+Tomcat8+Memcached实现负载均衡及session共享

node02和node03和node01的部署方法一致,注意index.jsp信息要区分开。

3> Nginx代理Tomcat

[root@node01 ~]# cd ~
[root@node01 ~]# yum install -y pcre pcre-devel openssl openssl-devel
[root@node01 ~]# yum install -y nginx
[root@node01 ~]# vim /etc/nginx/nginx.conf
[root@node01 ~]# systemctl start nginx
[root@node01 ~]# systemctl enable nginx

Nginx+Tomcat8+Memcached实现负载均衡及session共享

[root@node01 ~]# /usr/sbin/nginx -s reload

访问的请求会按权重轮流分发至各tomcat,但sessionID会随着页面刷新而变动。

4> 安装memcached

[root@node01 ~]# yum -y install libevent libevent-devel
[root@node01 ~]# systemctl start memcached
[root@node01 ~]# systemctl enable memcached
[root@node01 ~]# ps -ef|grep memcached
[root@node01 ~]# netstat -lnpt

5> 实现session共享

MSM(memcached session manager),MSM是一款实现Tomcat会话保持的管理组件,支持粘性和无粘性的配置,并且支持Memcached会话故障转移。
[root@node01 ~]# yum install -y git
[root@node01 ~]# git clone https://github.com/magro/memcached-session-manager.git
以下为我整理的jar包,下载拷贝到各tomcat下lib目录,可为你省去很多麻烦。
参考:https://github.com/magro/memcached-session-manager/wiki/SetupAndConfiguration#decide-which-serialization-strategy-to-use

wget wget wget wget wget wget wget wget wget wget

配置context.xml
[root@node01 ~]# cd /opt/tomcat8/conf/
[root@node01 ~]# cp context.xml context.xml.bk
[root@node01 ~]# vim context.xml

<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" memcachedNodes="n1:10.100.1.121:11211,n2:10.100.1.122:11211,n3:10.100.1.123:11211" lockingMode="auto" sticky="false" sessionBackupAsync="false" sessionBackupTimeout= "1000" copyCollectionsForSerialization="true" requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$" transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory" />

Nginx+Tomcat8+Memcached实现负载均衡及session共享

[root@node01 ~]# scp context.xml 10.100.1.122:/opt/tomcat8/conf/context.xml
[root@node01 ~]# scp context.xml 10.100.1.123:/opt/tomcat8/conf/context.xml

重启tomcat服务后检测效果,n3表示当前session保存在node03节点的mencached上,

Nginx+Tomcat8+Memcached实现负载均衡及session共享

Nginx+Tomcat8+Memcached实现负载均衡及session共享

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/1e7e97095dcf641c036147489bfbcc26.html