1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
| http { lua_shared_dict state 10m; lua_shared_dict mutex 1m; lua_shared_dict locks 1m; lua_shared_dict config 10m;
keepalive_timeout 60; lua_package_path '$prefix/lua/?.lua;;'; lua_package_cpath "$prefix/src/lualib/?.lua;;";
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() }
upstream binghe_server{ server 0.0.0.1; balancer_by_lua_block{ local checkups = require "resty.checkups.api" local balancer = require "ngx.balancer"; balancer.set_timeouts(1, 0.5, 0.5) -- 后端的连接和读写超时时间 balancer.set_more_tries(2) -- 连接失败后最多在重试 2 次 local status, code = balancer.get_last_failure() --获取上次失败信息 if status == "failed" then local last_peer = ngx.ctx.last_peer ngx.log(ngx.ERR, "get_last_failure: ", last_peer); -- 标记上次失败server checkups.feedback_status("up_http", last_peer.host, last_peer.port, true) end local peer,err = checkups.select_peer("up_http") --根据算法从服务器列表中获取一个服务地址 ngx.ctx.last_peer = peer --记录这次连接server balancer.set_current_peer(peer.host, peer.port) } keepalive 10; }
server { listen 8090; server_name localhost; charset utf-8;
location / { proxy_pass http://binghe_server; }
location = /update_upstream { access_log off; allow 127.0.0.1; deny all; default_type text/plain; content_by_lua_block { local host = ngx.req.get_uri_args()["host"] local port = ngx.req.get_uri_args()["port"] local weight = ngx.req.get_uri_args()["weight"]
if host == nil or port == nil then ngx.say("usage: /update_upstream?host=x.x.x.x&port=x") return end if type(port) == "string" then port= tonumber(port) end if weight == nil then weight = 10 end local checkups = require "resty.checkups.api" local save_config = require "save_config"
local ok = save_config.save_server(host,port,weight) if not ok then ngx.say("save server info error") end
local upstream_data = checkups.get_upstream("up_http") local new_server = { host = host, port = port, weight=weight, max_fails=3, fail_timeout=10 } table.insert(upstream_data.cluster[1].servers,new_server) local ok, err = checkups.update_upstream("up_http", upstream_data) if err then ngx.say(err) end ngx.say("update upstream ok") } } location = /delete_upstream { access_log off; allow 127.0.0.1; deny all; default_type text/plain; content_by_lua_block { local host = ngx.req.get_uri_args()["host"] local port = ngx.req.get_uri_args()["port"] local skey = ngx.req.get_uri_args()["skey"]
if (host == nil or port == nil) and skey == nil then ngx.say("usage: /delete_upstream?host=x.x.x.x&port=x \nor\nusage: /delete_upstream?skey=x") return end local checkups = require "resty.checkups.api" if skey ~= nil then local ok, err = checkups.delete_upstream(skey) if err then ngx.say(err) else ngx.say("ok") end return end if type(port) == "string" then port= tonumber(port) end local upstream_data = checkups.get_upstream("up_http") for i,v in ipairs(upstream_data.cluster[1].servers) do if v.host == host and v.port == port then table.remove(upstream_data.cluster[1].servers, i) local ok, err = checkups.update_upstream("up_http", upstream_data) if err then ngx.say(err) else local save_config = require "save_config" local ok = save_config.delete_server(port) if not ok then ngx.say("delete server info error") end ngx.say("delete upstream ok") end return end end ngx.say("failed") } }
location = /status { access_log off; allow 127.0.0.1; deny all; default_type application/json; content_by_lua_block { local cjson = require "cjson" local checkups = require "resty.checkups.api" ngx.say(cjson.encode(checkups.get_status())) } } } }
|