Niginx 集群負載均衡策略
?
?所需物料
1.Nginx服務
步驟略
本人 nginx version: nginx/1.16.0
?
2.Java Servlet 測試項目
新建java web 項目,項目名稱為:tt
import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;@WebServlet("/IndexServlet") public class IndexServlet extends HttpServlet {private static final long serialVersionUID = 1L;public IndexServlet() { }protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//輸出Session的IdSystem.out.println("[session-id]\t"+request.getSession().getId());//制造網絡請求延遲效果try {System.out.println("[-線程睡眠中-]");Thread.sleep(3000);System.out.println("[-線程睡眠結束-]");} catch (InterruptedException e) {e.printStackTrace();}System.out.println("");System.out.println("");System.out.println("");response.getWriter().append("Served at: ").append(request.getContextPath());}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}}
3.tomcat部署服務
共啟用了3個Tomcat,服務端口分別是:8081、8082、8083;
分別訪問?http://localhost:8081/tt/index
分別訪問?http://localhost:8082/tt/index
分別訪問?http://localhost:8083/tt/index
進行服務驗證,看服務是否可以正常訪問
?
?
?----------------------------好戲開始了----------------------------
集群調度:輪詢(默認)
調度規則:輪詢調取集群中的服務;
修改?nginx.conf 配置文件,重啟Nginx;
worker_processes 1;events {worker_connections 1024;
}http {include mime.types;default_type application/octet-stream;sendfile on;keepalive_timeout 65;#配置集群集合upstream tomcatserver1 {server 127.0.0.1:8081;server 127.0.0.1:8082; server 127.0.0.1:8083; } server { listen 80; server_name localhost; location / { proxy_pass http://tomcatserver1; index index.html index.htm; } error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}}
瀏覽器多次訪問??http://localhost/tt/index 進行測試。會看到3個Tomcat 被輪訓調用。
?
集群調度:ip_hash
調度規則:同一個session會被分配到同一個 服務中,主要解決集群session問題;
修改?nginx.conf 配置文件,重啟Nginx;
worker_processes 1;events {worker_connections 1024;
}http {include mime.types;default_type application/octet-stream;sendfile on;keepalive_timeout 65;#配置集群集合upstream tomcatserver1 {#調度方式 ip_haship_hash; server 127.0.0.1:8081;server 127.0.0.1:8082; server 127.0.0.1:8083; } server { listen 80; server_name localhost; location / { proxy_pass http://tomcatserver1; index index.html index.htm; } error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}}
集群調度:Weight
調度規則:根據權重來處理,權重越高被調用的概率越高,主要用于后端服務器性能不均的情況;
修改?nginx.conf 配置文件,重啟Nginx;
worker_processes 1;events {worker_connections 1024;
}http {include mime.types;default_type application/octet-stream;sendfile on;keepalive_timeout 65;#配置集群集合upstream tomcatserver1 {#調度方式 Weightserver 127.0.0.1:8081 weight=3;server 127.0.0.1:8082 weight=2; server 127.0.0.1:8083 weight=1; } server { listen 80; server_name localhost; location / { proxy_pass http://tomcatserver1; index index.html index.htm; } error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}}
集群調度:url_hash
調度規則:相同的url地址會被分配到同一個服務器,用于緩存數據,如:我將系統圖片都緩存在了8081、8082服務器,每當我請求圖片的時候必須去請求8081 或 8082服務器;
修改?nginx.conf 配置文件,重啟Nginx;
worker_processes 1;events {worker_connections 1024; }http {include mime.types;default_type application/octet-stream;sendfile on;keepalive_timeout 65;#配置集群集合upstream tomcatserver1 {#調度方式 url_hashhash $request_uri;server 127.0.0.1:8081;server 127.0.0.1:8082; server 127.0.0.1:8083; } server { listen 80; server_name localhost;# 假如請求靜態資源的路徑格式是 localhost:80/tt/state/xx/xx/…… location /tt/state { proxy_pass http://tomcatserver1; index index.html index.htm; } error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}}
?
?