Setting up Mongo Extension for Axon Framework on spring boot

好久不见. 提交于 2020-01-05 04:00:06

问题


So at first I added a properties file with:

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.

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


回答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.

AFAIK it's

spring.data.mongodb.password and spring.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 :

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




回答2:


The snippet of code you share does not correspond with Axon Framework release 4.x or Axon Mongo Extension release 4.x. The shift from version 3 to 4 has replaced almost all constructors of the infrastructure components in favor of the Builder pattern.

As such, you should not be able to do new MongoEventStorageEngine(...), but instead should do:

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!

Regarding the Mongo specifics, I'd trust @PolishCivil's statement by the way.

Hope this helps!



来源:https://stackoverflow.com/questions/55480321/setting-up-mongo-extension-for-axon-framework-on-spring-boot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!