zoukankan      html  css  js  c++  java
  • init.lua

    local delay = 5
    local handler
    handler = function (premature)
    --ngx.log(ngx.ERR,"----------timer-do")
    local resty_consul = require('resty.consul')
    local consul = resty_consul:new({
    host = "120.78.64.28",
    port = 8500,
    connect_timeout = (601000), -- 60s
    read_timeout = (60
    1000), -- 60s
    })

    local res, err = consul:list_keys("redis-cluster") -- Get all keys
    if not res then
        ngx.log(ngx.ERR, err)
        return
    end
    
    local keys = {}
    if res.status == 200 then
        keys = res.body
    end
    --local redis_addr = {}
    local ip_addr = ''
    --local ngx_re_split=require("ngx.re").split
    for key, value in ipairs(keys) do
        local res, err = consul:get_key(value)
        if not res then
            ngx.log(ngx.ERR, err)
            return
        end
    
        if (table.getn(keys) == key) then
        	ip_addr = ip_addr..res.body[1].Value
        else
        	ip_addr = ip_addr..res.body[1].Value..','
        end
        --local ip_addr = ngx_re_split(res.body[1].Value,":")
        --redis_addr[key]={ip=ip_addr[1],port=ip_addr[2]}
        --ngx.print(res.body[1].Value) -- Key value after base64 decoding
    end
    ngx.shared.redis_cluster_addr:set('redis-addr',ip_addr)
    

    end

    if (0==ngx.worker.id() ) then
    local ok, err = ngx.timer.at(0, handler)
    if not ok then
    ngx.log(ngx.ERR, "failed to create the timer: ", err)
    return
    end

    local ok, err = ngx.timer.every(delay, handler)
    if not ok then
        ngx.log(ngx.ERR, "failed to create the timer: ", err)
        return
    end
    

    end

  • 相关阅读:
    jsmin Javascript 的小巧的压缩工具
    pChart 支持中文显示
    使用 SyntaxHighlighter 实现代码高亮
    Linux Ubuntu 下阅读 CHM
    QueryPath Method Summary_方法速查手册
    QueryPath PHP 中的 jQuery
    SQL SELECT DISTINCT 语句
    写网页内容需要注意些什么?
    jQuery UI 弹出注册窗口_练习
    Smarty 中的 Foreach
  • 原文地址:https://www.cnblogs.com/xivzhou/p/14494918.html
Copyright © 2011-2022 走看看