Openresty+Lua+Rdkafka使用

本文介绍了如何在CentOS 7.9环境下安装OpenResty,集成lua-resty-kafka库,并配置Nginx以使用Lua脚本生产Kafka消息。详细步骤包括安装依赖、下载编译OpenResty、配置Nginx及lua脚本,最后提供了测试和生产消息的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Openresty+Lua+Rdkafka使用

  1. 安装openresty
    安装环境:centos 7.9

    cat /etc/redhat-release
    

    安装依赖库:

    yum -y install readline-devel pcre-devel openssl-devel gcc
    

    下载openresty release包:

    wget https://openresty.org/download/openresty-1.19.9.1.tar.gz
    

    配置编译安装:

    ./configure --prefix=/opt/openresty --with-luajit
    make && make install
    
  2. 安装lua-resty-kafka

    wget https://github.com/doujiang24/lua-resty-kafka/archive/master.zip
    

    将压缩包解压到/opt/openresty/lualib/resty/目录中。unzip lua-resty-kafka-master.zip

  3. 配置nginx.conf

    user  test;
    worker_processes  1;
    
    #error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;
    
    #pid        logs/nginx.pid;
    
    events {
        worker_connections  1024;
    }
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
    
        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  notice;
    
        sendfile        on;
        #tcp_nopush     on;
    
        #keepalive_timeout  0;
        keepalive_timeout  65;
        gzip  on;
    
        lua_package_path "/opt/openresty/lualib/resty/kafka/?.lua;;";
        lua_package_cpath "/opt/openresty/lualib/?.so;;";
    
        lua_shared_dict ngx_cache 128m;  # cache
        lua_shared_dict cache_lock 100k; # lock for cache
    
        server {
            listen       192.168.31.90:8080;
            server_name  localhost;
    
            #charset koi8-r;
    
            #access_log  logs/host.access.log  main;
    
            location / {
                root   html;
                index  index.html index.htm;
            }
    
            location = /message/produce {
                lua_code_cache on;
                charset utf-8;
                default_type 'application/json';
                content_by_lua_file "/opt/openresty/nginx/lua/message_produce.lua"; #引用的lua脚本
            }
    
            error_page  404              /404.html;
        }
    }
    
  4. 编辑message_produce.lua脚本,参考下面生产消息的demo。

  5. 操作openresty
    查看使用帮助:

    ./bin/openresty -h
    

    启动:

    ./bin/openresty
    

    重启:

    ./bin/openresty -s reload
    

    结束:

    ./bin/openresty -s stop
    
  6. 测试
    通过postman请求nginx接口,模拟生产消息,成功。
    在这里插入图片描述
    在这里插入图片描述

  7. 生产消息代码demo

    local producer = require "resty.kafka.producer"
    
    local broker_list = {
        {host = "192.168.31.90", port = 9092}
    }
    
    -- 接收post请求的body
    ngx.req.read_body()
    local topic_name = "test"
    local key = nil
    local message = ngx.req.get_body_data()
    
    -- 使用异步发送消息
    -- api_version: huawei cloud kafka version is 1.1.0 or 2.3.0, api_version should be 2.
    local producer_config = {
        producer_type = "async",
        api_version = 2,
        required_acks = 1
    }
    local async_producer = producer:new(broker_list, producer_config)
    
    local ok, err = async_producer:send(topic_name, key, message)
    if not ok then
        ngx.say("send err:", err)
        return
    else
        ngx.say("send success, ok:", ok)
    end
    
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值