Nginx多種負載均衡策略搭建

来源:https://www.cnblogs.com/enochzzg/archive/2019/06/27/11099354.html
-Advertisement-
Play Games

背景介紹 上篇介紹了利用Nginx反向代理實現負載均衡,本文詳細講述Nginx下的幾種負載均衡策略。 輪詢 輪詢,顧名思義,就是輪流請求,基於上篇文章的介紹,我們將負載均衡策略聚焦於 文件的 。 在瀏覽器中對 連續發出請求,根據nginx請求日誌可以看出web02與web03訪問的次數是相同的。 加 ...


背景介紹

上篇介紹了利用Nginx反向代理實現負載均衡,本文詳細講述Nginx下的幾種負載均衡策略。

輪詢

輪詢,顧名思義,就是輪流請求,基於上篇文章的介紹,我們將負載均衡策略聚焦於default.conf文件的upstream

upstream backend {
    server web02:80;
    server web03:80;
}

在瀏覽器中對localhost:8080連續發出請求,根據nginx請求日誌可以看出web02與web03訪問的次數是相同的。

web01    | 172.24.0.1 - - [26/Jun/2019:10:03:06 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" "-"
web02    | 172.24.0.3 - - [26/Jun/2019:10:03:06 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" "-"
web01    | 172.24.0.1 - - [26/Jun/2019:10:03:07 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" "-"
web03    | 172.24.0.3 - - [26/Jun/2019:10:03:07 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" "-"
web02    | 172.24.0.3 - - [26/Jun/2019:10:05:25 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" "-"
web01    | 172.24.0.1 - - [26/Jun/2019:10:05:25 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" "-"
web03    | 172.24.0.3 - - [26/Jun/2019:10:05:26 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" "-"
web01    | 172.24.0.1 - - [26/Jun/2019:10:05:26 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" "-"
web02    | 172.24.0.3 - - [26/Jun/2019:10:05:27 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" "-"
web01    | 172.24.0.1 - - [26/Jun/2019:10:05:27 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" "-"
web01    | 172.24.0.1 - - [26/Jun/2019:10:05:27 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" "-"
web03    | 172.24.0.3 - - [26/Jun/2019:10:05:27 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" "-"

加權輪詢

加權輪詢是指基於上文中的輪詢,加上一定的權重,使得某個伺服器被轉發的次數更多一些,加權通過在IP後方拼接weight實現。

upstream backend {
    server web02:80 weight=5;
    server web03:80 weight=1;
}

weight值越大,所分配的請求就越多,根據nginx的請求日誌可以看出web02被訪問了8次,web03被訪問了兩次。

web01    | 172.24.0.1 - - [26/Jun/2019:14:48:13 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" "-"
web02    | 172.24.0.3 - - [26/Jun/2019:14:48:13 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" "-"
web02    | 172.24.0.3 - - [26/Jun/2019:14:48:16 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" "-"
web01    | 172.24.0.1 - - [26/Jun/2019:14:48:16 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" "-"
web01    | 172.24.0.1 - - [26/Jun/2019:14:48:16 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" "-"
web02    | 172.24.0.3 - - [26/Jun/2019:14:48:16 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" "-"
web01    | 172.24.0.1 - - [26/Jun/2019:14:48:17 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" "-"
web03    | 172.24.0.3 - - [26/Jun/2019:14:48:17 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" "-"
web01    | 172.24.0.1 - - [26/Jun/2019:14:48:17 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" "-"
web02    | 172.24.0.3 - - [26/Jun/2019:14:48:17 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" "-"
web02    | 172.24.0.3 - - [26/Jun/2019:14:48:18 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" "-"
web01    | 172.24.0.1 - - [26/Jun/2019:14:48:18 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" "-"
web01    | 172.24.0.1 - - [26/Jun/2019:14:48:19 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" "-"
web02    | 172.24.0.3 - - [26/Jun/2019:14:48:19 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" "-"
web01    | 172.24.0.1 - - [26/Jun/2019:14:48:19 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" "-"
web02    | 172.24.0.3 - - [26/Jun/2019:14:48:19 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" "-"
web01    | 172.24.0.1 - - [26/Jun/2019:14:48:20 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" "-"
web02    | 172.24.0.3 - - [26/Jun/2019:14:48:20 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" "-"
web03    | 172.24.0.3 - - [26/Jun/2019:14:48:20 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" "-"
web01    | 172.24.0.1 - - [26/Jun/2019:14:48:20 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" "-"

ip_hash

如果一個客戶端的請求可能被轉發到任何一臺伺服器,可能會導致每台伺服器都緩存了該客戶端的信息,比較浪費資源,ip_hash作用就是通過客戶端IP的前三個部分計算出key,以便將同一個客戶端的請求轉發到指定的伺服器中,配置的方法比較簡單,只需要在upstream上方加一行代碼即可。

upstream backend {
    ip_hash;
    server web02:80;
    server web03:80;
}

重覆請求了5次,通過日誌看出請求一直被鎖定在了web03上。

web01    | 172.24.0.1 - - [27/Jun/2019:11:48:15 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" "-"
web03    | 172.24.0.2 - - [27/Jun/2019:11:48:15 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" "172.24.0.1"
web01    | 172.24.0.1 - - [27/Jun/2019:11:48:22 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" "-"
web03    | 172.24.0.2 - - [27/Jun/2019:11:48:22 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" "172.24.0.1"
web03    | 172.24.0.2 - - [27/Jun/2019:11:48:23 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" "172.24.0.1"
web01    | 172.24.0.1 - - [27/Jun/2019:11:48:23 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" "-"
web03    | 172.24.0.2 - - [27/Jun/2019:11:48:24 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" "172.24.0.1"
web01    | 172.24.0.1 - - [27/Jun/2019:11:48:24 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" "-"
web01    | 172.24.0.1 - - [27/Jun/2019:11:48:24 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" "-"
web03    | 172.24.0.2 - - [27/Jun/2019:11:48:24 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" "172.24.0.1"

url_hash

這種模式是將url進行哈希,然後定位到具體的某一臺伺服器,相同的url永遠會被轉發到同一臺伺服器上。

upstream backend {
    hash $request_uri;
    server web02:80;
    server web03:80;
}

通過瀏覽器訪問兩次http://localhost:8080,通過日誌可以看到轉發被固定在了web03。

web01    | 172.24.0.1 - - [27/Jun/2019:11:58:31 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" "-"
web03    | 172.24.0.4 - - [27/Jun/2019:11:58:31 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" "-"
web03    | 172.24.0.4 - - [27/Jun/2019:11:58:38 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" "-"
web01    | 172.24.0.1 - - [27/Jun/2019:11:58:38 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" "-"

在鏈接後面拼接一些參數,就可以看到轉發發生了變化,固定在了web02,隨著參數的變化,請求也會採用輪詢的方式轉發給伺服器,但使用過的url再訪問就不會再變化了。

web02    | 172.24.0.4 - - [27/Jun/2019:11:59:54 +0000] "GET /?a=1 HTTP/1.0" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" "-"
web01    | 172.24.0.1 - - [27/Jun/2019:11:59:54 +0000] "GET /?a=1 HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" "-"
web02    | 172.24.0.4 - - [27/Jun/2019:11:59:55 +0000] "GET /?a=1 HTTP/1.0" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" "-"
web01    | 172.24.0.1 - - [27/Jun/2019:11:59:55 +0000] "GET /?a=1 HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" "-"

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 第五章 解構:使數據訪問更便捷 對象解構 let node = { type: 'id', name: 'foo', }; let {type, name} = node; // type === 'id', name === 'foo' let {type: TYPE, name: NAME} = ...
  • 你不知道的js——作用域 1 作用域 1.1編譯原理 在傳統編譯語言的流程中,程式中的一段源代碼在執行之前會經歷三個步驟,統稱為“編 譯” 1分詞/詞法分析(Tokenizing/Lexing) 會將由字元組成的字元串分解成(對編程語言來說)有意義的代碼塊,這些代 碼塊被稱為詞法單元(token) ...
  • 獲取客戶端公網IP 獲取客戶端內網IP(IE不支持) 原文鏈接:https://ourcodeworld.com/articles/read/257/how-to-get-the-client-ip-address-with-javascript-only ...
  • 單個的點擊展示下拉菜單比較簡單。比如點擊右上角的設置按鈕會彈出修改密碼和退出功能等。 多個的點擊式展示下拉菜單,在之前有發過。 ...
  • 記錄對象的一些實用使用方法及屬性 // Object.assign() 多個對象合併 key相同則後面的覆蓋前面的 const target = { a: 1, b: 2 }; const source = { b: 4, c: 5 }; const returnedTarget = Object. ...
  • 1.Javascript是什麼?可以做什麼? 1) JavaScript是一種基於對象和事件驅動的解釋性腳本語言, 它具有與Java和C語言類似的語法。 2) JavaScript可直接嵌入HTML頁面。由客戶端的瀏覽器解釋執行代碼,不進行預編譯。 3) 所有現代的html頁面都使用Javascri ...
  • ...
  • 現在越來越多的項目就算是一個管理後端也偏向於使用前後端分離的部署方式去做,為了順應時代的潮流,一前後端分離就產生了跨域問題,所以許多同學把跨域和前後端分離項目聯繫在了一起,其實跨域產生的原因並不是前後端分離導致的,那我們一起來看一下,希望可以靠這一篇文章解答大家所有的跨域問題 一、跨域產生的條件 使 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...