springcloud(java)+spring-data-mongo 集成 mongodb 副本集集群

本贴最后更新于 2430 天前,其中的信息可能已经时移世易

springcloud(java)+spring-data-mongo 集成 mongodb 副本集集群

环境

springcloud版本Dalston.SR1 springboot版本1.5.2.RELEASE mongodb3.4.14 3台3分片
  1. 在 pomx.ml 引入 spring-boot-starter-data-mongodb
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency>

2.在 yml 里配置

spring: data: mongodb: uri: mongodb://admin:123456@192.168.31.1:20000,192.168.31.2:20000,192.168.31.3:20000/testdb?authSource=admin&connect=replicaSet&readPreference=secondaryPreferred&safe=true&authMechanism=SCRAM-SHA-1&maxPoolSize=500&minPoolSize=10
  1. uri 参数说明
  • uri 格式
mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
  • mongodb:// 这是固定的格式,必须要指定。
  • username:password@ 可选项,如果设置,在连接数据库服务器之后,驱动都会尝试登陆这个数据库
  • host1 必须的指定至少一个 host, host1 是这个 URI 唯一要填写的。它指定了要连接服务器的地址。如果要连接复制集,请指定多个主机地址。
  • portX 可选的指定端口,如果不填,默认为 27017
  • /database 如果指定 username:password@,连接并验证登陆指定数据库。若不指定,默认打开 test 数据库。
  • ?options 是连接选项。如果不使用/database,则前面需要加上/。所有连接选项都是键值对 name=value,键值对之间通过&或;(分号)隔开

标准的连接格式包含了多个选项(options),如下所示:

选项 描述
replicaSet=name 验证 replica set 的名称。 Impliesconnect=replicaSet.
safe=true|false true:在执行更新操作之后,驱动都会发送 getLastError 命令来确保更新成功。(还要参考 wtimeoutMS).false: 在每次更新之后,驱动不会发送 getLastError 来确保更新成功。
w=n 驱动添加 { w : n } 到 getLastError 命令. 应用于 safe=true。
wtimeoutMS=ms 驱动添加 { wtimeout : ms } 到 getlasterror 命令. 应用于 safe=true.
fsync=true|false true: 驱动添加 { fsync : true } 到 getlasterror 命令.应用于 safe=true
journal=true|false 如果设置为 true, 同步到 journal (在提交到数据库前写入到实体中). 应用于 safe=true
connectTimeoutMS=ms 可以打开连接的时间。
socketTimeoutMS=ms 发送和接受 sockets 的时间。

跟多参数参考:mongodb 官方文档

  1. 建立通用 model
    MongoOpenDoorLog.java
import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.index.CompoundIndex; import org.springframework.data.mongodb.core.index.CompoundIndexes; import org.springframework.data.mongodb.core.mapping.Document; import org.springframework.data.mongodb.core.mapping.Field; import java.io.Serializable; @Document(collection="open_door_log") @CompoundIndexes({ @CompoundIndex(name = "id_1_user_id_1_open_time_1", def = "{'user_id': 1, 'open_time': 1}"), @CompoundIndex(name = "door_id_1_community_id_1_build_id_1_cell_id_1", def = "{'door_id': 1, 'community_id': 1,'build_id':1,'cell_id':1}") }) public class MongoOpenDoorLog implements Serializable{ @Id private String id; private String tid; @Field("log_time") private long logTime; @Field("door_id") private String doorId; @Field("user_id") private String userId; @Field("user_type") private String userType; @Field("device_type") private String deviceType; @Field("device_sys") private String deviceSys; @Field("open_time") private long openTime; @Field("lock_type") private int lockType; @Field("open_type") private int openType; @Field("open_info") private String openInfo; @Field("area_id") private String areaId; private String area; @Field("community_id") private String communityId; @Field("community_name") private String communityName; @Field("build_id") private String buildId; @Field("build_name") private String buildName; @Field("build_num") private String buildNum; @Field("cell_id") private String cellId; @Field("cell_name") private String cellName; @Field("cell_num") private String cellNum; private int feedback; @Field("feedback_time") private long feedbackTime; private String img; private boolean deletec; /** * 创建时间 */ @Field("create_time") private long createTime; /** * 更新时间 */ @Field("update_time") private long updateTime; public void setId(String id){ this.id = id; } public String getId(){ return this.id; } public void setLogTime(long logTime){ this.logTime = logTime; } public long getLogTime(){ return this.logTime; } public void setDoorId(String doorId){ this.doorId = doorId; } public String getDoorId(){ return this.doorId; } public void setDeviceType(String deviceType){ this.deviceType = deviceType; } public String getDeviceType(){ return this.deviceType; } public void setDeviceSys(String deviceSys){ this.deviceSys = deviceSys; } public String getDeviceSys(){ return this.deviceSys; } public void setOpenTime(long openTime){ this.openTime = openTime; } public long getOpenTime(){ return this.openTime; } public void setLockType(int lockType){ this.lockType = lockType; } public int getLockType(){ return this.lockType; } public void setOpenType(int openType){ this.openType = openType; } public int getOpenType(){ return this.openType; } public void setOpenInfo(String openInfo){ this.openInfo = openInfo; } public String getOpenInfo(){ return this.openInfo; } public void setAreaId(String areaId){ this.areaId = areaId; } public String getAreaId(){ return this.areaId; } public void setArea(String area){ this.area = area; } public String getArea(){ return this.area; } public void setCommunityId(String communityId){ this.communityId = communityId; } public String getCommunityId(){ return this.communityId; } public void setCommunityName(String communityName){ this.communityName = communityName; } public String getCommunityName(){ return this.communityName; } public void setBuildId(String buildId){ this.buildId = buildId; } public String getBuildId(){ return this.buildId; } public void setBuildName(String buildName){ this.buildName = buildName; } public String getBuildName(){ return this.buildName; } public void setBuildNum(String buildNum){ this.buildNum = buildNum; } public String getBuildNum(){ return this.buildNum; } public void setCellId(String cellId){ this.cellId = cellId; } public String getCellId(){ return this.cellId; } public void setCellName(String cellName){ this.cellName = cellName; } public String getCellName(){ return this.cellName; } public void setCellNum(String cellNum){ this.cellNum = cellNum; } public String getCellNum(){ return this.cellNum; } public void setFeedback(int feedback){ this.feedback = feedback; } public int getFeedback(){ return this.feedback; } public void setFeedbackTime(long feedbackTime){ this.feedbackTime = feedbackTime; } public long getFeedbackTime(){ return this.feedbackTime; } public void setImg(String img){ this.img = img; } public String getImg(){ return this.img; } public boolean isDeletec() { return deletec; } public void setDeletec(boolean deletec) { this.deletec = deletec; } public long getCreateTime() { return createTime; } public void setCreateTime(long createTime) { this.createTime = createTime; } public long getUpdateTime() { return updateTime; } public void setUpdateTime(long updateTime) { this.updateTime = updateTime; } public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } public String getUserType() { return userType; } public void setUserType(String userType) { this.userType = userType; } public String getTid() { return tid; } public void setTid(String tid) { this.tid = tid; } }
  1. 建立通用 dao
    MongoBaseDao.java
import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.core.query.Update; import java.util.List; public interface MongoBaseDao<T> { //保存一个对象到mongodb public T save(T bean); // 根据id删除对象 public void deleteById(T t); // 根据对象的属性删除 public void deleteByCondition(T t); // 通过条件查询更新数据 public void update(Query query, Update update); // 根据id进行更新 public void updateById(String id, T t); // 通过条件查询实体(集合) public List<T> find(Query query); public List<T> findByCondition(T t); // 通过一定的条件查询一个实体 public T findOne(Query query); // 通过ID获取记录 public T get(String id); // 通过ID获取记录,并且指定了集合名(表的意思) public T get(String id, String collectionName); public MongoTemplate getMongoTemplate(); }
  1. 建立通用 daosuport
    MongoDaoSupport.java
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.core.query.Update; import java.io.Serializable; import java.lang.reflect.Field; import java.lang.reflect.ParameterizedType; import java.util.List; public abstract class MongoDaoSupport<T extends Serializable> implements MongoBaseDao<T> { @Autowired @Qualifier("mongoTemplate") protected MongoTemplate mongoTemplate; //保存一个对象到mongodb public T save(T bean) { mongoTemplate.save(bean); return bean; } // 根据id删除对象 public void deleteById(T t) { mongoTemplate.remove(t); } // 根据对象的属性删除 public void deleteByCondition(T t) { Query query = buildBaseQuery(t); mongoTemplate.remove(query, getEntityClass()); } // 通过条件查询更新数据 public void update(Query query, Update update) { mongoTemplate.updateMulti(query, update, this.getEntityClass()); } // 根据id进行更新 public void updateById(String id, T t) { Query query = new Query(); query.addCriteria(Criteria.where("id").is(id)); Update update = buildBaseUpdate(t); update(query, update); } // 通过条件查询实体(集合) public List<T> find(Query query) { return mongoTemplate.find(query, this.getEntityClass()); } public List<T> findByCondition(T t) { Query query = buildBaseQuery(t); return mongoTemplate.find(query, getEntityClass()); } // 通过一定的条件查询一个实体 public T findOne(Query query) { return mongoTemplate.findOne(query, this.getEntityClass()); } // 通过ID获取记录 public T get(String id) { return mongoTemplate.findById(id, this.getEntityClass()); } // 通过ID获取记录,并且指定了集合名(表的意思) public T get(String id, String collectionName) { return mongoTemplate.findById(id, this.getEntityClass(), collectionName); } // 根据vo构建查询条件Query private Query buildBaseQuery(T t) { Query query = new Query(); Field[] fields = t.getClass().getDeclaredFields(); for (Field field : fields) { field.setAccessible(true); try { Object value = field.get(t); if (value != null) { org.springframework.data.mongodb.core.mapping.Field queryField = field.getAnnotation(org.springframework.data.mongodb.core.mapping.Field.class); if (queryField != null) { query.addCriteria(Criteria.where(queryField.value()).is(value)); }else{ query.addCriteria(Criteria.where(field.getName()).is(value)); } } } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } return query; } private Update buildBaseUpdate(T t) { Update update = new Update(); Field[] fields = t.getClass().getDeclaredFields(); for (Field field : fields) { field.setAccessible(true); try { Object value = field.get(t); if (value != null) { update.set(field.getName(), value); } } catch (Exception e) { e.printStackTrace(); } } return update; } // 获取需要操作的实体类class @SuppressWarnings("unchecked") protected Class<T> getEntityClass() { return ((Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]); } public MongoTemplate getMongoTemplate() { return mongoTemplate; } }

8.建立 service 接口
MongoOpenDoorLogService.java

public interface MongoOpenDoorLogService extends MongoBaseDao<MongoOpenDoorLog> { }
  1. 建立 service 实现类
    MongoOpenDoorLogServiceImpl.java
import org.springframework.stereotype.Service; @Service("mongoOpenDoorLogServiceImpl") public class MongoOpenDoorLogServiceImpl extends MongoDaoSupport<MongoOpenDoorLog> implements MongoOpenDoorLogService{ }
  1. 暴露接口
    RequestObject.java
import com.zzwtec.common.validation.constraint.ZNotBlank; import com.zzwtec.common.validation.constraint.ZNotNullWithStr; import javax.validation.Valid; public class RequestObject<T> { @ZNotBlank private String requestId; @Valid @ZNotNullWithStr private T data; public RequestObject() { this.requestId = RequestIdContext.get(); } public RequestObject(T data) { this(); this.data = data; } public RequestObject(String requestId, T data) { this.requestId = requestId; this.data = data; } public String getRequestId() { return requestId; } public RequestObject<T> setRequestId(String requestId) { this.requestId = requestId; return this; } public T getData() { return data; } public RequestObject<T> setData(T data) { this.data = data; return this; } public static <T> RequestObject<T> build(T data) { return new RequestObject<T>(data); } public static <T> RequestObject<T> build(T data,String requestId) { return new RequestObject<T>(requestId,data); } public static <T> RequestObject<T> build(String requestId,T data) { return new RequestObject<T>(requestId,data); } @Override public String toString() { return "RequestObject [requestId=" + requestId + ", data=" + data + "]"; } }

ResultObject.java

public class ResultObject<T> { public static final String SUCCESS_CODE = "0"; public static final String SUCCESS_MSG = "success"; public static final String ERROR_CODE = "1"; public static final String ERROR_MSG = "error"; private String requestId; /** * 返回码 */ private String code; /** * 返回描述 */ private String msg; /** * 请求数据 */ private T data; public ResultObject() { } public ResultObject(String requestId) { this.requestId = requestId; } @SuppressWarnings("rawtypes") public ResultObject(RequestObject request) { this(request.getRequestId()); } @SuppressWarnings("rawtypes") public ResultObject(RequestObject request, String code, String msg) { this(request); this.code = code; this.msg = msg; } @SuppressWarnings("rawtypes") public ResultObject(RequestObject request, String code, String msg, T data) { this(request, code, msg); this.data = data; } public String getRequestId() { return requestId; } public ResultObject<T> setRequestId(String requestId) { this.requestId = requestId; return this; } public String getCode() { return code; } public ResultObject<T> setCode(String code) { this.code = code; return this; } public String getMsg() { return msg; } public ResultObject<T> setMsg(String msg) { this.msg = msg; return this; } public T getData() { return data; } public ResultObject<T> setData(T data) { this.data = data; return this; } public boolean isSuccess() { return SUCCESS_CODE.equals(code); } @Override public String toString() { return "ResultObject [requestId=" + requestId + ", code=" + code + ", msg=" + msg + ", data=" + data + "]"; } }

OpenDoorLogService.java

import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiOperation; import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @FeignClient(name = "a", fallback = OpenDoorLogServiceFallback.class) @RequestMapping("odls") @Api(value="OpenDoorLogService", description="mongodb开门日志操作外部服务接口", consumes = "application/json", produces = "application/json", protocols = "http") public interface OpenDoorLogService { /** * 添加单条开门日志 * @param request * @return */ @ApiOperation(value="add", notes="添加单条开门日志") @ApiImplicitParam(name = "request", required = true, dataType = "RequestObject<MongoOpenDoorLog>", paramType = "body") @RequestMapping(value = "add", method = RequestMethod.POST) public ResultObject<Void> saveOpenDoorLog(@GroupsValid({Add.class}) @RequestBody RequestObject<MongoOpenDoorLog> request); }

OpenDoorLogServiceController.java

import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; @RestController public class OpenDoorLogServiceController implements OpenDoorLogService { @Autowired private MongoOpenDoorLogService mongoOpenDoorLogService; @Override public ResultObject<Void> saveOpenDoorLog(@GroupsValid({Add.class}) @RequestBody RequestObject<MongoOpenDoorLog> request){ MongoOpenDoorLog t = request.getData(); mongoOpenDoorLogService.save(t); return ResultBuilder.success(request); } }
  1. 其它辅助类
    GroupsValid.java
import java.lang.annotation.*; @Target({ElementType.TYPE, ElementType.METHOD, ElementType.PARAMETER}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface GroupsValid { Class<?>[] value() default {}; }
Add.java public interface Add { } Delete.java public interface Delete { } Query.java public interface Query { } Update.java public interface Update { }
  • 集群
    29 引用 • 65 回帖 • 1 关注

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...

推荐标签 标签

  • 人工智能

    人工智能(Artificial Intelligence)是研究、开发用于模拟、延伸和扩展人的智能的理论、方法、技术及应用系统的一门技术科学。

    166 引用 • 314 回帖
  • MongoDB

    MongoDB(来自于英文单词“Humongous”,中文含义为“庞大”)是一个基于分布式文件存储的数据库,由 C++ 语言编写。旨在为应用提供可扩展的高性能数据存储解决方案。MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。它支持的数据结构非常松散,是类似 JSON 的 BSON 格式,因此可以存储比较复杂的数据类型。

    91 引用 • 59 回帖 • 3 关注
  • 电影

    这是一个不能说的秘密。

    122 引用 • 608 回帖
  • Java

    Java 是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由 Sun Microsystems 公司于 1995 年 5 月推出的。Java 技术具有卓越的通用性、高效性、平台移植性和安全性。

    3200 引用 • 8216 回帖
  • 小说

    小说是以刻画人物形象为中心,通过完整的故事情节和环境描写来反映社会生活的文学体裁。

    32 引用 • 108 回帖
  • Solo

    Solo 是一款小而美的开源博客系统,专为程序员设计。Solo 有着非常活跃的社区,可将文章作为帖子推送到社区,来自社区的回帖将作为博客评论进行联动(具体细节请浏览 B3log 构思 - 分布式社区网络)。

    这是一种全新的网络社区体验,让热爱记录和分享的你不再感到孤单!

    1441 引用 • 10069 回帖 • 495 关注
  • ActiveMQ

    ActiveMQ 是 Apache 旗下的一款开源消息总线系统,它完整实现了 JMS 规范,是一个企业级的消息中间件。

    19 引用 • 13 回帖 • 679 关注
  • 工具

    子曰:“工欲善其事,必先利其器。”

    298 引用 • 763 回帖
  • Logseq

    Logseq 是一个隐私优先、开源的知识库工具。

    Logseq is a joyful, open-source outliner that works on top of local plain-text Markdown and Org-mode files. Use it to write, organize and share your thoughts, keep your to-do list, and build your own digital garden.

    7 引用 • 69 回帖
  • 阿里巴巴

    阿里巴巴网络技术有限公司(简称:阿里巴巴集团)是以曾担任英语教师的马云为首的 18 人,于 1999 年在中国杭州创立,他们相信互联网能够创造公平的竞争环境,让小企业通过创新与科技扩展业务,并在参与国内或全球市场竞争时处于更有利的位置。

    43 引用 • 221 回帖 • 63 关注
  • 心情

    心是产生任何想法的源泉,心本体会陷入到对自己本体不能理解的状态中,因为心能产生任何想法,不能分出对错,不能分出自己。

    59 引用 • 369 回帖 • 1 关注
  • Kotlin

    Kotlin 是一种在 Java 虚拟机上运行的静态类型编程语言,由 JetBrains 设计开发并开源。Kotlin 可以编译成 Java 字节码,也可以编译成 JavaScript,方便在没有 JVM 的设备上运行。在 Google I/O 2017 中,Google 宣布 Kotlin 成为 Android 官方开发语言。

    19 引用 • 33 回帖 • 77 关注
  • C

    C 语言是一门通用计算机编程语言,应用广泛。C 语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。

    86 引用 • 165 回帖
  • 程序员

    程序员是从事程序开发、程序维护的专业人员。

    588 引用 • 3538 回帖
  • SVN

    SVN 是 Subversion 的简称,是一个开放源代码的版本控制系统,相较于 RCS、CVS,它采用了分支管理系统,它的设计目标就是取代 CVS。

    29 引用 • 98 回帖 • 690 关注
  • 锤子科技

    锤子科技(Smartisan)成立于 2012 年 5 月,是一家制造移动互联网终端设备的公司,公司的使命是用完美主义的工匠精神,打造用户体验一流的数码消费类产品(智能手机为主),改善人们的生活质量。

    4 引用 • 31 回帖 • 3 关注
  • Elasticsearch

    Elasticsearch 是一个基于 Lucene 的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于 RESTful 接口。Elasticsearch 是用 Java 开发的,并作为 Apache 许可条款下的开放源码发布,是当前流行的企业级搜索引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。

    117 引用 • 99 回帖 • 203 关注
  • Bug

    Bug 本意是指臭虫、缺陷、损坏、犯贫、窃听器、小虫等。现在人们把在程序中一些缺陷或问题统称为 bug(漏洞)。

    76 引用 • 1742 回帖 • 4 关注
  • Rust

    Rust 是一门赋予每个人构建可靠且高效软件能力的语言。Rust 由 Mozilla 开发,最早发布于 2014 年 9 月。

    58 引用 • 22 回帖 • 5 关注
  • FlowUs

    FlowUs.息流 个人及团队的新一代生产力工具。

    让复杂的信息管理更轻松、自由、充满创意。

    1 引用
  • 开源

    Open Source, Open Mind, Open Sight, Open Future!

    411 引用 • 3588 回帖
  • SendCloud

    SendCloud 由搜狐武汉研发中心孵化的项目,是致力于为开发者提供高质量的触发邮件服务的云端邮件发送平台,为开发者提供便利的 API 接口来调用服务,让邮件准确迅速到达用户收件箱并获得强大的追踪数据。

    2 引用 • 8 回帖 • 497 关注
  • 安装

    你若安好,便是晴天。

    132 引用 • 1184 回帖 • 1 关注
  • Hibernate

    Hibernate 是一个开放源代码的对象关系映射框架,它对 JDBC 进行了非常轻量级的对象封装,使得 Java 程序员可以随心所欲的使用对象编程思维来操纵数据库。

    39 引用 • 103 回帖 • 726 关注
  • 996
    13 引用 • 200 回帖 • 6 关注
  • PostgreSQL

    PostgreSQL 是一款功能强大的企业级数据库系统,在 BSD 开源许可证下发布。

    22 引用 • 22 回帖 • 1 关注
  • Vue.js

    Vue.js(读音 /vju ː/,类似于 view)是一个构建数据驱动的 Web 界面库。Vue.js 的目标是通过尽可能简单的 API 实现响应的数据绑定和组合的视图组件。

    267 引用 • 666 回帖