stream-lua-nginx-module 当前这个版本可以用在生产环境吗?在本地跑了一个demo发现接收不了数据

377 views
Skip to first unread message

tan winfan

unread,
Dec 19, 2016, 8:32:53 AM12/19/16
to openresty
ngx conf如下:
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;
        }  
}


lua代码如下:
local sock = assert(ngx.req.socket(true))
sock:settimeout(2000)  -- one second timeout
while true do
    local line, err, part = sock:receive()
    if line then
        ngx.say("received: ", line)
        ngx.log(ngx.ALERT, line)
        ngx.say("log end!")

    else
        ngx.say("failed to receive a line: ","line:", line,"err:",err, " [", part, "]")
        break
    end
    ngx.sleep(1)
    sock.close()
end
 
ngx.say("closed")

python 测试发送脚本如
import socket
import time

BUFSIZE = 1024
print "create socketing ..."
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('127.0.0.1', 1234))
while True:
    s.send("thunder!")
    data = s.recv(BUFSIZE)
    print data
    time.sleep(1)

print "done"

log打印输出结果如下:python打印显示如下:
create socketing ...
failed to receive a line: line:nilerr:timeout [thunder!]

closed

ngx.error log如下
2016/12/19 21:28:51 [error] 29003#0: *1 stream lua tcp socket read timed out while handling client connection, client: 127.0.0.1, server: 0.0.0.0:1234


1,为什么会超时,不接收Python发送过来的日志?
2,请问一下,如果通过python多线程大量发送包时,那么nginx这边是否解决了粘包的问题?还是需要自己解决!!

tan winfan

unread,
Dec 19, 2016, 8:42:42 AM12/19/16
to openresty


在 2016年12月19日星期一 UTC+8下午9:32:53,tan winfan写道:
ngx conf如下:
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;
        }  
}


lua代码如下:
local sock = assert(ngx.req.socket(true))
sock:settimeout(2000)  -- one second timeout
--while true do 这里去掉了while
    local line, err, part = sock:receive()
    if line then
        ngx.say("received: ", line)
        ngx.log(ngx.ALERT, line)
        ngx.say("log end!")

    else
        ngx.say("failed to receive a line: ","line:", line,"err:",err, " [", part, "]")
        break
    end
    ngx.sleep(1)
    sock.close()
--end
 
ngx.say("closed")

DeJiang Zhu

unread,
Dec 20, 2016, 1:02:34 AM12/20/16
to open...@googlegroups.com
Hello

2016-12-19 21:32 GMT+08:00 tan winfan <winl...@gmail.com>:
ngx conf如下:
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;
        }  
}


lua代码如下:
local sock = assert(ngx.req.socket(true))
sock:settimeout(2000)  -- one second timeout
while true do
    local line, err, part = sock:receive()

这是按行接收数据
 
    if line then
        ngx.say("received: ", line)
        ngx.log(ngx.ALERT, line)
        ngx.say("log end!")

    else
        ngx.say("failed to receive a line: ","line:", line,"err:",err, " [", part, "]")
        break
    end
    ngx.sleep(1)
    sock.close()
end
 
ngx.say("closed")

python 测试发送脚本如
import socket
import time

BUFSIZE = 1024
print "create socketing ..."
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('127.0.0.1', 1234))
while True:
    s.send("thunder!")

你这里没有换行符吧?
 
    data = s.recv(BUFSIZE)
    print data
    time.sleep(1)

print "done"

log打印输出结果如下:python打印显示如下:
create socketing ...
failed to receive a line: line:nilerr:timeout [thunder!]

closed

ngx.error log如下
2016/12/19 21:28:51 [error] 29003#0: *1 stream lua tcp socket read timed out while handling client connection, client: 127.0.0.1, server: 0.0.0.0:1234


1,为什么会超时,不接收Python发送过来的日志?
2,请问一下,如果通过python多线程大量发送包时,那么nginx这边是否解决了粘包的问题?还是需要自己解决!!

--
--
邮件来自列表“openresty”,专用于技术讨论!
订阅: 请发空白邮件到 openresty+subscribe@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 openresty+unsubscribe@googlegroups.com
归档: http://groups.google.com/group/openresty
官网: http://openresty.org/
仓库: https://github.com/agentzh/ngx_openresty
教程: http://openresty.org/download/agentzh-nginx-tutorials-zhcn.html

tan winfan

unread,
Dec 20, 2016, 2:37:39 AM12/20/16
to openresty
谢谢,doujiang

加了换行己解决,可以收到发送,
我想问一下,如何大量发送消息,会不会出现粘包的现像?????

我现在有想用resty-logger-socket 在log 阶段做一个收集请求日志到 stream tcp server当中,这样使业务 代码都 在跑在nginx当中
可以想想,这个业务接口性能并发相当高,因为接口返回给客户端的东西,客户端并不关系,只要保证接口是200接口就可以了,现在用log阶段,即使4xx,5xx也不用担心业务挂了

DeJiang Zhu

unread,
Dec 20, 2016, 3:17:43 AM12/20/16
to open...@googlegroups.com
Hello

在 2016年12月20日 下午3:37,tan winfan <winl...@gmail.com>写道:
谢谢,doujiang

加了换行己解决,可以收到发送,
我想问一下,如何大量发送消息,会不会出现粘包的现像?????

这个得你自己根据应用层协议来处理了


我现在有想用resty-logger-socket 在log 阶段做一个收集请求日志到 stream tcp server当中,这样使业务 代码都 在跑在nginx当中
可以想想,这个业务接口性能并发相当高,因为接口返回给客户端的东西,客户端并不关系,只要保证接口是200接口就可以了,现在用log阶段,即使4xx,5xx也不用担心业务挂了

--

tan winfan

unread,
Dec 20, 2016, 8:18:38 PM12/20/16
to openresty
请问一下doujiang

我昨天实验了一下,通过 log阶段做一个把请求参数日志写入到mysql,我的方法是这样子的,content_by_lua 阶段是“ngx.say(1)没什么逻辑”
我用的resty-logger-socket  这个模块在log阶段把请求参数 把这个消息发给 openresty stream tcp 1234端口,然后我在这个1234端服务 上接收到发过来的消息,直接插入数据mysql,我压测效果 qps 600多个,开了8个进程,lua_cache on 状态,这为什么这么慢,为什么这个tcp 1234端口会影响 整个nginx处理慢呢?
我应该如何优化这个性能呢?采取什么样的措施,除了不写队列的情况下,应该要怎么做,我的mysql insert 语句也简单,就几个字段而以,
我是想用一套nginx来完成这种收集日志,不想采用队列,因为这样还要为队列写一套消费消息处理的程序,

DeJiang Zhu

unread,
Dec 20, 2016, 9:25:17 PM12/20/16
to open...@googlegroups.com
Hello

在 2016年12月21日 上午9:18,tan winfan <winl...@gmail.com>写道:
请问一下doujiang

我昨天实验了一下,通过 log阶段做一个把请求参数日志写入到mysql,我的方法是这样子的,content_by_lua 阶段是“ngx.say(1)没什么逻辑”
我用的resty-logger-socket  这个模块在log阶段把请求参数 把这个消息发给 openresty stream tcp 1234端口,然后我在这个1234端服务 上接收到发过来的消息,直接插入数据mysql,我压测效果 qps 600多个,

你这压的是 mysql 吧?
 
开了8个进程,lua_cache on 状态,这为什么这么慢,为什么这个tcp 1234端口会影响 整个nginx处理慢呢?

建议你自己多分析,肯定是你哪里姿势不对
 
我应该如何优化这个性能呢?采取什么样的措施,除了不写队列的情况下,应该要怎么做,我的mysql insert 语句也简单,就几个字段而以,
我是想用一套nginx来完成这种收集日志,不想采用队列,因为这样还要为队列写一套消费消息处理的程序,

--

mike07026

unread,
Dec 20, 2016, 10:20:05 PM12/20/16
to openresty
lua-resty-logger-socket 库,是否每条消息都flush?pool_size 如何设置?

建议先确定哪部分降低了性能,再分析哪里写法不妥了。
1.无log阶段,记录原始qps
2.有log阶段,不调用log、flush等操作,记录qps
2.有log阶段,输出网络日志,stream tcp 接收到消息后不做任何处理(不操作mysql),记录qps
3.完整流程测试(你已做的测试),记录qps


tan winfan於 2016年12月21日星期三 UTC+8上午9時18分38秒寫道:

tan winfan

unread,
Dec 21, 2016, 1:14:05 AM12/21/16
to openresty
hello ,我的logger-socket配置如下
collect_log_service_1 = {
host = "127.0.0.1",
port = 6677, --部署时,需要填写logserver服务器port
sock_type = "tcp", --采用tcp传输
flush_limit = 1, --1就是时实传输,
--drop_limit = 5678, --默认1mb 超过会自动删除
timeout = 10000, --超时设置
pool_size = 100 --连接池大小
}
刚刚调试了一下,发现性能提升了不少,是我压测试姿势错了,8G内存,8核 mac os x系统 ,qps 2.9万

请问一下,lua-resty-mysql 有没异步的特性,在insert时,能异步进行吗?

在 2016年12月21日星期三 UTC+8上午11:20:05,mike07026写道:

tan winfan

unread,
Dec 21, 2016, 1:16:32 AM12/21/16
to openresty
不过后面发现,wrk压测总请求数是20万个的请求连接,理论上,一个连接请求,就是一条mysql记录,但是压测后,mysql记录总结果确少很多?这是为什么?


在 2016年12月21日星期三 UTC+8下午2:14:05,tan winfan写道:

Ming

unread,
Dec 21, 2016, 5:04:20 AM12/21/16
to open...@googlegroups.com
在 2016年12月21日 下午2:14,tan winfan <winl...@gmail.com>写道:
hello ,我的logger-socket配置如下
collect_log_service_1 = {
host = "127.0.0.1",
port = 6677, --部署时,需要填写logserver服务器port
sock_type = "tcp", --采用tcp传输
flush_limit = 1, --1就是时实传输,
--drop_limit = 5678, --默认1mb 超过会自动删除
timeout = 10000, --超时设置
pool_size = 100 --连接池大小
}
刚刚调试了一下,发现性能提升了不少,是我压测试姿势错了,8G内存,8核 mac os x系统 ,qps 2.9万

请问一下,lua-resty-mysql 有没异步的特性,在insert时,能异步进行吗?


lua-resty-mysql 是非阻塞的,在 insert 的时候,还可以处理其他的请求。
要提高性能,建议你调优 MySQL 参数,并每次 insert 多条记录。
 
在 2016年12月21日星期三 UTC+8上午11:20:05,mike07026写道:
lua-resty-logger-socket 库,是否每条消息都flush?pool_size 如何设置?

建议先确定哪部分降低了性能,再分析哪里写法不妥了。
1.无log阶段,记录原始qps
2.有log阶段,不调用log、flush等操作,记录qps
2.有log阶段,输出网络日志,stream tcp 接收到消息后不做任何处理(不操作mysql),记录qps
3.完整流程测试(你已做的测试),记录qps


tan winfan於 2016年12月21日星期三 UTC+8上午9時18分38秒寫道:
请问一下doujiang

我昨天实验了一下,通过 log阶段做一个把请求参数日志写入到mysql,我的方法是这样子的,content_by_lua 阶段是“ngx.say(1)没什么逻辑”
我用的resty-logger-socket  这个模块在log阶段把请求参数 把这个消息发给 openresty stream tcp 1234端口,然后我在这个1234端服务 上接收到发过来的消息,直接插入数据mysql,我压测效果 qps 600多个,开了8个进程,lua_cache on 状态,这为什么这么慢,为什么这个tcp 1234端口会影响 整个nginx处理慢呢?
我应该如何优化这个性能呢?采取什么样的措施,除了不写队列的情况下,应该要怎么做,我的mysql insert 语句也简单,就几个字段而以,
我是想用一套nginx来完成这种收集日志,不想采用队列,因为这样还要为队列写一套消费消息处理的程序,

--

Ming

unread,
Dec 21, 2016, 5:15:24 AM12/21/16
to open...@googlegroups.com
在 2016年12月21日 下午2:14,tan winfan <winl...@gmail.com>写道:
hello ,我的logger-socket配置如下
collect_log_service_1 = {
host = "127.0.0.1",
port = 6677, --部署时,需要填写logserver服务器port
sock_type = "tcp", --采用tcp传输
flush_limit = 1, --1就是时实传输,
--drop_limit = 5678, --默认1mb 超过会自动删除
timeout = 10000, --超时设置
pool_size = 100 --连接池大小
}
刚刚调试了一下,发现性能提升了不少,是我压测试姿势错了,8G内存,8核 mac os x系统 ,qps 2.9万

请问一下,lua-resty-mysql 有没异步的特性,在insert时,能异步进行吗?


lua-resty-mysql 是非阻塞的,在 insert 的时候,还可以处理其他的请求。
要提高性能,建议你调优 MySQL 参数,并每次 insert 多条记录。
 
在 2016年12月21日星期三 UTC+8上午11:20:05,mike07026写道:
lua-resty-logger-socket 库,是否每条消息都flush?pool_size 如何设置?

建议先确定哪部分降低了性能,再分析哪里写法不妥了。
1.无log阶段,记录原始qps
2.有log阶段,不调用log、flush等操作,记录qps
2.有log阶段,输出网络日志,stream tcp 接收到消息后不做任何处理(不操作mysql),记录qps
3.完整流程测试(你已做的测试),记录qps


tan winfan於 2016年12月21日星期三 UTC+8上午9時18分38秒寫道:
请问一下doujiang

我昨天实验了一下,通过 log阶段做一个把请求参数日志写入到mysql,我的方法是这样子的,content_by_lua 阶段是“ngx.say(1)没什么逻辑”
我用的resty-logger-socket  这个模块在log阶段把请求参数 把这个消息发给 openresty stream tcp 1234端口,然后我在这个1234端服务 上接收到发过来的消息,直接插入数据mysql,我压测效果 qps 600多个,开了8个进程,lua_cache on 状态,这为什么这么慢,为什么这个tcp 1234端口会影响 整个nginx处理慢呢?
我应该如何优化这个性能呢?采取什么样的措施,除了不写队列的情况下,应该要怎么做,我的mysql insert 语句也简单,就几个字段而以,
我是想用一套nginx来完成这种收集日志,不想采用队列,因为这样还要为队列写一套消费消息处理的程序,

--

tan winfan

unread,
Dec 21, 2016, 8:09:03 PM12/21/16
to openresty
hello,温总
 
    lua-resty-mysql 是非阻塞的,在 insert 的时候,还可以处理其他的请求。
   要提高性能,建议你调优 MySQL 参数,并每次 insert 多条记录。
请问一下,insert多条有没好的思路?
你指的是insert into table (`field`) values(values1),(values2),(values3)..... 这种

我的思路是把 values 放在一个共享变量中,如lua table中
来一条消息放到lua table中,当table size>100条记录时,这时就执行insert语句
在openresty stream tcp server中 采用什么方式的共享内存呢,多进程会不会导致锁呢?如何规避这种弊端,比如采用redis来做预保存将要插入的mysql的记录,当redis 某key size达到 100时就插入到mysql,插入完全又要把这个key清掉,那么如何保存这个消息不丢失呢? 
还有一个问题是如果insert into table (`field`) values(values1),(values2),(values3) 在这个sql中,values2是错误的插入失败,那么是不是values3将丢失

根据我上面所描述的一些问题
1,请问有没一种方法,可以很好实现 达到100条消息就 批量插入mysql,  用队列?还是上述所说思路,只要解决好共享内存锁问题?。。。。
2,这种批量插入sql失败是否可以做到事务回滚?


在 2016年12月19日星期一 UTC+8下午9:32:53,tan winfan写道:
ngx conf如下:

Ming

unread,
Dec 21, 2016, 10:11:35 PM12/21/16
to open...@googlegroups.com
如果你的终端请求,每次对应的是一个 MySQL 记录,而且量不是特别大,OpenResty 这边的逻辑还是一条一条来插入最简单。
在 MySQL 侧,可以通过调整写入参数,以及 RAID 卡的写缓存,来解决大量写入的问题。
毕竟,保持逻辑简单是最重要的。

--

Ming

unread,
Dec 21, 2016, 10:12:58 PM12/21/16
to open...@googlegroups.com
另外, 有些 MySQL proxy 也可以帮你完成批量插入的问题。
Reply all
Reply to author
Forward
0 new messages