Nginx service processing module
Prerequisites
- Linux Platform
- Nginx version is not lower than
1.9.11 - Installed tools for Nginx compilation
Module Features
- Service request/response messages are all in
json format. The cJSON library is compiled by default. Business developers can use it to parse/package packets, and of course they can also use other json parsing libraries (see example/third_test section) - By implementing a specific interface, compile the service into a dynamic library and load it into Nginx
advantage
- High performance , using Nginx's multi-process model to process business, and business developers can also use multi-threading in the implemented interface according to specific scenarios to further improve performance, and can also scale horizontally through load balancing.
- Strong , when the service crashes, Nginx will re-start the work process to ensure that the business does not stop processing
- Service smooth restart/upgrade . With the help of Nginx's smooth restart/upgrade function, service restart/upgrade can be elegantly restarted/upgrade.
Build
1. Download Nginx source code
# 这里以 nginx-1.24.0 为例, 也可以根据自己情况下载其他版本的 nginx 源码
wget http://nginx.org/download/nginx-1.24.0.tar.gz
2. Decompress and enter the Nginx source code directory
tar -zxvf nginx-1.24.0.tar.gz
cd nginx-1.24.0
3. Pull the code of this project
# 这里拉取到 nginx 源码的目录下, 也可以根据情况拉取到其他目录
git clone https://github.com/xukeawsl/ngx_http_service_module.git
4. Add the module to nginx and install it
# 如果你将项目代码放在其他路径, 则需要修改这里的模块路径
./configure --add-module=ngx_http_service_module
make
make install
Configuration commands
1. module_path (can only be configured in http block)
- This command configures the path to which the dynamic library belongs. If it is not configured, install the default search path search
- The first parameter is the path, followed by the dynamic library file name (full name)
module_path {
/ path/to /dir1 module1.so;
/ path/to /dir2 module2.so module3.so;
} 2. module_dependency (can only be configured in http block)
- This configuration sets the dependencies between dynamic libraries
- The first parameter is the dynamic library that is dependent, followed by the dynamic library it depends on
module_dependency {
module1. so module2.so;
module3. so module2.so libjsoncpp.so;
} 3. service (can only be configured in http block)
- Services provided by this configuration setting module
- The first parameter is the dynamic library file name, followed by the service name provided for the dynamic library (case sensitive)
- Service name needs to be globally unique
service {
module1. so srv_echo srv_datetime;
module2. so srv_sayHello;
module3. so srv_getSum;
} 4. service_mode (http, server, location blocks can all be configured)
- Used to enable module functions, the unenabled parts will not be processed for service
- Not enabled by default, the following example is enabled globally on the specified server, but the routing request for
/test is not processed
http {
server {
sevice_mode on;
location /test {
service_mode off;
}
}
}Service interface development
- Users need to include the
ngx_http_service_interface.h interface file in the project
// 接口形如下面的函数
#include "ngx_http_service_interface.h"
#include "cJSON.h"
void srv_sayHello ( const ngx_request_t * rqst , ngx_request_t * resp )
{
// do something...
}- Request/response structure
// 请求结构只包含一个 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 * );
};Service Call Rules
- The request method requires
POST - Specify the service to be called by the value of the http header
Service-Name -
Content-Type must be of type json - As follows, call the
srv_sayHello service
curl -X POST " http://localhost/service_test "
-H ' Content-Type: application/json '
-H ' Service-Name: srv_sayHello '
-d ' {"data": "xxxx"} '