You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2021/09/30 01:15:56 UTC

[GitHub] [apisix] siaron opened a new issue #5159: request help: 自定义负载均衡

siaron opened a new issue #5159:
URL: https://github.com/apache/apisix/issues/5159


   ### Issue description
   
   可以根据http请求中的某个header进行自定义负载均衡的选择吗? 另外在负载均衡的插件中ctx参数中可以拿到当前请求的header吗?
   
   ### Environment
   
   - apisix version (cmd: `apisix version`): 2.9
   - OS (cmd: `uname -a`): macos
   - OpenResty / Nginx version (cmd: `nginx -V` or `openresty -V`):
   - etcd version, if have (cmd: run `curl http://127.0.0.1:9090/v1/server_info` to get the info from server-info API):
   - apisix-dashboard version, if have:
   - the plugin runner version, if the issue is about a plugin runner (cmd: depended on the kind of runner):
   - luarocks version, if the issue is about installation (cmd: `luarocks --version`):
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] siaron edited a comment on issue #5159: request help: 自定义负载均衡

Posted by GitBox <gi...@apache.org>.
siaron edited a comment on issue #5159:
URL: https://github.com/apache/apisix/issues/5159#issuecomment-930916918


   - 简易版的根据header信息进行负载. original_destination.lua
   ```
   local core        = require("apisix.core")
   local pairs = pairs
   
   local _M = {}
   
   local function fetch_server_from_header(ctx, upstream)
       local key = upstream.key
       local server=ctx.var["http_" .. key]
       --TODO when server is nil .
       return server
   end
   
   function _M.new(up_nodes, upstream)
   
       local nodes_count = 0
       local servers, nodes = {}, {}
       for serv, weight in pairs(up_nodes) do
           nodes_count = nodes_count + 1
           servers[serv] = serv
       end
   
       return {
           upstream = upstream,
           get = function (ctx)
               local id
               if ctx.balancer_tried_servers then
                   if ctx.balancer_tried_servers_count == nodes_count then
                       return nil, "all upstream servers tried"
                   end
               else
                   id = fetch_server_from_header(ctx,upstream)
               end
               return servers[id]
           end,
           after_balance = function (ctx, before_retry)
               if not before_retry then
                   if ctx.balancer_tried_servers then
                       core.tablepool.release("balancer_tried_servers", ctx.balancer_tried_servers)
                       ctx.balancer_tried_servers = nil
                   end
   
                   return nil
               end
   
               if not ctx.balancer_tried_servers then
                   ctx.balancer_tried_servers = core.tablepool.fetch("balancer_tried_servers", 0, 2)
               end
   
               ctx.balancer_tried_servers[ctx.balancer_server] = true
               ctx.balancer_tried_servers_count = (ctx.balancer_tried_servers_count or 0) + 1
           end,
           before_retry_next_priority = function (ctx)
               if ctx.balancer_tried_servers then
                   core.tablepool.release("balancer_tried_servers", ctx.balancer_tried_servers)
                   ctx.balancer_tried_servers = nil
               end
   
               ctx.balancer_tried_servers_count = 0
           end,
       }
   end
   
   
   return _M
   ```
   - 使用插件
   ```
   curl http://127.0.0.1:9180/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -i -d '
   {
       "uri": "/index.html",
       "upstream": {
           "type": "original_destination",
           "key":"server_host",
           "nodes": {
               "127.0.0.1:20001": 1,
               "127.0.0.1:20002": 2
           }
       }
   }'
   ```
   -  测试 curl
   ```
   $ curl --header "server_host: 127.0.0.1:20002" http://127.0.0.1:9081/index.html
   this is 20002
   
   $ curl --header "server_host: 127.0.0.1:20001" http://127.0.0.1:9081/index.html
   this is 20001
   ```
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] siaron edited a comment on issue #5159: request help: 自定义负载均衡

Posted by GitBox <gi...@apache.org>.
siaron edited a comment on issue #5159:
URL: https://github.com/apache/apisix/issues/5159#issuecomment-930916918


   - 简易版的根据header信息进行负载. original_destination.lua
   ```
   local core        = require("apisix.core")
   local pairs = pairs
   
   local _M = {}
   
   local function fetch_server_from_header(ctx, upstream)
       local key = upstream.key
       local server=ctx.var["http_" .. key]
       --TODO when server is nil .
       return server
   end
   
   function _M.new(up_nodes, upstream)
   
       local nodes_count = 0
       local servers, nodes = {}, {}
       for serv, weight in pairs(up_nodes) do
           nodes_count = nodes_count + 1
           servers[serv] = serv
       end
   
       return {
           upstream = upstream,
           get = function (ctx)
               local id
               if ctx.balancer_tried_servers then
                   if ctx.balancer_tried_servers_count == nodes_count then
                       return nil, "all upstream servers tried"
                   end
               else
                   id = fetch_server_from_header(ctx,upstream)
               end
               return servers[id]
           end,
           after_balance = function (ctx, before_retry)
               if not before_retry then
                   if ctx.balancer_tried_servers then
                       core.tablepool.release("balancer_tried_servers", ctx.balancer_tried_servers)
                       ctx.balancer_tried_servers = nil
                   end
   
                   return nil
               end
   
               if not ctx.balancer_tried_servers then
                   ctx.balancer_tried_servers = core.tablepool.fetch("balancer_tried_servers", 0, 2)
               end
   
               ctx.balancer_tried_servers[ctx.balancer_server] = true
               ctx.balancer_tried_servers_count = (ctx.balancer_tried_servers_count or 0) + 1
           end,
           before_retry_next_priority = function (ctx)
               if ctx.balancer_tried_servers then
                   core.tablepool.release("balancer_tried_servers", ctx.balancer_tried_servers)
                   ctx.balancer_tried_servers = nil
               end
   
               ctx.balancer_tried_servers_count = 0
           end,
       }
   end
   
   
   return _M
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] siaron commented on issue #5159: request help: 自定义负载均衡

Posted by GitBox <gi...@apache.org>.
siaron commented on issue #5159:
URL: https://github.com/apache/apisix/issues/5159#issuecomment-930916918


   - 简易版的根据header信息进行负载. original_destination.lua
   `
   local core        = require("apisix.core")
   local pairs = pairs
   
   local _M = {}
   
   local function fetch_server_from_header(ctx, upstream)
       local key = upstream.key
       local server=ctx.var["http_" .. key]
       --TODO when server is nil .
       return server
   end
   
   function _M.new(up_nodes, upstream)
   
       local nodes_count = 0
       local servers, nodes = {}, {}
       for serv, weight in pairs(up_nodes) do
           nodes_count = nodes_count + 1
           servers[serv] = serv
       end
   
       return {
           upstream = upstream,
           get = function (ctx)
               local id
               if ctx.balancer_tried_servers then
                   if ctx.balancer_tried_servers_count == nodes_count then
                       return nil, "all upstream servers tried"
                   end
               else
                   id = fetch_server_from_header(ctx,upstream)
               end
               return servers[id]
           end,
           after_balance = function (ctx, before_retry)
               if not before_retry then
                   if ctx.balancer_tried_servers then
                       core.tablepool.release("balancer_tried_servers", ctx.balancer_tried_servers)
                       ctx.balancer_tried_servers = nil
                   end
   
                   return nil
               end
   
               if not ctx.balancer_tried_servers then
                   ctx.balancer_tried_servers = core.tablepool.fetch("balancer_tried_servers", 0, 2)
               end
   
               ctx.balancer_tried_servers[ctx.balancer_server] = true
               ctx.balancer_tried_servers_count = (ctx.balancer_tried_servers_count or 0) + 1
           end,
           before_retry_next_priority = function (ctx)
               if ctx.balancer_tried_servers then
                   core.tablepool.release("balancer_tried_servers", ctx.balancer_tried_servers)
                   ctx.balancer_tried_servers = nil
               end
   
               ctx.balancer_tried_servers_count = 0
           end,
       }
   end
   
   
   return _M
   `


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] spacewander closed issue #5159: request help: 自定义负载均衡

Posted by GitBox <gi...@apache.org>.
spacewander closed issue #5159:
URL: https://github.com/apache/apisix/issues/5159


   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] spacewander commented on issue #5159: request help: 自定义负载均衡

Posted by GitBox <gi...@apache.org>.
spacewander commented on issue #5159:
URL: https://github.com/apache/apisix/issues/5159#issuecomment-931019420


   Since you are already using Nacos, use the health check in Nacos is OK.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] spacewander commented on issue #5159: request help: 自定义负载均衡

Posted by GitBox <gi...@apache.org>.
spacewander commented on issue #5159:
URL: https://github.com/apache/apisix/issues/5159#issuecomment-938516119


   Consider solved. Feel free to reopen it if need.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] spacewander commented on issue #5159: request help: 自定义负载均衡

Posted by GitBox <gi...@apache.org>.
spacewander commented on issue #5159:
URL: https://github.com/apache/apisix/issues/5159#issuecomment-930698725


   You can write your own balancer: https://github.com/apache/apisix/tree/master/apisix/balancer
   
   > 另外在负载均衡的插件中ctx参数中可以拿到当前请求的header吗?
   
   Yes.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] siaron commented on issue #5159: request help: 自定义负载均衡

Posted by GitBox <gi...@apache.org>.
siaron commented on issue #5159:
URL: https://github.com/apache/apisix/issues/5159#issuecomment-930942277


   > You can write your own balancer: https://github.com/apache/apisix/tree/master/apisix/balancer
   > 
   > > 另外在负载均衡的插件中ctx参数中可以拿到当前请求的header吗?
   > 
   > Yes.
   
   另外针对后端不健康的服务摘除有什么最佳实践吗?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org