探索高效负载均衡与故障恢复:lua-resty-upstream 模块

探索高效负载均衡与故障恢复:lua-resty-upstream 模块

lua-resty-upstreamUpstream connection load balancing and failover module for Openresty项目地址:https://gitcode.com/gh_mirrors/lu/lua-resty-upstream

项目简介

lua-resty-upstream 是一个针对 OpenResty 的上游连接负载均衡和故障切换的 Lua 模块。它能够帮助你在 Nginx 上实现智能的请求分发策略,并在遇到服务失效时迅速做出反应。这个项目目前处于实验阶段,但已经稳定到可以用于生产环境。

项目技术分析

lua-resty-upstream 提供了几个核心组件:

  1. upstream.socket - 这个组件提供了一个 Lua TCP 客户端,支持多种连接方法,如 round-robin 负载均衡和基于健康检查的状态管理。你可以根据需求自定义连接行为。
  2. upstream.api - 允许你在运行时动态调整上游配置,例如创建、修改池和主机设置,这对于实时响应变化的服务环境非常有用。
  3. upstream.http - 包装了 lua-resty-http 库,提供了基于 HTTP 状态码的故障判断和重试机制。

应用场景

  1. 微服务架构 - 在分布式系统中,你需要有效地分配客户端请求到多个后端服务。
  2. 高可用性 - 当服务实例出现故障时,lua-resty-upstream 可以立即从服务列表中移除并尝试其他节点,确保业务连续性。
  3. 弹性扩展 - 根据负载自动调整服务的权重,平衡各个服务实例的压力。

项目特点

  1. 配置持久化 - 使用 Lua 共享字典来存储配置,即使在平滑重启后也能保持状态。
  2. 非阻塞处理 - process_failed_hosts 函数采用异步方式处理失败的主机,不会影响当前请求的性能。
  3. 灵活的负载均衡策略 - 支持轮询和基于权重的随机分配,还可以通过 upstream.api 动态调整。
  4. 健康检查 - 基于 HTTP 的健康检查功能可轻松识别和隔离异常服务。
  5. API 友好 - 提供清晰的 Lua API,易于集成和扩展。

示例代码

lua_shared_dict my_upstream_dict 1m;
init_by_lua '
    upstream_socket  = require("resty.upstream.socket")
    upstream_api = require("resty.upstream.api")

    upstream, configured = upstream_socket:new("my_upstream_dict")
    ...

    server {
        location / {
            content_by_lua '
                local sock, err = upstream:connect()
                upstream:process_failed_hosts()
            ';
        }
    }
';

总的来说,lua-resty-upstream 是一个强大且灵活的工具,为你的 Nginx 配置带来了更高级别的灵活性和可靠性。无论你是 Nginx 或者 OpenResty 的开发者,这个模块都值得添加到你的工具箱中。如果你正在寻求一种高性能的负载均衡解决方案,那么不妨试试 lua-resty-upstream

lua-resty-upstreamUpstream connection load balancing and failover module for Openresty项目地址:https://gitcode.com/gh_mirrors/lu/lua-resty-upstream

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Lua-Resty-Checkups是一个基于luaupstream管理和健康检查模块,由又拍云开源。特点:支持周期性upstream服务管理操作支持管理和健康检查支持upstream动态更新有利于加权轮询或哈希平衡支持 Nginx C upstream同步操作可使用级别和键值实现集群使用简介:-- config.lua _M = {} _M.global = {     checkup_timer_interval = 15,     checkup_shd_sync_enable = true,     shd_config_timer_interval = 1, } _M.ups1 = {     cluster = {         {             servers = {                 {host="127.0.0.1", port=4444, weight=10, max_fails=3, fail_timeout=10},             }         },     }, }lua_package_path "/path/to/lua-resty-checkups/lib/checkups/?.lua;/path/to/config.lua;;"; lua_shared_dict state 10m; lua_shared_dict mutex 1m; lua_shared_dict locks 1m; lua_shared_dict config 10m; server {     listen 12350;     return 200 12350; } server {     listen 12351;     return 200 12351; } init_worker_by_lua_block {     local config = require "config"     local checkups = require "resty.checkups.api"     checkups.prepare_checker(config)     checkups.create_checker() } server {     location = /12350 {         proxy_pass http://127.0.0.1:12350/;     }     location = /12351 {         proxy_pass http://127.0.0.1:12351/;     }     location = /t {         content_by_lua_block {             local checkups = require "resty.checkups.api"             local callback = function(host, port)                 local res = ngx.location.capture("/" .. port)                 ngx.say(res.body)                 return 1             end             local ok, err             -- connect to a dead server, no upstream available             ok, err = checkups.ready_ok("ups1", callback)             if err then ngx.say(err) end             -- add server to ups1             ok, err = checkups.update_upstream("ups1", {                     {                         servers = {                             {host="127.0.0.1", port=12350, weight=10, max_fails=3, fail_timeout=10},                         }                     },                 })             if err then ngx.say(err) end             ngx.sleep(1)             ok, err = checkups.ready_ok("ups1", callback)             if err then ngx.say(err) end             ok, err = checkups.ready_ok("ups1", callback)             if err then ngx.say(err) end             -- add server to new upstream             ok, err = checkups.update_upstream("ups2", {                     {                         servers = {                             {host="127.0.0.1", port=12351},                         }                     },                 })             if err then ngx.say(err) end             ngx.sleep(1)             ok, err = checkups.ready_ok("ups2", callback)             if err then ngx.say(err) end             -- add server to ups2, reset rr state             ok, err = checkups.update_upstream("ups2", {                     {                         servers = {                             {host="127.0.0.1", port=12350, weight=10, max_fails=3, fail_timeout=10},                             {host="127.0.0.1", port=12351, weight=10, max_fails=3, fail_timeout=10},                         }                     },                 })             if err then ngx.say(err) end             ngx.sleep(1)             ok, err = checkups.ready_ok("ups2", callback)             if err then ngx.say(err) end             ok, err = checkups.ready_ok("ups2", callback)             if err then ngx.say(err) end     } }Lua 配置示例:_M = {} -- Here is the global part _M.global = {     checkup_timer_interval = 15,     checkup_timer_overtime = 60,     default_heartbeat_enable = true,     checkup_shd_sync_enable = true,     shd_config_timer_interval = 1, } -- The rests parts are cluster configurations _M.redis = {     enable = true,     typ = "redis",     timeout = 2,     read_timeout = 15,     send_timeout = 15,     protected = true,     cluster = {         {   -- level 1             try = 2,             servers = {                 { host = "192.168.0.1", port = 6379, weight=10, max_fails=3, fail_timeout=10 },                 { host = "192.168.0.2", port = 6379, weight=10, max_fails=3, fail_timeout=10 },             }         },         {   -- level 2             servers = {                 { host = "192.168.0.3", port = 6379, weight=10, max_fails=3, fail_timeout=10 },             }         },     }, } _M.api = {     enable = false,     typ = "http",     http_opts = {         query = "GET /status HTTP/1.1\r\nHost: localhost\r\n\r\n",         statuses = {             [500] = false,             [502] = false,             [503] = false,             [504] = false,         },     },     mode = "hash",     cluster = {         dc1 = {             servers = {                 { host = "192.168.1.1", port = 1234, weight=10, max_fails=3, fail_timeout=10 },             }         },         dc2 = {             servers = {                 { host = "192.168.1.2", port = 1234, weight=10, max_fails=3, fail_timeout=10 },             }         }     } } _M.ups_from_nginx = {     timeout = 2,     cluster = {         {   -- level 1             upstream = "api.com",         },         {   -- level 2             upstream = "api.com",             upstream_only_backup = true,         },     }, } return _M
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

芮伦硕

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值