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; } }local sock = assert(ngx.req.socket(true))sock:settimeout(2000) -- one second timeoutwhile 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")import socketimport time
BUFSIZE = 1024print "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"create socketing ...failed to receive a line: line:nilerr:timeout [thunder!]
closed2016/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
ngx conf如下:lua代码如下: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;}}
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 thenngx.say("received: ", line)ngx.log(ngx.ALERT, line)ngx.say("log end!")elsengx.say("failed to receive a line: ","line:", line,"err:",err, " [", part, "]")breakendngx.sleep(1)sock.close()
--endngx.say("closed")
ngx conf如下:lua代码如下: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;}}local sock = assert(ngx.req.socket(true))sock:settimeout(2000) -- one second timeoutwhile true dolocal line, err, part = sock:receive()
if line thenngx.say("received: ", line)ngx.log(ngx.ALERT, line)ngx.say("log end!")elsengx.say("failed to receive a line: ","line:", line,"err:",err, " [", part, "]")breakendngx.sleep(1)sock.close()endngx.say("closed")python 测试发送脚本如import socketimport timeBUFSIZE = 1024print "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 datatime.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:12341,为什么会超时,不接收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
谢谢,doujiang加了换行己解决,可以收到发送,我想问一下,如何大量发送消息,会不会出现粘包的现像?????
我现在有想用resty-logger-socket 在log 阶段做一个收集请求日志到 stream tcp server当中,这样使业务 代码都 在跑在nginx当中可以想想,这个业务接口性能并发相当高,因为接口返回给客户端的东西,客户端并不关系,只要保证接口是200接口就可以了,现在用log阶段,即使4xx,5xx也不用担心业务挂了
--
请问一下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来完成这种收集日志,不想采用队列,因为这样还要为队列写一套消费消息处理的程序,
--
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 --连接池大小
}
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写道:lua-resty-logger-socket 库,是否每条消息都flush?pool_size 如何设置?建议先确定哪部分降低了性能,再分析哪里写法不妥了。1.无log阶段,记录原始qps2.有log阶段,不调用log、flush等操作,记录qps2.有log阶段,输出网络日志,stream tcp 接收到消息后不做任何处理(不操作mysql),记录qps3.完整流程测试(你已做的测试),记录qpstan 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来完成这种收集日志,不想采用队列,因为这样还要为队列写一套消费消息处理的程序,
--
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写道:
lua-resty-logger-socket 库,是否每条消息都flush?pool_size 如何设置?建议先确定哪部分降低了性能,再分析哪里写法不妥了。1.无log阶段,记录原始qps2.有log阶段,不调用log、flush等操作,记录qps2.有log阶段,输出网络日志,stream tcp 接收到消息后不做任何处理(不操作mysql),记录qps3.完整流程测试(你已做的测试),记录qpstan 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来完成这种收集日志,不想采用队列,因为这样还要为队列写一套消费消息处理的程序,
--
ngx conf如下:
--