//不需要认证的配置

package com.sea.elsa.subscribe.milestone.common.config;

import java.util.Collections;

import org.axonframework.eventhandling.saga.repository.SagaStore;
import org.axonframework.eventsourcing.eventstore.EventStorageEngine;
import org.axonframework.mongo.DefaultMongoTemplate;
import org.axonframework.mongo.MongoTemplate;
import org.axonframework.mongo.eventhandling.saga.repository.MongoSagaStore;
import org.axonframework.mongo.eventsourcing.eventstore.MongoEventStorageEngine;
import org.axonframework.mongo.eventsourcing.eventstore.MongoFactory;
import org.axonframework.mongo.eventsourcing.eventstore.documentperevent.DocumentPerEventStorageStrategy;
import org.axonframework.serialization.json.JacksonSerializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;

@Configuration
public class AxonConfig {


    @Value("${spring.data.mongodb.host}")
    private String mongoUrl;

    @Value("${spring.data.mongodb.database}")
    private String mongoDbName;


    @Value("${spring.data.mongodb.port}")
    private int mongoPort;

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

    @Bean(name = "axonMongoTemplate")
    public MongoTemplate axonMongoTemplate() {
        return new DefaultMongoTemplate(mongoClient(), mongoDbName);
    }

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

    @Bean
    public SagaStore sagaStore(){
        MongoTemplate mongoTemplate = new DefaultMongoTemplate(mongoClient());
        return new MongoSagaStore(mongoTemplate,  new JacksonSerializer());
    }
}

 

 

需要认证的配置:

package com.sea.elsa.subscribe.milestone.common.config;

import org.axonframework.eventhandling.saga.repository.SagaStore;
import org.axonframework.eventsourcing.eventstore.EventStorageEngine;
import org.axonframework.mongo.DefaultMongoTemplate;
import org.axonframework.mongo.MongoTemplate;
import org.axonframework.mongo.eventhandling.saga.repository.MongoSagaStore;
import org.axonframework.mongo.eventsourcing.eventstore.MongoEventStorageEngine;
import org.axonframework.mongo.eventsourcing.eventstore.documentperevent.DocumentPerEventStorageStrategy;
import org.axonframework.serialization.Serializer;
import org.axonframework.serialization.json.JacksonSerializer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.mongo.MongoClientFactory;
import org.springframework.boot.autoconfigure.mongo.MongoProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import com.mongodb.MongoClient;

@Configuration
public class CommandRepositoryConfiguration {

    @Value("${spring.data.mongodb.database}")
    private String mongoDbName;
    
    //@Value("${mongodb.username}")
    //private String mongoUserName;
    
    //@Value("${mongodb.password}")
    //private String mongoPass;

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

    //@Value("${mongodb.events.snapshot.collection.name}")
    //private String snapshotCollectionName;
    
    
    @Autowired
    private MongoProperties mongoProperties;
    
    @Autowired 
    private Environment env;
    
    @Qualifier("eventSerializer")
    @Bean
       public Serializer axonJsonSerializer() {
        return new JacksonSerializer();
    }
    


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

    @Bean(name = "axonMongoTemplate")
    public MongoTemplate axonMongoTemplate() {
        return new DefaultMongoTemplate(mongoClient(), mongoDbName);
    }

    @Bean
    public MongoClient mongoClient(){
    //    MongoFactory mongoFactory = new MongoFactory();
//        mongoFactory.setMongoAddresses(Arrays.asList(new ServerAddress(mongoUrl)));
////        new MongoClient(new ServerAddress(mongoUrl),new Mongocre,);
//        return mongoFactory.createMongo();
        
        
//          public MongoClient(final List<ServerAddress> seeds, final MongoCredential credential, final MongoClientOptions options) {
//                super(seeds, singletonList(credential), options);
//            }
        
        //return new MongoClient(Arrays.asList(new ServerAddress(mongoUrl)), MongoCredential.createCredential(mongoUserName, mongoDbName, mongoPass.toCharArray()),MongoClientOptions.builder().build());
        MongoClientFactory mongoClientFactory = new MongoClientFactory(mongoProperties, env);
        return mongoClientFactory.createMongoClient(null);
    }

    @Bean
    public SagaStore sagaStore(){
       MongoTemplate mongoTemplate = new DefaultMongoTemplate(mongoClient(),mongoDbName);
        return new MongoSagaStore(mongoTemplate,new JacksonSerializer());
    }
}

 

posted on 2019-05-14 14:33  lshan  阅读(192)  评论(0编辑  收藏  举报