-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpolaris.lua
executable file
·68 lines (55 loc) · 2.29 KB
/
polaris.lua
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
--[[
Author: andymatthu#tencent.com
Create date: 2020/06/11
Update date: 2020/11/04
Desc: 北极星sdk lua层封装
]]
local _M = {}
local polariswrapper = require "polariswrapper"
-- 显式初始化polaris_api(单进程只会初始化一次)
-- 注:需要指定polaris.yaml时需调用,否则可以直接调用get_server_host_port,包含了隐式初始化polaris_api
function _M.polaris_api_init(config_file)
local result, msg
if config_file ~= "" then
result, msg = polariswrapper.polaris_api_init(config_file)
else
result, msg = polariswrapper.polaris_api_init()
end
return result, msg
end
-- 设置北极星服务日志路径
function _M.polaris_log_settings(log_dir, log_level)
polariswrapper.polaris_log_settings(log_dir, log_level)
end
-- 通过北极星获取服务ip和端口
function _M.get_server_host_port(service_namespace, service_name)
-- 查询host port
local result, msg, host, port, instance_id, if_reuse = polariswrapper.polaris_get_one_node(service_namespace, service_name)
if result ~= 0 then
ngx.log(ngx.ERR, "polaris_get_one_node failed, result:"..result..", msg:"..msg)
return result, msg
end
ngx.log(ngx.INFO, "polaris_api if_reuse:"..if_reuse)
return 0, "ok", {host=host, port=port, instance_id=instance_id}
end
-- 进行服务限流
function _M.get_quota(service_namespace, service_name)
-- 查询host port
local result, msg, host, port, instance_id, if_reuse = polariswrapper.polaris_get_one_node(service_namespace, service_name)
if result ~= 0 then
ngx.log(ngx.ERR, "polaris_get_one_node failed, result:"..result..", msg:"..msg)
return result, msg
end
ngx.log(ngx.INFO, "polaris_api if_reuse:"..if_reuse)
return 0, "ok", {host=host, port=port, instance_id=instance_id}
end
-- 上报北极星调用结果
function _M.call_report(service_namespace, service_name, instance_id, result)
local result, msg = polariswrapper.polaris_service_call_report(service_namespace, service_name, instance_id, result)
if result ~= 0 then
ngx.log(ngx.ERR, "polaris_service_call_report failed, result:"..result..", msg:"..msg)
return result, msg
end
return 0, "ok"
end
return _M