[英]Spring-Integration-Kafka outbound-channel-adapter Send message
Using Spring-Integration-Kafka, With outbound-channel-adapter I am trying to send messages to a topic with name " test " 使用Spring-Integration-Kafka,通过出站通道适配器,我试图将消息发送到名称为“ test ”的主题
Through command line terminal, I started zookeeper, kafka and created topic with name "test" 通过命令行终端,我启动了zookeeper,kafka,并创建了名称为“ test”的主题
Spring XML configuration Spring XML配置
<int:publish-subscribe-channel id="inputToKafka" />
<int-kafka:outbound-channel-adapter id="kafkaOutboundChannelAdapter"
auto-startup="false"
channel="inputToKafka"
kafka-template="template"
sync="true"
topic="test">
</int-kafka:outbound-channel-adapter>
<bean id="template" class="org.springframework.kafka.core.KafkaTemplate">
<constructor-arg>
<bean class="org.springframework.kafka.core.DefaultKafkaProducerFactory">
<constructor-arg>
<map>
<entry key="bootstrap.servers" value="localhost:9092" />
</map>
</constructor-arg>
</bean>
</constructor-arg>
</bean>
JUnit Test Code JUnit测试代码
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
"classpath:kafka-outbound-context.xml"
})
public class ProducerTest{
@Autowired
@Qualifier("inputToKafka")
MessageChannel channel;
@Test
public void test_send_message() {
channel.send(MessageBuilder.withPayload("Test Message")
.setHeader(KafkaHeaders.TOPIC, "test").build());
}
}
The test case succeeds and on debug i find channel.send() returns true 测试用例成功,并且在调试时我发现channel.send()返回true
I inspect the topic through command line with below command, but I don't see any message in the test topic. 我使用以下命令通过命令行检查主题,但是在测试主题中没有看到任何消息。
bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning
bin / kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic测试--from-beginning
Can somebody why I don't see any messages on my test topic ? 有人可以为什么我在测试主题上看不到任何消息吗?
Have you looked in the logs? 你看过日志了吗? You need to configure key and value serializers, otherwise you'll get
您需要配置键和值序列化器,否则您将获得
Caused by: org.apache.kafka.common.config.ConfigException: Missing required configuration "key.serializer" which has no default value.
When using java: 使用Java时:
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
The map keys are key.serializer
and value.serializer
. 映射键为
key.serializer
和value.serializer
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.