簡體   English   中英

Spring Boot Kafka EmbeddedKafka 事務

[英]Spring Boot Kafka EmbeddedKafka Transactions

嗨,我想用 kafka-test 測試 kafka 消費者方法。 我有這個方法:

@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);
    }
}

測試

@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())
}

}

在這里,我在以下情況下出現交易錯誤:

  • 我在測試方法中添加了注釋 @Transaction("chained..."),我有關於副本的錯誤
  • 嘗試刪除“transaction-id-prefix”表單屬性,我有關於 ChainedKafkaTransaction.class bc 的錯誤,它試圖在屬性中找到它
  • 當我嘗試不在測試方法上添加 @Transaction 時,出現錯誤:在事務中運行我如何測試它?
  1. “當我嘗試不在測試方法上添加 @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. “我在測試方法中添加了注釋 @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] 測試方法 [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. “嘗試刪除“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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM