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

整體架構(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ù)器的地址,模板文件的位置,生成的配置文件的位置等等。除了consul和template塊,其他參數(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ā)。

? 版權(quán)聲明
THE END
喜歡就支持一下吧
點(diǎn)贊5 分享