1、Nginx安裝css
詳見前文:http://www.cnblogs.com/yixiwenwen/p/3574097.htmlhtml
2、memcached安裝和啓動java
詳見前文:http://www.cnblogs.com/yixiwenwen/p/3574137.htmlnginx
3、配置Nginx實現Tomcat的負載均衡和利用memcached實現session共享git
1. 修改nginx的配置文件nginx.confgithub
user nobody; worker_processes 4; error_log logs/error.log; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; gzip on; upstream www.yixi.com { server 192.168.137.46:100; server 192.168.137.46:101; server 192.168.137.46:102; } server { listen 80; server_name www.yixi.com; charset utf-8; location / { root html; index index.html index.htm; proxy_pass http://www.yixi.com; proxy_set_header X-Real-IP $remote_addr; client_max_body_size 100m; } location ~ ^/(WEB-INF)/ { deny all; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
2. Tomcat配置web
下載並解壓三個tomcat7;apache
下載相應的jar包到全部的tomcat的lib目錄下:瀏覽器
couchbase-client-1.2.2.jartomcat
javolution-5.4.3.1.jar
memcached-session-manager-1.6.4.jar
memcached-session-manager-tc7-1.6.4.jar
msm-javolution-serializer-1.6.4.jar
msm-kryo-serializer-1.6.4.jar
msm-xstream-serializer-1.6.4.jar
spymemcached-2.10.2.jar
下面修改Tomcat配置文件,讓Tomcat結合memcached實現session共享
[root@localhost lib]# vi /usr/local/tomcat/conf/context.xml
在配置文件中的<context></context>標籤裏面加入以下內容:
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" memcachedNodes="n1:localhost:11211" requestUriIgnorePattern=".*\.(png|gif|jpg|css|js)$" sessionBackupAsync="false" sessionBackupTimeout="100" transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory" copyCollectionsForSerialization="false" />
爲了測試在tomcat默認的webroot下的index.jsp中加入這些代碼:
SessionID:<%=session.getId()%> <BR> SessionIP:<%=request.getServerName()%> <BR> SessionPort:<%=request.getServerPort()%> <% out.println("This is Tomcat Server 111111!"); %>
(備註:當要同時啓動多個tomcat時須要修改一些配置
a.修改/etc目錄下的profile文件:
export CATALINA_HOME_test_1=/usr/local/tomcat_test/apache-tomcat-7-1 export CATALINA_HOME_test_2=/usr/local/tomcat_test/apache-tomcat-7-2 export CATALINA_HOME_test_3=/usr/local/tomcat_test/apache-tomcat-7-3
b.修改bin下的startup.sh和shutdown.sh
添加 export CATALINA_HOME=$CATALINA_2_HOME 利用profile中第二組設置
)
4.測試結果:
分別啓動nginx memcached tomcat
將host的文件中映射www.yixi.com到本身的ip;
在瀏覽器中訪問www.yixi.com
訪問的的是tomcat3 此時sessionID是:598EA38832CA14F8F14AC3EF0276AD3D-n1
刷新幾回發現訪問的仍是tomcat3,
爲了測試 先殺死tomcat3的進程!
再刷新...
發現當tomcat3掛掉以後 請求就到了tomcat1 而且咱們但願的sessionId仍是598EA38832CA14F8F14AC3EF0276AD3D-n1
再把tomcat1掛掉試試:
測試結果:
只剩下tomcat2還能用 並且sessionId是共享的。
這樣整個配置和測試就完成了
附件:
tomcat部分:http://download.csdn.net/detail/yixiwenwen/6986845
nginx.conf :http://download.csdn.net/detail/yixiwenwen/6986719
參考:
https://github.com/magro/memcached-session-manager/tree/memcached-session-manager-project-1.8.3
https://code.google.com/p/memcached-session-manager/wiki/SetupAndConfiguration