upyun / lua-resty-checkups

Manage Nginx upstreams in pure Lua.
259 stars 65 forks source link

how can i use dynamic upstream with lua-resty-checkups #47

Closed charles1503 closed 5 years ago

charles1503 commented 5 years ago

i want use lua-resty-checkups to realize a micro service gateway,this is my way:

  1. set upstream variable on set_by_lua
  2. set proxy_pass with upstream variable but, i find this way does not work, it throw a error : no resolver defined to resolve test_upstream,how can i resolve it? Did I do it the wrong way? this is my code:

nginx.conf

    lua_shared_dict state 10m;
    lua_shared_dict mutex 1m;
    lua_shared_dict locks 1m;
    lua_shared_dict config 10m;

    upstream charles{
        server 10.72.8.81:80 weight=2;
    }
    init_by_lua_block {
        local config = require "config"
        local checkups = require "resty.checkups.api"
        checkups.init(config)
    }

    init_worker_by_lua_block {
        local config = require "config"
        local checkups = require "resty.checkups.api"
        checkups.prepare_checker(config)
        checkups.create_checker()
    }
    server {
        location / {
            set $dynamic_proxy '';
            set_by_lua_file $test_var /opt/openresty/lua_script/dynamic_route.lua;
            proxy_pass http://$dynamic_proxy/;
        }

dynamic_route.lua

  local ngx_re = require "ngx.re"
  local uri = ngx.var.uri
  local array = ngx_re.split(uri, "/")
  local context_path = array[2]
  if "charles" == context_path then
   ngx.var.dynamic_proxy = "charles"
  end
  if "charles1" == context_path then
   ngx.var.dynamic_proxy = "test_upstream"
  end

config.lua

_M = {}
_M.global = {
    checkup_timer_interval = 15,
    checkup_shd_sync_enable = true,
    shd_config_timer_interval = 1,
}
_M.test_upstream = {
    cluster = {
        {
            servers = {
                { host = "10.72.8.81", port = 80, weight=10, max_fails=3, fail_timeout=10 },
            }
        },
    },
}
return _M

request test

http://localhost/charles

For online documentation and support please refer to openresty.org.
Commercial support is available at openresty.com.

Thank you for flying OpenResty.

#####  http://localhost/charles1
- request

curl http://localhost/charles1

- result
502 Bad Gateway

502 Bad Gateway


openresty/1.13.6.2
- error.log

2019/06/05 09:56:17 [error] 47#0: *2630 no resolver defined to resolve test_upstream, client: 172.17.0.1, server: , request: "GET /charles1 HTTP/1.1", host: "localhost"


- it sames couldn't get dynamic upstream with lua-resty-checkups component?
tokers commented 5 years ago

Hello!

When you try to combine proxy_pass and lua-resty-checkups, the common pattern is to resort the balancer_by_lua* block. Without this media, proxy_pass either finding the upstream from upstream group (defined in your nginx configuration files explicitly) or resolving it (while you didn't define a DNS resolver with the resolver directive, that's why Nginx complained it.).

However, a dummy upstream block is still required to lead proxy_pass to use it.

upstream dummy {
    balancer_by_lua_block {
        -- here you can use lua-resty-checkups to select the desired upsteram
        -- and set it through ngx.balancer
    }
    keepalive 100; # must be behind the balancer_by_lua_block.
}

server {
        location / {
            set $dynamic_proxy '';
            set_by_lua_file $test_var /opt/openresty/lua_script/dynamic_route.lua;
            proxy_pass http://dummy;
        }
}

In other words, dynamic_route.lua should be called in balancer_by_lua*. Of course yield-able APIs cannot be used here.

charles1503 commented 5 years ago

Hello!

When you try to combine proxy_pass and lua-resty-checkups, the common pattern is to resort the balancer_by_lua* block. Without this media, proxy_pass either finding the upstream from upstream group (defined in your nginx configuration files explicitly) or resolving it (while you didn't define a DNS resolver with the resolver directive, that's why Nginx complained it.).

However, a dummy upstream block is still required to lead proxy_pass to use it.

upstream dummy {
    balancer_by_lua_block {
        -- here you can use lua-resty-checkups to select the desired upsteram
        -- and set it through ngx.balancer
    }
    keepalive 100; # must be behind the balancer_by_lua_block.
}

server {
        location / {
            set $dynamic_proxy '';
            set_by_lua_file $test_var /opt/openresty/lua_script/dynamic_route.lua;
            proxy_pass http://dummy;
        }
}

In other words, dynamic_route.lua should be called in balancer_by_lua*. Of course yield-able APIs cannot be used here.

Thank you very much for solving my problem!!

tokers commented 5 years ago

Consider it was solved. Now it's time to close this issue. @huangnauh