Skip to content

Commit 044cc90

Browse files
authored
Merge pull request #2 from chuntaojun/main
build:add build polaris-lua script
2 parents 4d9fb7a + 36833bc commit 044cc90

21 files changed

+3143
-81
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ a.out
44
*.so
55
third_party/polaris_cpp_sdk/slib/*.a
66
third_party/polaris_cpp_sdk/dlib/*.so
7+
logs/

Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,6 @@ $(TARGET):$(OBJ)
1818
clean:
1919
rm -f $(OBJ)
2020
rm -f $(TARGET)
21+
rm -rf polaris-cpp
22+
cd third_party
23+
rm -rf polaris_cpp_sdk

build.sh

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/bin/bash
2+
3+
make clean
4+
5+
workdir=$(pwd)
6+
7+
rm -rf polaris-cpp
8+
cd ${workdir}/third_party
9+
rm -rf polaris_cpp_sdk
10+
11+
cd ${workdir}
12+
13+
polaris_cpp_tag=$1
14+
15+
git clone https://github.com/polarismesh/polaris-cpp
16+
17+
cd polaris-cpp
18+
19+
if [ "${polaris_cpp_tag}" != "" ]; then
20+
git checkout ${polaris_cpp_tag}
21+
fi
22+
23+
make package
24+
25+
mv polaris_cpp_sdk.tar.gz ../third_party
26+
27+
cd ${workdir}/third_party
28+
29+
tar -zxvf polaris_cpp_sdk.tar.gz
30+
31+
cd ${workdir}
32+
33+
make

polaris.lua

+13
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@ function _M.get_server_host_port(service_namespace, service_name)
4141
return 0, "ok", {host=host, port=port, instance_id=instance_id}
4242
end
4343

44+
-- 进行服务限流
45+
function _M.get_quota(service_namespace, service_name)
46+
-- 查询host port
47+
local result, msg, host, port, instance_id, if_reuse = polariswrapper.polaris_get_one_node(service_namespace, service_name)
48+
if result ~= 0 then
49+
ngx.log(ngx.ERR, "polaris_get_one_node failed, result:"..result..", msg:"..msg)
50+
return result, msg
51+
end
52+
ngx.log(ngx.INFO, "polaris_api if_reuse:"..if_reuse)
53+
54+
return 0, "ok", {host=host, port=port, instance_id=instance_id}
55+
end
56+
4457
-- 上报北极星调用结果
4558
function _M.call_report(service_namespace, service_name, instance_id, result)
4659
local result, msg = polariswrapper.polaris_service_call_report(service_namespace, service_name, instance_id, result)

polaris_sdk_lua_wrapper.c

+53-34
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@
1414
#include <lauxlib.h>
1515
#include "polaris/polaris_api.h"
1616

17-
#define ERR_CODE_ARGS_COUNT 1001 // 传入参数个数有误
18-
#define ERR_CODE_POLARIS_API_INIT 1002 // 北极星api初始化失败
19-
#define ERR_CODE_GET_POLARIS_API 1003 // 获取北极星api错误
20-
#define ERR_CODE_GET_SERVICE_NAMESPACE 1004 // 解析传入参数-命名空间-失败
21-
#define ERR_CODE_GET_SERVICE_NAME 1005 // 解析传入参数-服务名-失败
22-
#define ERR_CODE_GET_ONE_INSTANCE_REQ_NEW 1006 // 创建获取单个服务实例请求对象失败
23-
#define ERR_CODE_GET_ONE_INSTANCE 1007 // 获取单个服务实例失败
24-
#define ERR_CODE_GET_LOG_DIR 1008 // 解析传入参数-日志路径-失败
25-
#define ERR_CODE_GET_INSTANCE_ID 1009 // 解析传入参数-服务实例ID-失败
26-
#define ERR_CODE_POLARIS_SERVICE_CALL_RESULT_NEW 1010 // 创建服务实例调用结果上报对象失败
27-
#define ERR_CODE_CALL_RET_TIMEOUT 1011 // 获取单个服务实例超时
28-
#define ERR_CODE_GET_CONFIG_FILE 1012 // 解析传入参数-sdk配置文件路径-失败
17+
#define ERR_CODE_ARGS_COUNT 1001 // 传入参数个数有误
18+
#define ERR_CODE_POLARIS_API_INIT 1002 // 北极星api初始化失败
19+
#define ERR_CODE_GET_POLARIS_API 1003 // 获取北极星api错误
20+
#define ERR_CODE_GET_SERVICE_NAMESPACE 1004 // 解析传入参数-命名空间-失败
21+
#define ERR_CODE_GET_SERVICE_NAME 1005 // 解析传入参数-服务名-失败
22+
#define ERR_CODE_GET_ONE_INSTANCE_REQ_NEW 1006 // 创建获取单个服务实例请求对象失败
23+
#define ERR_CODE_GET_ONE_INSTANCE 1007 // 获取单个服务实例失败
24+
#define ERR_CODE_GET_LOG_DIR 1008 // 解析传入参数-日志路径-失败
25+
#define ERR_CODE_GET_INSTANCE_ID 1009 // 解析传入参数-服务实例ID-失败
26+
#define ERR_CODE_POLARIS_SERVICE_CALL_RESULT_NEW 1010 // 创建服务实例调用结果上报对象失败
27+
#define ERR_CODE_CALL_RET_TIMEOUT 1011 // 获取单个服务实例超时
28+
#define ERR_CODE_GET_CONFIG_FILE 1012 // 解析传入参数-sdk配置文件路径-失败
2929

3030
static polaris_api *polaris_api_ptr = NULL;
3131

@@ -70,7 +70,7 @@ void fill_ok_result(lua_State *l)
7070
}
7171

7272
/* 从栈上解析metadata table数据并设置 */
73-
void set_metadata_from_stack(lua_State *l, int index, polaris_get_one_instance_req* get_one_instance_req)
73+
void set_metadata_from_stack(lua_State *l, int index, polaris_get_one_instance_req *get_one_instance_req)
7474
{
7575
luaL_checktype(l, index, LUA_TTABLE);
7676

@@ -80,12 +80,12 @@ void set_metadata_from_stack(lua_State *l, int index, polaris_get_one_instance_r
8080
{
8181
lua_pushvalue(l, -2);
8282

83-
const char* key = lua_tostring(l, -1);
84-
const char* value = lua_tostring(l, -2);
83+
const char *key = lua_tostring(l, -1);
84+
const char *value = lua_tostring(l, -2);
8585
printf("%s => %s\n", key, value);
8686

8787
/* 设置 metadata */
88-
polaris_get_one_instance_req_add_metadata(get_one_instance_req, key, value);
88+
polaris_get_one_instance_req_add_src_service_metadata(get_one_instance_req, key, value);
8989
lua_pop(l, 2);
9090
}
9191
}
@@ -141,6 +141,8 @@ static int polaris_api_init(lua_State *l)
141141
* 1 string 命名空间
142142
* 2 string 服务名
143143
* 3 table metadata kv (可选参数)
144+
* 4 string 主调服务命名空间 (可选参数,传了metadata则必填)
145+
* 5 string 主调服务名 (可选参数,传了metadata则必填)
144146
*
145147
* 传出参数:
146148
* 1 integer result
@@ -200,10 +202,22 @@ static int polaris_get_one_node(lua_State *l)
200202
}
201203

202204
/* 检查传入参数是否有metadata并在获取实例 */
203-
if (lua_gettop(l) >= 3)
205+
if (lua_gettop(l) >= 5)
204206
{
207+
/* 获取主调命名空间 */
208+
const char *source_namespace = lua_tostring(l, 4);
209+
if (source_namespace == NULL)
210+
{
211+
return report_error(l, ERR_CODE_GET_SERVICE_NAMESPACE, "get source service_namespace failed");
212+
}
213+
/* 获取主调服务名 */
214+
const char *source_service_name = lua_tostring(l, 5);
215+
if (source_service_name == NULL)
216+
{
217+
return report_error(l, ERR_CODE_GET_SERVICE_NAMESPACE, "get source_service_name failed");
218+
}
205219
/* 设置源服务用于匹配规则路由 */
206-
polaris_get_one_instance_req_set_src_service(get_one_instance_req, service_namespace, service_name);
220+
polaris_get_one_instance_req_set_src_service_key(get_one_instance_req, source_namespace, source_service_name);
207221

208222
/* 填充匹配规则 */
209223
set_metadata_from_stack(l, 3, get_one_instance_req);
@@ -213,7 +227,8 @@ static int polaris_get_one_node(lua_State *l)
213227

214228
/* 获取实例 */
215229
ret = polaris_api_get_one_instance(polaris_api_ptr, get_one_instance_req, &instance);
216-
if (ret != 0) {
230+
if (ret != 0)
231+
{
217232
/* 转换一下错误码 */
218233
if (ret == POLARIS_CALL_RET_TIMEOUT)
219234
{
@@ -382,27 +397,31 @@ int polaris_service_call_report(lua_State *l)
382397
return 2;
383398
}
384399

400+
static int polaris_get_quota(lua_State *l)
401+
{
402+
}
403+
385404
/* 导出函数列表 */
386405
static luaL_Reg polariswrapper_libs[] =
387-
{
388-
/* 初始化polaris_api */
389-
{"polaris_api_init", polaris_api_init},
390-
/* 获取一个服务实例(如当前进程已经初始化polaris_api则复用,否则生成一个polaris_api) */
391-
{"polaris_get_one_node", polaris_get_one_node},
392-
/* 设置北极星日志路径&级别 */
393-
{"polaris_log_settings", polaris_log_settings},
394-
/* 服务实例调用结果上报(如当前进程已经初始化polaris_api则复用,否则生成一个polaris_api) */
395-
{"polaris_service_call_report", polaris_service_call_report},
396-
397-
{NULL, NULL}
398-
};
406+
{
407+
/* 初始化polaris_api */
408+
{"polaris_api_init", polaris_api_init},
409+
/* 获取一个服务实例(如当前进程已经初始化polaris_api则复用,否则生成一个polaris_api) */
410+
{"polaris_get_one_node", polaris_get_one_node},
411+
/* 设置北极星日志路径&级别 */
412+
{"polaris_log_settings", polaris_log_settings},
413+
/* 服务实例调用结果上报(如当前进程已经初始化polaris_api则复用,否则生成一个polaris_api) */
414+
{"polaris_service_call_report", polaris_service_call_report},
415+
// 限流申请访问配额
416+
{"polaris_get_quota", polaris_get_quota},
417+
{NULL, NULL}};
399418

400419
/**
401420
* 该C库的唯一入口函数 其函数签名等同于上面的注册函数
402421
*/
403-
int luaopen_polariswrapper(lua_State* l)
422+
int luaopen_polariswrapper(lua_State *l)
404423
{
405-
const char* lib_name = "polariswrapper";
424+
const char *lib_name = "polariswrapper";
406425
luaL_register(l, lib_name, polariswrapper_libs);
407426
return 1;
408-
}
427+
}

pre_build.sh

-42
This file was deleted.

test/polaris.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
global:
2+
serverConnector:
3+
addresses:
4+
- 119.91.66.223:8091

test/test_polaris_sdk_lua_wrapper.lua

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
package.cpath = "/data/app/openresty1.7.4.1/lua/polaris_test_demo/so/?.so;"
2-
package.path = "/data/app/openresty1.7.4.1/lua/polaris_test_demo/?.lua;"..package.path
1+
package.cpath = "../?.so;"
2+
package.path = "../?.lua;"..package.path
33

44
local polaris = require "polaris"
55

66
-- 显式初始化polaris_api
77
-- 注:需要指定polaris.yaml时需调用,否则可以直接调用get_server_host_port,包含了隐式初始化polaris_api
8-
polaris.polaris_api_init("/data/app/openresty1.7.4.1/nginx/conf/polaris.yaml")
8+
polaris.polaris_api_init("./polaris.yaml")
99

1010
-- 设置北极星服务日志路径
11-
polaris.polaris_log_settings("/data/app/openresty1.7.4.1/nginx/logs/polaris/log", 0)
11+
polaris.polaris_log_settings("../logs/polaris/log", 0)
1212

1313
-- 获取服务实例信息
14-
local get_host_result, msg, host_info = polaris.get_server_host_port("default", "echosvr.Greeter")
14+
local get_host_result, msg, host_info = polaris.get_server_host_port("Polaris", "polaris.discover")
1515

1616
if get_host_result == 0 then
1717
print(string.format("get host: %s, port %d, instance_id: %s", host_info.host, host_info.port, host_info.instance_id))

0 commit comments

Comments
 (0)