简体   繁体   English

在春季启动时为Axon Framework设置Mongo Extension

[英]Setting up Mongo Extension for Axon Framework on spring boot

So at first I added a properties file with: 所以首先我添加了一个属性文件:

spring.data.mongodb.uri=mongodb://axon:axon@aurl:27017/axonframework spring.data.mongodb.uri = mongodb:// axon:axon @ aurl:27017 / axonframework

which works but I was forced to use axonframework as db name because it is what was created in my mongo db. 可以,但是我被迫使用axonframework作为数据库名称,因为它是在我的mongo数据库中创建的。

Now controlling the db name and other details isn't an option in this case, so I went and checked around and found the following: 现在,在这种情况下,不能选择控制数据库名称和其他详细信息,因此我四处检查并发现以下内容:

@configuration
public class AxonConfiguration {

    @Value("${mongo.host:127.0.0.1}")
    private String mongoHost;

    @Value("${mongo.port:27017}")
    private int mongoPort;

    @Value("${mongo.db:test}")
    private String mongoDB;

    @Bean
    public MongoSagaStore sagaStore() {
        return new MongoSagaStore(axonMongoTemplate());
    }

    @Bean
    public TokenStore tokenStore(Serializer serializer) {
        return new MongoTokenStore(axonMongoTemplate(), serializer);
    }

    @Bean
    public EventStorageEngine eventStorageEngine(Serializer serializer) {
        return new MongoEventStorageEngine(serializer, null, axonMongoTemplate(), new DocumentPerEventStorageStrategy());
    }

    @Bean
    public MongoTemplate axonMongoTemplate() {
        return new DefaultMongoTemplate(mongo(), mongoDB);
    }

    @Bean
    public MongoClient mongo() {
        MongoFactory mongoFactory = new MongoFactory();
        mongoFactory.setMongoAddresses(Collections.singletonList(new ServerAddress(mongoHost, mongoPort)));
        return mongoFactory.createMongo();
    }
}

Now apparently this worked for people but what I'm not being able to get right is how am I supposed to set the username and password? 现在显然这对人们有用,但是我无法正确使用的是我应该如何设置用户名和密码?

I'm using axon 4.1, axonframework.extensions.mongo 4.1 我正在使用axon 4.1,axonframework.extensions.mongo 4.1

This issue is not really related to the axon itself but more likely to the spring configuration of the mongo client instance since the usage of mongo is just an extension over the axon framework. 这个问题与轴突本身并没有真正的关系,但更可能与mongo客户实例的春季配置有关,因为mongo的使用只是对轴突框架的扩展。

AFAIK it's AFAIK这是

spring.data.mongodb.password and spring.data.mongodb.username spring.data.mongodb.passwordspring.data.mongodb.username

Also theres one thing in the code you should consider changing 在代码中还有一件事,您应该考虑更改

return new DefaultMongoTemplate(mongo(), mongoDB);

You call the method that is specified as a bean, so instead in spring you should just wire it to your method parameter like so : 您调用了指定为bean的方法,因此在春季,您应该将其连接到方法参数,例如:

 public MongoTemplate axonMongoTemplate(MongoClient client) {
     return new DefaultMongoTemplate(client, mongoDB);
 }

The snippet of code you share does not correspond with Axon Framework release 4.x or Axon Mongo Extension release 4.x. 您共享的代码片段与Axon Framework 4.x版或Axon Mongo Extension 4.x版不对应。 The shift from version 3 to 4 has replaced almost all constructors of the infrastructure components in favor of the Builder pattern. 从版本3到版本4的转变已经取代了基础结构组件的几乎所有构造函数,而采用了Builder模式。

As such, you should not be able to do new MongoEventStorageEngine(...) , but instead should do: 因此,您不应执行new MongoEventStorageEngine(...) ,而应执行以下操作:

MongoEventStorageEngine.builder().mongoTemplate(axonMongoTemplate).build()

If you're still able to use the constructor, I assume you still have Axon 3 somewhere on the class path! 如果您仍然能够使用构造函数,那么我假设您在类路径上的某个地方仍然有Axon 3!

Regarding the Mongo specifics, I'd trust @PolishCivil's statement by the way. 关于Mongo的细节,顺便说一下,我相信@PolishCivil的声明。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM