brief introduction
This paper mainly talks about how to integrate spring boot when mongodb is in a cluster.
Default readers are familiar with maven, springboot, mongodb
Springboot+Mongodb integration
Introducing dependency
<!-- mongodb --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency>
Define profile
Custom profile, multi data source
topinfo: mongodb: base: # Address and port of mongos uri: mongodb://192.168.90.225:20000,192.168.90.226:20000,192.168.90.227:20000 database: topinfo_base warn: # Address and port of mongos uri: mongodb://192.168.90.225:20000,192.168.90.226:20000,192.168.90.227:20000 database: topinfo_warn
Configure parent class
package com.topinfo.ci.dataex.config; import java.util.List; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; /** * @Description: mongo Configuration class - parent class * @Author:Yang pan * @Since:2019 11:19:39 am, July 8, 2010 */ 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; } }
Basic configuration class
package com.topinfo.ci.dataex.config; import java.util.List; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; /** * @Description: mongo Configuration class * @Author:Yang pan * @Since:2019 11:19:39 am, July 8, 2010 */ @ConfigurationProperties(prefix = "topinfo.mongodb.base") @Component public class MongoBaseConfiguration extends MongoConfiguration { }
Alert configuration class
package com.topinfo.ci.dataex.config; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; /** * @Description: mongo Configuration class * @Author:Yang pan * @Since:2019 11:19:39 am, July 8, 2010 */ @ConfigurationProperties(prefix = "topinfo.mongodb.warn") @Component public class MongoWarnConfiguration extends MongoConfiguration { }
mongo configuration class
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:Yang pan * @Since:2019 11:22:57 am, July 8, 2010 */ @Configuration public class MongoConfig { @Autowired private MongoBaseConfiguration baseConfiguration; @Autowired private MongoWarnConfiguration warnConfiguration; /** *@Description: MongoDbFactory *@Author:Yang pan *@Since: 2019 4:02:33 PM, July 8, 2010 *@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)); } }
Use of dao layer
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 } }
The above is the main configuration class of the integration, which can be implemented by directly calling the server layer. Don't thank me! O(∩ ∩) O haha~