docker openresty kafka 简单调试

##创建openresty容器

docker run -itd --name openrest-test -p 8080:8080 -p 80:80 openresty/openresty:1.19.3.2-2-buster

#进去容器

docker exec -it openrest-test bash

####新建 nginx conf 文件 /etc/nginx/conf.d/testLua.conf

server {
	listen 8080;
	location / {
		default_type text/html;
		content_by_lua_file /usr/local/openresty/nginx/lua/testkafka.lua; 
	}
}

#### 安装kafka 库

#下载lua-resty-kafka:
apt-get install wget unzip -y
cd /usr/local/src/ 
wget https://github.com/doujiang24/lua-resty-kafka/archive/master.zip 
unzip master.zip -d /usr/local/src/ 


#拷贝kafka相关依赖脚本到openresty 
cp -rf /usr/local/src/lua-resty-kafka-master/lib/resty/kafka/ /usr/local/openresty/lualib/resty/
####新建lua kafka文件

mkdir /usr/local/openresty/nginx/lua

vi /usr/local/openresty/nginx/lua/testkafka.lua

local cjson = require "cjson"  
local producer = require "resty.kafka.producer"  
local broker_list = {  
  { host = "192.168.134.130", port = 9092 },  
}  
local log_json = {}  
log_json["uri"]=ngx.var.uri  
log_json["args"]=ngx.var.args  
log_json["host"]=ngx.var.host  
log_json["request_body"]=ngx.var.request_body  
log_json["remote_addr"] = ngx.var.remote_addr  
log_json["remote_user"] = ngx.var.remote_user  
log_json["time_local"] = ngx.var.time_local  
log_json["status"] = ngx.var.status  
log_json["body_bytes_sent"] = ngx.var.body_bytes_sent  
log_json["http_referer"] = ngx.var.http_referer  
log_json["http_user_agent"] = ngx.var.http_user_agent  
log_json["http_x_forwarded_for"] = ngx.var.http_x_forwarded_for  
log_json["upstream_response_time"] = ngx.var.upstream_response_time  
log_json["request_time"] = ngx.var.request_time  
local message = cjson.encode(log_json);  
local bp = producer:new(broker_list, { producer_type = "async" }) 
local ok, err = bp:send("test1", nil, message)  
ngx.say("<br>", message)
ngx.say("<br>kafka result:", ok)
ngx.say("<br>kafka error:", err)

## 新建 kafka docker 容器

vi /root/docker/docker-compose.yml

version: '2'
services:
  zookeeper:
    image: confluentinc/cp-zookeeper:6.2.0
    container_name: zookeeper
    mem_limit: 1024M
    environment:
      ZOOKEEPER_CLIENT_PORT: 2181
  kafka:
    image: confluentinc/cp-kafka:6.2.0
    container_name: kafka
    mem_limit: 1024M
    depends_on:
      - zookeeper
    ports: 
      - 9092:9092  
    environment:
      KAFKA_BROKER_NO: 1
      KAFKA_ADVERTISED_HOST_NAME: 192.168.134.130
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://192.168.134.130:9092
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
      KAFKA_HEAP_OPTS: "-Xmx512M -Xms16M"

docker-compose -f docker-compose.yml up -d

#进入容器

docker exec -it kafka /bin/bash

#运行

kafka-console-consumer --bootstrap-server localhost:9092 --topic test1

#等会来看数据,别关闭

##重启 openresty

openresty -s reload

#访问:

#http://192.168.134.130:8080/

#测试宿主IP:192.168.134.130

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fevan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值