Maven_Kafka 实例

Linux_Kafka 安装笔记

Maven_Kafka 实例_第1张图片

ConsumeTest

package com.demo.kafka.test;

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;
import org.junit.Before;
import org.junit.Test;

public class ConsumeTest {
    private Properties props;

    @Before
    public void init() {
        props = new Properties();
        props.put("bootstrap.servers", "192.168.216.138:9092,192.168.216.139:9092,192.168.216.140:9092");
        props.put("group.id", "testConsumer");
        props.put("enable.auto.commit", "true");
        props.put("auto.commit.interval.ms", "1000");
        props.put("session.timeout.ms", "30000");
        props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
    }

    @Test
    public void consume() {
        System.out.println("begin consumer");
        connectionKafka();
        System.out.println("finish consumer");
    }

    @SuppressWarnings("resource")
    public void connectionKafka() {
        KafkaConsumer consumer = new KafkaConsumer<>(props);
        consumer.subscribe(Arrays.asList("my-topic", "test-topic"));

        while (true) {
            ConsumerRecords records = consumer.poll(100);

            for (ConsumerRecord record : records) {
                System.out.println("收到消息:" + record.value());
            }
        }
    }
}

ProduceTest.java

package com.demo.kafka.test;

import java.util.Properties;
import java.util.Scanner;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class ProduceTest {
    private static Scanner scanner;

    private Producer producer;
    private Properties props;

    @Before
    public void init() {
        props = new Properties();
        props.put("bootstrap.servers", "192.168.216.138:9092,192.168.216.139:9092,192.168.216.140:9092");
        props.put("acks", "all");
        props.put("retries", 0);
        props.put("batch.size", 16384);
        props.put("linger.ms", 1);
        props.put("buffer.memory", 33554432);
        props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
    }

    @Test
    public void produce() {
        System.out.println("begin produce");
        connectionKafka();
        System.out.println("finish produce");
    }

    public void connectionKafka() {
        producer = new KafkaProducer<>(props);
        scanner = new Scanner(System.in);

        while (true) {
            System.out.println("请输入要发送的消息:");
            String value = scanner.nextLine();
            if (value.equals("exit")) {
                break;
            }

            producer.send(new ProducerRecord("my-topic", value));
        }

    }

    @After
    public void destroy() {
        producer.close();
    }

}

pom.xml

<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.0modelVersion>

    <groupId>com.demogroupId>
    <artifactId>maven-kafkaartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>jarpackaging>

    <name>maven-kafkaname>
    <url>http://maven.apache.orgurl>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    properties>

    <dependencies>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.12version>
            <scope>testscope>
        dependency>

        <dependency>
            <groupId>org.apache.kafkagroupId>
            <artifactId>kafka-clientsartifactId>
            <version>0.10.0.0version>
        dependency>

        <dependency>
            <groupId>org.apache.kafkagroupId>
            <artifactId>kafka_2.11artifactId>
            <version>0.10.0.0version>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-pluginartifactId>
                <version>3.5.1version>
                <configuration>
                    <source>1.7source>
                    <target>1.7target>
                    <encoding>UTF-8encoding>
                configuration>
            plugin>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-source-pluginartifactId>
                <version>3.0.1version>
                <executions>
                    <execution>
                        <id>attach-sourcesid>
                        <goals>
                            <goal>jargoal>
                        goals>
                    execution>
                executions>
            plugin>
        plugins>
    build>
project>

Maven_kafka 实例源码

你可能感兴趣的:(java,maven,kafka)