一、 安装memcached
centos:yum install memcached 即可
二、实现
我们采用Memcached管理Session,主要是memcached-session-manager开源tomcat插件改变
Tomcat原始的Session存储机制,将session的存储放到分布式缓存Memcached中,从而实现对Session的共享。
nginx与tomcat集群参考上一篇文章
我的操作是在一台服务器上启动了2个tomcat,多台的做法类似
2个tomcat分别用t1和t2表示
tomcat版本:tomcat7
1 配置tomcat7
如果要在一台服务器启动2个tomcat服务,必须修改其中一个tomcat的端口及配置
打开其中一个tomcat的conf/server.xml,修改
<Server port="8006" shutdown="SHUTDOWN">,原来是8005
<Connector port="8082" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8"/>
端口原来是8080
2 分别启动2个tomcat服务,看能否正常启动,如果正常启动,继续
3 创建一个jsp文件,叫test.jsp,内容:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
hello apache-tomcat-8080 test.jsp
sessionId:<%=session.getId()%>
<BR>
sessionIp:<%=request.getServerName()%>
<BR>
sessionPort:<%=request.getServerPort()%>
</body>
</html>
拷贝到t1(tomcat1)的ROOT/test.jsp目录
再修改成
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
hello apache-tomcat-8082 test.jsp
sessionId:<%=session.getId()%>
<BR>
sessionIp:<%=request.getServerName()%>
<BR>
sessionPort:<%=request.getServerPort()%>
</body>
</html>
拷贝到t2(tomccat2)ROOT/test.jsp
4 启动Memcached服务
/usr/bin/memcached -d -m 5 -u root -l 127.0.0.1 -p 11211 -c 256 -P /tmp/memcached.pid
5 修改2个tomcat/conf/context.xml 文件
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" memcachedNodes="n1:127.0.0.1:11211" sessionBackupAsync="false" sessionBackupTimeout="100" transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory" copyCollectionsForSerialization="false" requestUriIgnorePattern=".*.(png|gif|jpg|css|js)$"/>
(注意我使用的是tomcat7,如果是tomcat6,transcoderFactoryClass的类名称是不同的)
说明:
1.requestUriIgnorePatter:过滤图片等静态文件去触发Session备份到Memcached。
2.sessionBackupAsync:指定Session是否应该被异步保存到Memcached中。
3.backupThreadCount :用来异步保存Session的线程数。
4.sessionBackupTimeout :默认100毫秒,操作超过时间那么保存失败。
6 添加jar文件到tomcat的lib目录下,jar的下载地址:
http://download.csdn.net/detail/u013444177/8642805
7 分别重启2个tomcat,查看tail -f ./logs/catalina.out,如果后台没有报错,即OK了
8 访问http://www.xxxxx.cn/test.jsp,看页面的sessionId是否相同,
因为我配置了nginx的负载均衡,刷新第一次时,访问的是端口8080的tomcat,再刷新的
时候nginx反向代理到端口是8082的tomcat,就是说分别访问了2个tomcat下的test.jsp。
查看页面的session Id 是否相同,如果相同,就说明OK了,(敲文字敲的手腕酸了!)
参考文章 :http://blog.csdn.net/congcong68/article/details/41869203
MSM介绍:http://gong1208.iteye.com/blog/1596120
附带nginx.conf的配置:
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log;
#error_log /var/log/nginx/error.log notice;
#error_log /var/log/nginx/error.log info;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream localhost_server {
#ip_hash;
server 1.1.1.1:8080 weight=10;
server 1.1.1.1:8082 weight=10 down;
}
server {
listen 80;
server_name www.****.cn;
index index.html index.jsp;
root /home/wwwftp/ROOT;
#配置Nginx动静分离,定义的静态页面直接从Nginx发布目录读取。
location ~ .*.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$
{
root /home/wwwftp/ROOT;
index index.html index.jsp;
#expires定义用户浏览器缓存的时间为1天,如果静态页面不常更新,可以设置更长这样可以节省带宽和缓解服务器的压力
expires 1d;
}
#所有jsp、do的动态请求都交给后面的tomcat处理
location ~ (.jsp)|(.action)$
{
#tomcat地址
proxy_pass http://localhost_server;
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;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
#location / {
# proxy_connect_timeout 3;
# proxy_send_timeout 30;
# proxy_read_timeout 30;
# proxy_pass http://www.*****.cn;
# }
}
# Load config files from the /etc/nginx/conf.d directory
# The default server is in conf.d/default.conf
include /etc/nginx/conf.d/*.conf;
}
配置memcached时一定注意安全漏洞:详细参考:memcache 未授权访问漏洞