Commit a445c52586c163b943922a18a1d09c0b5a70ed59

Authored by fangcheng
1 parent 8f48c80334
Exists in master

数据同步、数据拉取封装、测试用例编写

Showing 13 changed files with 256 additions and 39 deletions

parent/base.common/src/main/java/com/lyms/base/common/enums/SyncParamConf.java View file @ a445c52
  1 +package com.lyms.base.common.enums;
  2 +
  3 +public enum SyncParamConf {
  4 +
  5 +
  6 +
  7 +
  8 + USER_INSERT(com.lyms.base.common.service.user.impl.UsersServiceImpl.class.getName(),"addUser"),
  9 + USER_UPDATE(com.lyms.base.common.service.user.impl.UsersServiceImpl.class.getName(),"updateUser"),
  10 + USER_DELETE(com.lyms.base.common.service.user.impl.UsersServiceImpl.class.getName(),"delete");
  11 +
  12 +
  13 + private String remoteClazz;
  14 +
  15 + public String getRemoteClazz() {
  16 + return remoteClazz;
  17 + }
  18 +
  19 + public void setRemoteClazz(String remoteClazz) {
  20 + this.remoteClazz = remoteClazz;
  21 + }
  22 +
  23 + public String getRemoteMethod() {
  24 + return remoteMethod;
  25 + }
  26 +
  27 + public void setRemoteMethod(String remoteMethod) {
  28 + this.remoteMethod = remoteMethod;
  29 + }
  30 +
  31 + private String remoteMethod;
  32 +
  33 + private SyncParamConf(String remoteClazz,String remoteMethod) {
  34 + this.remoteClazz = remoteClazz;
  35 + this.remoteMethod = remoteMethod;
  36 + }
  37 +
  38 +
  39 +}
parent/center.manager/src/main/java/com/lyms/cm/job/SyncFixJob.java View file @ a445c52
... ... @@ -16,6 +16,7 @@
16 16 import com.lyms.sync.channel.ChannelData;
17 17 import com.lyms.util.HttpUtils;
18 18 import com.lyms.util.JsonUtils;
  19 +import com.lyms.util.StrUtils;
19 20  
20 21 /**
21 22 * <li>@ClassName: SycnJob
... ... @@ -55,7 +56,7 @@
55 56 return;
56 57 ChannelData data = JsonUtils.jsonToBean(result, ChannelData.class);
57 58 // 处理回执消息
58   - if (data != null && data.getAck()) {
  59 + if (data != null && data.getAck() && StrUtils.isNotEmpty(data.getRemoteClazz()) && StrUtils.isNotEmpty(data.getRemoteMethod())) {
59 60 // 拉取到数据执行本地方法
60 61 System.out.println("执行: " + ToStringBuilder.reflectionToString(data));
61 62 Object handlResult = SyncHandler.handler(data);
parent/center.manager/src/main/java/com/lyms/cm/util/SyncComponent.java View file @ a445c52
  1 +package com.lyms.cm.util;
  2 +
  3 +import java.util.List;
  4 +
  5 +import org.springframework.beans.factory.annotation.Autowired;
  6 +import org.springframework.stereotype.Component;
  7 +
  8 +import com.baomidou.mybatisplus.mapper.EntityWrapper;
  9 +import com.lyms.base.common.entity.organ.OrganGroup;
  10 +import com.lyms.base.common.enums.SyncParamConf;
  11 +import com.lyms.base.common.service.organ.OrganGroupService;
  12 +import com.lyms.sync.ParamsAdpter;
  13 +import com.lyms.sync.SyncCenter;
  14 +import com.lyms.util.CollectionUtils;
  15 +import com.lyms.util.InstanceUtils;
  16 +import com.lyms.util.StrUtils;
  17 +
  18 +@Component
  19 +public class SyncComponent {
  20 +
  21 + @Autowired
  22 + public SyncCenter center;
  23 +
  24 + @Autowired
  25 + private OrganGroupService organGroupService;
  26 +
  27 + private List<String> getAllGroupIpList() {
  28 + List<String> ipList = InstanceUtils.newArrayList();
  29 + List<OrganGroup> list = organGroupService.selectList(new EntityWrapper<OrganGroup>().where("ifDel=0"));
  30 + if (CollectionUtils.isNotEmpty(list)) {
  31 + for (OrganGroup group : list) {
  32 + if (StrUtils.isNotEmpty(group.getClientaddress())) {
  33 + ipList.add(group.getClientaddress());
  34 + }
  35 + }
  36 + }
  37 + return ipList;
  38 + }
  39 +
  40 + /**
  41 + * <li>@Description:添加一个同步操作
  42 + * <li>@param syncParamConf
  43 + * <li>@param data data和对应方法参数个数和类型一致
  44 + * <li>@return
  45 + * <li>创建人:方承
  46 + * <li>创建时间:2017年4月11日
  47 + * <li>修改人:
  48 + * <li>修改时间:
  49 + */
  50 + public boolean addSyncOperation(SyncParamConf syncParamConf, Object... data) {
  51 + List<String> ipList = getAllGroupIpList();
  52 + ParamsAdpter adpter = null;
  53 + if (data != null && data.length > 0) {
  54 + adpter = ParamsAdpter.builder();
  55 + for (int i = 0; i < data.length; i++) {
  56 + adpter.push(data[i]);
  57 + }
  58 + }
  59 + for (String ip : ipList) {
  60 + String id = StrUtils.uuid();
  61 + center.pushTmp(id, ip, syncParamConf.getRemoteClazz(), syncParamConf.getRemoteMethod(), adpter);
  62 + }
  63 + return true;
  64 + }
  65 +
  66 +}
parent/center.manager/src/main/resources/logback.xml View file @ a445c52
... ... @@ -40,7 +40,7 @@
40 40 <logger name="org.quartz.core" level="WARN"/>
41 41 <logger name="org.springframework.scheduling.quartz" level="WARN"/>
42 42 <logger name="org.apache.commons" level="WARN"/>
43   -
  43 + <logger name="org.apache.http" level="WARN"/>
44 44 <logger name="org.apache.velocity" level="WARN"/>
45 45 <logger name="org.apache.velocity.tools.view" level="ERROR"/>
46 46  
parent/center.manager/src/main/resources/xml/app-timer.xml View file @ a445c52
... ... @@ -14,7 +14,7 @@
14 14  
15 15 <!-- 同步远程数据, 定时任务处理同步任务 -->
16 16 <task:scheduled-tasks>
17   - <!-- <task:scheduled ref="syncTmpJob" method="excute" cron="*/10 * * * * ?"/> -->
  17 + <task:scheduled ref="syncTmpJob" method="excute" cron="*/10 * * * * ?"/>
18 18 <task:scheduled ref="syncFixJob" method="excute" cron="*/30 * * * * ?"/>
19 19 </task:scheduled-tasks>
20 20  
parent/center.manager/src/test/java/center/manager/test/user/SyncTest.java View file @ a445c52
  1 +package center.manager.test.user;
  2 +
  3 +import org.junit.Test;
  4 +import org.springframework.beans.factory.annotation.Autowired;
  5 +
  6 +import com.lyms.base.common.entity.user.Users;
  7 +import com.lyms.base.common.enums.SyncParamConf;
  8 +import com.lyms.cm.util.SyncComponent;
  9 +
  10 +public class SyncTest extends BaseTest {
  11 +
  12 + @Autowired
  13 + private SyncComponent syncComponent;
  14 +
  15 + @Test
  16 + public void testpushSync(){
  17 + String name = "test123";
  18 + Users entity = new Users();
  19 + entity.setId(name);
  20 + entity.setAccount(name);
  21 + entity.setName(name);
  22 + syncComponent.addSyncOperation(SyncParamConf.USER_INSERT, entity,null);
  23 + }
  24 +
  25 +}
parent/center.manager/src/test/java/center/manager/test/user/WorkTest.java View file @ a445c52
... ... @@ -33,7 +33,7 @@
33 33 @Test
34 34 public void pushFix() {
35 35 String id = StrUtils.uuid();
36   - String remote = "http://127.0.0.1:8080/hospital.web/remote/sycn";
  36 + String remote = "http://127.0.0.1:8080/hospital.web/remote/sync";
37 37 String remoteClazz = "com.lyms.hospital.service.sys.impl.SyncDataBasicServiceImpl";
38 38 String remoteMethod = "selectOne";
39 39 center.pushFix(id, remote, remoteClazz, remoteMethod, null);
... ... @@ -50,7 +50,7 @@
50 50 @Test
51 51 public void pushTmp() {
52 52 String id = StrUtils.uuid();
53   - String remote = "http://127.0.0.1:9090/hospital.web/remote/sycn";
  53 + String remote = "http://127.0.0.1:9090/hospital.web/remote/sync";
54 54 String remoteClazz = "com.lyms.hospital.service.sys.impl.SyncDataBasicServiceImpl";
55 55 String remoteMethod = "selectOne";
56 56 // 参数构建如下注释方式
parent/hospital.web/src/main/java/com/lyms/hospital/controller/RouterController.java View file @ a445c52
... ... @@ -26,8 +26,8 @@
26 26 @RequestMapping("/remote")
27 27 public class RouterController extends BaseController {
28 28  
29   - @RequestMapping(value = "/sycn", produces = { MediaType.APPLICATION_JSON_VALUE })
30   - public Object remote(HttpServletRequest request) throws ClassNotFoundException, IOException {
  29 + @RequestMapping(value = "/pull", produces = { MediaType.APPLICATION_JSON_VALUE })
  30 + public Object pull(HttpServletRequest request) throws ClassNotFoundException, IOException {
31 31 ChannelData data = SyncUtils.conver(request);
32 32 System.out.println("接受到数据: " + ToStringBuilder.reflectionToString(data));
33 33 Object handResult = SyncHandler.handler(data);
... ... @@ -39,6 +39,20 @@
39 39 data.setAck(true);
40 40 return data;
41 41 }
  42 +
  43 + @RequestMapping(value = "/push", produces = { MediaType.APPLICATION_JSON_VALUE })
  44 + public Object push(HttpServletRequest request) throws ClassNotFoundException, IOException {
  45 + ChannelData data = SyncUtils.conver(request);
  46 + System.out.println("接受到数据: " + ToStringBuilder.reflectionToString(data));
  47 + Object handResult = SyncHandler.handler(data);
  48 + if (data.getLoop()) {
  49 + data = (ChannelData) handResult;
  50 + } else {
  51 + data.setData(ParamsAdpter.builder().push(handResult).toJsonString());
  52 + }
  53 + return data;
  54 + }
  55 +
42 56  
43 57 }
parent/hospital.web/src/main/java/com/lyms/hospital/service/sys/SyncDataBasicService.java View file @ a445c52
... ... @@ -3,6 +3,7 @@
3 3 import java.io.Serializable;
4 4 import java.util.List;
5 5  
  6 +import com.lyms.base.common.enums.SyncParamConf;
6 7 import com.lyms.hospital.entity.sys.SyncDataBasic;
7 8 import com.lyms.sync.channel.ChannelData;
8 9 import com.lyms.web.service.BaseService;
... ... @@ -47,6 +48,17 @@
47 48 public ChannelData selectOne();
48 49  
49 50 public Boolean updateBasic(Serializable id);
  51 + /**
  52 + * <li>@Description:向同步库插入一个需要提交给center的同步任务
  53 + * <li>@param syncParamConf
  54 + * <li>@param dataObjects 对象方法参数列表
  55 + * <li>@return
  56 + * <li>创建人:方承
  57 + * <li>创建时间:2017年4月11日
  58 + * <li>修改人:
  59 + * <li>修改时间:
  60 + */
  61 + public boolean addSyncData(SyncParamConf syncParamConf,Object ...dataObjects);
50 62  
51 63 }
parent/hospital.web/src/main/java/com/lyms/hospital/service/sys/impl/SyncDataBasicServiceImpl.java View file @ a445c52
1 1 package com.lyms.hospital.service.sys.impl;
2 2  
3 3 import java.io.Serializable;
  4 +import java.util.Date;
4 5 import java.util.List;
5 6  
6 7 import org.springframework.beans.factory.annotation.Autowired;
7 8  
8 9  
... ... @@ -9,11 +10,14 @@
9 10  
10 11 import com.baomidou.mybatisplus.mapper.EntityWrapper;
11 12 import com.baomidou.mybatisplus.service.impl.ServiceImpl;
  13 +import com.lyms.base.common.enums.SyncParamConf;
12 14 import com.lyms.hospital.dao.sys.SyncDataBasicMapper;
13 15 import com.lyms.hospital.entity.sys.SyncDataBasic;
14 16 import com.lyms.hospital.service.sys.SyncDataBasicService;
  17 +import com.lyms.sync.ParamsAdpter;
15 18 import com.lyms.sync.channel.ChannelData;
16 19 import com.lyms.util.JsonUtils;
  20 +import com.lyms.util.StrUtils;
17 21  
18 22 /**
19 23 * <p>
20 24  
21 25  
22 26  
23 27  
24 28  
25 29  
... ... @@ -25,44 +29,70 @@
25 29 */
26 30 @Service
27 31 public class SyncDataBasicServiceImpl extends ServiceImpl<SyncDataBasicMapper, SyncDataBasic>
28   - implements SyncDataBasicService {
  32 + implements SyncDataBasicService {
29 33  
30   - @Autowired
31   - private SyncDataBasicMapper basicMapper;
  34 + @Autowired
  35 + private SyncDataBasicMapper basicMapper;
32 36  
33   - public Integer deleteLogicById(Serializable id) {
34   - return baseMapper.deleteLogicById(id);
35   - }
  37 + public Integer deleteLogicById(Serializable id) {
  38 + return baseMapper.deleteLogicById(id);
  39 + }
36 40  
37   - @Override
38   - @Transactional
39   - public Boolean updateBasic(List<Serializable> ids) {
40   - Integer tag = basicMapper.updateBatchByIds(ids);
41   - return tag >= 1 ? Boolean.TRUE : Boolean.FALSE;
42   - }
  41 + @Override
  42 + @Transactional
  43 + public Boolean updateBasic(List<Serializable> ids) {
  44 + Integer tag = basicMapper.updateBatchByIds(ids);
  45 + return tag >= 1 ? Boolean.TRUE : Boolean.FALSE;
  46 + }
43 47  
44   - @Override
45   - public Boolean updateBasic(Serializable id) {
46   - SyncDataBasic dataBasic = baseMapper.selectById(id);
47   - dataBasic.setIfsuc(1);
48   - Integer tag = baseMapper.updateById(dataBasic);
49   - return tag >= 1;
50   - }
  48 + @Override
  49 + public Boolean updateBasic(Serializable id) {
  50 + SyncDataBasic dataBasic = baseMapper.selectById(id);
  51 + dataBasic.setIfsuc(1);
  52 + Integer tag = baseMapper.updateById(dataBasic);
  53 + return tag >= 1;
  54 + }
51 55  
52   - @Override
53   - public ChannelData selectOne() {
54   - EntityWrapper<SyncDataBasic> ew = new EntityWrapper<SyncDataBasic>();
55   - ew.where("ifsuc=0");
56   - List<SyncDataBasic> datas = baseMapper.selectList(ew);
  56 + @Override
  57 + public ChannelData selectOne() {
  58 + EntityWrapper<SyncDataBasic> ew = new EntityWrapper<SyncDataBasic>();
  59 + ew.where("ifsuc=0");
  60 + List<SyncDataBasic> datas = baseMapper.selectList(ew);
57 61  
58   - // 单条同步,避免过于复杂的消息回执
59   - SyncDataBasic basic = datas != null && datas.size() > 0 ? datas.get(0) : null;
60   - if (basic != null) {
61   - ChannelData data = JsonUtils.jsonToBean(basic.getData(), ChannelData.class);
62   - return data;
63   - }
64   - return ChannelData.emtpy();
65   - }
  62 + // 单条同步,避免过于复杂的消息回执
  63 + SyncDataBasic basic = datas != null && datas.size() > 0 ? datas.get(0) : null;
  64 + if (basic != null) {
  65 + ChannelData data = JsonUtils.jsonToBean(basic.getData(), ChannelData.class);
  66 + return data;
  67 + }
  68 + return ChannelData.emtpy();
  69 + }
  70 +
  71 + @Override
  72 + public boolean addSyncData(SyncParamConf syncParamConf, Object... dataObjects) {
  73 + SyncDataBasic entity = new SyncDataBasic();
  74 + String uid = StrUtils.uuid();
  75 + entity.setId(uid);
  76 + Date now = new Date();
  77 + entity.setCreateTime(now);
  78 +
  79 + ChannelData cdata = new ChannelData();
  80 + cdata.setAck(true);
  81 + ParamsAdpter adpter = ParamsAdpter.builder();
  82 + for(Object param : dataObjects){
  83 + adpter.push(param);
  84 + }
  85 + cdata.setData(adpter.toJsonString());
  86 + cdata.setId(uid);
  87 + cdata.setLoop(false);
  88 + cdata.setRemoteClazz(syncParamConf.getRemoteClazz());
  89 + cdata.setRemoteMethod(syncParamConf.getRemoteMethod());
  90 + cdata.setTs(now.getTime());
  91 +
  92 + entity.setData(cdata.toJsonString());
  93 + Integer tag = baseMapper.insert(entity);
  94 + return tag >= 1 ? true : false;
  95 + }
66 96  
67 97 }
parent/hospital.web/src/main/resources/app-context.xml View file @ a445c52
... ... @@ -16,6 +16,7 @@
16 16 <bean id="placeholder" class="com.lyms.spring.PropertiesUtil">
17 17 <property name="locations">
18 18 <list>
  19 + <value>classpath:conf.properties</value>
19 20 <value>classpath:jdbc.properties</value>
20 21 <value>classpath:redis.properties</value>
21 22 </list>
parent/hospital.web/src/main/resources/dev/conf.properties View file @ a445c52
  1 +token.prefix=token:user:
parent/hospital.web/src/test/java/test/hospital/sync/SyncTest.java View file @ a445c52
  1 +package test.hospital.sync;
  2 +
  3 +import org.junit.Test;
  4 +import org.springframework.beans.factory.annotation.Autowired;
  5 +
  6 +import com.lyms.base.common.entity.user.Users;
  7 +import com.lyms.base.common.enums.SyncParamConf;
  8 +import com.lyms.hospital.service.sys.SyncDataBasicService;
  9 +import com.lyms.util.DateTimeUtils;
  10 +
  11 +import test.hospital.BaseServiceTest;
  12 +
  13 +public class SyncTest extends BaseServiceTest{
  14 +
  15 + @Autowired
  16 + private SyncDataBasicService syncDataBasicService;
  17 +
  18 + @Test
  19 + public void addSync(){
  20 + Users u = new Users();
  21 + String uid = DateTimeUtils.getDateTime();
  22 + u.setId(uid);
  23 + u.setAccount(uid);
  24 + u.setName(uid);
  25 + syncDataBasicService.addSyncData(SyncParamConf.USER_INSERT, u,null);
  26 + }
  27 +
  28 +}