一、配置mongodb多数据源
1、application.yml配置文件
spring:
data:
mongodb:
primary:
host: 192.168.0.11
port: 27017
database: VISCACHE
secondary:
host: 192.168.0.11
port: 27017
database: jf_dc_db
2、AbstractMongoConfig.java
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
@Getter
@Setter
public abstract class AbstractMongoConfig {
private String host,database;
private int port;
public MongoDbFactory mongoDbFactory() throws Exception{
return new SimpleMongoDbFactory(new MongoClient(host,port),database);
}