OS : winsows7
JDK : 7
準備軟體
Server
JAR
- memcached-session-manager-1.6.1.jar (含java序列)
- memcached-session-manager-tc6-1.6.1.jar (for Tomcat 6)
- msm-javolution-serializer-1.6.1.jar
- javolution-5.4.3.1.jar
- spymemcached-2.7.3.jar
NOTE: 注意用太新版本的jar反而會錯,tomcat會啟動不了。
資源列表:
memcached在client可用:
1.解壓 nginx、 tomcat、memcached
2. 將下載的 jar 放置 兩個 tomcat 的 /lib 底下
3.安裝 memcached
- 管理員開啟命令列,執行 memcached.exe –d install 。
查詢啟動:
- tasklist /fi "imagename eq memcached.exe"
- 可以進到管理中將其改為手動啟動。
預設的port是 11211
指令:
-p 監聽的Port
-l 連接的IP地址, 預設是本機 (localhost)
-d start 啟動memcached服務
-d restart 重起memcached服務
-d stop|shutdown 關閉正在運行的memcached服務
-d install 安裝memcached服務
-d uninstall 卸載memcached服務
-u 以的身份運行(僅在以root運行的時候有效)
-m 最大記憶體使用,單位MB。默認64MB
-M 記憶體耗盡時返回錯誤,而不是刪除項目
-c 最大同時連接數,默認是1024
-f 塊大小增長因子,默認是1.25
-n 最小分配空間,key+value+flags 預設是48
-h 顯示幫助
Note:windows7要使用管理員開啟命令模式
4. 設定兩台 tomcat context.xml
<:Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:localhost:11211"
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"/>
5.設定 兩台 tomcat server.xml
- <Server port="8005" shutdown="SHUTDOWN"> (tomcat 1)
<Server port="9005" shutdown="SHUTDOWN"> (tomcat 2) - <Connector port="8080" protocol="HTTP/1.1" (tomcat1)
connectionTimeout="20000"
redirectPort="8443" />
<Connector port="9080" protocol="HTTP/1.1" (tomcat2)
connectionTimeout="20000"
redirectPort="8443" /> - <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> (tomcat1)
<Connector port="9009" protocol="AJP/1.3" redirectPort="8443" /> (tomcat2) - <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1" /> (tomcat1)
<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm2" /> (tomcat2)
測試:
- 設定好,進入tomcat 的bin,執行 startup.bat 啟動 tomcat。
- 瀏覽器 localhost:8080 與 localhost:9080 測試一下。
5. 設定 nginx 反向代理 (reverse proxy) 與 load balancer
- 打開 conf/nginx.confupstream 127.0.0.1 { #weigth 值越高被分配到的機率越大 server 127.0.0.1:8080 weight=1; server 127.0.0.1:9080 weight=1; }
#user nobody;
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 { //負載平衡有多種分配方式,可以看nginx官方文件
#weigth 權值越高被分配到的機率越大
server 127.0.0.1:8080 weight=1;
server 127.0.0.1:9080 weight=1;
}
server {
listen 80; //偵聽80port
server_name localhost;
charset utf-8;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
proxy_pass http://tomcat-server; //導向 tomcat-server
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
client_max_body_size 100m;
}
#static resource
location ~ .*\.(htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css|eot|svg|ttf|woff)$ //可用靜態資源型態
{
root public; //這是指nginx底下有新增一個資料夾叫public
expires 7d;
}
#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 html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# 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;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
6. 寫一個網頁
<%@ 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>
<%
out.println( "tomcat1 SESSION ID:" + session.getId()); //tomcat2 要改 tomcat2
%>
</body>
</html>
7.將war檔佈署到 tomcat1 與 tomcat2
8.啟動所有的server
9.瀏覽器開啟兩頁輸入
- http://localhost:8080/webs/
Note webs是你自己的專案名稱
若看到session id 一樣就是成功了
重點參考文章:
沒有留言:
張貼留言