首頁  >  文章  >  運維  >  怎麼用Consul-template+Nginx實現Thrift Consul負載平衡

怎麼用Consul-template+Nginx實現Thrift Consul負載平衡

王林
王林轉載
2023-05-15 08:19:10748瀏覽

整體架構

我們先看下整個框架的架構是什麼樣子的,這裡我們有三個服務提供者和三個服務呼叫者,它們透過ConsulNginx,以及Consul-template 來實現負載平衡。

說明 本範例是進行 RPC 的負載平衡,RPC 是 tcp協議,所以 Nginx 要設定 tcp 模組,支援 tcp 負載平衡。

  1. Consul 叢集 用於服務註冊,註冊多個服務實例,對外提供 RPC 服務。

  2. Consul-template 來即時監控Consul 中服務的狀態,配合自身一個模板文件,產生Nginx 的設定檔。

  3. Nginx 使用自身的設定檔和第二步驟產生的設定文件,進行負載平衡。

Nginx安裝

  1. 安裝最新版Nginx,保證Nginx 版本在1.9.0以上

  2. 1.9.0  版本以上才支援TCP 轉發,據說不是預設安裝了該模組,安裝完成可以查詢一下,如果有-- with-stream參數,表示已經支援TCP。如果沒有就重新編譯增加參數安裝。


  3. 我的Nginx 安裝在/etc/nginx目錄下

  4. 安裝完成使用nginx -t監控一下是否成功。

Consul-template

本文旨在負載平衡,Consul 叢集建立不作介紹。

1.下載對應系統版本檔 

2.解壓縮,並複製到PATH路徑下

[silence@centos145 ~]$ tar xzvf consul-template_0.19.4_linux_amd64.tgz
[silence@centos145 ~]$ mv ./consul-template /usr/sbin/consul-template

3.找地方新建個資料夾,並建立三個檔案

4.config.hcl主要用來配置consul-template的啟動參數項,包括consul 伺服器的位址,模板檔案的位置,產生的設定檔的位置等等。除了consultemplate區塊,其他參數可選。

5.Consul區塊配置Consul伺服器位址和連接埠

consul {
  auth {
    enabled  = false
    username = "test"
    password = "test"
  }


  address = "172.20.132.196:8500"
  retry {
    enabled = true
    attempts = 12
    backoff = "250ms"
    max_backoff = "1m"
  }


}

6.template區塊配置模板的路徑和生成文件的位置,以及生成文件後需要執行的命令。在我們這裡我們需要nginx重新載入設定文件,所以設定的指令為nginx -s reload

template {
  source = "/etc/nginx/consul-template/template.ctmpl"
  destination = "/etc/nginx/consul-template/nginx.conf"
  create_dest_dirs = true
  command = "/usr/sbin/nginx -s reload"
  command_timeout = "30s"
  error_on_missing_key = false
  perms = 0600
  backup = true
  left_delimiter  = "{{"
  right_delimiter = "}}"
  wait {
    min = "2s"
    max = "10s"
  }
}

7.template.ctmpl寫,因為這裡只需要伺服器位址和連接埠號碼就可以,所以範本檔案如下:

[root@centos145 consul-template]# cat template.ctmpl
stream {


    log_format main '$remote_addr - [$time_local] '
      '$status';


    access_log /var/log/nginx/tcp_access.log main;


    upstream cloudsocket {
 \{\{range service "ad-rpc-device-server"}}server \{\{.Address}}:\{\{.Port}};{{end}}
    }


    server {
 listen 8888;
 proxy_pass cloudsocket;
    }
}

8.啟動consul-template  consul-template -config=./config .hcl

使用config.hcl設定檔是為了簡化指令consul-template -consul-addr=172.20.132.196:8500 -template=./template.ctmpl:./nginx. conf

9.初始的nignx.conf檔案為空的,在啟動後內容為

[root@centos145 consul-template]# cat nginx.conf
stream {


    log_format main '$remote_addr - [$time_local] '
      '$status';


    access_log /var/log/nginx/tcp_access.log main;


    upstream cloudsocket {
 server 172.20.139.77:8183;
    }


    server {
 listen 8888;
 proxy_pass cloudsocket;
    }
}

確保服務已經成功註冊到Consul中,即可以看到伺服器位址和連接埠已經配置進去了。

10.在nginx的安裝目錄的nginx.conf中引入consul-template產生的設定檔 include /etc/nginx/consul-template/nginx.conf;

#注意產生的設定檔不能喝nginx本身的設定檔內容重複! ! !

11.啟動一個服務實例,查看產生的nginx.conf檔案會發現在upstream cloudsocket{}中會動態增加服務列表,並且隨著服務的加入和離開,動態變化。

[root@centos145 consul-template]# cat nginx.conf
stream {


    log_format main '$remote_addr - [$time_local] '
      '$status';


    access_log /var/log/nginx/tcp_access.log main;


    upstream cloudsocket {
 server 172.20.139.77:8183;
    }


    server {
 listen 8888;
 proxy_pass cloudsocket;
    }
}

再啟動一個,服務清單變成兩個了

[root@centos145 consul-template]# cat nginx.conf
stream {


    log_format main '$remote_addr - [$time_local] '
      '$status';


    access_log /var/log/nginx/tcp_access.log main;


    upstream cloudsocket {
 server 172.20.139.77:8183;server 172.20.139.77:8184;
    }


    server {
 listen 8888;
 proxy_pass cloudsocket;
    }
}

12.thrift客戶端在呼叫的時候只需要設定Nginx#的位址和連接埠就可以了,不需要設定服務的位址和連接埠了,Nginx會自動做轉送。

以上是怎麼用Consul-template+Nginx實現Thrift Consul負載平衡的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除