zoukankan      html  css  js  c++  java
  • springboot kafka生产者

    pom文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.4.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.example</groupId>
        <artifactId>kafka_demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>kafka_demo</name>
        <description>Demo project for Spring Boot</description>
    
    
        <properties>
            <java.version>1.8</java.version>
        </properties>
    
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    
    
            <!-- https://mvnrepository.com/artifact/org.apache.kafka/kafka-clients -->
            <dependency>
                <groupId>org.apache.kafka</groupId>
                <artifactId>kafka-clients</artifactId>
                <version>2.2.0</version>
            </dependency>
    
    
        </dependencies>
    
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    
    </project>

    第一种: 常见的生产者:

    package com.example.kafka_demo;
    import org.apache.kafka.clients.producer.*;
    import java.util.Properties;
    
    public class Demo {
        public static void main(String[] args) {
            Properties props = new Properties();
            props.put("bootstrap.servers", "192.168.8.20:9092");
            props.put("acks", "0");
            props.put("retries", 0);
    //        props.put("batch.size", 16384);
            props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
            props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
            Producer producer = new KafkaProducer(props);
            //正常发送消息的producer
    //        ProducerRecord<String, String> record = new ProducerRecord<>("first", "333");
            //带key的producer
    //        ProducerRecord<String, String> record = new ProducerRecord<>("first","key", "3312333");
            //指定分区的producer
            ProducerRecord<String, String> record = new ProducerRecord<>("first",0,"key", "lover");
            producer.send(record);
            producer.close();
        }
    }

    第二种: 生产者带回调函数

    package com.example.kafka_demo;
    
    
    import org.apache.kafka.clients.producer.*;
    
    
    import java.util.Properties;
    
    
    
    
    public class Demo {
    
    
    
    
    
    
        public static void main(String[] args) {
            Properties props = new Properties();
            props.put("bootstrap.servers", "192.168.8.20:9092");
            props.put("acks", "1");
            props.put("retries", 0);
    //        props.put("batch.size", 16384);
            props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
            props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
            Producer producer = new KafkaProducer(props);
            ProducerRecord<String, String> record = new ProducerRecord<>("first", 0, "123", "haasdf");
            producer.send(record, (metadata, e) -> {
                if (e != null) {
                    e.printStackTrace();
                }
                System.out.println("success:"+ metadata);
            });
            producer.close();
        }
    }
  • 相关阅读:
    运算符重载
    C++ 画星号图形——圆形(核心代码记录)
    C++ 画星号图形——空心三角形(星号左对齐)(核心代码介绍)
    C++ 画星号图形——空心三角形(星号居中对齐)(核心代码介绍)
    QMap迭代器
    QVector也是隐式数据共享的
    调试dump文件
    How does the compilation and linking process work?
    when to use reinterpret_cast
    构造类的时候,提供类型转换
  • 原文地址:https://www.cnblogs.com/spicy/p/10956222.html
Copyright © 2011-2022 走看看