日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Niginx 集群负载均衡策略

發(fā)布時間:2023/12/13 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Niginx 集群负载均衡策略 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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());//制造網(wǎng)絡請求延遲效果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

進行服務驗證,看服務是否可以正常訪問

?

?

?----------------------------好戲開始了----------------------------

集群調度:輪詢(默認)

調度規(guī)則:輪詢調取集群中的服務;

修改?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

調度規(guī)則:同一個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

調度規(guī)則:根據(jù)權重來處理,權重越高被調用的概率越高,主要用于后端服務器性能不均的情況;

修改?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

調度規(guī)則:相同的url地址會被分配到同一個服務器,用于緩存數(shù)據(jù),如:我將系統(tǒng)圖片都緩存在了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;# 假如請求靜態(tài)資源的路徑格式是 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;}}}

?

?

轉載于:https://www.cnblogs.com/devan/p/11237016.html

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結

以上是生活随笔為你收集整理的Niginx 集群负载均衡策略的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。