【原创】运维基础之OpenResty(Nginx+Lua)+Kafka
使用docker部署
1 下载
# wget https://github.com/doujiang24/lua-resty-kafka/archive/v0.06.tar.gz
# tar xvf v0.06.tar.gz
2 准备配置文件testkafka.conf
# vi testkafka.conf
lua_package_path "/usr/local/openresty/lualib/resty/kafka/?.lua;;";
lua_need_request_body on;
server {
listen ;
server_name testkafka;
location /test {
content_by_lua '
local testfile = "/tmp/test.log" local cjson = require "cjson"
local client = require "resty.kafka.client"
local producer = require "resty.kafka.producer" local broker_list = {
{ host = "127.0.0.1", port = }
} local topic = "test"
local key = "key"
local message = "halo world" -- usually we do not use this library directly
local cli = client:new(broker_list)
local brokers, partitions = cli:fetch_metadata(topic)
if not brokers then
ngx.say("fetch_metadata failed, err:", partitions)
end
--ngx.say("brokers: ", cjson.encode(brokers), "; partitions: ", cjson.encode(partitions)) -- sync producer_type
local p = producer:new(broker_list) local f = io.open(testfile, "a+")
f:write(topic .. ":" .. key .. ":" .. message .. "\\n")
f:close() local offset, err = p:send(topic, key, message)
if not offset then
ngx.say("send err:", err)
return
end
ngx.say("send success, offset: ", tonumber(offset)) -- this is async producer_type and bp will be reused in the whole nginx worker
local bp = producer:new(broker_list, { producer_type = "async" }) local ok, err = bp:send(topic, key, message)
if not ok then
ngx.say("send err:", err)
return
end ngx.say("host : ", ngx.var.host)
ngx.say("uri : ", ngx.var.uri)
ngx.say("args : ", ngx.var.args)
ngx.say("body : ", ngx.req.get_body_data())
ngx.say("client ip : ", ngx.var.remote_addr)
ngx.say("time : ", ngx.var.time_local)
ngx.say("send success, ok:", ok)
';
}
}
功能:发送kafka、写日志到/tmp/test.log,打印请求信息
修改其中broker的ip和端口,以及topic名;
3 启动docker
$ docker -d -p 80:80 -v /path/to/testkafka.conf:/etc/nginx/conf.d/testkafka.conf -v /path/to/lua-resty-kafka-0.06/lib/resty/kafka:/usr/local/openresty/lualib/resty/kafka openresty/openresty
挂载testkafka.conf以及kafka lib目录
4 测试
# curl 'http://testkafka/test?a=1&b=2' -d 'hello' -x 127.0.0.1:82
send success, offset: 13
host : testkafka
uri : /test
args : a=1&b=2
body : hello
client ip : 172.17.0.1
time : 08/Mar/2019:14:26:20 +0000
send success, ok:true
5 更多
1)可以将nginx访问日志发送到kafka
2)可以将请求数据作为消息发送到kafka(从uri中的path解析出topic)
6 报错
有可能报错:no resolver defined to resolve
这是因为kafka broker配置的是hostname,而不是ip,而nginx遇到hostname必须通过dns解析,而不能依靠/etc/hosts来解析,所以会报以上错误,这时有两种解决方法:
1)安装dnsmasq;
2)修改kafka配置中的advertised.host.name,将其修改为ip即可;
参考:https://github.com/doujiang24/lua-resty-kafka
【原创】运维基础之OpenResty(Nginx+Lua)+Kafka的更多相关文章
- 【原创】运维基础之OpenResty
openresty 1.15.8.1 官方:https://openresty.org/en/ 一 简介 OpenResty® is a dynamic web platform based on N ...
- 【nginx运维基础(6)】Nginx的Rewrite语法详解
概述 重写URL是非常有用的一个功能,因为它可以让你提高搜索引擎阅读和索引你的网站的能力:而且在你改变了自己的网站结构后,无需要求用户修改他们的书签,无需其他网站修改它们的友情链接:它还可以提高你的网 ...
- 【nginx运维基础(5)】Nginx的location攻略
概述 location 有"定位"的意思, 根据Uri来进行不同的定位. 在虚拟主机的配置中,是必不可少的,location可以把网站的不同部分,定位到不同的处理方式上.伪静态,反 ...
- 【nginx运维基础(4)】Nginx的日志管理(日志格式与定时分割日志)
Nginx日志主要分为两种:访问日志和错误日志.日志开关在Nginx配置文件(一般在server段来配置)中设置,两种日志都可以选择性关闭,默认都是打开的. 访问日志access_log #日志格式设 ...
- 【nginx运维基础(3)】Nginx的编译PHP
Apache默认是把PHP作为本身的一个模块(mod_php)来运行的,而Nginx是以FastCGI方式运行的.所以使用Nginx+PHP就是直接配置为FastCGI模式. 安装PHP 下载地址: ...
- 【nginx运维基础(2)】Nginx的配置文件说明及虚拟主机配置示例
配置文件说明 #定义Nginx运行的用户和用户组 user www www; #nginx进程数,建议设置为当前主机的CPU总核心数. worker_processes 8; #全局错误日志定义类型, ...
- 【nginx运维基础(1)】Nginx的编译安装与使用
nginx的官方手册: http://nginx.org/en/docs/ 编译安装 下载地址: http://nginx.org/en/download.html # 为了支持rewrite功能,我 ...
- 【原创】大叔问题定位分享(36)openresty(nginx+lua)中获取不到post数据,ngx.req.get_body_data返回nil
openresty(nginx+lua)中获取不到post数据,ngx.req.get_body_data返回nil This function returns nil if the request ...
- (转)OpenResty(nginx+lua) 开发入门
原文:https://blog.csdn.net/enweitech/article/details/78519398 OpenResty 官网:http://openresty.org/ Open ...
随机推荐
- C#窗口闪烁问题解决
https://www.cnblogs.com/AndyDai/p/5203798.html 开发WinForm 程序时经常会遇到闪屏的问题,这会给用户造成很差的使用体验,所以必须妥善解决好这个问题. ...
- php实现无限级分类查询(递归、非递归)
递归函数实现方式 上面提到,递归函数的也是借助于栈的机制实现的,但是底层对于栈的处理对于程序员来说都是透明的,程序员只需要关心应用的实现逻辑.所以说使用递归处理上述问题理解起来比较容易,代码也比较简洁 ...
- 利用PHP连接数据库——实现用户数据的增删改查的整体操作实例
main页面(主页面) <table width="100%" border="1" cellpadding="0" cellspac ...
- Linux pmap 工具
pmap 用来查看当前进程占用内存使用详细 pmap 格式: -x, --extended # 显示扩展的信息 -d, --device # 显示设备的信息 -q, --quiet # 不显示头或脚注 ...
- [C++]线性链表之顺序表<一>
顺序表中数据元素的存储地址是其序号的线性函数,只要确定了存储顺序表的起始地址(即 基地址),计算任意一个元素的存储地址的时间是相等的,具有这一特点的存储结构称为[随机存储]. 使用的基本数据结构:数组 ...
- rabbitMQ学习2-Python与rabbitmq
python客户端 # rabbitmq官方推荐的python客户端pika模块 pip3 install pika 应用场景1:单发送单接收 1.生产-消费者模型 P 是生产者 C 是消费者 中间h ...
- LinkedStack的底层实现
package zy813ture; import java.util.EmptyStackException; public class MyLinkedStack1 { private Node ...
- CPU缓存一致性协议—MESI详解
MESI(也称伊利诺斯协议)是一种广泛使用的支持写回策略的缓存一致性协议,该协议被应用在Intel奔腾系列的CPU中. MESI协议中的状态 CPU中每个缓存行使用的4种状态进行标记(使用额外的两位b ...
- day 5 - 1 字典(dict)
dict dict key 必须是不可变数据类型,可哈希value:任意数据类型 dict 优点:使用二分查询来搜索数据存储了大量的关系型数据特点:无序的 数据类型划分:可变数据类型,不可变数据类型不 ...
- java程序内存监控