ホームページ  >  記事  >  運用・保守  >  Consul-template+Nginx を使用して Thrift Consul 負荷分散を実装する方法

Consul-template+Nginx を使用して Thrift Consul 負荷分散を実装する方法

王林
王林転載
2023-05-15 08:19:10749ブラウズ

全体のアーキテクチャ

まず、フレームワーク全体のアーキテクチャがどのようなものかを見てみましょう。ここには、Consul と ## を渡す 3 つのサービス プロバイダーと 3 つのサービス呼び出し元があります。 #Nginx および Consul-template を使用して負荷分散を実現します。

説明 この例は RPC ロード バランシング用です。RPC は tcp プロトコルであるため、Nginx は tcp ロード バランシングをサポートするように tcp モジュールを構成する必要があります。

  1. Consul クラスターは、サービスの登録、複数のサービス インスタンスの登録、および外部への #RPC サービスの提供に使用されます。

  2. Consul-template は、Consul のサービスのステータスをリアルタイムで監視するために使用され、次のように Nginx## を生成します。独自のテンプレート ファイル # 設定ファイル。

  3. Nginx

    独自の構成ファイルと、負荷分散の 2 番目のステップで生成された構成ファイルを使用します。

  4. Nginx のインストール

    最新バージョンの
  1. Nginx

    をインストールし、Nginx バージョンがis 1.9.0 上記

  2. 1.9.0 バージョン以降では、
  3. TCP

    フォワーディングのみがサポートされており、このモジュールはデフォルトではインストールされていないとのことです。 -- with-stream パラメータがある場合は、TCP がすでにサポートされていることを示します。そうでない場合は、再コンパイルして、インストール用のパラメーターを追加します。


  4. 私の Nginx は
  5. /etc/nginx

    ディレクトリにインストールされています

  6. インストールが完了したら、
  7. nginx -t

    を使用して、インストールが成功したかどうかを監視します。

  8. Consul-template

この記事は負荷分散を目的としており、Consul クラスターの構築については紹介しません。

1. 対応するシステム バージョン ファイルをダウンロードします

2. それを解凍し、

PATH

パス <pre class="brush:php;toolbar:false">[silence@centos145 ~]$ tar xzvf consul-template_0.19.4_linux_amd64.tgz [silence@centos145 ~]$ mv ./consul-template /usr/sbin/consul-template</pre>3. にコピーします。新しいフォルダーを作成し、3 つのファイルを作成します。

4.

config.hcl

は主に、consul-template の起動パラメーターを構成するために使用されます。 consul サーバーのアドレス、テンプレート ファイルの場所、生成された設定ファイルの場所など。 consul ブロックと template ブロックを除き、他のパラメーターはオプションです。 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 が必要なので、set コマンドは 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 .hclconfig.hcl 構成ファイルは、コマンド consul-template -consul-addr=172.20.132.196:8500 -template=./template.ctmpl: を簡素化するために使用されます。 /nginx.conf

9. 初期の

nignx.conf
ファイルは空であり、起動後の内容は

<pre class="brush:php;toolbar:false">[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;     } }</pre> であることを確認してください。 Consul への登録に成功すると、サーバーのアドレスとポートが設定されたことがわかります。

10.

consul-template
によって生成された設定ファイルを、

nginx インストール ディレクトリ nginx.conf に導入します。 include /etc/nginx/consul-template/nginx.conf;生成された設定ファイルは、nginx 自体の設定ファイルの内容を複製できないことに注意してください。 ! !

11. サービス インスタンスを開始し、生成された

nginx.conf
ファイルを確認します。サービス リストが

upstream Cloudsocket{}# に動的に追加されることがわかります。 ##、サービスの参加と離脱に応じて動的に変化します。 <pre class="brush:php;toolbar:false">[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;     } }</pre>もう 1 つ開始すると、サービス リストは 2 つになります<pre class="brush:php;toolbar:false">[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;     } }</pre>12.

thrift

クライアントは呼び出し時にのみ設定する必要があります

Nginxx

アドレスとポートがあれば十分です。サービスのアドレスとポートを設定する必要はありません。Nginx が自動的に転送します。

以上がConsul-template+Nginx を使用して Thrift Consul 負荷分散を実装する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はyisu.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。