• 消息中间件系列【java整合kafka】


    1.创建一个maven工程,引入pom坐标

            <dependency>
                <groupId>org.apache.kafka</groupId>
                <artifactId>kafka-clients</artifactId>
                <version>2.0.0</version>
            </dependency>
            <dependency>
                <groupId>org.apache.kafka</groupId>
                <artifactId>kafka_2.11</artifactId>
                <version>2.0.0</version>
                <exclusions>
                    <exclusion>
                        <groupId>org.apache.zookeeper</groupId>
                        <artifactId>zookeeper</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>org.slf4j</groupId>
                        <artifactId>slf4j-log4j12</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>log4j</groupId>
                        <artifactId>log4j</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>

    2.创建生产者

    package com.fast.kafka;
    
    import org.apache.kafka.clients.producer.KafkaProducer;
    import org.apache.kafka.clients.producer.ProducerConfig;
    import org.apache.kafka.clients.producer.ProducerRecord;
    import org.apache.kafka.common.serialization.StringSerializer;
    
    import java.util.Properties;
    
    public class ProducerFastStart {
        private static final String brokerList = "localhost:9092";
        private static final String topic = "ceshi";
        public static void main(String[] args) {
            Properties properties = new Properties();
            //设置key序列化器
            properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
            //设置重试次数
            properties.put(ProducerConfig.RETRIES_CONFIG,3);
            //设置值序列化器
            properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,StringSerializer.class.getName());
            //设置集群地址
            properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,brokerList);
    
            KafkaProducer<String, String> producer = new KafkaProducer<String, String>(properties);
            ProducerRecord record = new ProducerRecord<>(topic,"kafka-demo","zhangsan");
            producer.send(record);
            producer.close();
        }
    }

    3.创建消费者,注意标红的和生产者的不一样

    package com.fast.kafka;
    
    import org.apache.kafka.clients.consumer.ConsumerConfig;
    import org.apache.kafka.clients.consumer.ConsumerRecord;
    import org.apache.kafka.clients.consumer.ConsumerRecords;
    import org.apache.kafka.clients.consumer.KafkaConsumer;
    import org.apache.kafka.common.serialization.StringDeserializer;
    import org.apache.kafka.common.serialization.StringSerializer;
    import java.time.Duration;
    import java.util.Collections;
    import java.util.Properties;
    
    public class ConsumerFastStart {
        private static final String brokerList = "localhost:9092";
        private static final String topic = "ceshi";
        private static final String groupId = "group.demo";
    
        public static void main(String[] args) {
            Properties properties = new Properties();
            properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
            properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,StringDeserializer.class.getName());
            properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,brokerList);
            properties.put(ConsumerConfig.GROUP_ID_CONFIG,groupId);
    
            KafkaConsumer<String, String> consumer = new KafkaConsumer<>(properties);
            consumer.subscribe(Collections.singleton(topic));
            while (true){
                ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(1000));
                for (ConsumerRecord<String, String> record : records) {
                    System.out.println("record = " + record);
                }
            }
        }
    }
  • 相关阅读:
    macOS 在终端中使用 adb命令,每次都要source ~/.bash_profile 才生效
    判断一个数是奇数还是偶数?
    使用SQL Server 扩展事件来创建死锁的跟踪
    sql server阻塞(block)处理
    sqlserver的CTE实现递归查询
    sqlserver 行转列
    sqlserver字符串多行合并为一行
    git alias 配置别名,让命令更简洁,提高效率
    vim 快捷键
    Git bash 命令行中的快捷键
  • 原文地址:https://www.cnblogs.com/hujunwei/p/16473811.html
Copyright © 2020-2023  润新知