


How to use Consul-template+Nginx to implement Thrift Consul load balancing
Overall Architecture
Let’s first take a look at what the architecture of the entire framework looks like. Here we have three service providers and three service callers, which pass Consul
and Nginx
, and Consul-template
to achieve load balancing.
Explanation This example is for RPC load balancing. RPC is a tcp protocol, so Nginx needs to configure the tcp module to support tcp load balancing.
Consul
Cluster is used for service registration, registering multiple service instances, and providingRPC
services to the outside world.Consul-template
is used to monitor the status of services inConsul
in real time, and generatesNginx## with its own template file. # configuration file.
Nginx
Use your own configuration file and the configuration file generated in the second step for load balancing.
- Install the latest version of
Nginx
, ensure that the
Nginxversion is 1.9.0 The above
- 1.9.0 version or above only supports
TCP
forwarding. It is said that this module is not installed by default. You can check it after the installation is completed. If there is
-- The with-streamparameter indicates that
TCPis already supported. If not, recompile and add parameters for installation.
- My Nginx is installed in the
/etc/nginx
directory
- After the installation is completed, use
nginx -t
to monitor whether it is successful.
This article aims at load balancing and does not introduce the construction of Consul cluster.1. Download the corresponding system version file 2. Unzip it and copy it to the
PATH path
[silence@centos145 ~]$ tar xzvf consul-template_0.19.4_linux_amd64.tgz [silence@centos145 ~]$ mv ./consul-template /usr/sbin/consul-template
config.hcl is mainly used to configure the startup parameters of
consul-template, including
consul The address of the server, the location of the template file, the location of the generated configuration file, etc. Except for
consul and
template blocks, other parameters are optional.
ConsulBlock configuration
ConsulServer address and port
consul { auth { enabled = false username = "test" password = "test" } address = "172.20.132.196:8500" retry { enabled = true attempts = 12 backoff = "250ms" max_backoff = "1m" } }
templateThe path and sum of the block configuration template The location of the generated file, and the commands that need to be executed after generating the file. Here we need
nginx to reload the configuration file, so the set command is
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" } }
template.ctmpl Write, because only the server address and port number are needed here, the template file is as follows:
[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; } }
consul-template
consul-template -config=./config .hcl
The config.hcl configuration file is used to simplify the command consul-template -consul-addr=172.20.132.196:8500 -template=./template.ctmpl:./nginx. conf9. The initial
nignx.conf file is empty, and after startup the content is
[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; } }
Make sure the service has been successfully registered to Consul , you can see that the server address and port have been configured.10. Introduce the configuration file generated by
consul-template into
nginx.conf of the
nginx installation directory
include /etc/nginx/consul-template/nginx.conf;
Note that the generated configuration file cannot duplicate the content in nginx’s own configuration file! ! !11. Start a service instance and check the generated
nginx.conf file. You will find that the service list will be dynamically added in
upstream cloudsocket{}, and Dynamic changes as services join and leave.
[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; } }
thriftThe client only needs to configure when calling
Nginx The address and port are sufficient. There is no need to configure the address and port of the service.
Nginx will automatically forward it.
The above is the detailed content of How to use Consul-template+Nginx to implement Thrift Consul load balancing. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

Starting an Nginx server requires different steps according to different operating systems: Linux/Unix system: Install the Nginx package (for example, using apt-get or yum). Use systemctl to start an Nginx service (for example, sudo systemctl start nginx). Windows system: Download and install Windows binary files. Start Nginx using the nginx.exe executable (for example, nginx.exe -c conf\nginx.conf). No matter which operating system you use, you can access the server IP
