spring boot MongoDb配置和多数据源
配置文件:
# MongoDB配置项
mongodb.base.host: 192.168.1.204
mongodb.base.port:
mongodb.base.database: xxx
mongodb.base.username: xxxx
mongodb.base.password: 18148E275C234393
java配置:
package com.test.framework.mongodb.base.config; import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.mongo.MongoProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
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.MongoCredential;
import com.mongodb.ServerAddress; @Configuration
public class MongodbBaseConfig { public static final String mongoPropertiesBase = "mongoPropertiesBase";
public static final String mongoClientBase = "mongoClientBase";
public static final String mongoDbFactoryBase = "mongoDbFactoryBase";
public static final String mongoTemplateBase = "mongoTemplateBase"; @Bean(name = mongoPropertiesBase)
@ConfigurationProperties(prefix = "mongodb.base")
@Primary
public MongoProperties setMongoProperties() {
return new MongoProperties();
} @Bean(name = mongoClientBase)
@Primary
public MongoClient setMongoClient(@Qualifier(mongoPropertiesBase) MongoProperties mongoProperties) {
// MongoDB地址
ServerAddress serverAddress = new ServerAddress(mongoProperties.getHost(), mongoProperties.getPort()); // 连接认证(用户名,数据库,密码 )
MongoCredential mongoCredential = MongoCredential.createCredential(mongoProperties.getUsername(),
mongoProperties.getDatabase(), mongoProperties.getPassword()); // 连接选项
MongoClientOptions mongoClientOptions = new MongoClientOptions.Builder().build(); // new MongoClientOptions.Builder()
// .connectTimeout(Integer.parseInt(connectTimeout)) // 链接超时时间
// .socketTimeout(Integer.parseInt(socketTimeout)) // read数据超时时间
// .readPreference(ReadPreference.secondary()) // 最近优先策略
// //.autoConnectRetry(false) // 是否重试机制
// .connectionsPerHost(Integer.parseInt(perHost)) // 每个地址最大请求数
// .maxWaitTime(Integer.parseInt(waitTime)) // 长链接的最大等待时间
// .threadsAllowedToBlockForConnectionMultiplier(Integer.parseInt(connectionMultiplier)).build();
// // 一个socket最大的等待请求数
// //.writeConcern(WriteConcern.NORMAL).build(); return new MongoClient(serverAddress, mongoCredential, mongoClientOptions);
} @Bean(name = mongoDbFactoryBase)
@Primary
public MongoDbFactory setMongoDbFactory(@Qualifier(mongoClientBase) MongoClient mongoClient,
@Qualifier(mongoPropertiesBase) MongoProperties mongoProperties) throws Exception { return new SimpleMongoDbFactory(mongoClient, mongoProperties.getDatabase());
} @Primary
@Bean(name = mongoTemplateBase)
public MongoTemplate setMongoTemplate(@Qualifier(mongoDbFactoryBase) MongoDbFactory mongoDbFactory)
throws Exception {
return new MongoTemplate(mongoDbFactory);
} }
spring boot MongoDb配置和多数据源的更多相关文章
- Spring Boot下配置MyBatis多数据源
http://m.blog.csdn.net/article/details?id=51481911
- Spring Boot 2.x Redis多数据源配置(jedis,lettuce)
Spring Boot 2.x Redis多数据源配置(jedis,lettuce) 96 不敢预言的预言家 0.1 2018.11.13 14:22* 字数 65 阅读 727评论 0喜欢 2 多数 ...
- Spring Boot + Mybatis 配置多数据源
Spring Boot + Mybatis 配置多数据源 Mybatis拦截器,字段名大写转小写 package com.sgcc.tysj.s.common.mybatis; import java ...
- Spring Boot 自动配置之@Conditional的使用
Spring Boot自动配置的"魔法"是如何实现的? 转自-https://sylvanassun.github.io/2018/01/08/2018-01-08-spring_ ...
- 玩转spring boot——properties配置
前言 在以往的java开发中,程序员最怕大量的配置,是因为配置一多就不好统一管理,经常出现找不到配置的情况.而项目中,从开发测试环境到生产环境,往往需要切换不同的配置,如测试数据库连接换成生产数据库连 ...
- Spring Boot Mongodb
Spring注解学习,有助于更好的理解下面代码: @ConditionOnClass表明该@Configuration仅仅在一定条件下才会被加载,这里的条件是Mongo.class位于类路径上 @En ...
- Spring Boot HikariCP 一 ——集成多数据源
其实这里介绍的东西主要是参考的另外一篇文章,数据库读写分离的. 参考文章就把链接贴出来,里面有那位的代码,简单明了https://gitee.com/comven/dynamic-datasource ...
- Spring Boot自动配置原理、实战
Spring Boot自动配置原理 Spring Boot的自动配置注解是@EnableAutoConfiguration, 从上面的@Import的类可以找到下面自动加载自动配置的映射. org.s ...
- Spring Boot 揭秘与实战 附录 - Spring Boot 公共配置
Spring Boot 公共配置,配置 application.properties/application.yml 文件中. 摘自:http://docs.spring.io/spring-boot ...
随机推荐
- Android多媒体-MediaPlayer唤醒锁及音频焦点
MediaPlayer的唤醒锁 一般使用MediaPlayer播放音频流,推荐使用一个Service来承载MediaPlayer,而不是直接在Activity里使用.可是Android系统的功耗设计里 ...
- python包格式
1 egg和wheel 前者扩展名是.egg,后者扩展名是.whl 它们都是python的模块.后者用来替换前者. wheel是轮子的意思,就是说,有了.whl包就不需要重新再造轮子了.
- http trigger 事件源是事件的生产者,函数是事件的处理者
以函数计算作为 API 网关后端服务_用户指南(开放 API)_API 网关-阿里云 https://help.aliyun.com/document_detail/54788.html 创建触发器 ...
- Massive Data Mining学习记录
第一周: 学习PageRank, 知识点:每个节点的权值由其他节点的投票决定,所有节点的权值和为1 当节点很多时候必须转换成矩阵运算来计算节点的最终值,由马尔可夫链可以证明,这个值可以迭代得到 问题: ...
- YTU 2636: B3 指向基类的指针访问派生类的成员函数
2636: B3 指向基类的指针访问派生类的成员函数 时间限制: 1 Sec 内存限制: 128 MB 提交: 433 解决: 141 题目描述 领导类(Leader)和工程师类(Engineer ...
- 【前端】JavaScript表达式-新手必看
转载请注明出处:http://www.cnblogs.com/shamoyuu/p/6145384.html 一.什么是表达式 表达式就是JavaScript里一个短句,JavaScript解释器会将 ...
- Vsftpd软件包的获取与安装
11.2 Vsftpd简介 Vsftpd是一种在GPL许可下开放源代码的FTP服务器,用于多种UNIX系统和Linux系统.Vsftpd也称为Very Secure FTP Daemon,它是一种安 ...
- js DOM操作练习
1.有如下html,如果用js获得被选中的option的text描述(非value)<select id="select_id"> <option vlue ...
- 微信H5页面 - 调酒
这是微信H5的一款小游戏(一款酒的推广活动),主要游戏页面如下: 游戏规则: 点击选择2个元素(圈圈图片),放入瓶中,使它们与瓶中已有的三个元素碰撞,调配佳酿. 只有选择正确的2个元素搭配,才可以调配 ...
- bzoj 1999: [Noip2007]Core树网的核【树的直径+单调队列】
我要懒死了,所以依然是lyd的课件截图 注意是min{max(max(d[uk]),dis(u1,ui),dis(uj,un))},每次都从这三个的max里取min #include<iostr ...