最近由于项目需要,所以学习了 nginx+tomcat7+memcached集成负载均衡。下面直接说这次学习到的知识,有什么不对的地方欢迎指出( nginx+tomcat7+memcached集成 负载均衡 搭建步骤):
环境及框架介绍:
- 服务器环境:windows server 2012
- jdk 1.7
- tomcat 7
- nginx 1.10.1
- memcached-2.5
搭建步骤:
- tomcat 7解压版的,修改server.xml!要注意多个tomcat,端口号等不能一样,另外需要把Engine 注释打开,加上jvmRoute,如下:
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">
- 安装nginx
-
- 下载地址:http://nginx.org/ ,建议下载稳定版本,我用的是1.10
- 因为我服务器是win版,下载后安装很简单:cmd转到nginx文件夹bin下,一句命令即可:nginx -s install
- 什么都不需要该,直接再来一句命令:nginx -s start 或nginx,就已经启动了,默认地址:http://localhost,看到 欢迎页面,说明安装成功!
- 安装成功后,就需要与tomcat集成,主要修改conf/nginx.conf配置文件:(下面是我的配置)
-
这时候已经与tomcat集成了,修改过nginx配置后,记得重置一下nginx,命令如下:nginx -s reload#user nobody; #此参数修改为与CPU个数一致 worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; pid logs/nginx.pid; worker_rlimit_nofile 51200; events { worker_connections 51200; } 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; server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 32k; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 60; tcp_nodelay on; #gzip on; #下面是配置关联tomcat关键代码 upstream abc.tomcats.com { #ip_hash; server 127.0.0.1:8080 ; server 127.0.0.1:8090 ; } server { listen 8085; server_name abc.tomcats.com; proxy_redirect off; access_log logs/abc.tomcats.com.log combined; #charset koi8-r; #access_log logs/host.access.log main; #充许客户端请求的最大单个文件字节数 client_max_body_size 10m; client_body_buffer_size 128k; #跟后端服务器连接的超时时间 proxy_connect_timeout 10; #连接成功后等候后端服务器响应时间 proxy_read_timeout 600; #后端服务器数据回传时间 proxy_send_timeout 600; #代理请求缓存区 proxy_buffer_size 8k; #同上,保存用几个buffer每个最大空间是多少 proxy_buffers 4 32k; #如果系统很忙时可以申请更大的proxy_buffers,官方推荐*2 proxy_busy_buffers_size 64k; #缓存临时文件的大小 proxy_temp_file_write_size 64k; location / { root html; index index.html index.htm; proxy_pass http://abc.tomcats.com; proxy_redirect default; proxy_set_header Host $host; proxy_set_header X-Real-Ip $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location /status{ stub_status on; access_log off; auth_basic "NginxStatus"; auth_basic_user_file htpasswd; } location ~ \.jsp$ { proxy_pass http://abc.tomcats.com; } location ~ \.(html|js|css|png|gif)$ { root html; proxy_pass http://abc.tomcats.com; } #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 ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
- 重启nginx后,接着启动2个tomcat,最后访问地址:http://localhost:8085/test/rt.jsp("test/rt.jsp"是tomcat中项目路径),看到对应页面就表示成功拉。
- 当你关掉一个tomcat,路径还能正常访问表示nginx 配置成功;
- 但是这时候还存2个tomcat的session值不能共享问题,通过比较找到了memcached,Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载。它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态、数据库驱动网站的速度。
- memcached(附件memcached-win64-1.4.4-14.zip)
- 安装:memcached.exe –d install
- 启动:memcached.exe –d start
- 需要的jar(附件memjar),放到tomcat 目录的lib下
- 修改tomcat 的context.xml配置:
-
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" memcachedNodes="n1:127.0.0.1:11211" sticky="false" lockingMode="auto" sessionBackupAsync="false" sessionBackupTimeout="1000" transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory" />
- 最后重启tomcat、nginx,再次测试,查看session值是否一致(如何获取session值这里就不说了)
- 最后的最后这次初体验就结束了!