• Kafka+Log4j2日志


    默认你已经安装配置了Zookeeper和Kafka。

    为了目录清晰,我的Kafka配置文件的Zookeeper部分是:加上了节点用来存放Kafka信息

    启动Zookeeper,然后启动Kafka。

    Zookeeper的节点树:根目录下有专门的Kafka存放节点【以前没有配这个,结果Kafka的一大堆东西全部跑到根节点上了,很乱】

    接下来是代码部分了。

    依赖包:

    Log4j2配置文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <Configuration monitorInterval="1800">
    
        <Filter type="ThresholdFilter" level="trace" />
    
        <Appenders>
            <Console name="console" target="SYSTEM_OUT">
                <!--控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch) -->
                <ThresholdFilter level="trace" onMatch="ACCEPT"
                    onMismatch="DENY" />
                <PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n" />
            </Console>
    
            <Kafka name="Kafka" topic="my-topic" syncSend="false">
                <PatternLayout pattern="%date %message" />
                <Property name="bootstrap.servers">192.168.127.129:9092,192.168.127.130:9092,192.168.127.131:9092</Property>
            </Kafka>
        </Appenders>
        <Loggers>
            <Root level="WARN">
                <!-- TRACE < DEBUG < INFO < WARN < ERROR < FATAL -->
                <AppenderRef ref="console" />
            </Root>
            <Logger name="kafkaLog" level="trace">
                <AppenderRef ref="Kafka" />
            </Logger>
        </Loggers>
    </Configuration>

    生产者:

    package learn.kafka.log4j;
    
    import org.apache.logging.log4j.LogManager;
    import org.apache.logging.log4j.Logger;
    
    public class SimpleProducer {
    
        private static Logger log = LogManager.getLogger("kafkaLog");
        
        public static void main(String[] args) {
            for (int i = 10; i < 20; i++) {
                log.info("Hello---" + i);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        
    }

    消费者:

    package learn.kafka.log4j;
    
    import java.util.Arrays;
    import java.util.Properties;
    
    import org.apache.kafka.clients.consumer.ConsumerRecord;
    import org.apache.kafka.clients.consumer.ConsumerRecords;
    import org.apache.kafka.clients.consumer.KafkaConsumer;
    
    public class SimpleConsumer {
    
        public static void main(String[] args) {
            Properties props = new Properties();
            props.put("bootstrap.servers", "192.168.127.129:9092,192.168.127.130:9092,192.168.127.131:9092");
            props.put("group.id", "testGroup");
            props.put("enable.auto.commit", "true");
            props.put("auto.commit.interval.ms", "1000");
            props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
            props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
            KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
            //订阅的topic,多个用逗号隔开
            consumer.subscribe(Arrays.asList("my-topic"));
            while (true) {
                ConsumerRecords<String, String> records = consumer.poll(100);
                for (ConsumerRecord<String, String> record : records)
                    System.out.printf("offset = %d, key = %s, value = %s%n", record.offset(), record.key(), record.value());
            }
            //consumer.close();
        }
    }

    先运行消费者,让它监听等待。

    在运行生产者,让它生产消息。

    你会发现每隔一秒输出一行信息。信息在value后面。

     

  • 相关阅读:
    hdu 4009 Transfer water(最小型树图)
    如何使用java调用DLL运行C++(初篇)
    腾讯笔试题(2015)
    md5算法原理一窥(其一)
    hdu 3038 How Many Answers Are Wrong ( 带 权 并 查 集 )
    Java 基础知识点(必知必会其二)
    Java 基础知识点(必知必会其一)
    web基础之hibernate(一篇)
    web基础之Structs(一篇)
    mysql知识初篇(一)
  • 原文地址:https://www.cnblogs.com/LUA123/p/7746357.html
Copyright © 2020-2023  润新知