<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                ### 1. 配置Splash服務 {#1-配置splash服務} 要搭建 Splash 負載均衡首先我們需要有多個 Splash 服務,假如在這里我在四臺遠程主機的 8050 端口上都開啟了 Splash 服務,它們的服務地址分別為:41.159.27.223:8050、41.159.27.221:8050、41.159.27.9:8050、41.159.117.119:8050,四個服務完全一致,都是通過 Docker 的 Splash 鏡像開啟的,訪問任何一個服務都可以使用 Splash 服務。 ### 2. 配置負載均衡 {#2-配置負載均衡} 接下來我們可以選用任意一臺帶有公網 IP 的主機來配置負載均衡,首先需要在這臺主機上裝好 Nginx,然后修改 Nginx 的配置文件 nginx.conf,添加如下內容: ``` http { upstream splash { least_conn; server 41.159 .27 .223 : 8050 ; server 41.159 .27 .221 : 8050 ; server 41.159 .27 .9 : 8050 ; server 41.159 .117 .119 : 8050 ; } server { listen 8050 ; location / { proxy_pass http://splash; } } } ``` 這樣我們通過 upstream 字段定義了一個名字叫做 splash 的服務集群配置,least\_conn 代表最少鏈接負載均衡,它適合處理請求處理時間長短不一造成服務器過載的情況。 或者我們也可以不指定配置,配置如下: ``` upstream splash { server 41.159 .27 .223 : 8050 ; server 41.159 .27 .221 : 8050 ; server 41.159 .27 .9 : 8050 ; server 41.159 .117 .119 : 8050 ; } ``` 這樣默認以輪詢策略實現負載均衡,每個服務器的壓力相同,此策略適合服務器配置相當,無狀態且短平快的服務使用。 另外我們還可以指定權重,配置如下: ``` upstream splash { server 41.159 .27 .223 : 8050 weight= 4 ; server 41.159 .27 .221 : 8050 weight= 2 ; server 41.159 .27 .9 : 8050 weight= 2 ; server 41.159 .117 .119 : 8050 weight= 1 ; } ``` 我們通過 weight 指定了各個服務的權重,權重越高分配到處理的請求越多,假如不同的服務器配置差別比較大的話,就可以使用此種配置。 最后還有一種 IP 哈希負載均衡,配置如下: ``` upstream splash { ip_hash; server 41.159 .27 .223 : 8050 ; server 41.159 .27 .221 : 8050 ; server 41.159 .27 .9 : 8050 ; server 41.159 .117 .119 : 8050 ; } ``` 服務器根據請求客戶端的 IP 地址進行哈希計算,確保使用同一個服務器響應請求,這種策略適合有狀態的服務,如用戶登錄后訪問某個頁面的情形。不過對于 Splash 來說不需要。 我們可以根據不同的情形選用不同的配置,配置完成后重啟一下 Nginx 服務: ``` sudo nginx -s reload ``` 這樣直接訪問 Nginx 所在服務器的 8050 端口即可實現負載均衡了。 ### 3. 配置認證 {#3-配置認證} 現在 Splash 是公開訪問的,如果我們不想讓其被公開訪問還可以配置認證,仍然借助于 Nginx 即可,可以在 server 的 location 字段中添加一個 auth\_basic 和 auth\_basic\_user\_file 字段,配置如下: ``` http { upstream splash { least_conn; server 41.159 .27 .223 : 8050 ; server 41.159 .27 .221 : 8050 ; server 41.159 .27 .9 : 8050 ; server 41.159 .117 .119 : 8050 ; } server { listen 8050 ; location / { proxy_pass http://splash; auth_basic "Restricted" ; auth_basic_user_file /etc/nginx/conf.d/.htpasswd; } } } ``` 在這里使用的用戶名密碼配置放置在 /etc/nginx/conf.d 目錄,我們需要使用 htpasswd 命令創建,例如創建一個用戶名為 admin 的文件,命令如下: ``` htpasswd -c .htpasswd admin ``` 接下就會提示我們輸入密碼,輸入兩次之后,就會生成密碼文件,查看一下內容: ``` cat .htpasswd admin:5ZBxQr0rCqwbc ``` 配置完成之后我們重啟一下 Nginx 服務,運行如下命令: ``` sudo nginx -s reload ``` 這樣訪問認證就成功配置好了。 ### 4. 測試 {#4-測試} 最后我們可以用代碼來測試一下負載均衡的配置,看看到底是不是每次請求會切換IP,利用[http://httpbin.org/get](http://httpbin.org/get)測試即可,代碼實現如下: ``` import requests from urllib.parse import quote import re lua = ''' function main(splash, args) local treat = require("treat") local response = splash:http_get("http://httpbin.org/get") return treat.as_string(response.body) end ''' url = 'http://splash:8050/execute?lua_source=' + quote(lua) response = requests.get(url, auth=( 'admin' , 'admin' )) ip = re.search( '(\d+\.\d+\.\d+\.\d+)' , response.text).group( 1 ) print(ip) ``` 這里的 URL 中的 splash 請自行替換成自己的 Nginx 服務器 IP,在這里我修改了 Hosts 添加了 splash 別名。 多次運行代碼之后可以發現每次請求的 IP 都會變化: 如第一次的結果: ``` 41.159.27.223 ``` 第二次的結果: ``` 41.159.27.9 ``` 這就說明負載均衡已經成功實現了。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看