Spring Cloud Stream Binder

时间:2018-11-15 15:28:21

标签: spring-cloud-stream spring-kafka

任何想法都将不胜感激,我正在尝试将测试写为;

@ExtendWith(SpringExtension.class)
@EmbeddedKafka(count = 1, controlledShutdown = true, topics = { "input", "output" }, brokerProperties = { "broker.id=2",
    "listeners=PLAINTEXT://127.0.0.1:9092" })
class BindingTest {

@Autowired
private ApplicationContext applicationContext;

@Autowired
private EmbeddedKafkaBroker embeddedKafka;

@Autowired
private CustomBindings cBindings;

/**
 * @throws java.lang.Exception
 */
@BeforeEach
void setUp() throws Exception {
}

/**
 * @throws java.lang.Exception
 */
@AfterEach
void tearDown() throws Exception {
    embeddedKafka.getKafkaServers().forEach(b -> b.shutdown());
}

@Test
void test0() {
    String KEY = "KEY";
    String testMessage = "TESTMESSAGE";
    Message<String> message = MessageBuilder.withPayload(testMessage)
            .setHeader(KafkaHeaders.MESSAGE_KEY, KEY).build();
    cBindings.output().send(message);

}

@SpringBootApplication
@EnableBinding(CustomBindings.class)
public static class BindingApplication {

}

}

spring.cloud.stream.bindings.output.destination =输出 spring.cloud.stream.bindings.output.contentType = application / json

spring.cloud.stream.bindings.output.producer.header-mode =原始 spring.cloud.stream.bindings.output.producer.use-native-encoding = true

spring.cloud.stream.kafka.streams.bindings.output.producer.keySerde == org.apache.kafka.common.serialization.StringSerializer spring.cloud.stream.kafka.streams.bindings.output.producer.valueSerde == org.apache.kafka.common.serialization.StringSerializer

仍然得到

消息处理程序[org.springframework.cloud.stream.binder.kafka.KafkaMessageChannelBinder$ProducerConfigurationMessageHandler@71f0806b]中发生错误;嵌套的异常是org.apache.kafka.common.errors.SerializationException:无法将[B类的值转换为value.serializer中指定的org.apache.kafka.common.serialization.StringSerializer类,failedMessage = GenericMessage [有效载荷=字节[8],标头= {id = c91897bc-2e4e-0a74-bc05-17fb31b690f6,kafka_messageKey = KEY,contentType = application / json,时间戳= 1542295539746}]

这对我没有意义

1 个答案:

答案 0 :(得分:1)

从外观上看,这不是Kafka Streams应用程序,而是带有Kafka活页夹的常规Spring Cloud Stream应用程序。因此,您不需要这两个属性。 Foo(float* x = (a default value should be {0,250}))

此外,为了解决错误,您需要从配置 spring.cloud.stream.kafka.streams.bindings.output.producer.keySerde==org.apache.kafka.common.serialization.StringSerializer spring.cloud.stream.kafka.streams.bindings.output.producer.valueSerde==org.apache.kafka.common.serialization.StringSerializer 中删除此行。

通过将本机编码设置为true,您要求Kafka进行序列化,这将依赖于默认的spring.cloud.stream.bindings.output.producer.use-native-encoding=true。如果您确实打算进行本机序列化,则需要设置适当的值序列化器(ByteArraySerializer)。但是由于这是一个测试,所以建议您删除此属性,然后查看测试是否通过。