溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么用Consul-template+Nginx實(shí)現(xiàn)Thrift Consul負(fù)載均衡

發(fā)布時(shí)間:2022-02-25 09:59:45 來源:億速云 閱讀:204 作者:iii 欄目:開發(fā)技術(shù)

今天小編給大家分享一下怎么用Consul-template+Nginx實(shí)現(xiàn)Thrift Consul負(fù)載均衡的相關(guān)知識(shí)點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識(shí),所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。

整體架構(gòu)

我們先看下整個(gè)框架的架構(gòu)是什么樣子的,這里我們有三個(gè)服務(wù)提供者和三個(gè)服務(wù)調(diào)用者,它們通過 ConsulNginx,以及 Consul-template 來實(shí)現(xiàn)負(fù)載均衡。

說明 本例子是進(jìn)行 RPC 的負(fù)載均衡,RPC 是 tcp協(xié)議,所以 Nginx 要配置 tcp 模塊,支持 tcp 負(fù)載均衡。

  1. Consul 集群 用于服務(wù)注冊,注冊多個(gè)服務(wù)實(shí)例,對外提供 RPC 服務(wù)。

  2. Consul-template 用于實(shí)時(shí)監(jiān)測 Consul 中服務(wù)的狀態(tài),配合自身一個(gè)模板文件,生成 Nginx 的配置文件。

  3. Nginx 使用自身的配置文件和第二步生成的配置文件,進(jìn)行負(fù)載均衡。

Nginx安裝

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

  2. 1.9.0  版本以上才支持 TCP 轉(zhuǎn)發(fā),據(jù)說不是默認(rèn)安裝了該模塊,安裝完成可以查詢一下,如果有--with-stream參數(shù),表示已經(jīng)支持TCP。如果沒有就重新編譯增加參數(shù)安裝。


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

  4. 安裝完成使用nginx -t監(jiān)測一下是否成功。

Consul-template

本文旨在負(fù)載均衡,Consul 集群搭建不作介紹。

1.下載對應(yīng)系統(tǒng)版本文件 

2.解壓,并復(fù)制到PATH路徑下

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

3.找個(gè)地方新建個(gè)文件夾,并創(chuàng)建三個(gè)文件

4.config.hcl主要用來配置consul-template的啟動(dòng)參數(shù)項(xiàng),包括consul服務(wù)器的地址,模板文件的位置,生成的配置文件的位置等等。除了consultemplate塊,其他參數(shù)可選。

5.Consul塊配置Consul服務(wù)器地址和端口

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塊配置模板的路徑和生成文件的位置,以及生成文件后需要執(zhí)行的命令。在我們這里我們需要nginx重新加載配置文件,所以設(shè)置的命令為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編寫,因?yàn)檫@里只需要服務(wù)器地址和端口號(hào)就可以,所以模板文件如下:

[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.啟動(dòng)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文件為空的,在啟動(dòng)后內(nèi)容為

[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;
    }
}

確保服務(wù)已經(jīng)成功注冊到Consul中,即可以看到服務(wù)器地址和端口已經(jīng)配置進(jìn)去了。

10.在nginx的安裝目錄的nginx.conf中引入consul-template生成的配置文件 include /etc/nginx/consul-template/nginx.conf;

注意生成的配置文件不能喝nginx本身的配置文件中內(nèi)容重復(fù)?。?!

11.啟動(dòng)一個(gè)服務(wù)實(shí)例,查看生成的nginx.conf文件會(huì)發(fā)現(xiàn)在upstream cloudsocket{}中會(huì)動(dòng)態(tài)增加服務(wù)列表,并且隨著服務(wù)的加入和離開,動(dòng)態(tài)變化。

[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;
    }
}

再啟動(dòng)一個(gè),服務(wù)列表變成兩個(gè)了

[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客戶端在調(diào)用的時(shí)候只需要配置Nginx的地址和端口就可以了,不需要配置服務(wù)的地址和端口了,Nginx會(huì)自動(dòng)做轉(zhuǎn)發(fā)。

以上就是“怎么用Consul-template+Nginx實(shí)現(xiàn)Thrift Consul負(fù)載均衡”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會(huì)為大家更新不同的知識(shí),如果還想學(xué)習(xí)更多的知識(shí),請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI