Skip to content
This repository has been archived by the owner on Aug 26, 2022. It is now read-only.

Commit

Permalink
support etcd
Browse files Browse the repository at this point in the history
  • Loading branch information
huangnauh committed Sep 28, 2017
1 parent 8a09f4e commit 53b06b8
Show file tree
Hide file tree
Showing 12 changed files with 370 additions and 236 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ nginx/uwsgi_temp/*
!nginx/conf/nginx.conf
!nginx/conf/slardar*
!nginx/conf/stream*

config/*
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ INSTALL_DIRS=$(INSTALL_SRCDIR)/modules \
$(INSTALL_LIBDIR)/resty \
$(INSTALL_LIBDIR)/resty/core \
$(INSTALL_LIBDIR)/resty/checkups \
$(INSTALL_LIBDIR)/resty/consul \
$(INSTALL_LIBDIR)/resty/store \
$(INSTALL_LIBDIR)/resty/logger \
$(INSTALL_ETCDIR)

Expand All @@ -82,7 +82,7 @@ ifndef DEV
$(INSTALL_F) nginx/app/lib/ngx/*.lua $(INSTALL_LIBDIR)/ngx
$(INSTALL_F) nginx/app/lib/resty/core/*.lua $(INSTALL_LIBDIR)/resty/core
$(INSTALL_F) nginx/app/lib/resty/logger/*.lua $(INSTALL_LIBDIR)/resty/logger
$(INSTALL_F) nginx/app/lib/resty/consul/*.lua $(INSTALL_LIBDIR)/resty/consul
$(INSTALL_F) nginx/app/lib/resty/store/*.lua $(INSTALL_LIBDIR)/resty/store
$(INSTALL_F) nginx/app/lib/resty/checkups/*.lua $(INSTALL_LIBDIR)/resty/checkups
$(INSTALL_F) nginx/app/src/*.lua $(INSTALL_SRCDIR)
$(INSTALL_F) nginx/app/etc/*.lua $(INSTALL_ETCDIR)
Expand Down
35 changes: 19 additions & 16 deletions nginx/app/etc/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,47 +21,50 @@ _M.global = {
-- sync upstream list from shared memory every 1s
shd_config_timer_interval = 1,

-- If no_consul is set to true, Slardar will continue start or reload
-- even if getting data from consul failed.
-- If no_store is set to true, Slardar will continue start or reload
-- even if getting data from key-value store failed.
-- Remember to set this value to false when you need to read persisted
-- upstreams or lua codes from consul.
no_consul = true,
-- upstreams or lua codes from key-value store.
no_store = true,
}


_M.consul = {
-- connect to consul will timeout in 5s.
_M.store = {
-- key-value store type
type = "etcd",

-- connect to key-value store will timeout in 5s.
timeout = 5,

-- disable checkups heartbeat to consul.
-- disable checkups heartbeat to key-value store.
enable = false,

-- consul k/v prefix.
-- Slardar will read upstream list from config/slardar/upstreams.
config_key_prefix = "config/slardar/",
-- key-value store prefix.
-- Slardar will read upstream list from {config_key_prefix}/upstreams/.
config_key_prefix = "config/slardar",

-- positive cache ttl(in seconds) for dynamic configurations from consul.
-- positive cache ttl(in seconds) for dynamic configurations from key-value store.
config_positive_ttl = 10,

-- negative cache ttl(in seconds) for dynamic configurations from consul.
-- negative cache ttl(in seconds) for dynamic configurations from key-value store.
config_negative_ttl = 5,

-- enable or disable dynamic configurations cache from consul.
-- enable or disable dynamic configurations cache from key-value store.
config_cache_enable = true,

cluster = {
{
servers = {
-- change these to your own consul http addresses
{ host = "127.0.0.1", port = 8500 },
-- change these to your own key-value store http addresses
{ host = "127.0.0.1", port = 2379 },
},
},
},
}

_M.load_init = {
-- load_init module name for lua-resty-load
module_name = "resty.consul.load"
module_name = "resty.store.load"
}


Expand Down
83 changes: 0 additions & 83 deletions nginx/app/lib/resty/consul/api.lua

This file was deleted.

84 changes: 0 additions & 84 deletions nginx/app/lib/resty/consul/load.lua

This file was deleted.

22 changes: 6 additions & 16 deletions nginx/app/lib/resty/load.lua
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,8 @@ local function pre_load(config)
local mod = module:new(config)

local load_version, err
if type(mod.lversion) == "function" then
load_version, err = mod:lversion()
if type(mod.version) == "function" then
load_version, err = mod:version()
if err then
return nil, err
end
Expand All @@ -258,32 +258,22 @@ local function pre_load(config)
return nil, err
end

local script_keys, err = mod:lkeys()
local list, err = mod:list()
if err then
return nil, err
end

if not script_keys then
log(WARN, "no code to load")
return true
end

if type(script_keys) ~= "table" then
return nil, "script_keys invalid"
end

if not next(script_keys) then
if not next(list) then
log(WARN, "no code to load")
return true
end

local skeys = {}
for _, skey in ipairs(script_keys) do
for skey, code in ipairs(list) do
local ok = pcall(require, skey)
if not ok then
local code = mod:lget(skey)
if not code then
return nil, "fail to get code from consul"
return nil, "fail to get code from key-value store"
end
local ok, err = load_dict:safe_set(CODE_PREFIX .. skey, code)
if not ok then
Expand Down
Loading

0 comments on commit 53b06b8

Please sign in to comment.