简体   繁体   中英

Kafka Producer does not write to kafka topic

sorry for the noob question: I am writing to kafka using akka but I cannot see it in the kafka console consumer.

Config for writing to kafka:

kafka {
  bootstrap.servers = "localhost:9002"
  auto.offset.reset = "earliest"
}

I have code to write to a kafka topic using akka:

class ServiceKafkaProducer(topicName: String, actorSystem: ActorSystem, configuration: Configuration) {
  val bootstrapServers: String = configuration
    .getString("kafka.bootstrap.servers")
    .getOrElse(
      throw new Exception("No config element foe kafka.bootstrap.servers")
    )

  val producerSettings: ProducerSettings[String, String] = ProducerSettings(
    actorSystem,
    new StringSerializer,
    new StringSerializer
  ).withBootstrapServers(bootstrapServers)

  val producer: KafkaProducer[String, String] = producerSettings.createKafkaProducer()

  def send(logRecordStr: String): Unit = {
    Logger.debug(s"Inside ServiceKafkaProducer, writing to $topicName")
    Logger.debug(logRecordStr)
    producer.send(
      new ProducerRecord(topicName, logRecordStr)
    )
  }
}

 def createTag(text: String, createdBy: UUID): Unit = {
  Logger.debug("Inside TagEventProducer#createTag")
  val tagId = UUID.randomUUID()

  val event = TagCreated(tagId, text, createdBy)
  println(event)
  val record = createLogRecord(event)

  send(record.encode)
}

LOGS

```[debug] - application - Inside TagEventProducer#createTag
TagCreated(d393d223-9eb6-45e3-8610-56a3f65c84cc,scala,f5b61ca0-0ccc-4064-94c1-cba2a5a4087b)
[debug] - application - Inside ServiceKafkaProducer, writing to tags
[debug] - application - {"id":"ed27f0d1-6b6c-469b-af97-1929dc6a5cc7","action":"tag-created","data":{"id":"d393d223-9eb6-45e3-8610-56a3f65c84cc","text":"scala","createdBy":"f5b61ca0-0ccc-4064-94c1-cba2a5a4087b"},"timestamp":1542776716868}```

(edited) I am running kafka using the spotify docker image like this:

version: '3.5'
services:
  kafka:
    image: 'spotify/kafka'
    hostname: kafka
    environment:
      - ADVERTISED_HOST=kafka
      - ADVERTISED_PORT=9092
    ports:
      - "9092:9092"
      - "2181:2181"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    networks:
      - kafka_net
  kafkaManager:
    image: 'sheepkiller/kafka-manager'
    environment:
      - ZK_HOSTS=kafka:2181
      - APPLICATION_SECRET=letmein
    ports:
      - "8000:8000"
    networks:
      - kafka_net
networks:
  kafka_net:
    name: my_network

First of all, you're using wrong port (9002 instead of 9092) in the producer. Try using bootstrap.servers = kafka:9092 instead of localhost:9002 in the producer, because your advertised host is set as kafka

Are you sure that your config for writing to Kafka is as follows?

bootstrap.servers = "localhost:9002"
auto.offset.reset = "earliest"

I think it should be localhost:9092 . You might have committed a typo here.

Set kafka.bootstrap.servers to {kafka_docker_host_api}:9092 . And if your problem won't fix, try to use producer.flush() . Kafka producer doesn't send messages immediately. So if you want to force message sending, you need to flush.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM