之前由于项目当中要用到负载均衡集群的问题,看到网上很多都是这样文章,都是DEMO级别的配置,于是自己也搭建了一番,DEMO可以通过,把实际项目部署到里面去就不行了,根本没有涉及用到真正项目场景之中用到解决方案,我真是百思不得其解,自己又针对本身项目看了一下,才发现session和cookie根本没有绑定成功,只好自己动手写了一个tm-session-mem-1.0.0.jar对session和cookie进行管理包。
本文环境操作系统是windows2008
1.安装Nginx
Nginx官网:http://nginx.org/
下载最新稳定版本,下载完成之后,进入到cmd 找到下载目录输入nginx.exe,安装成功之后,看看任务管理器是否有nginx,有说明安装成功.
1.1 再ngnix目录找到conf文件夹里面有一个配置文件nginx.conf,我的配置如下:
#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;
upstream 127.0.0.1 {
server 127.0.0.1:8181 weight=1; #这里是配置toncat8181端口
server 127.0.0.1:8282 weight=2; #这里是配置toncat8282端口
}
#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;
server {
listen 8888; #更改默认80端口
server_name localhost; #配置服务器IP
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
proxy_pass http://127.0.0.1;
proxy_set_header Host $host:8888; #还要加这一句,要不然不起作用,更改端口
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#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;
# server_name localhost;
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_timeout 5m;
# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
Tomcat官网:http://tomcat.apache.org/
2.1 本次使用Tomcat版本:apache-tomcat-7.0.55.tar.gz,解压成两个,只需要更改端口8181和8282,Tomcat能正常启动不报错。里面不 需要配置Mencached什么,很干净的一个Tomcat.把项目放在webapps下面.
3.自己写了一个session和cookie进行管理包
这里面用到java_memcached-release_1.5.jar 需要配置一些东西,如下:
3.1 项目 web.xml 加一个过滤器如下:
<!-- 添加memcached过滤器 -->
<filter>
<filter-name>MemcachedSessionFilter</filter-name>
<filter-class>com.strawren.memsession.filter.MemcachedSessionFilter</filter-class>
<init-param>
<param-name>sessionId</param-name>
<param-value>sid</param-value>
</init-param>
</filter>
<!-- add memcached -->
<filter-mapping>
<filter-name>MemcachedSessionFilter</filter-name>
<url-pattern>*.html</url-pattern>
<dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
3.2 配置一个 system.properties 添加如下:
#添加session
memcached.servers=127.0.0.1:11211
emcached.poolname=sessionIdSock
4.安装一个memcached服务,首先要启动这个,要不然项目会卡死,进不去。
最后我打包一个 tm-session-mem-1.0.0.jar 只需要把这个jar包导入你项目当中就ok.
下载jar包资源文件URL:http://download.csdn.net/detail/barney_yangbeiyan/8409801