Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

OpenResty + Lua dynamically adds Zuul nodes

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Hidden worry

   in the Spring Cloud micro-service architecture, the gateway Zuul of the front door of all requests undertakes the main function of request forwarding and plays an important role in the back-end service. When the business volume soars, thanks to the horizontal expansion capability of Spring Cloud, often adding nodes and machines can greatly improve the system support, but only adding services without gateways will have performance bottlenecks. The conclusion from practical experience is that the processing capacity of a single Zuul is very limited, so the expansion nodes are often expanded together with services and Zuul, and then add a soft load to the upper layer of the request. Nginx is usually used (the Nginx evenly distributes the request to the Zuul load layer, which solves the problem "perfectly"), as shown below:

This approach shown above in    has two disadvantages in practice:

After the backend Zuul is running for a period of time, one of the Zuul is down, and some of the services requested by the front end are dead. The reason is simple: Nginx is not related to the back-end Zuul. After the Zuul goes down, Nginx will still distribute the request.

If you add a new machine and do a new scale-out for Zuul, you need to change the configuration of Nginx and configure the newly extended Zuul.

Solution:

   OpenResty integrates Nginx and Lua to implement a scalable Web platform with a large number of sophisticated Lu α libraries, third-party modules, and most dependencies. It can quickly build dynamic Web applications, Web services and dynamic gateways that deal with ultra-high concurrency and high scalability. We can use Lu α script module and registry to build a mechanism of dynamic increase and decrease of services, obtain services with registry status of UP through Lua, and dynamically add them to the equilibrium list of Nginx.

Practice

The    Spring Cloud Chinese community has opened up the relevant Lua plug-in source code (Github address) for the above scenario.

OpenResty installation and configuration

1. Environment

Yum-y install readline-devel pcre-devel openssl-devel gcc

2. Download and extract the OpenResty package

Wget https://openresty.org/download/openresty-1.13.6.1.tar.gz

Tar-zxvf openresty-1.13.6.1.tar.gz

3. Download the ngx_cache_purge module, which is used to clean the nginx cache

Cd openresty-1.13.6.1/bundle

Wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz

Tar-zxvf ngx_cache_purge-2.3.tar.gz

Download the nginx_upstream_check_module module, which is used for upstream health check

Wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/v0.3.0.tar.gz

Tar-zxvf v0.3.0.tar.gz

5. Increase in OpenResty configuration

Cd openresty-1.13.6.1

. / configure-- prefix=/usr/local/openresty-- with-http_realip_module-- with-pcre-- with-luajit-- add-module=./bundle/ngx_cache_purge-2.3/-- add-module=./bundle/nginx_upstream_check_module-0.3.0/-J2

6. Compile and install

Make

Make install

7. OpenResty does not have a http module and needs to be installed separately

Cd / usr/local/openresty/lualib/resty

Wget https://raw.githubusercontent.com/pintsized/lua-resty-http/master/lib/resty/http_headers.lua

Wget https://raw.githubusercontent.com/pintsized/lua-resty-http/master/lib/resty/http.lua

8. Copy the project script here

Copy dynamic_eureka_balancer.lua into this dir

9. Nginx configuration file

Vim / usr/local/openresty/nginx/conf/nginx.conf

The Nginx configuration is as follows

Http {

# sharing cache area

Lua_shared_dict dynamic_eureka_balancer 128m

Init_worker_by_lua_block {

-- introduce the Lua plug-in file

Local file = require "resty.dynamic_eureka_balancer"

Local balancer = file:new ({dict_name= "dynamic_eureka_balancer"})

-- Eureka server list

Balancer.set_eureka_service_url ({"127.0.0.1) 8888", "127.0.0.1) 9999"})

-- The service name that needs to be monitored

Balancer.watch_service ({"zuul", "client"})

}

Upstream springcloud_cn {

Server 127.0.0.1 because empty upstream block is rejected by nginx 666; # Required, because empty upstream block is rejected by nginx (nginx+ can use 'zone' instead)

Balancer_by_lua_block {

-- The zuul name that needs to be monitored

Local service_name = "zuul"

Local file = require "resty.dynamic_eureka_balancer"

Local balancer = file:new ({dict_name= "dynamic_eureka_balancer"})

-balancer.ip_hash (service_name)-IP Hash LB

Balancer.round_robin (service_name)-Round Robin LB

}

}

Server {

Listen 80

Server_name localhost

Location / {

Proxy_pass http://springcloud_cn/;

Proxy_set_header Host $http_host

Proxy_set_header X-Real-IP $remote_addr

Proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for

Proxy_set_header X-Forwarded-Proto $scheme

}

Error_page 500 502 503 504 / 50x.html

Location = / 50x.html {

Root html

}

}

}

The principle of    implementation is to use Lua scripts to regularly pull the information of the service according to the configured service name and Eureka address, provide / eureka/apps/ {serviceId} endpoints in Eureka, and return the registration information of the service, so we only need to take the service with a status of UP and add its address to the Nginx load list. This project enables dynamic awareness between Nginx and Zuul without manually configuring Nginx load and Zuul load, which is extremely friendly for application elastic expansion.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report