OpenResty 使用 balancer_by_lua_block 做负载均衡
OpenResty 使用 balancer_by_lua_block 做负载均衡,需用到:
1)OpenResty的"ngx.balancer"模块
2)需调用 balancer.set_current_peer("127.0.0.1", port_random) 设置ip + port
编辑nginx配置文件:
cd /usr/local/openresty/nginx/
vim conf/nginx.conf
配置如下:
user www www;
worker_processes 4;
error_log logs/error.log;
pid logs/nginx.pid;
events {
use epoll;
multi_accept on;
worker_connections 102400;
}
## 处理 HTTP、HTTPS
http {
include mime.types;
default_type application/octet-stream;
access_log logs/access.log main;
keepalive_timeout 65;
gzip on;
# 关闭lua缓存, 修改外部lua脚本时, 不用重启nginx
lua_code_cache off;
## server 8088
server {
listen 8088;
server_name localhost;
charset utf-8;
location /balancer {
default_type text/html;
content_by_lua_block {
ngx.say("balancer to 8088");
}
}
}
## server 8089
server {
listen 8089;
server_name localhost;
charset utf-8;
location /balancer {
default_type text/html;
content_by_lua_block {
ngx.say("balancer to 8089");
}
}
}
## 负载均衡[8088,8089]
server {
listen 8099;
listen 8080;
server_name localhost;
charset utf-8;
location /balancer {
default_type text/html;
proxy_pass http://server_balancer;
}
}
## 负载平衡: 8088, 8089
upstream server_balancer {
server 0.0.0.0;
balancer_by_lua_block {
local balancer = require "ngx.balancer"
-- 内部端口映射服务
local port = { 8088, 8089 }
local port_random = ""
-- 1. 入参端口做hash映射
local port_params = ngx.req.get_uri_args()["port"] or 0
ngx.log(ngx.ERR, "port_params = ", port_params)
-- 2. 入参"ip:port"做hash映射
local var_port = ngx.var.server_port
local var_ip = ngx.var.remote_addr
local var_key = var_ip .. ":" .. var_port
local var_hash = ngx.crc32_long(var_key)
local var_idx = (var_hash % 2) + 1
port_random = port[var_idx]
ngx.log(ngx.ERR, "var_port = ", var_port, ", var_ip = ", var_ip, ", var_key = ", var_key,
", var_hash = ", var_hash, ", var_idx = ", var_idx, ", port_random = ", port_random);
-- 3. 获取系统随机数做hash
math.randomseed(ngx.time())
port_params = math.random(1, 100)
local hash = (port_params % 2) + 1
ngx.log(ngx.ERR, "hash = ", hash)
port_random = port[hash]
ngx.log(ngx.ERR, "port_random = ", port_random)
ngx.log(ngx.ERR, "port_params = ", port_params, ", hash = ", hash, ", port_random = ", port_random)
local ok, err = balancer.set_current_peer("127.0.0.1", port_random)
if not ok then
ngx.log(ngx.ERR, "failed to set the current peer: ", err)
return ngx.exit(500)
end
ngx.log(ngx.DEBUG, "current peer success, port_random = ", port_random)
}
}
}
执行nginx命令,使脚本生效:
# openresty -t
nginx: [alert] lua_code_cache is off; this will hurt performance in /usr/local/openresty/nginx/conf/nginx.conf:40
nginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successful
#
# openresty -s reload
nginx: [alert] lua_code_cache is off; this will hurt performance in /usr/local/openresty/nginx/conf/nginx.conf:40
访问链接,验证结果:
1)http://48.10.204.18:8088/balancer?port=2
显示结果:balancer to 8088
2)http://48.10.204.18:8088/balancer?port=2
显示结果:balancer to 8089
3)http://48.10.204.18:8080/balancer?port=2
显示结果:balancer to 8088 或 balancer to 8089 (随机显示端口号)
参考推荐:
OpenResty = Nginx + Lua 介绍和安装教程
版权所有: 本文系米扑博客原创、转载、摘录,或修订后发表,最后更新于 2025-03-24 16:45:12
侵权处理: 本个人博客,不盈利,若侵犯了您的作品权,请联系博主删除,莫恶意,索钱财,感谢!