Spring Modulith应用事件外部化未触发问题求助
问题:Spring Modulith事件无法通过Kafka外部化传递
我正尝试通过Kafka实现Spring Modulith应用事件的外部化,但无法正常工作。问题在于应用事件未通过Kafka发送,目前没有任何反应,也未抛出异常,这让调试变得困难,因为没有明确的错误提示。
我已准备了完整的Spring Boot应用,在src/main/resources/environment目录下有两个Docker Compose脚本——一个用于Kafka,一个用于MariaDB。应用启动正常,可通过REST API创建账户并执行财务交易,但没有事件发布到Kafka。
有没有人遇到过类似问题,或者能给出我可能遗漏的点的提示或建议?任何帮助都将不胜感激!
补充:用于明确工作流的ApplicationModuleTest代码
package de.homebrewed.financemanager.workflow; import static org.assertj.core.api.Assertions.assertThat; import de.homebrewed.financemanager.events.AccountCreationEvent; import de.homebrewed.financemanager.events.FinancialTransactionCreationEvent; import de.homebrewed.financemanager.external.persistance.entity.AccountEntity; import de.homebrewed.financemanager.external.persistance.entity.FinancialTransactionEntity; import de.homebrewed.financemanager.external.persistance.repository.AccountRepository; import de.homebrewed.financemanager.external.persistance.repository.FinancialTransactionRepository; import de.homebrewed.financemanager.shared.commands.CreateAccountCommand; import de.homebrewed.financemanager.shared.commands.CreateTransactionCommand; import java.math.BigDecimal; import java.time.LocalDateTime; import java.util.UUID; import lombok.RequiredArgsConstructor; import org.junit.jupiter.api.Test; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureWebMvc; import org.springframework.modulith.test.ApplicationModuleTest; import org.springframework.modulith.test.Scenario; import org.springframework.test.context.ActiveProfiles; @ActiveProfiles("test") @ApplicationModuleTest(ApplicationModuleTest.BootstrapMode.DIRECT_DEPENDENCIES) @AutoConfigureWebMvc @RequiredArgsConstructor class TransactionWorkflowModuleTest { private final AccountRepository accountRepository; private final FinancialTransactionRepository financialTransactionRepository; @Test void createAccount(Scenario scenario) { // 1st Creating an account (usually via REST) var uuidAccount = UUID.randomUUID().toString(); BigDecimal initialBalance = new BigDecimal("10000"); CreateAccountCommand createAccountCommand = new CreateAccountCommand("test checking account", initialBalance); scenario .publish(() -> new AccountCreationEvent(uuidAccount, createAccountCommand)) .andWaitForStateChange(accountRepository::findAll) .andVerify( result -> { assertThat(result).hasSize(1); AccountEntity actualAccountEntity = result.getFirst(); assertThat(actualAccountEntity.getBalance()).isEqualTo(initialBalance); assertThat(actualAccountEntity.getId()).isNotNull(); }); // 2nd Creating a transaction to change account (subtract or add) balance var uuidTransaction = UUID.randomUUID().toString(); BigDecimal amountToPay = new BigDecimal("100"); CreateTransactionCommand createTransactionCommand = new CreateTransactionCommand( "PAYMENT", amountToPay, "USD", LocalDateTime.now(), 1L, 1L, Boolean.FALSE); scenario .publish( () -> new FinancialTransactionCreationEvent(uuidTransaction, createTransactionCommand)) .andWaitForStateChange(financialTransactionRepository::findAll) .andVerify( result -> { assertThat(result).hasSize(1); FinancialTransactionEntity financialTransactionEntity = result.getFirst(); assertThat(financialTransactionEntity.getId()).isNotNull(); assertThat(financialTransactionEntity.getCleared()).isTrue(); }); // If transaction was cleared successfully, then the amount of balance has changed in the created account assertThat(accountRepository.findAll().getFirst().getBalance()) .isEqualTo(initialBalance.subtract(amountToPay)); } }
内容的提问来源于stack exchange,提问作者F4k3d
相关产品推荐
相关产品推荐

