1.实例:
log.lua文件
local cjson = require "cjson.safe"
local logger = require "resty.logger.socket"
if not logger.initted() then
local ok, err = logger.init {
host = "127.0.0.1", --部署时,需要填写logserver服务器ip
port = 1234,--部署时,需要填写logserver服务器port
sock_type = "tcp",--采用udp传输
flush_limit = 1,--1就是时实传输,
--drop_limit = 5678, --默认1mb 超过会自动删除
timeout = 10000,--超时设置
pool_size = 100 --连接池大小
}
if not ok then
ngx.log(ngx.ERR, "failed to initialize the logger: ", err)
return
end
end
client_ip = ngx.var.remote_addr
local args = ngx.req.get_post_args(10)
if not args then
ngx.say("failed to get post args")
return
end
-- ngx.req.read_body()
logstr = cjson.encode(args).."\n"
local bytes, err = logger.log(logstr)
if err then
ngx.log(ngx.ERR, "failed to log message: ", err)
return
end
日志阶段的配置文件
server{
lua_code_cache on;#lua代码缓存,开发期间为off,上线后改为on
lua_need_request_body on;
listen 8088;
location /v2{
content_by_lua_block {
ngx.say(1);
}
log_by_lua_file /Users/guanguan/workspace/lua.api.stream/log.lua;
}
access_log /usr/local/openresty/nginx/logs/upgrade_access.log main;
error_log /usr/local/openresty/nginx/logs/upgrade_error.log debug;
}
2.stream-lua-nginx-module模块的代码
stream.lua文件
local cjson = require "cjson.safe"
local sock = assert(ngx.req.socket(true))
sock:settimeout(1000) -- one second timout
local data = sock:receive('*l')
ngx.log(ngx.ERR,"服务端接收数据:",data)
receive有几种方式,读取固定字节数,读取行(*l),读取到所有(到关闭连接),读取任意数据马上返回(*b,需要patch),此种还未合并到master分支,需要编译:https://github.com/openresty/stream-lua-nginx-module/pull/33
stream配置文件
#user root;
worker_processes 8;
error_log logs/error.log;
error_log logs/error.log notice;
error_log logs/error.log info;
#pid logs/nginx.pid;
events {
#use epoll;
worker_connections 1024;
}
stream {
# define a TCP server listening on the port 1234:
server {
listen 1234;
content_by_lua_file /Users/guanguan/workspace/lua.api.stream/stream.lua;
error_log /usr/local/openresty/nginx/logs/stream_error.log debug;
}
}
http {
include mime.types;
default_type application/octet-stream;
lua_code_cache off;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
error_log logs/error.log error;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
#server {
# listen 80;
# server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
# location / {
# root html;
# index index.html index.htm;
#}
#location /v1{
# default_type application/json;
# content_by_lua_block{
# ngx.say("hello!");
#}
#}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
# error_page 500 502 503 504 /50x.html;
#location = /50x.html {
# root html;
#}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
# }
#include /usr/local/openresty/nginx/conf/vhosts/*.conf;
include /usr/local/openresty/nginx/conf/vstream/*.conf;
}
性能如下: