您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

基于Java方式如何实现数据同步

2024/3/26 11:18:35发布20次查看
业务背景在新系统中设置定时任务需要实时把客户系统中的数据及时同步过来,保持数据的一致性。
实现逻辑1.根据客户提供的接口,本系统中采用http的post请求方式获取接口数据。
2.由于客户提供的接口必带页码和页面容量,因此会涉及到多次请求接口才能拿到全量数据,因此相同的操作可以采用递归的方式进行。
3.每次请求一次接口根据页面容量(pagesize)可获取多条数据,此时可以采用批量添加数据库的操作,使用批量sql添加语句。
4.由于数据同步需要保持两个系统数据的一致性,因此需要使用定时任务并规定同步频率,例如:一天一次或者一天两次。
5.定时任务的使用会产生数据重复的问题,因此根据某个唯一字段建立唯一索引来避免数据重复添加的问题。
6.唯一索引的建立可以避免同一记录重复添加的问题,但是避免不了同一条记录除唯一索引字段之外其它字段对应数据发生变化问题,因此使用replace into添加sql语句可以解决此问题。
提示: a. 如果发现表中已经有此行数据(根据主键或者唯一索引判断)则先删除此行数据,然后插入新的数据。 b. 不然的话,直接插入新的数据。
使用技术1.设置定时任务。
2.采用http的post方式获取接口数据。
3.涉及多页数据采用递归方式循环调用。
4.批量操作数据库(replace into)。
5.建立唯一索引避免重复插入数据。
代码详情1.studentmapper.java
/** * 批量添加数据同步接口学生数据 * @param studentlist * @return */ int addbatchstudent(@param(value = "studentlist") list<student> studentlist);
2.syncstudentserviceimpl.java代码如下:
import java.util.hashmap;import java.util.list;import java.util.map;import org.springframework.beans.factory.annotation.autowired;import org.springframework.stereotype.service;import com.alibaba.fastjson.jsonarray;import com.alibaba.fastjson.jsonobject;import ***.common.utils.httputils;import ***.common.utils.stringutils;import ***.entity.sync.student;import ***.mapper.sync.studentmapper;import ***.service.sync.syncstudentservice;import ***.vo.studentvo;import lombok.extern.slf4j.slf4j;/** * 数据同步的学生实现类 * @author hc * @create 2021/03/25 11:20 * @version 1.0 * @since 1.0 */@service@slf4jpublic class syncstudentserviceimpl implements syncstudentservice { @autowired private studentmapper studentmapper; @autowired private httputils httputils; @override public void bulkaddstudent(studentvo studentvo) { log.info("调取学生接口传的参数对象studentvo:"+studentvo); log.info("开始调取学生接口获取第" + studentvo.getpageindex() + "页数据"); //如何页面容量小于100,则按100计算 if(studentvo.getpagesize() < 100) { studentvo.setpagesize(100); } //根据当前页码和页面容量调取获取学生数据接口 jsonobject jsonobject = this.sendstudenthttppost(studentvo); //判断返回jsonobject是否为null if (stringutils.isnotnull(jsonobject) && jsonobject.containskey("errcode") && jsonobject.getinteger("errcode") == 0) { if(jsonobject.containskey("data")){ jsonarray jsonarray = jsonobject.getjsonarray("data"); //通过判断获取的jsonobject对象中key值为data是否为null和其 jsonarray的长度来判断是否进行递归 //提示:此判断方法好于通过计算总页码的方式来递归拿数据(对获取的total依赖过大,因此放弃此方式) if(jsonobject.getstring("data") != null && jsonarray.size() > 0) { log.info("当前数据加载到几页》》》》:{}", studentvo.getpageindex()); //调取批量添加数据 this.addstudentcycledata(jsonobject, studentvo); //页码加1,继续调取下一页数据 studentvo.setpageindex(studentvo.getpageindex() + 1); //采用递归方式直至循环结束 this.bulkaddstudent(studentvo); }else { log.info("学生数据同步结束》》》"); } } } } /** * 批量添加学生数据 * @param jsonobject * @param areavo * @return */ public void addstudentcycledata(jsonobject jsonobject, studentvo studentvo){ list<student> studentlist = null; //判断jsonarray时候为空 if (jsonobject != null && stringutils.isnotblank(jsonobject.getstring("data"))) { //把jsonarray转成对应实体类集合 studentlist = jsonobject.parsearray(jsonobject.getstring("data"), student.class); } try { log.info("学生接口第" + studentvo.getpageindex() + "页数据开始入库..."); //调取方法批量进行添加学生数据 studentmapper.addbatchstudent(studentlist); log.info("学生接口第" + studentvo.getpageindex() + "页数据入库成功..."); } catch (exception e) { log.error("学生批量添加数据库异常:{}", e.getmessage()); } } /** * 根据studentvo(当前页码和页面容量)发送获取学生数据的请求 * @param studentvo * @return */ public jsonobject sendstudenthttppost(studentvo studentvo){ jsonobject jsonobject = null; string studenturl = "http://*****/async-api/jc/student"; try { if (stringutils.isnotempty(studenturl)) { map<string, object> param = new hashmap<>(); param.put("pageindex", studentvo.getpageindex()); param.put("pagesize", studentvo.getpagesize()); log.info("开始发起http请求..."); jsonobject = httputils.sendhttppost(param, studenturl); } } catch (exception e) { log.error("调取客户学生同步接口出现异常:{},页面容量为:{},页码:{}", e.getmessage(), studentvo.getpagesize(), studentvo.getpageindex()); } return jsonobject; }}
3.studentvo.java代码如下:
import lombok.data;/** * 数据同步接口获取学生数据传的参数vo类 * @author hc * @create 2021/3/11 10:35 * @version 1.0 * @since 1.0 */@datapublic class studentvo{ //当前页码(初始值为0) private integer pageindex = 0; //页码容量 private integer pagesize;}
4.httputils.java代码如下:
import com.alibaba.fastjson.jsonobject;import lombok.extern.slf4j.slf4j;import org.springframework.beans.factory.annotation.autowired;import org.springframework.http.httpentity;import org.springframework.http.httpheaders;import org.springframework.http.responseentity;import org.springframework.stereotype.component;import org.springframework.web.client.httpclienterrorexception;import org.springframework.web.client.resttemplate;import java.util.map;/** * http请求工具类 * @author hc * @create 2021/3/4 * @version 1.0 */@component@slf4jpublic class httputils { @autowired private resttemplate resttemplate; /** * 发送http的post请求方法 * @param param * @return */ public jsonobject sendhttppost(integer type, map<string, object> param, string url){ log.info("调取同步接口url:{}", url); jsonobject jsonobject = null; //发起http的post准备工作 httpheaders httpheaders = new httpheaders(); httpheaders.add("content-type", "application/json"); httpentity<map<string, object>> httpentity = new httpentity<>(param, httpheaders); responseentity<string> response = null; try { log.info("param参数为:{}",param.tostring()); response = resttemplate.postforentity(url, httpentity, string.class); } catch (httpclienterrorexception e) { log.error("发起http请求报错信息:{}",e.getresponsebodyasstring()); } string bodydata = response.getbody(); if (stringutils.isnotempty(bodydata)) { jsonobject = jsonobject.parseobject(bodydata); } return jsonobject; }}
5.studentmapper.xml的sql语句如下:
<!-- 批量添加数据同步接口中获取的学生数据 --><insert id="addbatchstudent" parametertype="***.entity.sync.student"> replace into xf_clue_sync_student(id, student_code, student_name, status, create_date, update_date) <foreach collection="studentlist" item="student" open="values" separator="," > (#{student.id,jdbctype=bigint}, #{student.studentcode,jdbctype=integer}, #{student.studentname,jdbctype=varchar}, #{student.status,jdbctype=integer}, #{student.createdate,jdbctype=varchar}, #{student.updatedate,jdbctype=varchar}) </foreach></insert>
功能小结1.定时任务配置相关代码此处不再展示,springboot框架使用注解的方式即可设置定时任务以及调取频率。
2.数据同步接口开发需要根据具体应用场景采用不同的方法,需视情况而定,例如:也可以使用kettle工具等等。
以上就是基于java方式如何实现数据同步的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product