对新手问题很抱歉:我正在使用akka写信给kafka,但在kafka控制台使用者中看不到它。
用于写入kafka的配置:
kafka {
bootstrap.servers = "localhost:9002"
auto.offset.reset = "earliest"
}
我有使用akka写给kafka主题的代码:
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)
}
日志
```[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}```
(已编辑) 我正在使用Spotify docker映像运行kafka,如下所示:
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
答案 0 :(得分:2)
首先,您在生产者中使用了错误的端口(9002而不是9092)。尝试在生产者中使用bootstrap.servers = kafka:9092
而不是localhost:9002
,因为您宣传的主机设置为 kafka
答案 1 :(得分:0)
您确定用于写入Kafka的配置如下吗?
bootstrap.servers = "localhost:9002"
auto.offset.reset = "earliest"
我认为应该是localhost:9092
。您可能在这里输入了错字。
答案 2 :(得分:0)
将kafka.bootstrap.servers
设置为{kafka_docker_host_api}:9092
。如果您的问题无法解决,请尝试使用producer.flush()
。 Kafka生产者不会立即发送消息。因此,如果要强制发送邮件,则需要刷新。