Nginx Kafka Module 安装与演示

Nginx Kafka Module 安装与演示


GitHub 链接

Nginx kafka module is used to receive http post data and deliver messages to kafka

Nginx Kafka Module 是一个 Nginx 的模块,用来接收 http 发送的数据,并投递给 Kafka

适用场景举例:在网站上记录用户行为(比如点击了某一个链接),直接投递给 Kafka ,省去了采集日志的环节,或避免了复杂的业务逻辑

ZooKeeper 、Kafka 集群的搭建(略)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# 先安装编译安装过程所需要的依赖
yum install -y git make gcc gcc-c++ openssl openssl-devel libtool python3

# 创建临时目录
mkdir ~/1211 && cd ~/1211

# 获取源码
git clone git://github.com/brg-liuwei/ngx_kafka_module
git clone git://github.com/edenhill/librdkafka
wget http://nginx.org/download/nginx-1.19.10.tar.gz
ls


1
2
3
4
5
6
7
8
9
# 编译安装 librdkafka
cd ~/1211/librdkafka/
./configure
make && make install

# 添加到动态链接库
ls -Alpht /usr/local/lib
echo "/usr/local/lib" >> /etc/ld.so.conf
ldconfig


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 解压,编译安装
cd ~/1211
tar -zxf nginx-1.19.10.tar.gz
ls
cd nginx-1.19.10
./configure --add-module=/root/1211/ngx_kafka_module/
make && make install

# 添加环境变量
echo "# Nginx" >> /etc/profile
echo "export NGINX_HOME=/usr/local/nginx" >> /etc/profile
echo "export PATH=\$PATH:\$NGINX_HOME/sbin" >> /etc/profile
source /etc/profile

# 验证环境变量
echo $NGINX_HOME
nginx -v

# 启动 nginx
nginx

# 验证 nginx 启动(检查 80 端口)
netstat -anpo -46

# 防火墙开启 80、443 端口
firewall-cmd --list-all
firewall-cmd --add-service=http
firewall-cmd --add-service=https
firewall-cmd --runtime-to-permanent
systemctl restart firewalld
firewall-cmd --list-all

此时使用浏览器访问,能正常看到 Nginx 的欢迎页面:

1211a.png
1


1
2
3
4
5
6
7
cd /usr/local/nginx/conf
ls
cp nginx.conf nginx.conf.orgn.bak
nano nginx.conf
	# 按如下内容修改

kafka-console-consumer.sh --bootstrap-server centos7-1:9092 --topic test_nginx --from-beginning
  1. server { 上方,添加 kafka 的 broker 列表

    •  1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      
      ## ...
      
      http {
      	## ...
      
          # kafka 配置
          kafka;
              kafka_broker_list centos7-1:9092 centos7-2:9092 centos7-3:9092;
      
      	server {
      	listen       80;
      
      	## ...
      
  2. server { } 内,添加监听路径和目标主题

    •  1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      
      ## ...
      	# kafka 配置
          kafka;
              kafka_broker_list centos7-1:9092 centos7-2:9092 centos7-3:9092;
      
      	server {
      
      		## ...
      
              location / {
                  root   html;
                  index  index.html index.htm;
              }
      
              # 监听 kafka 请求
              location = /kafka/test {
      
                  kafka_topic test_nginx;
              }
      
      ## ...
      
  3. 重启 Nginx

    • 1
      
      nginx -s reload
      


  1. 创建主题,开启 console 消费者

    • 1
      2
      3
      4
      5
      6
      7
      8
      
      # 创建主题
      afka-topics.sh --zookeeper centos7-1:2181/mykafka --create --topic test_nginx --partitions 1 --replication-factor 1
      # 描述主题
      kafka-topics.sh --zookeeper centos7-1:2181/mykafka --describe --topic test_nginx
      
      # 开启 console 消费者
      kafka-console-consumer.sh --bootstrap-server \
      centos7-1:9092 --topic test_nginx --from-beginning
      
  2. 使用 curl 发送 http 请求,进行测试

    • 1
      2
      3
      
      curl localhost/kafka/test -d "zhangsan"
      curl localhost/kafka/test -d "lisi"
      curl localhost/kafka/test -d "liuyi" -v
      
    • 1211b.png
      2
  3. console 消费者成功拉取到消息


参考 1 :官方 Readme

参考 2

参考 3

相关内容