最简单的春季卡夫卡生产者和消费者

Let's now build and run the simples example of a Kafka Consumer and then a Kafka Producer using spring-kafka. If you need assistance with Kafka, spring boot or docker which are used in this article, or want to checkout the sample application from this post please check the References section below.

The first step is to create a simple Spring Boot maven Application and make sure to have spring-kafka dependency to pom.xml

<dependency>
  <groupId>org.springframework.kafka</groupId>
  <artifactId>spring-kafka</artifactId>
</dependency>

Create a Spring Kafka Consumer

现在,让我们使用spring-boot默认配置,用spring-kafka编写最简单的Kafka使用者。

创建一个名为简单消费者并添加一个方法@KakfaListener注解。

package io.stockgeeks.springkafka.springkafkaapp;

import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;

@Service
public class SimpleConsumer {
  @KafkaListener(id = "simple-consumer", topics = "simple-message")
  public void consumeMessage(String message) {
    System.out.println("Got message: " + message);
  }
}

就是这样,这就是全部,因为我们依靠的是spring-boot默认配置。

Start Kafka and Zookeeper

As we've seen in details on this other article, we're going to be using docker-compose to run our local Kafka for development, let's start our Kafka and Zookeeper containers:

docker-compose up -d

确保容器正在运行:

docker ps

您应该看到Kafka和Zookeeper正在运行:

console-kafka-zookeeper-running-docker

Run the app

Let's now compile and run the application, if you need more detailed instructions please check this post, run the following commands to build and run the application:

mvn clean package 

然后运行它:

mvn spring-boot:run

该应用程序将启动,您将在标准输出上看到使用者的配置,正在使用的Kafka版本和一条消息。在x秒内启动SpringKafkaApplication。

app-started

确保保持应用程序运行,不要关闭正在运行的终端窗口。 现在,让我们与Kafka控制台生产者一起生成一些消息,并让我们的使用者处理这些消息并将其注销。

Produce message using the Kafka console producer

打开一个新的终端并输入Kafka运行容器,以便我们可以使用控制台生产者:

docker exec -it kafka /bin/bash

一旦进入容器cd / opt / kafka / bin,我们正在使用的此特定图像中Kafka的命令行脚本位于此文件夹中。 如果您使用其他docker映像,则这些脚本可能位于其他位置。

运行控制台生产者,它将使您能够向Kafka发送消息:

./kafka-console-producer.sh --broker-list localhost:9092 --topic simple-message

kafka-console-producer

现在,控制台将阻止,您可以编写消息并按Enter,每次执行此操作时,都会向简单主题。 尝试发送一些消息,并在运行Spring Boot应用程序的外壳程序中查看应用程序标准输出,其中该外壳程序处理这些消息并进行打印。

console-producer-spring-kafka-consumer

Write a simple producer

是时候创建我们的春季卡夫卡制作人了。 创建一个名为简单制作人,我们将再次为生产者使用默认值,就像为消费者使用默认值一样。

package io.stockgeeks.springkafka.springkafkaapp;

import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

@Service
public class SimpleProducer {

  private KafkaTemplate<String, String> simpleProducer;

  public SimpleProducer(KafkaTemplate<String, String> simpleProducer) {
    this.simpleProducer = simpleProducer;
  }
  public void send(String message) {
    simpleProducer.send("simple-message", message);
  }
}

Write an endpoint

现在让我们创建一个简单的端点,该端点将接收一条文本消息并将其发布到Kafka,我们现在总是返回200 OK。

package io.stockgeeks.springkafka.springkafkaapp;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class MessageApi {

  private final SimpleProducer simpleProducer;

  public MessageApi(SimpleProducer simpleProducer) {
    this.simpleProducer = simpleProducer;
  }

  @PostMapping("/message")
  public ResponseEntity<String> message(@RequestBody String message) {
    simpleProducer.send(message);
    return ResponseEntity.ok("Message received: " + message);
  }
}

生成并运行该应用程序。

mvn clean package && mvn spring-boot:run
现在在开发计算机上运行应用程序时,您很可能会出错,这是因为您的应用程序在正常的主机网络中运行,而Kafka和zookeeper在“ docker网络”中运行。有一些方法可以解决此问题,最好的方法是在启动容器时将开发机器的主机名传递给docker-compose,如果您从该项目中打开docker-compose文件,则会在KAFKA_ADVERTISED_LISTENERS:... LISTENER_DOCKER_EXTERNAL喜欢$ {DOCKER_HOST_IP:-kafka}:9092这告诉compose默认情况下尝试使用传入的主机名或Kafka,检查compose文件中的注释以找到解决方法,然后查看下面的参考部分以了解更多详细信息。

Send some messages with curl

现在确保观看应用程序终端,并在另一个终端窗口中使用curl发送一些消息:

curl -X POST http://localhost:8080/api/message -d "yet more fun" -H "Content-Type: text/plain"

您应该在执行curl的同一终端上看到响应,还应检查使用者处理该消息并将其打印到运行应用程序的终端上。

spring-kafka-curl

Done

这样就完成了。 现在,您已经创建了可能的最简单的Spring Boot应用程序,该应用程序生成和使用来自Kafka的消息。 它看起来如此简单的原因是我们依赖于Spring Boot和spring-kafka的默认配置。

If you want to know more about how Spring Boot or Kafka Works please take a look at the links in the next session where you'll find some references with further details.

我们将在另一篇文章中介绍测试您的消费者和生产者。 编码愉快。

References

小号ource code with the application created in this post.

To set up your environment with java, maven, docker and docker-compose, please check how to set up your environment for the example tutorials.

If you need some quick introduction to Kafka: Kafka - Crash Course

For some insights on how to use docker-compose for local development, please check this post: One to run them all where you will also learn some useful Kafka commands.

If you're new to Spring Boot, please take a look at Spring Boot - Crash Course

docker compose environment variables to understand the configuration for the Kafka Advertise Listener.

from: https://dev.to//thegroo/spring-kafka-producer-and-consumer-41oc

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值