共计 4150 个字符,预计需要花费 11 分钟才能阅读完成。
简介
本文主要讲在 mongodb 在分片集群的情况下,springboot 如何进行集成。
默认读者熟悉 maven、springboot、mongodb
Springboot+Mongodb 集成
引入依赖
<!-- mongodb -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
定义配置文件
自定义配置文件,多数据源的
topinfo:
mongodb:
base:
# mongos 的地址和端口
uri: mongodb://192.168.90.225:20000,192.168.90.226:20000,192.168.90.227:20000
database: topinfo_base
warn:
# mongos 的地址和端口
uri: mongodb://192.168.90.225:20000,192.168.90.226:20000,192.168.90.227:20000
database: topinfo_warn
配置父类
package com.topinfo.ci.dataex.config;
import java.util.List;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* @Description: mongo 配置类 - 父类
* @Author: 杨攀
* @Since:2019 年 7 月 8 日上午 11:19:39
*/
public class MongoConfiguration {
private String uri;
private String database;
public String getUri() {return uri;}
public void setUri(String uri) {this.uri = uri;}
public String getDatabase() {return database;}
public void setDatabase(String database) {this.database = database;}
}
基础配置类
package com.topinfo.ci.dataex.config;
import java.util.List;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* @Description: mongo 配置类
* @Author: 杨攀
* @Since:2019 年 7 月 8 日上午 11:19:39
*/
@ConfigurationProperties(prefix = "topinfo.mongodb.base")
@Component
public class MongoBaseConfiguration extends MongoConfiguration { }
预警配置类
package com.topinfo.ci.dataex.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* @Description: mongo 配置类
* @Author: 杨攀
* @Since:2019 年 7 月 8 日上午 11:19:39
*/
@ConfigurationProperties(prefix = "topinfo.mongodb.warn")
@Component
public class MongoWarnConfiguration extends MongoConfiguration { }
mongo 配置类
package com.topinfo.ci.dataex.config;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoClientURI;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
/**
* @Description: mongoConfig
* @Author: 杨攀
* @Since:2019 年 7 月 8 日上午 11:22:57
*/
@Configuration
public class MongoConfig {
@Autowired
private MongoBaseConfiguration baseConfiguration;
@Autowired
private MongoWarnConfiguration warnConfiguration;
/**
*@Description: MongoDbFactory
*@Author: 杨攀
*@Since: 2019 年 7 月 8 日下午 4:02:33
*@param config
*@return
*@throws Exception
*/
@Bean
public MongoDbFactory mongoDbFactory(MongoConfiguration config) throws Exception {MongoClientURI uri = new MongoClientURI(config.getUri());
MongoClient mongoClient = new MongoClient(uri);
MongoDbFactory dbFactory = new SimpleMongoDbFactory(mongoClient, config.getDatabase());
return dbFactory;
}
@Bean(name="baseMongoTemplate")
public MongoTemplate baseMongoTemplate() throws Exception {return new MongoTemplate(mongoDbFactory(baseConfiguration));
}
@Bean(name="warnMongoTemplate")
public MongoTemplate warnMongoTemplate() throws Exception {return new MongoTemplate(mongoDbFactory(warnConfiguration));
}
}
dao 层的使用
package com.topinfo.ci.dataex.dao.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSON;
import com.topinfo.ci.dataex.bean.UserBean;
import com.topinfo.ci.dataex.dao.TestDao;
@Component
public class TestDaoImpl implements TestDao {
@Autowired
@Qualifier("baseMongoTemplate")
private MongoTemplate mongoTemplate;
@Override
public void saveUser(UserBean user) {String userJson = JSON.toJSONString(user);
mongoTemplate.save(userJson, "tx");
}
@Override
public void saveUserList(List<UserBean> userList) {// TODO Auto-generated method stub}
@Override
public void updateUser(UserBean user) {// TODO Auto-generated method stub}
@Override
public void deleteUserById(Long id) {// TODO Auto-generated method stub}
}
以上是 集成的主要配置类,server 层直接调用到即可实现。千万别谢我!!!O(∩_∩)O 哈哈~
正文完