


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-template3. 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.
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" } }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" } }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; } }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. 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; } }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; } }12.
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!

NGINXUnitischosenfordeployingapplicationsduetoitsflexibility,easeofuse,andabilitytohandledynamicapplications.1)ItsupportsmultipleprogramminglanguageslikePython,PHP,Node.js,andJava.2)Itallowsdynamicreconfigurationwithoutdowntime.3)ItusesJSONforconfigu

NGINX can be used to serve files and manage traffic. 1) Configure NGINX service static files: define the listening port and file directory. 2) Implement load balancing and traffic management: Use upstream module and cache policies to optimize performance.

NGINX is suitable for handling high concurrency and static content, while Apache is suitable for dynamic content and complex URL rewrites. 1.NGINX adopts an event-driven model, suitable for high concurrency. 2. Apache uses process or thread model, which is suitable for dynamic content. 3. NGINX configuration is simple, Apache configuration is complex but more flexible.

NGINX and Apache each have their own advantages, and the choice depends on the specific needs. 1.NGINX is suitable for high concurrency, with simple deployment, and configuration examples include virtual hosts and reverse proxy. 2. Apache is suitable for complex configurations and is equally simple to deploy. Configuration examples include virtual hosts and URL rewrites.

The purpose of NGINXUnit is to simplify the deployment and management of web applications. Its advantages include: 1) Supports multiple programming languages, such as Python, PHP, Go, Java and Node.js; 2) Provides dynamic configuration and automatic reloading functions; 3) manages application lifecycle through a unified API; 4) Adopt an asynchronous I/O model to support high concurrency and load balancing.

NGINX started in 2002 and was developed by IgorSysoev to solve the C10k problem. 1.NGINX is a high-performance web server, an event-driven asynchronous architecture, suitable for high concurrency. 2. Provide advanced functions such as reverse proxy, load balancing and caching to improve system performance and reliability. 3. Optimization techniques include adjusting the number of worker processes, enabling Gzip compression, using HTTP/2 and security configuration.

The main architecture difference between NGINX and Apache is that NGINX adopts event-driven, asynchronous non-blocking model, while Apache uses process or thread model. 1) NGINX efficiently handles high-concurrent connections through event loops and I/O multiplexing mechanisms, suitable for static content and reverse proxy. 2) Apache adopts a multi-process or multi-threaded model, which is highly stable but has high resource consumption, and is suitable for scenarios where rich module expansion is required.

NGINX is suitable for handling high concurrent and static content, while Apache is suitable for complex configurations and dynamic content. 1. NGINX efficiently handles concurrent connections, suitable for high-traffic scenarios, but requires additional configuration when processing dynamic content. 2. Apache provides rich modules and flexible configurations, which are suitable for complex needs, but have poor high concurrency performance.


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

WebStorm Mac version
Useful JavaScript development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Zend Studio 13.0.1
Powerful PHP integrated development environment

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
