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

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

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 参数说明

mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]

标准的连接格式包含了多个选项(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 {
}

赞助商 我要投放

回帖
请输入回帖内容 ...