Skip to content

Commit 4d9fb7a

Browse files
authored
Merge pull request #1 from chuntaojun/main
first commit and init polaris-lua
2 parents b0c3841 + e058f76 commit 4d9fb7a

File tree

14 files changed

+2021
-2
lines changed

14 files changed

+2021
-2
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
*.tar.gz
2+
a.out
3+
*.o
4+
*.so
5+
third_party/polaris_cpp_sdk/slib/*.a
6+
third_party/polaris_cpp_sdk/dlib/*.so

Makefile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
CC=gcc
2+
3+
INC= -I./third_party/polaris_cpp_sdk/include \
4+
-I./third_party/lua/include
5+
LIB=-L./third_party/polaris_cpp_sdk/slib -lpolaris_api -lprotobuf -lm -lstdc++ -L./third_party/lua/lib/ -llua
6+
7+
CFLAGS= -g -Wall -fPIC
8+
TARGET=./polariswrapper.so
9+
10+
OBJ+=./polaris_sdk_lua_wrapper.o
11+
12+
$(TARGET):$(OBJ)
13+
$(CC) $(CFLAGS) -o $@ $^ $(LIB) -shared -pthread -lz -lrt
14+
15+
%.o: %.c
16+
$(CC) $(CFLAGS) $(INC) -c -o $@ $<
17+
18+
clean:
19+
rm -f $(OBJ)
20+
rm -f $(TARGET)

README-zh.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Polaris Lua
2+
3+
[English Document](./README.md)
4+
5+
## 1. 项目介绍
6+
7+
本项目在北极星已有的C++ sdk [polaris-cpp](https://github.com/polarismesh/polaris-cpp) 项目的基础上,实现了北极星服务发现的lua层封装,可集成到OpenResty等使用Lua脚本的系统中使用。
8+
9+
## 2. 快速上手
10+
11+
### 2.1 目录结构
12+
```
13+
.
14+
├── lua // lua的 C 静态库,头文件
15+
│ ├── include
16+
│ └── lib
17+
18+
`-- third_party
19+
`-- polaris_cpp_sdk // 编译生成的 polaris-cpp 静态库 & 动态库,头文件,需要自行编译
20+
21+
├── polaris_sdk_lua_wrapper.c // 供 lua 使用的 C API 封装以及入口函数
22+
├── polaris.lua // lua 层的北极星服务发现模块封装
23+
├── test // 测试用例
24+
│ └── test_polaris_sdk_lua_wrapper.lua
25+
26+
├── pre_build.sh // 编译前执行,识别当前系统环境,链接合适的北极星sdk库
27+
├── Makefile // Makefile 文件
28+
└── README.md // 说明文档
29+
```
30+
31+
### 2.2 使用方法
32+
33+
1. 拉取北极星c++ sdk的最新代码,`git clone https://github.com/polarismesh/polaris-cpp`
34+
2. 编译`polaris-cpp`及打包
35+
```
36+
# 编译
37+
make
38+
# 测试
39+
make test
40+
# 打包,可添加参数package_name指定打包名,默认polaris_cpp_sdk
41+
make package # package_name=polaris_cpp_sdk
42+
```
43+
3. 将`polaris-cpp`编译出来的产物`polaris_cpp_sdk`拷贝到本项目下的`third_party/polaris_cpp_sdk`
44+
4. 执行 `bash pre_build.sh`
45+
5. 执行 `make` 生成 `polariswrapper.so`
46+
6. 将`polariswrapper.so`,`polaris.lua` 文件上传到 OpenResty 项目的合适路径,编写自己的业务 lua 文件使用,业务lua的编写示例可参考测试用例 `test_polaris_sdk_lua_wrapper.lua`

README.md

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,40 @@
1-
# polaris-lua
2-
polaris sdk for lua language
1+
# Polaris Lua
2+
3+
[中文文档](./README-zh.md)
4+
5+
## 1. Project Introduction
6+
7+
Based on the existing C++ sdk [polaris-cpp](https://github.com/polarismesh/polaris-cpp) project of Polaris, this project implements the Lua layer encapsulation of Polaris service discovery, which can be integrated into OpenResty and other systems that use Lua scripts.
8+
## 2. Get started quickly
9+
### 2.1 Directory Structure
10+
```
11+
.
12+
├── lua // Lua C static library, header file
13+
│ ├── include
14+
│ └── lib
15+
16+
`-- third_party
17+
`-- polaris_cpp_sdk // Compile the generated polaris-cpp static library & dynamic library, header file, you need to compile it yourself
18+
19+
├── polaris_sdk_lua_wrapper.c // C API wrapper and entry function for lua
20+
├── polaris.lua // Polaris service discovery module package at the lua layer
21+
├── test // Test case
22+
│ └── test_polaris_sdk_lua_wrapper.lua
23+
24+
├── pre_build.sh // Execute before compiling, identify the current system environment, link the appropriate Polaris SDK library
25+
```
26+
27+
### 2.2 Instructions
28+
29+
1. Pull the latest code of Polaris c++ sdk, `git clone https://github.com/polarismesh/polaris-cpp`
30+
2. Compile `polaris-cpp` and package
31+
```
32+
make
33+
make test
34+
make package # package_name=polaris_cpp_sdk
35+
```
36+
3. Copy the product `polaris_cpp_sdk` compiled by `polaris-cpp` to `third_party/polaris_cpp_sdk` under this project
37+
4. Execute `bash pre_build.sh`
38+
5. Execute `make` to generate `polariswrapper.so`
39+
6. 将`polariswrapper.so`,`polaris.lua` 文件上传到 OpenResty 项目的合适路径,编写自己的业务 lua 文件使用,业务lua的编写示例可参考测试用例 `test_polaris_sdk_lua_wrapper.lua`
40+

polaris.lua

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
--[[
2+
Author: andymatthu#tencent.com
3+
Create date: 2020/06/11
4+
Update date: 2020/11/04
5+
Desc: 北极星sdk lua层封装
6+
]]
7+
8+
local _M = {}
9+
10+
local polariswrapper = require "polariswrapper"
11+
12+
-- 显式初始化polaris_api(单进程只会初始化一次)
13+
-- 注:需要指定polaris.yaml时需调用,否则可以直接调用get_server_host_port,包含了隐式初始化polaris_api
14+
function _M.polaris_api_init(config_file)
15+
local result, msg
16+
if config_file ~= "" then
17+
result, msg = polariswrapper.polaris_api_init(config_file)
18+
else
19+
result, msg = polariswrapper.polaris_api_init()
20+
end
21+
22+
return result, msg
23+
end
24+
25+
-- 设置北极星服务日志路径
26+
function _M.polaris_log_settings(log_dir, log_level)
27+
polariswrapper.polaris_log_settings(log_dir, log_level)
28+
end
29+
30+
31+
-- 通过北极星获取服务ip和端口
32+
function _M.get_server_host_port(service_namespace, service_name)
33+
-- 查询host port
34+
local result, msg, host, port, instance_id, if_reuse = polariswrapper.polaris_get_one_node(service_namespace, service_name)
35+
if result ~= 0 then
36+
ngx.log(ngx.ERR, "polaris_get_one_node failed, result:"..result..", msg:"..msg)
37+
return result, msg
38+
end
39+
ngx.log(ngx.INFO, "polaris_api if_reuse:"..if_reuse)
40+
41+
return 0, "ok", {host=host, port=port, instance_id=instance_id}
42+
end
43+
44+
-- 上报北极星调用结果
45+
function _M.call_report(service_namespace, service_name, instance_id, result)
46+
local result, msg = polariswrapper.polaris_service_call_report(service_namespace, service_name, instance_id, result)
47+
if result ~= 0 then
48+
ngx.log(ngx.ERR, "polaris_service_call_report failed, result:"..result..", msg:"..msg)
49+
return result, msg
50+
end
51+
52+
return 0, "ok"
53+
end
54+
55+
return _M

0 commit comments

Comments
 (0)