1,附件1中为WIDNOW版本的 Memcached.EXE
2,附件2为已经成功运行了Memcached的TOMCAT,LIB中已经集成了Memcached所需要的包,SERVER.XML中已经配置了MANAGER
3,NGINX使用upstream 且不要使用HASH,否则SSSION没必要了。
http://blog.csdn.net/cdnight/article/details/18958613
以下附一个资料
http://blog.csdn.net/shirdrn/article/details/9731215
这里模拟了一个账号数据库,对用户的登录请求进行处理。
最后打成WAR包,部署到Tomcat集群中的每个结点上。
Memcached安装、配置、启动
在Ubuntu系统下,如果没有安装Memcached,可以执行如下命令进行安装:
- sudo apt-get install memcached
然后,启动Memcached服务进程,这里,我启动了两个服务进程,分别监听端口11211、11311,启动命令如下所示:
- sudo /usr/bin/memcached -m 64 -d -u memcache -p 11211 -l 192.168.1.104 -c 5000 -P /tmp/n1-memcached.pid
- sudo /usr/bin/memcached -m 64 -d -u memcache -p 11311 -l 192.168.1.104 -c 5000 -P /tmp/n2-memcached.pid
查询Memcached服务状态:
- shirdrn@dev:~/servers/cluster/nginx_tomcat_memcached$ ps -ef | grep memcached
- memcache 1578 1 0 Jan25 ? 00:00:00 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1
- memcache 3300 1 0 Jan25 ? 00:00:00 /usr/bin/memcached -m 64 -d -u memcache -p 11211 -l 192.168.1.104 -c 5000 -P /tmp/n1-memcached.pid
- memcache 3308 1 0 00:00 ? 00:00:00 /usr/bin/memcached -m 64 -d -u memcache -p 11311 -l 192.168.1.104 -c 5000 -P /tmp/n2-memcached.pid
- shirdrn 3318 3095 0 00:00 pts/5 00:00:00 grep --color=auto memcached
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服务器实例,所在目录分别为tomcat-1和tomcat-2,对应的conf/server.xml配置内容如下:
- tomcat-1/conf/server.xml
- <?xml version='1.0' encoding='utf-8'?>
- <Server port="8085" shutdown="SHUTDOWN">
- <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
- <Listener className="org.apache.catalina.core.JasperListener" />
- <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
- <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
- <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
- <GlobalNamingResources>
- <Resource name="UserDatabase" auth="Container" type="org.apache.catalina.UserDatabase"
- description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
- pathname="conf/tomcat-users.xml" />
- </GlobalNamingResources>
- <Service name="Catalina">
- <Connector port="8088" protocol="HTTP/1.1" connectionTimeout="20000"
- redirectPort="8443" />
- <Connector port="8089" protocol="AJP/1.3" redirectPort="8443" />
- <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">
- <Realm className="org.apache.catalina.realm.LockOutRealm">
- <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
- resourceName="UserDatabase" />
- </Realm>
- <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" deployOnStartup="true">
- <Context docBase="/home/shirdrn/servers/cluster/nginx_tomcat_memcached/webapps/session.war" path="/session" reloadable="true">
- <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
- memcachedNodes="n1:192.168.1.104:11211,n2:192.168.1.104:11311"
- 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="localhost_access_log." suffix=".txt"
- pattern="%h %l %u %t "%r" %s %b" />
- </Host>
- </Engine>
- </Service>
- </Server>
这里面,failover的Memcached结点是n1,也就是说,如果tomcat-1和n1在同一台机器上的话,session数据会优先复制到存储到Memcached结点n2,这样即使n1所在的结点宕机了,n2所在结点仍然存储了之前的session数据。
- tomcat-2/conf/server.xml
- <?xml version='1.0' encoding='utf-8'?>
- <Server port="9085" shutdown="SHUTDOWN">
- <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
- <Listener className="org.apache.catalina.core.JasperListener" />
- <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
- <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
- <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
- <GlobalNamingResources>
- <Resource name="UserDatabase" auth="Container" type="org.apache.catalina.UserDatabase"
- description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
- pathname="conf/tomcat-users.xml" />
- </GlobalNamingResources>
- <Service name="Catalina">
- <Connector port="9088" protocol="HTTP/1.1" connectionTimeout="20000"
- redirectPort="9443" />
- <Connector port="9089" protocol="AJP/1.3" redirectPort="9443" />
- <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">
- <Realm className="org.apache.catalina.realm.LockOutRealm">
- <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
- resourceName="UserDatabase" />
- </Realm>
- <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" deployOnStartup="true">
- <Context docBase="/home/shirdrn/servers/cluster/nginx_tomcat_memcached/webapps/session.war" path="/session" reloadable="true">
- <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
- memcachedNodes="n1:192.168.1.104:11211,n2:192.168.1.104:11311"
- 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="localhost_access_log." suffix=".txt"
- pattern="%h %l %u %t "%r" %s %b" />
- </Host>
- </Engine>
- </Service>
- </Server>
和上面类似,failover的Memcached结点是n2,也就是说,如果tomcat-2和n2在同一台机器上的话,Session数据会优先复制到存储到Memcached结点n1,这样即使n2所在的结点宕机了,n1所在结点仍然存储了之前的Session数据。
上面的配置实现了Tomcat集群中,通过Memcached实现了Sticky Session(粘性Session),主要是通过配置failover结点来达到目的的。如果采用Non-sticky Session方式,就不需要考虑failover的问题,详细配置扩参考文档(https://code.google.com/p/memcached-session-manager/wiki/SetupAndConfiguration),下面截取一个片段:
The following example shows a configuration for non-sticky sessions. In this case there's no need for failoverNodes, as sessions are served by all tomcats round-robin and they're not bound to a single tomcat. For non-sticky sessions the configuration (for both/all tomcats) would look like this: <Context> ... <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" memcachedNodes="n1:host1.yourdomain.com:11211,n2:host2.yourdomain.com:11211" sticky="false" sessionBackupAsync="false" lockingMode="uriPattern:/path1|/path2" requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$" transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory" /> </Context> |
分别启动tomcat-1和tomcat-2,如下所示:
- shirdrn@dev:~/servers/cluster/nginx_tomcat_memcached$ tomcat-1/bin/catalina.sh start
- Using CATALINA_BASE: /home/shirdrn/servers/cluster/nginx_tomcat_memcached/tomcat-1
- Using CATALINA_HOME: /home/shirdrn/servers/cluster/nginx_tomcat_memcached/tomcat-1
- Using CATALINA_TMPDIR: /home/shirdrn/servers/cluster/nginx_tomcat_memcached/tomcat-1/temp
- Using JRE_HOME: /home/hadoop/installation/jdk1.6.0_30
- Using CLASSPATH: /home/shirdrn/servers/cluster/nginx_tomcat_memcached/tomcat-1/bin/bootstrap.jar:/home/shirdrn/servers/cluster/nginx_tomcat_memcached/tomcat-1/bin/tomcat-juli.jar
- shirdrn@dev:~/servers/cluster/nginx_tomcat_memcached$ tomcat-2/bin/catalina.sh start
- Using CATALINA_BASE: /home/shirdrn/servers/cluster/nginx_tomcat_memcached/tomcat-2
- Using CATALINA_HOME: /home/shirdrn/servers/cluster/nginx_tomcat_memcached/tomcat-2
- Using CATALINA_TMPDIR: /home/shirdrn/servers/cluster/nginx_tomcat_memcached/tomcat-2/temp
- Using JRE_HOME: /home/hadoop/installation/jdk1.6.0_30
- Using CLASSPATH: /home/shirdrn/servers/cluster/nginx_tomcat_memcached/tomcat-2/bin/bootstrap.jar:/home/shirdrn/servers/cluster/nginx_tomcat_memcached/tomcat-2/bin/tomcat-juli.jar
可以查看两个tomcat的启动日志,对比一下:
- tomcat-1的日志
- shirdrn@dev:~/servers/cluster/nginx_tomcat_memcached$ tail -100f tomcat-1/logs/catalina.out
- Jan 26, 2013 12:03:25 AM org.apache.catalina.core.AprLifecycleListener init
- INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /home/hadoop/installation/jdk1.6.0_30/jre/lib/i386/client:/home/hadoop/installation/jdk1.6.0_30/jre/lib/i386:/home/hadoop/installation/jdk1.6.0_30/jre/../lib/i386:/usr/java/packages/lib/i386:/lib:/usr/lib
- Jan 26, 2013 12:03:26 AM org.apache.coyote.AbstractProtocol init
- INFO: Initializing ProtocolHandler ["http-bio-8088"]
- Jan 26, 2013 12:03:26 AM org.apache.coyote.AbstractProtocol init
- INFO: Initializing ProtocolHandler ["ajp-bio-8089"]
- Jan 26, 2013 12:03:26 AM org.apache.catalina.startup.Catalina load
- INFO: Initialization processed in 1743 ms
- Jan 26, 2013 12:03:26 AM org.apache.catalina.core.StandardService startInternal
- INFO: Starting service Catalina
- Jan 26, 2013 12:03:26 AM org.apache.catalina.core.StandardEngine startInternal
- INFO: Starting Servlet Engine: Apache Tomcat/7.0.26
- Jan 26, 2013 12:03:27 AM de.javakaffee.web.msm.MemcachedSessionService startInternal
- INFO: MemcachedSessionService starts initialization... (configured nodes definition n1:192.168.1.104:11211,n2:192.168.1.104:11311, failover nodes n1)
- 2013-01-26 00:03:27.441 INFO net.spy.memcached.MemcachedConnection: Added {QA sa=/192.168.1.104:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
- 2013-01-26 00:03:27.442 INFO net.spy.memcached.MemcachedConnection: Added {QA sa=/192.168.1.104:11311, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
- Jan 26, 2013 12:03:27 AM de.javakaffee.web.msm.RequestTrackingHostValve <init>
- INFO: Setting ignorePattern to .*\.(png|gif|jpg|css|js)$
- 2013-01-26 00:03:27.459 INFO net.spy.memcached.MemcachedConnection: Connection state changed for sun.nio.ch.SelectionKeyImpl@5b0668
- 2013-01-26 00:03:27.460 INFO net.spy.memcached.MemcachedConnection: Connection state changed for sun.nio.ch.SelectionKeyImpl@1d5a0
- Jan 26, 2013 12:03:27 AM de.javakaffee.web.msm.MemcachedSessionService setLockingMode
- INFO: Setting lockingMode to null
- Jan 26, 2013 12:03:27 AM de.javakaffee.web.msm.MemcachedSessionService createTranscoderFactory
- INFO: Creating transcoder factory de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory
- Jan 26, 2013 12:03:27 AM de.javakaffee.web.msm.MemcachedSessionService startInternal
- INFO: MemcachedSessionService finished initialization, sticky true, operation timeout 1000, with node ids [n2] and failover node ids [n1]
- Jan 26, 2013 12:03:27 AM org.apache.catalina.startup.HostConfig deployDirectory
- INFO: Deploying web application directory /home/shirdrn/servers/cluster/nginx_tomcat_memcached/tomcat-1/webapps/host-manager
- Jan 26, 2013 12:03:27 AM org.apache.catalina.startup.HostConfig deployDirectory
- INFO: Deploying web application directory /home/shirdrn/servers/cluster/nginx_tomcat_memcached/tomcat-1/webapps/manager
- Jan 26, 2013 12:03:27 AM org.apache.catalina.startup.HostConfig deployDirectory
- INFO: Deploying web application directory /home/shirdrn/servers/cluster/nginx_tomcat_memcached/tomcat-1/webapps/docs
- Jan 26, 2013 12:03:27 AM org.apache.catalina.startup.HostConfig deployDirectory
- INFO: Deploying web application directory /home/shirdrn/servers/cluster/nginx_tomcat_memcached/tomcat-1/webapps/examples
- Jan 26, 2013 12:03:28 AM org.apache.catalina.startup.HostConfig deployDirectory
- INFO: Deploying web application directory /home/shirdrn/servers/cluster/nginx_tomcat_memcached/tomcat-1/webapps/ROOT
- Jan 26, 2013 12:03:28 AM org.apache.coyote.AbstractProtocol start
- INFO: Starting ProtocolHandler ["http-bio-8088"]
- Jan 26, 2013 12:03:28 AM org.apache.coyote.AbstractProtocol start
- INFO: Starting ProtocolHandler ["ajp-bio-8089"]
- Jan 26, 2013 12:03:28 AM org.apache.catalina.startup.Catalina start
- INFO: Server startup in 1487 ms
- tomcat-2的日志
- shirdrn@dev:~/servers/cluster/nginx_tomcat_memcached$ tail -100f tomcat-2/logs/catalina.out
- Jan 26, 2013 12:04:34 AM org.apache.catalina.core.AprLifecycleListener init
- INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /home/hadoop/installation/jdk1.6.0_30/jre/lib/i386/client:/home/hadoop/installation/jdk1.6.0_30/jre/lib/i386:/home/hadoop/installation/jdk1.6.0_30/jre/../lib/i386:/usr/java/packages/lib/i386:/lib:/usr/lib
- Jan 26, 2013 12:04:34 AM org.apache.coyote.AbstractProtocol init
- INFO: Initializing ProtocolHandler ["http-bio-9088"]
- Jan 26, 2013 12:04:34 AM org.apache.coyote.AbstractProtocol init
- INFO: Initializing ProtocolHandler ["ajp-bio-9089"]
- Jan 26, 2013 12:04:34 AM org.apache.catalina.startup.Catalina load
- INFO: Initialization processed in 742 ms
- Jan 26, 2013 12:04:34 AM org.apache.catalina.core.StandardService startInternal
- INFO: Starting service Catalina
- Jan 26, 2013 12:04:34 AM org.apache.catalina.core.StandardEngine startInternal
- INFO: Starting Servlet Engine: Apache Tomcat/7.0.26
- Jan 26, 2013 12:05:07 AM org.apache.catalina.util.SessionIdGenerator createSecureRandom
- INFO: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [33,094] milliseconds.
- Jan 26, 2013 12:05:07 AM de.javakaffee.web.msm.MemcachedSessionService startInternal
- INFO: MemcachedSessionService starts initialization... (configured nodes definition n1:192.168.1.104:11211,n2:192.168.1.104:11311, failover nodes n2)
- 2013-01-26 00:05:07.873 INFO net.spy.memcached.MemcachedConnection: Added {QA sa=/192.168.1.104:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
- 2013-01-26 00:05:07.876 INFO net.spy.memcached.MemcachedConnection: Added {QA sa=/192.168.1.104:11311, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
- Jan 26, 2013 12:05:07 AM de.javakaffee.web.msm.RequestTrackingHostValve <init>
- INFO: Setting ignorePattern to .*\.(png|gif|jpg|css|js)$
- 2013-01-26 00:05:07.896 INFO net.spy.memcached.MemcachedConnection: Connection state changed for sun.nio.ch.SelectionKeyImpl@701a27
- 2013-01-26 00:05:07.897 INFO net.spy.memcached.MemcachedConnection: Connection state changed for sun.nio.ch.SelectionKeyImpl@1d5a0
- Jan 26, 2013 12:05:07 AM de.javakaffee.web.msm.MemcachedSessionService setLockingMode
- INFO: Setting lockingMode to null
- Jan 26, 2013 12:05:07 AM de.javakaffee.web.msm.MemcachedSessionService createTranscoderFactory
- INFO: Creating transcoder factory de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory
- Jan 26, 2013 12:05:07 AM de.javakaffee.web.msm.MemcachedSessionService startInternal
- INFO: MemcachedSessionService finished initialization, sticky true, operation timeout 1000, with node ids [n1] and failover node ids [n2]
- Jan 26, 2013 12:05:08 AM org.apache.catalina.startup.HostConfig deployDirectory
- INFO: Deploying web application directory /home/shirdrn/servers/cluster/nginx_tomcat_memcached/tomcat-2/webapps/host-manager
- Jan 26, 2013 12:05:08 AM org.apache.catalina.startup.HostConfig deployDirectory
- INFO: Deploying web application directory /home/shirdrn/servers/cluster/nginx_tomcat_memcached/tomcat-2/webapps/manager
- Jan 26, 2013 12:05:08 AM org.apache.catalina.startup.HostConfig deployDirectory
- INFO: Deploying web application directory /home/shirdrn/servers/cluster/nginx_tomcat_memcached/tomcat-2/webapps/docs
- Jan 26, 2013 12:05:08 AM org.apache.catalina.startup.HostConfig deployDirectory
- INFO: Deploying web application directory /home/shirdrn/servers/cluster/nginx_tomcat_memcached/tomcat-2/webapps/examples
- Jan 26, 2013 12:05:08 AM org.apache.catalina.startup.HostConfig deployDirectory
- INFO: Deploying web application directory /home/shirdrn/servers/cluster/nginx_tomcat_memcached/tomcat-2/webapps/ROOT
- Jan 26, 2013 12:05:08 AM org.apache.coyote.AbstractProtocol start
- INFO: Starting ProtocolHandler ["http-bio-9088"]
- Jan 26, 2013 12:05:08 AM org.apache.coyote.AbstractProtocol start
- INFO: Starting ProtocolHandler ["ajp-bio-9089"]
- Jan 26, 2013 12:05:08 AM org.apache.catalina.startup.Catalina start
- INFO: Server startup in 34163 ms
已经成功启动了。
Nginx安装、配置、启动
在Ubuntu系统下,如果没有安装Nginx,可以执行如下命令进行安装:
- sudo apt-get install nginx
默认情况下,Nginx使用默认的配置文件/etc/nginx/nginx.conf,该配置文件直接包含了虚拟目录配置文件/etc/nginx/sites-available/default,修改该文件的内容为,如下所示:
- # You may add here your
- # server {
- # ...
- # }
- # statements for each of your virtual hosts to this file
- ##
- # You should look at the following URL's in order to grasp a solid understanding
- # of Nginx configuration files in order to fully unleash the power of Nginx.
- # http://wiki.nginx.org/Pitfalls
- # http://wiki.nginx.org/QuickStart
- # http://wiki.nginx.org/Configuration
- #
- # Generally, you will want to move this file somewhere, and start with a clean
- # file but keep this around for reference. Or just disable in sites-enabled.
- #
- # Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
- ##
- upstream dev.shirdrn.org {
- server 192.168.1.104:8088 weight=1;
- server 192.168.1.104:9088 weight=1;
- }
- server {
- #listen 80; ## listen for ipv4; this line is default and implied
- #listen [::]:80 default ipv6only=on; ## listen for ipv6
- root /usr/share/nginx/www/session; # session是我们配置的虚拟目录,实际直接指向Tomcat下名为session的web应用
- index index.html index.htm;
- # Make site accessible from http://localhost/
- ####### server_name localhost;
- server_name dev.shirdrn.org; # Nginx服务所在主机
- charset utf-8;
- location / {
- proxy_pass http://dev.shirdrn.org; #直接代理tomcat集群
- proxy_set_header X-Real-IP $remote_addr;
- client_max_body_size 100m;
- # First attempt to serve request as file, then
- # as directory, then fall back to index.html
- ####### try_files $uri $uri/ /index.html;
- # Uncomment to enable naxsi on this location
- # include /etc/nginx/naxsi.rules
- }
- location ~ ^/(WEB-INF)/ { # 禁止访问Tomcat下web应用的WEB-INF目录下的资源
- deny all;
- }
- location /doc/ {
- alias /usr/share/doc/;
- autoindex on;
- allow 127.0.0.1;
- deny all;
- }
- # Only for nginx-naxsi : process denied requests
- #location /RequestDenied {
- # For example, return an error code
- #return 418;
- #}
- #error_page 404 /404.html;
- # redirect server error pages to the static page /50x.html
- #
- #error_page 500 502 503 504 /50x.html;
- #location = /50x.html {
- # root /usr/share/nginx/www;
- #}
- # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
- #
- #location ~ \.php$ {
- # fastcgi_split_path_info ^(.+\.php)(/.+)$;
- # # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
- #
- # # With php5-cgi alone:
- # fastcgi_pass 127.0.0.1:9000;
- # # With php5-fpm:
- # fastcgi_pass unix:/var/run/php5-fpm.sock;
- # fastcgi_index index.php;
- # include fastcgi_params;
- #}
- # deny access to .htaccess files, if Apache's document root
- # concurs with nginx's one
- #
- #location ~ /\.ht {
- # deny all;
- #}
- }
- # another virtual host using mix of IP-, name-, and port-based configuration
- #
- #server {
- # listen 8000;
- # listen somename:8080;
- # server_name somename alias another.alias;
- # root html;
- # index index.html index.htm;
- #
- # location / {
- # try_files $uri $uri/ /index.html;
- # }
- #}
- # HTTPS server
- #
- #server {
- # listen 443;
- # server_name localhost;
- #
- # root html;
- # index index.html index.htm;
- #
- # ssl on;
- # ssl_certificate cert.pem;
- # ssl_certificate_key cert.key;
- #
- # ssl_session_timeout 5m;
- #
- # ssl_protocols SSLv3 TLSv1;
- # ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv3:+EXP;
- # ssl_prefer_server_ciphers on;
- #
- # location / {
- # try_files $uri $uri/ /index.html;
- # }
- #}
上面配置中根目录为/usr/share/nginx/www,因为我们使用到了一个虚拟目录来对应Tomcat下部署的web应用的path,所以需要在/usr/share/nginx/www下面创建这个对应的虚拟目录:
- shirdrn@dev:~/servers/cluster/nginx_tomcat_memcached$ sudo mkdir /usr/share/nginx/www/session
最后,可以启动Nginx服务器,执行如下命令:
- shirdrn@dev:~$ sudo /etc/init.d/nginx start
查询Nginx服务启动状态:
- shirdrn@dev:~/servers/cluster/nginx_tomcat_memcached$ sudo ps -ef | grep nginx
- root 3199 1 0 Jan25 ? 00:00:00 nginx: master process /usr/sbin/nginx
- www-data 3200 3199 0 Jan25 ? 00:00:00 nginx: worker process
- www-data 3201 3199 0 Jan25 ? 00:00:00 nginx: worker process
- www-data 3202 3199 0 Jan25 ? 00:00:00 nginx: worker process
- www-data 3203 3199 0 Jan25 ? 00:00:00 nginx: worker process
- shirdrn 3327 3095 0 00:01 pts/5 00:00:00 grep --color=auto nginx
这是就可以通过Nginx访问(代理)Web应用首页了。
集群验证
上面配置完成,服务都已经启动成功,可以直接请求链接http://dev.shirdrn.org/session,就会直接访问部署在Tomcat服务器上的Java Web应用,看到登录页面。
使用前面准备的Web应用session中账号数据进行模拟登录,就可以查看Memcached缓存的数据:
- telnet 192.168.1.104 11211
telnet到memcached服务端口,查看缓存统计数据。
- shirdrn@dev:~$ telnet 192.168.1.104 11211
- Trying 192.168.1.104...
- Connected to 192.168.1.104.
- Escape character is '^]'.
- ERROR
- stats
- STAT pid 3300
- STAT uptime 2148
- STAT time 1359131741
- STAT version 1.4.13
- STAT libevent 2.0.16-stable
- STAT pointer_size 32
- STAT rusage_user 0.052003
- STAT rusage_system 0.084005
- STAT curr_connections 7
- STAT total_connections 9
- STAT connection_structures 8
- STAT reserved_fds 20
- STAT cmd_get 23
- STAT cmd_set 6
- STAT cmd_flush 0
- STAT cmd_touch 0
- STAT get_hits 2
- STAT get_misses 21
- STAT delete_misses 0
- STAT delete_hits 0
- STAT incr_misses 0
- STAT incr_hits 0
- STAT decr_misses 0
- STAT decr_hits 0
- STAT cas_misses 0
- STAT cas_hits 0
- STAT cas_badval 0
- STAT touch_hits 0
- STAT touch_misses 0
- STAT auth_cmds 0
- STAT auth_errors 0
- STAT bytes_read 2675
- STAT bytes_written 2835
- STAT limit_maxbytes 67108864
- STAT accepting_conns 1
- STAT listen_disabled_num 0
- STAT threads 4
- STAT conn_yields 0
- STAT hash_power_level 16
- STAT hash_bytes 262144
- STAT hash_is_expanding 0
- STAT expired_unfetched 0
- STAT evicted_unfetched 0
- STAT bytes 858
- STAT curr_items 2
- STAT total_items 6
- STAT evictions 0
- STAT reclaimed 0
- END
- telnet 192.168.1.104 11311
- shirdrn@dev:~$ telnet 192.168.1.104 11311
- Trying 192.168.1.104...
- Connected to 192.168.1.104.
- Escape character is '^]'.
- stats
- STAT pid 3308
- STAT uptime 2398
- STAT time 1359132000
- STAT version 1.4.13
- STAT libevent 2.0.16-stable
- STAT pointer_size 32
- STAT rusage_user 0.004000
- STAT rusage_system 0.128008
- STAT curr_connections 7
- STAT total_connections 9
- STAT connection_structures 8
- STAT reserved_fds 20
- STAT cmd_get 1
- STAT cmd_set 2
- STAT cmd_flush 0
- STAT cmd_touch 0
- STAT get_hits 0
- STAT get_misses 1
- STAT delete_misses 1
- STAT delete_hits 0
- STAT incr_misses 0
- STAT incr_hits 0
- STAT decr_misses 0
- STAT decr_hits 0
- STAT cas_misses 0
- STAT cas_hits 0
- STAT cas_badval 0
- STAT touch_hits 0
- STAT touch_misses 0
- STAT auth_cmds 0
- STAT auth_errors 0
- STAT bytes_read 644
- STAT bytes_written 2109
- STAT limit_maxbytes 67108864
- STAT accepting_conns 1
- STAT listen_disabled_num 0
- STAT threads 4
- STAT conn_yields 0
- STAT hash_power_level 16
- STAT hash_bytes 262144
- STAT hash_is_expanding 0
- STAT expired_unfetched 1
- STAT evicted_unfetched 0
- STAT bytes 0
- STAT curr_items 0
- STAT total_items 2
- STAT evictions 0
- STAT reclaimed 1
- END
我们可以停掉某个Tomcat服务,然后观察Session会话数据的复制过程,能够保证当前集群中的会话数据不丢失,另一个结点仍然能提供基于该存在Session的服务。
相关推荐
标题“nginx tomcat memcached session共享”涉及到的是在分布式系统中如何实现会话(session)共享的技术方案。在这个场景下,我们通常有三个关键组件:Nginx作为反向代理服务器,Tomcat作为应用服务器,而...
通过上述步骤,我们可以成功地在Windows7环境下,利用Nginx、Memcached和Tomcat搭建一个session共享的集群环境。这种方式不仅可以提高系统的可用性和扩展性,还能保证用户在不同服务器间的会话一致性。
最后,提供的压缩包文件`nginx+tomcat+memcached`可能包含了上述所有组件的配置文件、jar包和安装脚本,帮助用户快速搭建和测试这样的系统。在部署时,务必根据实际网络环境和服务器配置进行适当的调整,确保所有...
在构建分布式系统时,Session共享是一个关键问题,特别是在使用多种服务器技术如Nginx、Tomcat和Memcached的环境中。本文将深入探讨如何利用这些组件实现跨服务器的Session共享,以确保用户在登录后能够在整个系统中...
【Nginx、memcached和Tomcat集群配置实现session共享与负载均衡】 在Windows环境下,构建一个基于Nginx的高可用性Web集群,通常包括Nginx作为反向代理和负载均衡器,memcached用于session共享,以及多个Tomcat实例...
【Nginx+Tomcat+Memcached实现Tomcat集群和session共享】 在现代Web应用程序的部署中,为了提高系统的可用性和可扩展性,通常会采用集群技术。将多个Tomcat服务器组成一个集群,通过负载均衡策略分发用户请求,以...
- 在Nginx配置中,启用负载均衡和 sticky session(粘滞会话),确保客户端的请求始终路由到同一台Tomcat服务器。 6. **安全性与性能优化**:虽然Memcached提供了高速的数据存储,但也需要注意安全性问题,如防止...
总结来说,实现Nginx、Tomcat集群与Memcached的Session共享,需要正确配置Nginx的反向代理设置,引入并配置Tomcat的Memcached Session Manager,以及确保所有必要的jar包已经正确安装。这一过程虽然涉及多个环节,但...
- **配置Nginx**:在Nginx的配置文件中设置负载均衡策略,可以实现基于session的会话粘滞性(sticky session),确保同一用户请求始终被转发到处理其session的特定Tomcat实例。 3. **安装指南**: - **安装...
通过在Nginx配置中设置session sticky(粘滞会话),可以确保同一个用户的请求始终被转发到同一个服务器,从而保持session的一致性。如果该服务器出现故障,Nginx可以将用户重定向到其他服务器,并在新的服务器上...
为了设置session共享,你需要在Tomcat的配置文件(如`context.xml`或`server.xml`)中配置Memcached Session Manager,并指定Spy Memcached为客户端,同时在Nginx配置中启用session sticky或负载均衡策略。...
标题中的“nginx实现多个tomcat7直接session共享所需jar包”指的是在分布式系统环境中,使用Nginx作为反向代理服务器,将用户请求分发到多个Tomcat7应用服务器上,同时实现用户会话(Session)在这些服务器之间的...
为了实现session共享,我们需要在每个Tomcat服务器上配置一个Memcached session管理器。首先,确保你的Tomcat服务器已经安装了Memcached的Java客户端库,如spymemcached。 在`$CATALINA_BASE/conf/context.xml`中,...
在构建高性能、高可用性的Web应用系统时,通常会采用Nginx作为反向代理和负载均衡器,Tomcat作为Java应用服务器,而Memcached作为分布式缓存来处理session共享问题。本文将详细讲解如何利用Nginx、Tomcat和Memcached...
在集群环境中,为了确保用户会话在各个服务器之间的一致性,通常需要一种策略来共享Session数据,如使用Nginx的sticky session或共享Session存储(如Memcached或Redis)。 搭建步骤: 1. **系统环境准备**:选择...
nginx+tomcat7+session共享 kryo序列化所需要包 1.将上面所提到的包全部拷贝到tomcat的lib下(三台tomcat都需要) 2.修改每台tomcat的conf目录下得context.xml文件或者server.xml文件,在其中加入如下任意一段代码...
### Nginx、Tomcat与MyBatis集群部署详解 #### 一、负载均衡与Session共享...综上所述,通过合理配置Nginx、Tomcat以及实现Session共享机制,可以有效地提升Web应用的并发处理能力和稳定性,满足高流量网站的需求。
4. 如果需要非粘滞(non-sticky)session模式,即即使服务器故障也能保持session,可能还需要配置负载均衡器或反向代理(如Nginx、HAProxy)来处理session复制。 完成以上配置后,`Tomcat8`实例就可以通过`...
配置Nginx的`upstream`模块,将用户请求均匀分配到各个Tomcat实例,同时利用`proxy_pass`指令将session相关请求指向Memcached服务器。 通过以上步骤,你可以在Tomcat7中成功地实现了基于Memcached的session共享,...
2. **Sticky Session**:这是最简单的Session共享方式,通过Nginx配置,确保用户的所有请求都被转发到同一台Tomcat服务器,这样Session就无需在服务器之间传递。 3. **Session Replication**:另一种方法是通过网络...