美文网首页
Kafka配置Demo整理

Kafka配置Demo整理

作者: 莫夏_b560 | 来源:发表于2019-11-14 00:50 被阅读0次

1、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 https://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-test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>kafka-test</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</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.kafka</groupId>
            <artifactId>spring-kafka</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.kafka</groupId>
            <artifactId>spring-kafka-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.7</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2、properties

spring.kafka.bootstrap-servers=XXX.XX.XX.XXX:9092
spring.kafka.consumer.group-id=test-consumer-group
spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.consumer.value-deserializer=org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer
spring.kafka.producer.value-serializer=org.apache.kafka.common.serialization.StringSerializer

3、KafkaSender

package com.example.kafkatest;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.stereotype.Service;
import org.springframework.util.concurrent.ListenableFuture;

import java.util.Date;
import java.util.UUID;

@Service
public class KafkaSender {

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    private Gson gson = new GsonBuilder().create();

    public void send() {
        Messages message = new Messages();
        message.setId(System.currentTimeMillis());
        message.setMsg(UUID.randomUUID().toString());
        message.setSendTime(new Date());
        ListenableFuture<SendResult<String, String>> test0 = kafkaTemplate.send("newtopic", gson.toJson(message));
    }
}

4、KafkaReceiver

package com.example.kafkatest;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class KafkaReceiver {

    @KafkaListener(topics = {"newtopic"})
    public void listen(ConsumerRecord<?, ?> record) {
        Optional<?> kafkaMessage = Optional.ofNullable(record.value());
        if (kafkaMessage.isPresent()) {
            Object message = kafkaMessage.get();
            System.out.println("record =" + record);
            System.out.println("message =" + message);

        }
    }

}

5、Messages

package com.example.kafkatest;

import java.util.Date;

public class Messages {

    private Long id;

    private String msg;

    private Date sendTime;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Date getSendTime() {
        return sendTime;
    }

    public void setSendTime(Date sendTime) {
        this.sendTime = sendTime;
    }
}

6、KafkaTestApplication

package com.example.kafkatest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class KafkaTestApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(KafkaTestApplication.class, args);

        KafkaSender sender = context.getBean(KafkaSender.class);
        for (int i = 0; i <3; i++) {
            System.out.println("##########################");
            sender.send();

            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}

相关文章

  • Kafka配置Demo整理

    1、pom 2、properties 3、KafkaSender 4、KafkaReceiver 5、Messag...

  • Kafka相关文章索引(2)

    基本常识 kafka主要配置 Kafka配置说明 Kafka学习整理四(Producer配置) Kafka学习整理...

  • [Kafka 101-3] 使用Java API消费数据实战

    Kafka 101系列文章目标:整理学习Kafka的基本使用、Kafka的常用配置和Kafka的重要原理。 本文介...

  • CDH 组件配置

    kafka外网访问配置 进入kafka 点击配置 搜索kafka.properties 的 Kafka Broke...

  • kafka——入门

    内容 kafka安装 kafka基本知识 kafka demo例子 引用 一 安装kafka docker 安装k...

  • kafka配置目录树

    最近没那么忙,抽点时间整理下kafka的知识,以下是kafka存储在zookeeper上的配置目录树 绘制软件:M...

  • Flink Kafka Doris实战demo

    Flink Kafka Doris实战demo 环境: Flink 1.12 Doris 0.12 Kafka 1...

  • Kafka demo

    kafka在工作中用到,需要有生产者提供数据。最后自己做了一个demo出来。可以正常发送和接受数据。数据可以通过可...

  • kafka配置KAFKA_LISTENERS和KAFKA_ADV

    kafka配置KAFKA_LISTENERS和KAFKA_ADVERTISED_LISTENERS 介绍kafka...

  • kafka 搭建

    kafka 搭建 安装 下载kafka 解压 配置zookeeper zookeeper安装 配置zookeepe...

网友评论

      本文标题:Kafka配置Demo整理

      本文链接:https://www.haomeiwen.com/subject/tzllictx.html