You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 17:33:28