APISIX源码解析-执行阶段【init】
ngx_lua 模块执行顺序与阶段
init_worker()
1、初始化woker.event
local ok, err = we.configure({shm = "worker-events", interval = 0.1})
2、初始化注册中心
local ok, err = we.configure({shm = "worker-events", interval = 0.1})
注册中心支持:consul、dns、eureka、nacos example:
#discovery: # service discovery center
# dns:
# resolver:
# - "127.0.0.1:8600" # use the real address of your dns server
# eureka:
# host: # it's possible to define multiple eureka hosts addresses of the same eureka cluster.
# - "http://127.0.0.1:8761"
# prefix: "/eureka/"
# fetch_interval: 30 # default 30s
# weight: 100 # default weight for node
# timeout:
# connect: 2000 # default 2000ms
# send: 2000 # default 2000ms
# read: 5000 # default 5000ms
3、初始化balancer
require("apisix.balancer").init_worker()
# 这边没有实现任何方法
function _M.init_worker()
end
4、初始化admin route
router = route.new(uri_route)
local uri_route = {
{
paths = [[/apisix/admin/*]],
methods = {"GET", "PUT", "POST", "DELETE", "PATCH"},
handler = run,
},
{
paths = [[/apisix/admin/stream_routes/*]],
methods = {"GET", "PUT", "POST", "DELETE", "PATCH"},
handler = run_stream,
},
{
paths = [[/apisix/admin/plugins/list]],
methods = {"GET"},
handler = get_plugins_list,
},
{
paths = reload_event,
methods = {"PUT"},
handler = post_reload_plugins,
},
}
## 同时会同步一次插件到etcd中
sync_local_conf_to_etcd(true)
5、初始化后台定时器timer
local timer, err = core.timer.new("background", background_timer, opts)
6、初始化plugin 将config-default.yaml 和 config.yaml 文件中配置的插件放到 local_plugins 和 local_plugins_hash中,同时local_plugins_hash按优先级排序
local ok, err = load(http_plugin_names)
local function load(plugin_names)
local processed = {}
for _, name in ipairs(plugin_names) do
if processed[name] == nil then
processed[name] = true
end
end
core.log.warn("new plugins: ", core.json.delay_encode(processed))
#### 先删除
for name in pairs(local_plugins_hash) do
unload_plugin(name)
end
core.table.clear(local_plugins)
core.table.clear(local_plugins_hash)
### 再加载
for name in pairs(processed) do
load_plugin(name, local_plugins)
end
if #local_plugins > 1 then
sort_tab(local_plugins, sort_plugin)
end
### 再优先级排序 加入 local_plugins_hash
for i, plugin in ipairs(local_plugins) do
local_plugins_hash[plugin.name] = plugin
if local_conf and local_conf.apisix
and local_conf.apisix.enable_debug then
core.log.warn("loaded plugin and sort by priority:",
" ", plugin.priority,
" name: ", plugin.name)
end
end
_M.load_times = _M.load_times + 1
core.log.info("load plugin times: ", _M.load_times)
return true
end
|