简体   繁体   English

Spring-Integration-Kafka出站通道适配器发送消息

[英]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.serializervalue.serializer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Integration outbound-channel-adapter destination-expression MQMDMessageContext - Spring Integration outbound-channel-adapter destination-expression MQMDMessageContext int-jms 的 Java 配置:来自 Spring 集成的出站通道适配器 - Java configuration for int-jms:outbound-channel-adapter from spring integration 如何使Spring Integration HTTP outbound-channel-adapter参与全局事务 - How to make Spring Integration HTTP outbound-channel-adapter participate in Global Transaction 如何声明两个出站通道适配器以发布不同的消息 - How to declare two outbound-channel-adapter for publishing different message Spring Integration Kafka消息驱动通道适配器接收消息 - Spring Integration Kafka message-driven-channel-adapter receive message 测试kafka出站通道适配器时如何接收kafka消息 - How to recieve kafka message when testing kafka outbound channel adapter 使用<int-jpa:outbound-channel-adapter保留实体列表 - Persist list of Entities with <int-jpa:outbound-channel-adapter 在Spring-Integration-Kafka中使用@Gateway - Use @Gateway with Spring-Integration-Kafka 如何使用Spring集成或出站通道适配器将单个文件发送到多个主机 - How to send a single file to multiple host using spring integration or using outbound channel adapter Spring Integration Java DSL中的JPA出站通道适配器配置 - JPA outbound channel adapter config in Spring Integration Java DSL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM