feature: generated an uuid for each apisix instance, and this uuid will

be reported to heartbeat service center.
This commit is contained in:
Yuansheng 2019-06-10 16:32:24 +08:00 committed by YuanSheng Wang
parent 2e318e53c2
commit d5fa0598fa
7 changed files with 107 additions and 3 deletions

View File

@ -25,14 +25,12 @@ env:
- OPENRESTY_PREFIX=/usr/local/openresty
install:
- sudo luarocks install lua-resty-http
- sudo luarocks install lua-typeof
- wget -qO - https://openresty.org/package/pubkey.gpg | sudo apt-key add -
- sudo apt-get -y install software-properties-common
- sudo add-apt-repository -y "deb http://openresty.org/package/ubuntu $(lsb_release -sc) main"
- sudo apt-get update
- sudo apt-get install openresty
- sudo luarocks install apisix-*.rockspec
- sudo luarocks install apisix-*.rockspec --only-deps
- git clone https://github.com/openresty/test-nginx.git test-nginx
script:

View File

@ -20,6 +20,7 @@ dependencies = {
"lua-resty-etcd = 0.5",
"lua-resty-balancer = 0.02rc5",
"lua-resty-ngxvar = 0.2",
"lua-resty-jit-uuid = 0.0.7"
}
build = {

View File

@ -22,6 +22,16 @@ function _M.init()
require("jit.opt").start("minstitch=2", "maxtrace=4000",
"maxrecord=8000", "sizemcode=64",
"maxmcode=4000", "maxirconst=1000")
--
local seed, err = core.utils.get_seed_from_urandom()
if not seed then
core.log.warn('failed to get seed from urandom: ', err)
seed = ngx.now() * 1000 + ngx.worker.pid()
end
math.randomseed(seed)
core.id.init()
end

View File

@ -11,6 +11,8 @@ return {
schema = require("apisix.core.schema"),
ctx = require("apisix.core.ctx"),
timer = require("apisix.core.timer"),
id = require("apisix.core.id"),
utils = require("apisix.core.utils"),
consumer = require("apisix.consumer"),
tablepool= require("tablepool"),
}

63
lua/apisix/core/id.lua Normal file
View File

@ -0,0 +1,63 @@
local log = require("apisix.core.log")
local uuid = require('resty.jit-uuid')
local smatch = string.match
local prefix = ngx.config.prefix()
local apisix_uid
local _M = {version = 0.1}
local function rtrim(str)
return smatch(str, "^(.-)%s*$")
end
local function read_file(path)
local file = io.open(path, "rb") -- r read mode and b binary mode
if not file then
return nil
end
local content = file:read("*a") -- *a or *all reads the whole file
file:close()
return rtrim(content)
end
local function write_file(path, data)
local file = io.open(path ,"w+")
if not file then
return nil, "failed to open file[" .. path .. "] for writing"
end
file:write(data)
file:close()
return true
end
function _M.init()
local uid_file_path = prefix .. "/conf/apisix.uid"
apisix_uid = read_file(uid_file_path)
if apisix_uid then
return
end
apisix_uid = uuid.generate_v4()
log.warn("not found apisix uid, generate a new one: ", apisix_uid)
local ok, err = write_file(uid_file_path, apisix_uid)
if not ok then
log.error(err)
end
end
function _M.get()
return apisix_uid
end
return _M

28
lua/apisix/core/utils.lua Normal file
View File

@ -0,0 +1,28 @@
local log = require("apisix.core.log")
local open = io.open
local _M = {version = 0.1}
function _M.get_seed_from_urandom()
local frandom, err = open("/dev/urandom", "rb")
if not frandom then
return nil, 'failed to open /dev/urandom: ' .. err
end
local str = frandom:read(4)
frandom:close()
if not str then
return nil, 'failed to read data from /dev/urandom'
end
local seed = 0
for i = 1, 4 do
seed = 256 * seed + str:byte(i)
end
return seed
end
return _M

View File

@ -2,6 +2,7 @@
local config_etcd = require("apisix.core.config_etcd").new()
local apisix_version = require("apisix.core.version")
local apisix_id = require("apisix.core.id")
local timer = require("apisix.core.timer")
local json = require("apisix.core.json")
local log = require("apisix.core.log")
@ -51,6 +52,7 @@ local function report()
version = apisix_version,
plugins = config_etcd.local_conf().plugins,
etcd_version = etcd_version,
uuid = apisix_id.get(),
}
local args, err = encode_args(info)