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

时间:2016-12-13 09:33:15

标签: java spring apache-kafka spring-integration

使用Spring-Integration-Kafka,使用出站通道适配器我试图将消息发送到名为" 测试"

的主题

通过命令行终端,我启动了zookeeper,kafka并创建了名为" test"

的主题

Spring XML配置

FileInputStream keyfis = new FileInputStream("test");
byte[] byteRead = new byte[keyfis.available()];
keyfis.read(byteRead);
keyfis.close();
String textRead=new String(byteRead); 

String[] parts = textRead.split(":");
byte[] encrAESkey=parts[0].getBytes();
byte[] myIV=parts[1].getBytes();
byte[] myencrtext=parts[2].getBytes();

JUnit测试代码

<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>

测试用例成功,在调试时我发现channel.send()返回true

我通过命令行使用以下命令检查主题,但我在测试主题中看不到任何消息。

  

bin / kafka-console-consumer.sh --bootstrap-server localhost:9092    - 主题测试 - 从头开始​​

有人可以为什么我在测试主题上看不到任何消息?

1 个答案:

答案 0 :(得分:1)

你看过日志了吗?您需要配置键和值序列化器,否则您将获得

Caused by: org.apache.kafka.common.config.ConfigException: Missing required configuration "key.serializer" which has no default value.

使用java时:

    props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);

地图密钥为key.serializervalue.serializer

相关问题