轴突org.axonframework.commandhandling.NoHandlerForCommandException:没有节点知道

时间:2018-09-13 18:23:39

标签: spring-boot axon

当尝试使用Spring Cloud实现DistributedCommandBus时,我间歇性地收到以下错误。我有理由相信,我的聚合根类,其命令处理程序和配置Bean类的自动配置会发生某种竞争状况。

  

org.axonframework.commandhandling.NoHandlerForCommandException:否   已知接受的节点。

我正在使用Axon版本3.3.5。

这是我的配置类:

@Configuration
@AutoConfigureBefore(CustomerAggregate.class)
public class AxonConfig {
    @Value("${mongo.servers}")
    private String mongoUrl;

    @Value("${mongo.db}")
    private String mongoDbName;

    @Value("${axon.events.collection.name}")
    private String eventsCollectionName;

    @Value("${axon.snapshot.collection.name}")
    private String snapshotCollectionName;

    @Value("${axon.saga.collection.name}")
    private String sagaCollectionName;

    @Bean
    @Primary
    public CommandGateway commandGateway(@Qualifier("distributedBus") DistributedCommandBus commandBus) throws Exception {
        return new DefaultCommandGateway(commandBus, new IntervalRetryScheduler(Executors.newSingleThreadScheduledExecutor(), 1000, 10));
    }

    @Bean
    @Primary
    @Qualifier("springCloudRouter")
    public CommandRouter springCloudCommandRouter(DiscoveryClient client, Registration localServiceInstance) {
        return new SpringCloudCommandRouter(client, localServiceInstance, new AnnotationRoutingStrategy());
    }

    @Bean
    @Primary
    @Qualifier("springCloudConnector")
    public SpringHttpCommandBusConnector connector() {
        return new SpringHttpCommandBusConnector(new SimpleCommandBus(), new RestTemplate(), new JacksonSerializer());
    }

    @Bean
    @Primary
    @Qualifier("distributedBus")
    public DistributedCommandBus springCloudDistributedCommandBus(@Qualifier("springCloudRouter") CommandRouter router) {
        return new DistributedCommandBus(router, connector());
    }

    @Bean
    @Primary
    public AggregateFactory<CustomerAggregate> aggregateFactory(){
        return new GenericAggregateFactory<CustomerAggregate>(CustomerAggregate.class);
    }

    @Bean
    @Primary
    public EventCountSnapshotTriggerDefinition countSnapshotTriggerDefinition(){
        return new EventCountSnapshotTriggerDefinition(snapShotter(), 3);
    }

    @Bean
    @Primary
    public Snapshotter snapShotter(){
        return new AggregateSnapshotter(eventStore(), aggregateFactory());
    }

    @Bean
    @Primary
    public EventSourcingRepository<CustomerAggregate> customerAggregateRepository(){
        return new EventSourcingRepository<>(aggregateFactory(), eventStore(), countSnapshotTriggerDefinition());
    }

    @Bean(name = "axonMongoTemplate")
    public MongoTemplate axonMongoTemplate() {
        return new DefaultMongoTemplate(mongoClient(), mongoDbName)
            .withDomainEventsCollection(eventsCollectionName)
            .withSnapshotCollection(snapshotCollectionName)
            .withSagasCollection(sagaCollectionName);
    }

    @Bean
    public MongoClient mongoClient() {
        MongoFactory mongoFactory = new MongoFactory();
        mongoFactory.setMongoAddresses(Arrays.asList(new ServerAddress(mongoUrl)));
        return mongoFactory.createMongo();
    }

    @Bean
    @Primary
    public MongoEventStorageEngine engine() {
        return new MongoEventStorageEngine(new JacksonSerializer(), null, axonMongoTemplate(), new DocumentPerEventStorageStrategy());
    }

    @Bean
    @Primary
    public EventStore eventStore() {
        return new EmbeddedEventStore(engine());
    }
}

这是我带有命令处理程序的聚合类:

@Aggregate(repository = "customerAggregateRepository")
public class CustomerAggregate {
    Logger logger = LoggerFactory.getLogger(this.getClass());

    @AggregateIdentifier
    private String id;
    private String firstName;
    private String lastName;
    private String email;

    private CustomerAggregate() {}

    public String getId() {
        return id;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public String getEmail() {
        return email;
    }

    @CommandHandler
    public CustomerAggregate(CreateCustomer cmd) {
        logger.debug("Received creation command: " + cmd.toString());
        apply(new CustomerCreated(cmd.getId(),cmd.getFirstName(),cmd.getLastName(), cmd.getEmail()));
    }

    @CommandHandler
    public void on(UpdateCustomer cmd) {
        logger.debug("Received update command: " + cmd.toString());
        apply(new CustomerUpdated(this.id,cmd.getFirstName(),cmd.getLastName(), cmd.getEmail()));
    }

    @CommandHandler
    public void on(UpdateCustomerEmail cmd) {
        logger.debug("Received update command for existing customer: " + cmd.toString());
        apply(new CustomerUpdated(cmd.getId(), this.firstName, this.lastName, cmd.getEmail()));
    }

    // Various event handlers...
}

非常感谢您的帮助。

0 个答案:

没有答案