ngx_http_service_module
1.0.0
1.9.11json 格式, 默认将 cJSON 库编译进去了, 业务开发人员可以使用其进行报文解析/打包, 当然也可以使用其他的 json 解析库(见 example/third_test 部分)# 这里以 nginx-1.24.0 为例, 也可以根据自己情况下载其他版本的 nginx 源码
wget http://nginx.org/download/nginx-1.24.0.tar.gztar -zxvf nginx-1.24.0.tar.gz
cd nginx-1.24.0# 这里拉取到 nginx 源码的目录下, 也可以根据情况拉取到其他目录
git clone https://github.com/xukeawsl/ngx_http_service_module.git# 如果你将项目代码放在其他路径, 则需要修改这里的模块路径
./configure --add-module=ngx_http_service_module
make
make installmodule_path {
/path/to/dir1 module1.so;
/path/to/dir2 module2.so module3.so;
}module_dependency {
module1.so module2.so;
module3.so module2.so libjsoncpp.so;
}service {
module1.so srv_echo srv_datetime;
module2.so srv_sayHello;
module3.so srv_getSum;
}/test 的路由请求不处理http {
server {
sevice_mode on;
location /test {
service_mode off;
}
}
}ngx_http_service_interface.h 接口文件// 接口形如下面的函数
#include "ngx_http_service_interface.h"
#include "cJSON.h"
void srv_sayHello(const ngx_request_t *rqst, ngx_request_t *resp)
{
// do something...
}// 请求结构只包含一个 const char * 指针指向 json 请求字符串
struct ngx_json_request_s {
const char *data;
};
// 响应结构包含一个 char * 指针指向 json 响应字符串和一个回调函数用于释放响应字符串的内存
// 因为对于 c++ 实现的服务可能使用 new 来分配内存, 那就需要指定正确的内存释放方式
struct ngx_json_response_s {
char *data;
void (*release)(void *);
};POSTService-Name 的值来指定调用的服务Content-Type 必须为 json 类型srv_sayHello 服务curl -X POST "http://localhost/service_test"
-H 'Content-Type: application/json'
-H 'Service-Name: srv_sayHello'
-d '{"data": "xxxx"}'