Nginx+tomcat+memcached缓存共享session
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Nginx+tomcat+memcached缓存共享session相关的知识,希望对你有一定的参考价值。
nginx+tomcat+memcached缓存共享session
session 的序列化方案官方推荐的有 4 种:
1. java serialization
2. msm-kryo-serializer
3. msm-javolution-serializer
4. msm-xstream-serializer
这里我使用的是msm-javolution-serializer
基本架构
系统:CentOS 6.3
前端nginx代理:192.168.1.211
后端tomcat+memcached群:192.168.1.212,192.168.1.213
一、nginx安装配置
1、创建运行nginx的用户
[[email protected] ~]#useradd -r nginx
2、安装nginx
先安装依赖软件pcre,openssl-devel等
[[email protected] ~]#tar -xf pcre-8.37.tar.gz -C /usr/local/ #nginx编译需要的是pcre的源码
[[email protected] ~]#yum -y install openssl-devel
编译安装nginx
[[email protected] ~]#tar -xf nginx-1.9.9.tar.gz
[[email protected] ~]#cd nginx-1.9.9
[[email protected] ~]#./configure ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-http_flv_module --with-pcre=/usr/local/pcre-8.37
[[email protected] ~]#make;make install
3、配置nginx的代理
[[email protected] ~]#vim /usr/local/nginx/conf/nginx.conf
user nginx nginx;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://tomcat;
proxy_set_headerHost$host;
proxy_set_headerX-Real-IP$remote_addr;
proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
upstream tomcat {
server 192.168.1.212:8080;
server 192.168.1.213:8080;
}
}
4、启动nginx
[[email protected] ~]# /usr/local/nginx/sbin/nginx
二、tomcat安装配置
1、jdk的安装配置
[[email protected] ~]# ./jdk-8u73-linux-x64.rpm
查找jdk家目录
[[email protected] ~]#find /usr -type d -name "jdk*"
配置java(jdk)环境变量
[[email protected] ~]#vim /etc/profile.d/java.sh
export JAVA_HOME=/usr/java/jdk1.8.0_73
export PATH=$PATH:$JAVA_HOME/bin
读取变量
[[email protected] ~]# . /etc/profile.d/java.sh
确认jdk生效
[[email protected] ~]# java -version
java version "1.8.0_73"
Java(TM) SE Runtime Environment (build 1.8.0_73-b02)
Java HotSpot(TM) 64-Bit Server VM (build 25.73-b02, mixed mode)
2、tomcat的安装配置
[[email protected] ~]#tar -xf apache-tomcat-7.0.68.tar.gz -C /usr/local/
[[email protected] ~]#cd /usr/local/
[[email protected] ~]#ln -sv apache-tomcat-7.0.68 tomcat
下载msm的库文件
使用Javolution序列化框架,需要提供如下库文件:
https://memcached-session-manager.googlecode.com/files/memcached-session-manager-1.6.3.jar
https://memcached-session-manager.googlecode.com/files/memcached-session-manager-tc7-1.6.3.jar
https://memcached-session-manager.googlecode.com/files/msm-javolution-serializer-1.6.2.jar
http://memcached-session-manager.googlecode.com/svn/maven/javolution/javolution/5.4.3.1/javolution-5.4.3.1.jar
https://spymemcached.googlecode.com/files/spymemcached-2.8.4.jar
把以上5个库文件放到 /usr/local/tomcat/lib/
修改tomcat的配置文件context.xml以实现tomcat的session会话共享
[[email protected] ~]#vim /usr/local/tomcat/conf/context.xml #将下面配置复制到<Context> 和 </Context>之间,注意IP的实际修改
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:192.168.1.212:11211,n2:192.168.1.213:11211"
requestUriIgnorePattern=".*\.(png|gif|jpg|css|js)$"
sessionBackupAsync="false"
sessionBackupTimeout="100"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"
copyCollectionsForSerialization="false"
/>
三、memcached的安装配置
[[email protected] ~]#yum -y install memcached
添加用户运行memcached的用户memcache
[[email protected] ~]#useradd -r memcache
开启memcached
[[email protected] ~]#memcached -m 64 -d -u memcache -p 11211 -l 192.168.1.212 -c 1000 -P /tmp/memcached.pid
参数说明
-d选项是启动一个守护进程;
-m是分配给Memcache使用的内存数量,单位是MB,我这里是64MB;
-u是运行Memcache的用户;
-l是监听的服务器IP地址,我这里指定了服务器的本机IP地址;
-p是设置Memcache监听的端口,我这里设置了11211,最好是1024以上的端口;
-c选项是最大运行的并发连接数,默认是1024,我这里设置了1000,按照你服务器的负载量来设定,
-P是设置保存Memcache的pid文件,我这里是保存在 /tmp/memcached.pid
四、启动tomcat,查看日志确认MemcachedSessionService是否正常加载成功
[[email protected] ~]#grep ‘MemcachedSessionService‘ /usr/local/tomcat/logs/catalina.2016-03-05.log
INFO: MemcachedSessionService finished initialization, sticky true, operation timeout 1000, with node ids [n1] and failover node ids [n2]
看到以上完成初始化的信息,说明正常启动了。
五、192.168.1.212和192.168.1.213两台都分别完成以上第二至第四步操作。
六、测试
212和213分别编辑测试用的index.jsp文件
[[email protected] ~]#vim index.jsp
<%@ page contentType="text/html;charset=UTF-8" isELIgnored="false"%>
SessionID:<%=session.getId()%>
<BR>
SessionIP:<%=request.getServerName()%>
<BR>
SessionPort:<%=request.getServerPort()%>
<%
out.println("This is Tomcat Server 212");
%>
清空/usr/local/tomcat/webapps/ROOT 目录内容(注意先备份),把index.jsp放到此目录
浏览器访问192.168.1.211
出现如下内容
SessionID:37AA312466CCBD3374F5C0842B2D3A0F-n1
SessionIP:192.168.1.211
SessionPort:80 This is Tomcat Server 213
连续刷新访问,若SessionID没有变化则表示配置成功。
以上是关于Nginx+tomcat+memcached缓存共享session的主要内容,如果未能解决你的问题,请参考以下文章
redis缓存服务器(nginx+tomcat+redis+mysql实现session会话共享)
redis缓存服务器(nginx+tomcat+redis+mysql实现session会话共享)
redis缓存服务器(Nginx+Tomcat+redis+MySQL实现session会话共享)