如何在akka-stream-kafka中从配置(application.conf)创建ProducerSettings?

时间:2017-08-04 15:09:52

标签: config akka-stream

我正在尝试学习和使用akka-stream-kafka并正在阅读其[文档] [1]。在Producer settings部分中,它告诉我们可以使用编程方式和配置来创建ProducerSettings。有一个程序构造的例子,但是没有如何通过配置创建它的例子。程序化结构很简单,下面就是一个例子。但是我想使用配置库构造并希望配置来自application.conf,因为它会给我更多控制权。我似乎无法在谷歌上找到它的一个例子。

val producerSettings = ProducerSettings(system, new ByteArraySerializer, new StringSerializer)
  .withBootstrapServers("localhost:9092")

1 个答案:

答案 0 :(得分:3)

文档只是将您转发到{3}}的Apache Kafka Javadoc,因为它包含一堆常量,可以用作akka.kafka.producer.kafka-clients配置部分中的键。

从文档扩展参考配置,例如:

# Properties for akka.kafka.ProducerSettings can be
# defined in this section or a configuration section with
# the same layout. 
akka.kafka.producer {
  # Tuning parameter of how many sends that can run in parallel.
  parallelism = 100

  # How long to wait for `KafkaProducer.close`
  close-timeout = 60s

  # Fully qualified config path which holds the dispatcher configuration
  # to be used by the producer stages. Some blocking may occur.
  # When this value is empty, the dispatcher configured for the stream
  # will be used.
  use-dispatcher = "akka.kafka.default-dispatcher"

  # Properties defined by org.apache.kafka.clients.producer.ProducerConfig
  # can be defined in this configuration section.
  kafka-clients {
    bootstrap.servers = "localhost:9092"
    enable.auto.commit = true
    auto.commit.interval.ms = 10000
    acks = "all"
    retries = 0
    batch.size = 16384
  }
}

application.conf默认情况下会加载ActorSystem文件的内容,因此每当您按照以下方式创建ProducerSettings对象时,都应该从{{1 }}。您不需要将配置显式传递给构造函数。

akka.kafka.producer