mac下nginx+tomcat+memcached集群配置和共享session
首先分别安装nginx(版本1.5.8)、tomcat(版本7.0)、memcached(版本1.4.17),具体安装说明请分别参考下面的文章。
nginx安装说明 http://stephen830.iteye.com/blog/2002195
tomcat安装说明 http://stephen830.iteye.com/blog/2001939
部署多个tomcat http://stephen830.iteye.com/blog/2008789
在同一台机器上部署多个tomcat,请取消环境变量CATALINA_HOME
memcached安装说明 http://stephen830.iteye.com/blog/2009678
启动memcached ,开3个后台进程。
sudo ./memcached -p 11211 -m 64 -u root -d
sudo ./memcached -p 11311 -m 64 -u root -d
sudo ./memcached -p 11411 -m 64 -u root -d
Tomcat集群配置
Tomcat 集群配置,集群中各个结点通过共享存储在缓存Memcached中session来实现session的共享:如果有一台机器上的Tomcat服务停掉 了,对于其他对等服务器上的session数据仍然可以从Memcached缓存中读取,从而不会发生session丢失的问题。
对于满足这种配置的实现方案,可以在 https://code.google.com/p/memcached-session-manager和 https://spymemcached.googlecode.com上下载相关的jar文件。因为基于不同的序列化方案,可以有多种配置方法,下面是选择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
下载上述jar文件,拷贝到$CATALINA_HOME/lib目录下,然后需要配置$CATALINA_HOME/conf/server.xml文件。
Tomcat集群中,每个结点的$CATALINA_HOME/conf/server.xml基本配置都是相同的,不同配置内容分别如下:
- 如果在同一台机器上,要保证各个服务端口不相冲突
- <Manager>元素中failoverNodes属性值不同
tomcat-01
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat01">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="localhost" appBase="" unpackWARs="true" autoDeploy="true">
<Context path="" docBase="/PROJECT_demo/demo" debug="0" reloadable="true">
<!-- MySQL DBCP -->
<Resource name="jdbc/demo" auth="Container" type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/demo?useUnicode=true"
username="demo" password="12345678" maxActive="100" maxIdle="30"
maxWait="10000" />
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:127.0.0.1:11211,n2:127.0.0.1:11311,n3:127.0.0.1:11411"
failoverNodes="n1"
requestUriIgnorePattern=".*\.(png|gif|jpg|css|js)$"
sessionBackupAsync="false"
sessionBackupTimeout="100"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"
copyCollectionsForSerialization="false" />
</Context>
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="www.nowagame.com." suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
</Engine>
内容中的
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat01">
配置jvmRoute参数
memcachedNodes="n1:127.0.0.1:11211,n2:127.0.0.1:11311,n3:127.0.0.1:11411"
n1,n2,n3是对应的3个节点,127.0.0.1为ip,11211、11311、11411分别为memcached的端口号,与上面memcached的启动相对应。
failoverNodes="n1"
表明该tomcat的Memcached结点是n1,也就是说,如果tomcat01和n1在同一台机器上的话,session数据会优先复制到存储到Memcached结点n2、n3,这样即使n1所在的结点宕机了,n2、n3所在结点仍然存储了之前的session数据。
同样配置tomcat-02
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat02">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="localhost" appBase="" unpackWARs="true" autoDeploy="true">
<Context path="" docBase="/PROJECT_demo/demo" debug="0" reloadable="true">
<!-- MySQL DBCP -->
<Resource name="jdbc/demo" auth="Container" type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/demo?useUnicode=true"
username="demo" password="12345678" maxActive="100" maxIdle="30"
maxWait="10000" />
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:127.0.0.1:11211,n2:127.0.0.1:11311,n3:127.0.0.1:11411"
failoverNodes="n2"
requestUriIgnorePattern=".*\.(png|gif|jpg|css|js)$"
sessionBackupAsync="false"
sessionBackupTimeout="100"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"
copyCollectionsForSerialization="false" />
</Context>
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="www.nowagame.com." suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
</Engine>
配置tomcat-03
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat03">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="localhost" appBase="" unpackWARs="true" autoDeploy="true">
<Context path="" docBase="/PROJECT_demo/demo" debug="0" reloadable="true">
<!-- MySQL DBCP -->
<Resource name="jdbc/demo" auth="Container" type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/demo?useUnicode=true"
username="demo" password="12345678" maxActive="100" maxIdle="30"
maxWait="10000" />
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:127.0.0.1:11211,n2:127.0.0.1:11311,n3:127.0.0.1:11411"
failoverNodes="n3"
requestUriIgnorePattern=".*\.(png|gif|jpg|css|js)$"
sessionBackupAsync="false"
sessionBackupTimeout="100"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"
copyCollectionsForSerialization="false" />
</Context>
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="www.nowagame.com." suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
</Engine>
配置nginx.conf参数
user root owner;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include 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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream tomcat_server {
server 127.0.0.1:8080;
server 127.0.0.1:8081;
server 127.0.0.1:8082;
}
server {
listen 80;
server_name localhost;
root /PROJECT_demo/demo;
location /{
index index.jsp;
}
location ~ \.(jsp|do)?$ {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://tomcat_server;
}
location ~ .*\.(js|css|gif|jpg|jpeg|png|bmp|swf)$ {
expires 24h;
}
if (!-e $request_filename){
rewrite "^/games/game_([0-9]+)\.html$" /games/game_show.jsp?id=$1 last;
rewrite "^/games/page_([0-9]+)\.html$" /games/index.jsp?p=$1 last;
rewrite "^/games/category_([0-9]+)\.html$" /games/index.jsp?c=$1 last;
rewrite "^/games/category_([0-9]+)/page_([0-9]+)\.html$" /games/index.jsp?c=$1&p=$2 last;
rewrite "^/blogs/blog_([0-9]+)\.html$" /blogs/blog_show.jsp?id=$1 last;
rewrite "^/blogs/page_([0-9]+)\.html$" /blogs/index.jsp?p=$1 last;
rewrite "^/blogs/category_([0-9]+)\.html$" /blogs/index.jsp?c=$1 last;
rewrite "^/blogs/category_([0-9]+)/page_([0-9]+)\.html$" /blogs/index.jsp?c=$1&p=$2 last;
rewrite "^/links/category_([0-9]+)\.html$" /links/index.jsp?c=$1 last;
}
}
}
分别启动tomcat01,tomcat02,tomcat03,nginx,然后制作一个web页面,配合tomcat日志来观察session的变化情况。
标签:index,tomcat,com,nginx,mac,session,jsp,memcached From: https://blog.51cto.com/u_16160131/6475594