App下載

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

猿友 2020-08-20 10:46:19 瀏覽數(shù) (4690)
反饋

今天給大家分享一個(gè)采用 Consul 實(shí)現(xiàn)的負(fù)載均衡的方案,很多小伙伴都知道 Nginx 可以實(shí)現(xiàn)負(fù)載均衡,但是可能沒(méi)實(shí)現(xiàn)過(guò)結(jié)合 Consul,今天就給大家分享一下。

整體架構(gòu)

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

整體架構(gòu)

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

  1. Consul 集群 用于服務(wù)注冊(cè),注冊(cè)多個(gè)服務(wù)實(shí)例,對(duì)外提供 RPC 服務(wù)。
  2. Consul-template 用于實(shí)時(shí)監(jiān)測(cè) 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ù)說(shuō)不是默認(rèn)安裝了該模塊,安裝完成可以查詢一下,如果有--with-stream參數(shù),表示已經(jīng)支持TCP。如果沒(méi)有就重新編譯增加參數(shù)安裝。
  3. Nginx 安裝
  4. 我的 Nginx 安裝在/etc/nginx目錄下
  5. 安裝完成使用nginx -t監(jiān)測(cè)一下是否成功。

Consul-template

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

1.下載對(duì)應(yīng)系統(tǒng)版本文件 https://releases.hashicorp.com/consul-template/

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è)文件

創(chuàng)建文件

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

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編寫(xiě),因?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配置文件是為了簡(jiǎn)化命令 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)成功注冊(cè)到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ù)的加入和離開(kāi),動(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ā)。

(推薦教程:Nginx 入門(mén)指南

總結(jié)

今天給大家介紹了一個(gè)新的負(fù)載均衡實(shí)現(xiàn)方案,這種方案對(duì)于一些小規(guī)模的集群還是很不錯(cuò)的,當(dāng)然如果是大集群,還是采用阿里云或者騰訊云提供的方案才是最好的。想自己實(shí)現(xiàn)的小伙伴環(huán)境去嘗試搭建一下,還是很好玩的。

文章來(lái)源:公眾號(hào)-- Java極客技術(shù)

作者:鴨血粉絲

以上就是關(guān)于Consul-template+Nginx 實(shí)現(xiàn)Thrift Consul負(fù)載均衡的相關(guān)介紹了,希望對(duì)大家有所幫助。

0 人點(diǎn)贊