Mongodb Axon框架-在Axon配置中配置多个EventStore
我们有一个用例,其中每个聚合根应该有不同的事件存储。我们使用了以下配置,其中目前只有一个事件存储配置如下Mongodb Axon框架-在Axon配置中配置多个EventStore,mongodb,spring-boot,cqrs,event-sourcing,axon,Mongodb,Spring Boot,Cqrs,Event Sourcing,Axon,我们有一个用例,其中每个聚合根应该有不同的事件存储。我们使用了以下配置,其中目前只有一个事件存储配置如下 @Configuration @EnableDiscoveryClient public class AxonConfig { private static final String DOMAIN_EVENTS_COLLECTION_NAME = "coll-capture.domainEvents"; //private static fina
@Configuration
@EnableDiscoveryClient
public class AxonConfig {
private static final String DOMAIN_EVENTS_COLLECTION_NAME = "coll-capture.domainEvents";
//private static final String DOMAIN_EVENTS_COLLECTION_NAME_TEST =
//"coll-capture.domainEvents-test";
@Value("${mongodb.database}")
private String databaseName;
@Value("${spring.application.name}")
private String appName;
@Bean
public RestTemplate restTemplate() {
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new
HttpComponentsClientHttpRequestFactory(httpClient);
return new RestTemplate(clientHttpRequestFactory);
}
@Bean
@Profile({"uat", "prod"})
public CommandRouter springCloudHttpBackupCommandRouter(DiscoveryClient discoveryClient,
Registration localInstance,
RestTemplate restTemplate,
@Value("${axon.distributed.spring-
cloud.fallback-url}") String messageRoutingInformationEndpoint) {
return new SpringCloudHttpBackupCommandRouter(discoveryClient,
localInstance,
new AnnotationRoutingStrategy(),
serviceInstance -> appName.equalsIgnoreCase(serviceInstance.getServiceId()),
restTemplate,
messageRoutingInformationEndpoint);
}
@Bean
public Repository<TestEnquiry> testEnquiryRepository(EventStore eventStore) {
return new EventSourcingRepository<>(TestEnquiry.class, eventStore);
}
@Bean
public Repository<Test2Enquiry> test2enquiryRepository(EventStore eventStore) {
return new EventSourcingRepository<>(Test2Enquiry.class, eventStore);
}
@Bean
public EventStorageEngine eventStorageEngine(MongoClient client) {
MongoTemplate mongoTemplate = new DefaultMongoTemplate(client, databaseName)
.withDomainEventsCollection(DOMAIN_EVENTS_COLLECTION_NAME);
return new MongoEventStorageEngine(mongoTemplate);
}
}
@配置
@EnableDiscoveryClient
公共类Axonfig{
私有静态最终字符串DOMAIN\u EVENTS\u COLLECTION\u NAME=“coll capture.domainEvents”;
//私有静态最终字符串域\u事件\u集合\u名称\u测试=
//“coll capture.domainEvents测试”;
@值(${mongodb.database}”)
私有字符串数据库名;
@值(${spring.application.name}”)
私有字符串appName;
@豆子
公共RestTemplate RestTemplate(){
CloseableHttpClient httpClient=HttpClientBuilder.create().build();
HttpComponents客户端HttpRequestFactory客户端HttpPrequestFactory=新建
Http组件客户端Http请求工厂(Http客户端);
返回新的RestTemplate(clientHttpRequestFactory);
}
@豆子
@配置文件({“uat”、“prod”})
公共命令路由器springCloudHttpBackupCommandRouter(发现客户端发现客户端,
注册地点,
RestTemplate RestTemplate,
@值(“${axon.distributed.spring-
cloud.fallback url}”)字符串messageRoutingInformationEndpoint){
返回新的SpringCloudHttpBackupCommandRouter(discoveryClient,
地方性,
新注释RoutingStrategy(),
serviceInstance->appName.equalsIgnoreCase(serviceInstance.getServiceId()),
restTemplate,
消息路由信息端点);
}
@豆子
公共存储库TesteQueryRepository(EventStore EventStore){
返回新的EventSourcingRepository(testeQuery.class、eventStore);
}
@豆子
公共存储库Test2eQueryRepository(EventStore EventStore){
返回新的EventSourcingRepository(Test2eQuery.class,eventStore);
}
@豆子
公共事件存储引擎事件存储引擎(MongoClient客户端){
MongoTemplate MongoTemplate=新的默认MongoTemplate(客户端,数据库名)
.withDomainEventsCollection(域\事件\集合\名称);
返回新的MongoEvents存储引擎(mongoTemplate);
}
}
现在,我们还想在EventStorageEngine中配置“域\事件\集合\名称\测试”(仅举个例子)。我们如何实现对多个事件存储的相同支持,并选择跟踪过程作为它们应属于的集合如果您要分离事件流,那么从事件处理的角度将它们组合起来确实是必要的。特别是当有多个事件流时,将事件流分离到不同的存储解决方案是合理的
如果要定义跟踪事件处理器使用的[消息源/事件存储区],则必须处理
事件处理配置器。更具体地说,您应该调用EventProcessingConfigurer#registerTrackingEventProcessor(字符串,函数),但是为什么您希望每个聚合都有一个不同的事件存储?