1.nginx是一款輕量級兼備高性能的Http和反向代理服務器。所謂反向代理就是指用戶發起訪問請求,由代理服務器接受,而後將請求轉發給正式服務器,而且將正式服務器處理完的數據返回給客戶單,此時代理服務器就表現爲一個服務器。php
進入nginx的安裝目錄後,輸入相關命令,一個窗口一閃而過,在任務管理器中能夠找到它的進程。html
瀏覽器輸入localhost,能夠看到nginx的歡迎頁面!java
修改nginx的配置文件,nginx
worker_processes:工做進程個數,web
worker_connections:單個進程最大鏈接數,redis
server:每個server至關於一個代理服務器,瀏覽器
listern:監聽端口,默認80,tomcat
server_name:當前服務的域名,能夠有多個,用空格分割,服務器
location:表示匹配的路徑,配置/ 表示全部請求都被匹配到這裏,session
index:當沒有指定主頁時,默認會選擇這個指定的文件,可多個,空格分割 ,
proxy_pass:請求轉向自定義的服務器列表,
upstream name{} :服務器集羣名稱
server.xml依次修改 Server的port爲8005,8006,8007 ,Connector的port爲8081 8082 8083,8009 8010 8011
context.xml增長
並在webapps\ROOT下增長session.jsp ,加上標記tomcat 1 tomcat 2 tomcat 3
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>shared session</title> </head> <body> <br>session id=<%=session.getId()%> <br>tomcat 1 </body> </html>
將以下幾個jar包複製到${TOMCAT_HOME}/lib下
tomcat-redis-session-manager-VERSION.jar jedis-2.5.2.jar commons-pool2-2.2.jar
3.修改Ngnix的配置文件增長負載均衡配置
#user nobody; #nginx進程數,建議設置爲等於CPU總核心數 worker_processes 4; error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #進程pid文件 pid logs/nginx.pid; events { worker_connections 65535; } 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 mt.com { #upstream的負載均衡,weight是權重,可根據機器配置定義權重.weight參數表示權值,權值越高被分配的概率越大. server 127.0.0.1:8081 weight=1; server 127.0.0.1:8082 weight=2; server 127.0.0.1:8083 weight=3; } server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { #root html; index index.html index.htm; proxy_pass http://mt.com; proxy_set_header X-Real-IP $remote_addr; #容許客戶端請求的最大單文件字節數 client_max_body_size 100m; } #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; # } #} }
4.啓動redis,tomcat1,tomcat2,tomcat3 訪問localhost/session.jsp且不斷刷新
能夠看到每次刷新均可能訪問不一樣的tomcat,實現了負載均衡,並且session都是一致的,保證了session的一致性
原文參照: https://www.cnblogs.com/zhrxidian/p/5432886.html
https://www.cnblogs.com/linjiqin/p/5761281.html
http://blog.csdn.net/lipc_/article/details/52766884
https://www.cnblogs.com/machanghai/p/5956195.html