Table of Contents
Overall Architecture
Home Operation and Maintenance Nginx How to use Consul-template+Nginx to implement Thrift Consul load balancing

How to use Consul-template+Nginx to implement Thrift Consul load balancing

May 15, 2023 am 08:19 AM
nginx

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.

  1. Consul Cluster is used for service registration, registering multiple service instances, and providing RPC services to the outside world.

  2. Consul-template is used to monitor the status of services in Consul in real time, and generates Nginx## with its own template file. # configuration file.

  3. Nginx Use your own configuration file and the configuration file generated in the second step for load balancing.

Nginx installation

  1. Install the latest version of

    Nginx, ensure that the Nginx version is 1.9.0 The above

  2. 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-stream parameter indicates that TCP is already supported. If not, recompile and add parameters for installation.


  3. My Nginx is installed in the

    /etc/nginx directory

  4. After the installation is completed, use

    nginx -t to monitor whether it is successful.

Consul-template

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
Copy after login
3. Find a place Create a new folder and create three files

4.

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.

5.

ConsulBlock configurationConsulServer 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"
  }


}
Copy after login
6.

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"
  }
}
Copy after login
7.

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;
    }
}
Copy after login
8. Start

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. conf

9. 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;
    }
}
Copy after login
Copy after login
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;
    }
}
Copy after login
Copy after login
Start one more and the service list becomes two

[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;
    }
}
Copy after login
12.

thriftThe client only needs to configure when callingNginx 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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to configure nginx in Windows How to configure nginx in Windows Apr 14, 2025 pm 12:57 PM

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.

How to check the name of the docker container How to check the name of the docker container Apr 15, 2025 pm 12:21 PM

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).

How to start containers by docker How to start containers by docker Apr 15, 2025 pm 12:27 PM

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 check whether nginx is started How to check whether nginx is started Apr 14, 2025 pm 01:03 PM

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.

How to create containers for docker How to create containers for docker Apr 15, 2025 pm 12:18 PM

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]

How to check nginx version How to check nginx version Apr 14, 2025 am 11:57 AM

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 cloud server domain name in nginx How to configure cloud server domain name in nginx Apr 14, 2025 pm 12:18 PM

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.

How to start nginx server How to start nginx server Apr 14, 2025 pm 12:27 PM

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

See all articles