简体   繁体   English

Spring Boot Kafka EmbeddedKafka 事务

[英]Spring Boot Kafka EmbeddedKafka Transactions

Hi I want test kafka consumer method with kafka-test.嗨,我想用 kafka-test 测试 kafka 消费者方法。 I have this method:我有这个方法:

@KafkaListener(topics = TOPIC_OWNER)
@Transactional("chainedKafkaTransactionManager")
public void consumeFromOwnerTopic(ConsumerRecord<String, AccessAvro> accessAvro) {
    final UUID userId = textMapper.toUUID(accessAvro.value().getUserId());
    User user = getUserFromDatabase(userId);
    userRepository.save(user);
    }
}

Test测试

@SpringBootTest
@ActiveProfiles("test")
@TestPropertySource(locations = "classpath:bootstrap-test.yml")
@EmbeddedKafka(partitions = 1, topics = "access-owner", bootstrapServersProperty =         
"spring.kafka.bootstrap-servers")
class AccessConsumerTest extends Specification {

@Autowired
private KafkaTemplate<String, AccessAvro> template

AccessAvro prepareAccessAvro(){
    AccessAvro accessAvro = new AccessAvro();
    accessAvro.restaurantId = UUID.randomUUID().toString()
    accessAvro.userId = UUID.randomUUID().toString()
    return accessAvro
}

def "should add role from consumed topic"(){
    given:
    sendKafkaAcces()
    expect:
    1==1
}


private ListenableFuture<SendResult<String, AccessAvro>> sendKafkaAcces() {
    template.send("access-owner", prepareAccessAvro())
}

} }

Here I have error with transaction when:在这里,我在以下情况下出现交易错误:

  • I add annotation @Transaction("chained...") to the test method, I have error about replicas我在测试方法中添加了注释 @Transaction("chained..."),我有关于副本的错误
  • Try to delete "transaction-id-prefix" form properties, I have error about ChainedKafkaTransaction.class bc it's try to find it in properties尝试删除“transaction-id-prefix”表单属性,我有关于 ChainedKafkaTransaction.class bc 的错误,它试图在属性中找到它
  • When I try to not add @Transaction on test method, I have error: run in transaction How I can test it ?当我尝试不在测试方法上添加 @Transaction 时,出现错误:在事务中运行我如何测试它?
  1. "When I try to not add @Transaction on test method, I have error: run in transaction" “当我尝试不在测试方法上添加 @Transaction 时,出现错误:在事务中运行”

在此处输入图片说明

No transaction is in process; possible solutions: run the template operation within the scope of a template.executeInTransaction() operation, start a transaction with @Transactional before invoking the template method, run in a transaction started by a listener container when consuming a record
  1. "I add annotation @Transaction("chained...") to the test method, I have error about replicas" “我在测试方法中添加了注释 @Transaction("chained..."),我有关于副本的错误”

在此处输入图片说明

948 ERROR 5020 --- [quest-handler-7] kafka.server.KafkaApis                   : 
[KafkaApi-0] Number of alive brokers '1' does not meet the required replication factor 
'3' for the transactions state topic (configured via 
'transaction.state.log.replication.factor'). This error can be ignored if the cluster 
is starting up and not all brokers are up yet.
2020-11-10 22:18:53.023  WARN 5020 --- [           main] 
o.s.test.context.TestContextManager      : Caught exception while invoking 
'beforeTestMethod' callback on TestExecutionListener 

[org.springframework.test.context.transaction.TransactionalTestExecutionListener@5b619d14] for test method [public void com.przemarcz.auth.service.AccessConsumerTest.$spock_feature_0_0()] and test instance [com.przemarcz.auth.service.AccessConsumerTest@63c31664] [org.springframework.test.context.transaction.TransactionalTestExecutionListener@5b619d14] 测试方法 [public void com.przemarcz.auth.service.AccessConsumerTest.$spock_feature_0_0()] 和测试实例 [com.przemarcz.auth.service.AccessConsumerTest@ 63c31664]

org.springframework.transaction.CannotCreateTransactionException: Could not create 
Kafka transaction; nested exception is 
org.apache.kafka.common.errors.TimeoutException: Timeout expired after 
60000milliseconds while awaiting InitProducerId; nested exception is 
org.springframework.transaction.CannotCreateTransactionException: Could not create 
Kafka transaction; nested exception is 
org.apache.kafka.common.errors.TimeoutException: Timeout expired after 
60000milliseconds while awaiting InitProducerId
  1. "Try to delete "transaction-id-prefix" form properties, I have error about ChainedKafkaTransaction.class bc it's try to find it in properties" “尝试删除“transaction-id-prefix”表单属性,我有关于 ChainedKafkaTransaction.class bc 的错误,它试图在属性中找到它

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.kafka.transaction.KafkaTransactionManager<java.lang.Object, java.lang.Object>' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

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

相关问题 Spring Kafka 测试 - 使用 EmbeddedKafka 在 @KafkaListener 中未接收数据 - Spring Kafka Test - Not receiving data in @KafkaListener with EmbeddedKafka 使用 spring-kafka-test 中的 @EmbeddedKafka 测试侦听器 - Testing listener with @EmbeddedKafka from spring-kafka-test Spring 使用 Spring 引导 2.7.2 Kafka 在初始化期间失败,并出现异常:“ProducerFactory”必须支持事务` - Spring Boot 2.7.2 with Spring Kafka fails during initialisation with Exception: The 'ProducerFactory' must support transactions` 使用带有 MockSchemaRegistryClient 的 EmbeddedKafka 使用 Spring 云流创建测试时出现问题 - Problem creating tests with Spring cloud streams kafka streams using embeddedKafka with MockSchemaRegistryClient spring 启动卡夫卡 LocalDateTime - spring boot kafka LocalDateTime Spring Cloud Stream (Hoxton) Kafka Producer/Consumer 无法与 EmbeddedKafka 进行集成测试 - Spring Cloud Stream (Hoxton) Kafka Producer / Consumer not working in integration tests with EmbeddedKafka 如何在Spring Boot中管理事务 - How to manage transactions in spring boot Spring Boot Kafka Junit失败 - Spring boot kafka junit is failing Spring Boot Kafka批量转发 - Spring Boot Kafka Batch Forwarding 用kafka进行Spring Boot分布式处理 - spring boot distributed processing with kafka
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM