下载软件:
1 2 3 4 5 | wget http: //www.minunix.com/docs/web/openssl-1.0.1c.tar.gz Wget http: //www.minunix.com/docs/web/pcre-8.31.tar.gz wget http: //www.minunix.com/docs/web/zlib-1.2.7.tar.gz wget http: //www.minunix.com/docs/web/nginx-1.2.8.tar.gz wget http: //nginx-upstream-jvm-route.googlecode.com/files/nginx-upstream-jvm-route-0.1.tar.gz |
环境安装:
1 2 | tar xf openssl- 1.0 .1c.tar.gz && tar xf pcre- 8.31 .tar.gz && tar xf zlib- 1.2 . 7 .tar.gz tar xf nginx-upstream-jvm-route- 0.1 .tar.gz |
JDK安装就略过了 只需要下载安装然后在/etc/profile添加路径就行了
1 2 3 | export JAVA_HOME=/usr/java/jdk1. 7 .0_09 export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH:$HOME/bin |
安装nginx
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | tar xf nginx-upstream-jvm-route- 0.1 .tar.gz tar zxvf nginx- 1.2 . 8 .tar.gz cd nginx patch -p0 < ../nginx-upstream-jvm-route/jvm_route.patch ##这一步很重要 不执行的话后面的nginx。conf配置会报错jvm错误 ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx -- with -http_gzip_static_module -- with -http_stub_status_module -- with -select_module -- with -poll_module -- with -http_sub_module -- with -http_ssl_module -- with -md5=/usr/lib64 -- with -sha1=/usr/lib64 --pid-path=/usr/local/nginx/ var /run/nginx.pid -- with -pcre=/usr/local/soft/pcre- 8.21 -- with -zlib=/usr/local/soft/zlib- 1.2 . 7 -- with -openssl=/usr/local/soft/openssl- 1.0 .1d --add-module=/usr/local/soft/nginx_upstream_jvm_route/ make && make install |
##到此支持jvm-route模块的nginx安装完毕 下面是配置文件nginx.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | user nginx nginx; worker_processes 1 ; pid logs/nginx.pid; events { use epoll; worker_connections 1024 ; } http { include mime.types; default_type application/octet-stream; sendfile on; tcp_nopush on; keepalive_timeout 35 ; fastcgi_connect_timeout 300 ; fastcgi_send_timeout 300 ; fastcgi_read_timeout 300 ; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 256k; gzip on; gzip_types text/javascript text/plain text/css application/xml application/x-javascript; gzip_disable "MSIE [1-6]\.(?!.*SV1)" ; proxy_connect_timeout 300 ; proxy_send_timeout 300 ; proxy_read_timeout 300 ; proxy_buffer_size 16k; proxy_buffers 4 32k; proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header Connection Close; server_names_hash_max_size 1024 ; server_names_hash_bucket_size 1024 ; upstream minunix.com { server 192.168 . 43.139 : 8080 ; server 192.168 . 43.139 : 9080 ; ip_hash; jvm_route $cookie_JSESSIONID|sessionid reverse; } server { listen 80 ; server_name localhost; root / var /www; index index.jsp index.html index.htm; charset utf- 8 ; #access_log logs/host.access.log main; location / { proxy_pass http: //minunix.com; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_redirect off; client_max_body_size 0 ; location ~ .*\.(php|php5)?$ { fastcgi_pass 127.0 . 0.1 : 9000 ; fastcgi_index index.php; include fastcgi.conf; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } location ~ .*\.(js|css)?$ { expires 12h; } } #error_page 404 / 404 .html; error_page 500 502 503 504 /50x.html; location = /50x.html { # root html; } } } |
安装tomcat 下载apache-tomcat 在/usr/local中有tomcat1 tomcat2
tomcat1修改
1 | <Engine name= "Catalina" defaultHost= "localhost" >增加jvmRoute= "tomcat1" |
tomcat2修改
1 2 | <Engine name= "Catalina" defaultHost= "localhost" >增加jvmRoute= "tomcat2" 并且需要把原来默认的 8080 8005 8009 改成其他的端口我就改成 9080 9005 9009 |
开始测试
tomcat1测试文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | vim /usr/local/tomcat1/webapps/aa/index.jsp <%@ page language= "java" import = "java.util.*" pageEncoding= "UTF-8" %> <% %> <html> <head> </head> <body> 888888 <br /> <%out.print(request.getSession()) ;%> <!--输出session--> <br /> <%out.println(request.getHeader( "Cookie" )); %> <!--输出Cookie--> </body> </html> |
tomcat2测试文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | vim /usr/local/tomcat2/webapps/aa/index.jsp <%@ page language= "java" import = "java.util.*" pageEncoding= "UTF-8" %> <% %> <html> <head> </head> <body> 9999999 <br /> <%out.print(request.getSession()) ;%> <!--输出session--> <br /> <%out.println(request.getHeader( "Cookie" )); %> <!--输出Cookie--> </body> </html> |
启动nginx和tomcat之后访问http://192.168.43.139/aa/会发现tomcat1和tomcat2不断在切换 这一步说明负载成功。那么现在实现session。
修改nginx.conf将上面upstream一段修改为如下
1 2 3 4 5 6 | upstream minunix.com { server 192.168 . 0.4 : 8080 srun_id=tomcat1; ##不增加srun_id就会不断两个页面访问增加之后就实现了session server 192.168 . 0.52 : 8080 srun_id=tomcat2; ## ip_hash; jvm_route $cookie_JSESSIONID|sessionid reverse; } |
重启nginx
现在访问页面会不断出现8888 或者99999 在一个页面不会变 停止某个tomcat就会显示另外一个
联系客服