首页 > 系统相关 >flume + kafka 读取 nginx日志 并同步到clickhouse

flume + kafka 读取 nginx日志 并同步到clickhouse

时间:2023-02-20 16:56:15浏览次数:60  
标签:flume log -- kafka topic nginx agent1

1.kafka安装运行

kafka中包含了 zookeeper,无需单独安装zookeeper

先启动zookeeper

bin/zookeeper-server-start.sh -daemon config/zookeeper.properties

然后启动kafka

bin/kafka-server-start.sh -daemon config/server.properties

启动一个消费端,topic是 flume_topic

bin/kafka-topics.sh --create --topic flume_topic --partitions 1 --replication-factor 1 --bootstrap-server localhost:9092

--create 是创建主题的动作指令,

--bootstrap-server 指定了连接的 Kafka 集群地址,

--topic 指定了所要创建主题的名称,

--replication-factor 指定了副本因子,

--partitions 指定了分区个数。

即创建了一个分区为 1、副本因子为 1 的主题 flume_topic   或者使用kafkacat: kafkacat -b localhost:9092 -C -o -10 -t flume_topic   2.flume安装运行

在conf目录新建一个flume-kafka.conf配置

# flume-kafka.conf: A Flume configuration file with a Kafka sink

# Name the components on this agent
agent1.sources = source1
agent1.sinks = sink1
agent1.channels = channel1

# Describe/configure the source
agent1.sources.source1.type = exec
agent1.sources.source1.command = tail -F /var/log/nginx/access.log
agent1.sources.source1.batchSize = 100
agent1.sources.source1.interceptors = timestamp_interceptor
agent1.sources.source1.interceptors.timestamp_interceptor.type = timestamp

# Describe/configure the sink
agent1.sinks.sink1.type = org.apache.flume.sink.kafka.KafkaSink
agent1.sinks.sink1.topic = flume_topic
agent1.sinks.sink1.brokerList = localhost:9092

# Use a channel which buffers events in memory
agent1.channels.channel1.type = memory
agent1.channels.channel1.capacity = 1000
agent1.channels.channel1.transactionCapacity = 100

# Bind the source and sink to the channel
agent1.sources.source1.channels = channel1
agent1.sinks.sink1.channel = channel1

 

启动flume

./bin/flume-ng agent -daemon --conf /data/software/apache-flume-1.11.0-bin/conf --conf-file conf/flume-kafka.conf --name agent1
--name agent1 对应上面flume-kafka.conf中的agent1

3.测试

访问nginx

 

 kafka消费端收到消息

 

4.修改nginx配置

将指定请求的参数输出到 postdata.log 中,调试没问题后就去修改flume中的监控路径变成现在这个postdata.log

/etc/nginx/sites-available/default 做了如下修改,输出的日志格式为json

log_format postdata escape=json '{"timestamp": "$time_iso8601", '
                        '"remote_addr": "$remote_addr", '
                        '"request_body": "$request_body", '
                        '"agent": "$http_user_agent"'
                        ' }';


server {
        listen 80 default_server;
        listen [::]:80 default_server;

        location = /post {
                access_log  /var/log/nginx/postdata.log  postdata;
                proxy_pass http://127.0.0.1/post_ok;
        }
        location = /post_ok {
                # turn off logging here to avoid double logging
                access_log off;
                return 200;
        }
}

测试修改: 

nginx -t

修改生效: 

nginx -s reload

 

5.修改flume配置

 

 

 修改好后再次运行flume,测试接口就可以看到flume_top消费监听打印了

 

 

6.配置clickhouse

CREATE TABLE queue1 (
message String
) ENGINE = Kafka('localhost:9092', 'flume_topic', 'group1', 'JSONAsString');
CREATE MATERIALIZED VIEW queue_msg TO msg AS
SELECT
    generateUUIDv4() AS id,
    JSONExtractString(message, 'timestamp') AS timestamp,
    JSONExtractString(message, 'remote_addr') AS remote_addr,
    JSONExtract(JSONExtract(message, 'request_body', 'String'), 'name', 'String') AS name,
    JSONExtractString(message, 'agent') AS agent
FROM queue1
  CREATE TABLE msg ( id UUID,       timestamp String,       remote_addr String,       name String,       agent String   ) ENGINE = ReplacingMergeTree() order by id ;

 

7.通过postman测试

 

 

 

 

 

 

收到数据,完成了配置。

 

标签:flume,log,--,kafka,topic,nginx,agent1
From: https://www.cnblogs.com/AwenDF/p/17126862.html

相关文章

  • nginx镜像编译安装nginx_accept_language_module
    本文重点内容:编译生成nginx_accept_language_module镜像容器启动后,dockerlogs没日志输出多阶段编译,优化镜像大小​Dockerfile内容如下:FROMdaocloud.io/library/centos:7a......
  • nginx 注册Linux 开机启动
    nginx注册Linux开机启动前提已经安装好了,nginx(查看之前的文章) cd  /usr/lib/systemd/system/[root@machine138keepalived]#cd/usr/lib/systemd/system/[r......
  • nginx配置要点记录
    location/test/{proxy_passhttp://127.0.0.1:8088/test/;#proxy_set_headerHost$proxy_host;proxy_set_headerHost$......
  • 【Docker-3】Docker-Compose安装常用软件Nginx、Mysql、Redis、Java
    创建应用目录结构nginxconf.d-放置自定义的配置文件default.conflog-日志存储位置nginx.conf -nginx总配置文件docker-compose.ymlmysqlconf-放置自......
  • 框架和Nginx
    分布式:micorservice、framework、springsecurity  Nginx方向代理:正向代理就是客户端通过代理访问不同的服务器例如访问外网,反向代理就是客户端直接访问反向代理服务......
  • centos 6.4 安装 nginx
    yum源中暂时没有Nginx等软件包,所以我们需要使用EPEL的yum源 先更新系统yumupdate 添加atomic源[root@localhost~]#wgethttp://www.atomicorp.com/installers/atomic[r......
  • 一步一步教你Nginx优化和防盗链
         Nginx优化和防盗链一、在Centos01上安装Nginx,设置根目录/www/使用域名www.stz.con访问1、在Centos01上安装Nginx依赖程序1)挂载系统光盘配置本地yum仓库[......
  • Kafka面试题
    Kafka面试题1、Kafka是什么1.broker:Kafka服务器,负责消息存储和转发2.topic:消息类别,Kafka按照topic来分类消息3.partition:topic的分区,一个topic可以包......
  • 一步一步教你Nginx平滑升级和动静分离
    一、在Centos01安装Nginx服务,配置网站主页,使用www.szt.com访问Nginx1、在Centos01上安装Nginx依赖1)挂载光盘配置本地yum仓库[root@centos01~]#mount/dev/cdrom/mnt/moun......
  • Docker 中使用Nginx网站的搭建
    Docker中使用Nginx网站的搭建使用Nginx来搭建完整的前置站点,实现后向的代理,这篇文章中简单介绍一个搭建的步骤,至于Nginx的知识,Docker的使用可以参考对应的文档。前提......