增加kafka数据同步消息

This commit is contained in:
wanggeng 2021-12-27 15:57:04 +08:00
parent 898be955bd
commit 87c43ee40b
10 changed files with 332 additions and 36 deletions

View File

@ -14,8 +14,11 @@ public interface ISmartCityUserCenterConsts {
*/
String APP_CLIENT_NAME = "city-governance";
String KAFKA_TOPIC_GRID = "C0021";
String KAFKA_TOPIC_GRID_POINT = "C0022";
String KAFKA_TABLE_SYNC_TOPIC = "tableSync";
String KAFKA_TABLE_SYNC_GRID_MEMBER = "C0021";
String KAFKA_TABLE_SYNC_GRID = "C0022";
String KAFKA_TABLE_SYNC_GRID_POINT = "C0023";
String KAFKA_DATA_SAVE = "save";
String KAFKA_DATA_UPDATE = "update";

View File

@ -1,15 +1,29 @@
package cn.com.tenlion.usercenter.kafka;
import cn.com.tenlion.usercenter.service.teammembergrid.ITeamMemberGridService;
import cn.com.tenlion.usercenter.consts.ISmartCityUserCenterConsts;
import cn.com.tenlion.usercenter.pojo.dtos.kafka.SyncDataDTO;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import ink.wgink.interfaces.map.*;
import ink.wgink.interfaces.user.IUserUpdateAfterHandler;
import ink.wgink.module.map.pojo.dtos.grid.GridDTO;
import ink.wgink.module.map.service.grid.IGridPointService;
import ink.wgink.module.map.service.grid.IGridRelationService;
import ink.wgink.module.map.service.grid.IGridService;
import ink.wgink.pojo.dtos.user.UserDTO;
import ink.wgink.service.user.service.IUserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.Map;
/**
* @ClassName: KafKaPublishConsumer
* @Description: kafka消费者
@ -19,36 +33,137 @@ import org.springframework.stereotype.Component;
*/
@Component
public class KafKaPublishConsumer implements ApplicationEventPublisherAware {
private static final Logger LOG = LoggerFactory.getLogger(KafKaPublishConsumer.class);
private ApplicationEventPublisher applicationEventPublisher;
@Autowired
private ITeamMemberGridService teamMemberGridService;
private IGridService gridService;
@Autowired
private IGridPointService gridPointService;
@Autowired
private IGridRelationService gridRelationService;
@Autowired
private IUserService userService;
@Autowired
private IGridSaveAfterHandler gridSaveAfterHandler;
@Autowired
private IGridDeleteAfterHandler gridDeleteAfterHandler;
@Autowired
private IGridUpdateAfterHandler gridUpdateAfterHandler;
@Autowired
private IGridPointSaveAfterHandler gridPointSaveAfterHandler;
@Autowired
private IGridPointDeleteAfterHandler gridPointDeleteAfterHandler;
@Autowired
private IGridRelationSaveAfterHandler gridRelationSaveAfterHandler;
@Autowired
private IGridRelationDeleteAfterHandler gridRelationDeleteAfterHandler;
@Autowired
private IUserUpdateAfterHandler userUpdateAfterHandler;
public KafKaPublishConsumer() {
}
@KafkaListener(topics = {"C0021"})
public void gridMember(ConsumerRecord<?, ?> record) {
JSONObject publishObject = JSON.parseObject(record.value().toString());
System.out.println("收到消息:" + publishObject.toJSONString());
public void gridRelation(ConsumerRecord<?, ?> record) {
String recordValue = record.value().toString();
LOG.debug("Grid Relation(C0021) recordValue: {}", recordValue);
SyncDataDTO<Map<String, Object>> syncDataDTO = getSyncData(recordValue);
if (StringUtils.equals(syncDataDTO.getAction(), ISmartCityUserCenterConsts.KAFKA_DATA_SAVE)) {
if (StringUtils.isBlank(syncDataDTO.getUid())) {
LOG.error("Grid Relation(C0021) save error, uid is empty");
return;
}
UserDTO userDTO = userService.get(syncDataDTO.getUid());
if (userDTO == null) {
LOG.error("Grid Relation(C0021) save error, userDTO is null");
}
gridRelationSaveAfterHandler.handle(userDTO);
} else if (StringUtils.equals(syncDataDTO.getAction(), ISmartCityUserCenterConsts.KAFKA_DATA_UPDATE)) {
if (StringUtils.isBlank(syncDataDTO.getUid())) {
LOG.error("Grid Relation(C0021) update error, uid is empty");
return;
}
userUpdateAfterHandler.handle(syncDataDTO.getUid());
} else if (StringUtils.equals(syncDataDTO.getAction(), ISmartCityUserCenterConsts.KAFKA_DATA_DELETE)) {
if (StringUtils.isBlank(syncDataDTO.getUid())) {
LOG.error("Grid Relation(C0021) delete error, uid is empty");
return;
}
gridRelationDeleteAfterHandler.handle(Arrays.asList(syncDataDTO.getUid().split("_")));
}
}
@KafkaListener(topics = {"C0022"})
public void mapGrid(ConsumerRecord<?, ?> record) {
JSONObject publishObject = JSON.parseObject(record.value().toString());
System.out.println("收到消息:" + publishObject.toJSONString());
String recordValue = record.value().toString();
LOG.debug("Grid(C0022) recordValue: {}", recordValue);
SyncDataDTO<Map<String, Object>> syncDataDTO = getSyncData(recordValue);
if (StringUtils.equals(syncDataDTO.getAction(), ISmartCityUserCenterConsts.KAFKA_DATA_SAVE)) {
String uid = syncDataDTO.getUid();
if (StringUtils.isBlank(uid)) {
LOG.error("Grid(C0022) save error, uid is empty");
return;
}
GridDTO gridDTO = gridService.get(uid);
if (gridDTO == null) {
LOG.error("Grid(C0022) save error, uid is null");
return;
}
gridSaveAfterHandler.handle(gridDTO.getGridId(), gridDTO.getGridCode(), gridDTO.getGridName(), gridDTO.getFillColor(), gridDTO.getAreaCode(), gridDTO.getAreaName());
} else if (StringUtils.equals(syncDataDTO.getAction(), ISmartCityUserCenterConsts.KAFKA_DATA_UPDATE)) {
String uid = syncDataDTO.getUid();
if (StringUtils.isBlank(uid)) {
LOG.error("Grid(C0022) update error, uid is empty");
return;
}
GridDTO gridDTO = gridService.get(uid);
if (gridDTO == null) {
LOG.error("Grid(C0022) update error, grid is null");
return;
}
gridUpdateAfterHandler.handle(gridDTO.getGridId(), gridDTO.getGridName(), gridDTO.getFillColor());
} else if (StringUtils.equals(syncDataDTO.getAction(), ISmartCityUserCenterConsts.KAFKA_DATA_DELETE)) {
String uid = syncDataDTO.getUid();
if (StringUtils.isBlank(uid)) {
LOG.error("Grid(C0022) delete error, uid is empty");
return;
}
gridDeleteAfterHandler.handler(Arrays.asList(uid.split("_")));
}
}
@KafkaListener(topics = {"C0023"})
public void mapGridPoint(ConsumerRecord<?, ?> record) {
JSONObject publishObject = JSON.parseObject(record.value().toString());
System.out.println("收到消息:" + publishObject.toJSONString());
String recordValue = record.value().toString();
LOG.debug("Grid Point(C0023) recordValue: {}", recordValue);
SyncDataDTO<Map<String, Object>> syncDataDTO = getSyncData(recordValue);
if (StringUtils.equals(syncDataDTO.getAction(), ISmartCityUserCenterConsts.KAFKA_DATA_SAVE)) {
String gridId = String.valueOf(syncDataDTO.getData().get("grid_id"));
if (StringUtils.isBlank(gridId)) {
LOG.error("Grid Point(C0023) delete error, gridId is empty");
return;
}
// 删除原有点
gridPointDeleteAfterHandler.handle(Arrays.asList(gridId));
// 保存新的点
gridPointSaveAfterHandler.handle(gridId);
} else if (StringUtils.equals(syncDataDTO.getAction(), ISmartCityUserCenterConsts.KAFKA_DATA_DELETE)) {
String keyValue = syncDataDTO.getKeyValue();
if (keyValue.isEmpty()) {
LOG.error("Grid Point(C0023) delete error, keyValue is empty");
return;
}
gridPointDeleteAfterHandler.handle(Arrays.asList(keyValue.split("_")));
}
}
private SyncDataDTO<Map<String, Object>> getSyncData(String msg) {
return JSON.parseObject(msg, SyncDataDTO.class);
}
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.applicationEventPublisher = applicationEventPublisher;
}
}

View File

@ -38,8 +38,8 @@ public class GridDeleteAfterHandlerImpl implements IGridDeleteAfterHandler {
syncDataDTO.setUid(WStringUtil.listToStr(gridIds, "_"));
syncDataDTO.setAction(ISmartCityUserCenterConsts.KAFKA_DATA_DELETE);
syncDataDTO.setData(new HashMap<>());
syncDataDTO.setTableNumber(ISmartCityUserCenterConsts.KAFKA_TOPIC_GRID);
kafkaTemplate.send(ISmartCityUserCenterConsts.KAFKA_TOPIC_GRID, JSONObject.toJSONString(syncDataDTO));
syncDataDTO.setTableNumber(ISmartCityUserCenterConsts.KAFKA_TABLE_SYNC_GRID);
kafkaTemplate.send(ISmartCityUserCenterConsts.KAFKA_TABLE_SYNC_TOPIC, JSONObject.toJSONString(syncDataDTO));
}
}

View File

@ -2,8 +2,8 @@ package cn.com.tenlion.usercenter.service.grid.impl;
import cn.com.tenlion.usercenter.consts.ISmartCityUserCenterConsts;
import cn.com.tenlion.usercenter.pojo.dtos.kafka.SyncDataDTO;
import com.alibaba.fastjson.JSONObject;
import ink.wgink.interfaces.map.IGridPointDeleteAfterHandler;
import ink.wgink.module.map.service.grid.IGridPointService;
import ink.wgink.util.string.WStringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
@ -24,8 +24,6 @@ import java.util.Map;
public class GridPointDeleteAfterHandlerImpl implements IGridPointDeleteAfterHandler {
@Autowired
private KafkaTemplate kafkaTemplate;
@Autowired
private IGridPointService gridPointService;
@Override
public void handle(List<String> gridIds) {
@ -37,7 +35,7 @@ public class GridPointDeleteAfterHandlerImpl implements IGridPointDeleteAfterHan
syncDataDTO.setKeyValue(WStringUtil.listToStr(gridIds, "_"));
syncDataDTO.setAction(ISmartCityUserCenterConsts.KAFKA_DATA_DELETE);
syncDataDTO.setData(new HashMap());
syncDataDTO.setTableNumber(ISmartCityUserCenterConsts.KAFKA_TOPIC_GRID_POINT);
syncDataDTO.setTableNumber(ISmartCityUserCenterConsts.KAFKA_TABLE_SYNC_GRID_POINT);
kafkaTemplate.send(ISmartCityUserCenterConsts.KAFKA_TABLE_SYNC_TOPIC, JSONObject.toJSONString(syncDataDTO));
}
}

View File

@ -6,6 +6,7 @@ import com.alibaba.fastjson.JSONObject;
import ink.wgink.interfaces.map.IGridPointSaveAfterHandler;
import ink.wgink.module.map.pojo.dtos.grid.GridPointDTO;
import ink.wgink.module.map.service.grid.IGridPointService;
import ink.wgink.util.UUIDUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
@ -38,15 +39,15 @@ public class GridPointSaveAfterHandlerImpl implements IGridPointSaveAfterHandler
List<GridPointDTO> gridPointDTOs = gridPointService.list(gridId);
gridPointDTOs.forEach(gridPointDTO -> {
Map<String, Object> data = new HashMap<>(6);
data.put("grid_id", gridId);
data.put("lng", gridPointDTO.getLng());
data.put("lat", gridPointDTO.getLat());
SyncDataDTO<Map> syncDataDTO = new SyncDataDTO<>();
syncDataDTO.setKeyId("grid_id");
syncDataDTO.setKeyValue(gridId);
syncDataDTO.setUid(UUIDUtil.getUUID());
syncDataDTO.setAction(ISmartCityUserCenterConsts.KAFKA_DATA_SAVE);
syncDataDTO.setData(data);
syncDataDTO.setTableNumber(ISmartCityUserCenterConsts.KAFKA_TOPIC_GRID_POINT);
kafkaTemplate.send(ISmartCityUserCenterConsts.KAFKA_TOPIC_GRID_POINT, JSONObject.toJSONString(syncDataDTO));
syncDataDTO.setTableNumber(ISmartCityUserCenterConsts.KAFKA_TABLE_SYNC_GRID_POINT);
kafkaTemplate.send(ISmartCityUserCenterConsts.KAFKA_TABLE_SYNC_TOPIC, JSONObject.toJSONString(syncDataDTO));
});
}

View File

@ -0,0 +1,40 @@
package cn.com.tenlion.usercenter.service.grid.impl;
import cn.com.tenlion.usercenter.consts.ISmartCityUserCenterConsts;
import cn.com.tenlion.usercenter.pojo.dtos.kafka.SyncDataDTO;
import com.alibaba.fastjson.JSONObject;
import ink.wgink.interfaces.map.IGridRelationDeleteAfterHandler;
import ink.wgink.util.string.WStringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
/**
* @ClassName: GridRelationDeleteAfterHandlerImpl
* @Description: 网格关系删除
* @Author: wanggeng
* @Date: 2021/12/24 3:49 PM
* @Version: 1.0
*/
@Service
public class GridRelationDeleteAfterHandlerImpl implements IGridRelationDeleteAfterHandler {
@Autowired
private KafkaTemplate kafkaTemplate;
@Override
public void handle(List<String> relationIds) {
if (relationIds.isEmpty()) {
return;
}
SyncDataDTO<Map> syncDataDTO = new SyncDataDTO<>();
syncDataDTO.setUid(WStringUtil.listToStr(relationIds, "_"));
syncDataDTO.setAction(ISmartCityUserCenterConsts.KAFKA_DATA_DELETE);
syncDataDTO.setTableNumber(ISmartCityUserCenterConsts.KAFKA_TABLE_SYNC_GRID_MEMBER);
kafkaTemplate.send(ISmartCityUserCenterConsts.KAFKA_TABLE_SYNC_TOPIC, JSONObject.toJSONString(syncDataDTO));
}
}

View File

@ -0,0 +1,88 @@
package cn.com.tenlion.usercenter.service.grid.impl;
import cn.com.tenlion.usercenter.consts.ISmartCityUserCenterConsts;
import cn.com.tenlion.usercenter.pojo.dtos.kafka.SyncDataDTO;
import com.alibaba.fastjson.JSONObject;
import ink.wgink.interfaces.map.IGridRelationSaveAfterHandler;
import ink.wgink.module.map.pojo.dtos.grid.GridDTO;
import ink.wgink.module.map.service.grid.IGridService;
import ink.wgink.pojo.dtos.user.UserDTO;
import ink.wgink.service.user.service.IUserService;
import ink.wgink.util.string.WStringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @ClassName: GridRelationSaveAfterHandlerImpl
* @Description: 网格关系新增
* @Author: wanggeng
* @Date: 2021/12/24 3:48 PM
* @Version: 1.0
*/
@Service
public class GridRelationSaveAfterHandlerImpl implements IGridRelationSaveAfterHandler {
@Autowired
private KafkaTemplate kafkaTemplate;
@Autowired
private IUserService userService;
@Autowired
private IGridService gridService;
@Override
public void handle(List<String> relationIds) {
if (relationIds.isEmpty()) {
return;
}
List<UserDTO> userDTOs = userService.listByUserIds(relationIds);
if (userDTOs.isEmpty()) {
return;
}
userDTOs.forEach(userDTO -> {
handle(userDTO);
});
}
@Override
public void handle(UserDTO userDTO) {
List<GridDTO> gridDTOs = gridService.listByRelationId(userDTO.getUserId());
if (gridDTOs.isEmpty()) {
return;
}
List<String> areaNames = new ArrayList<>();
List<String> areaCodes = new ArrayList<>();
List<String> gridCodes = new ArrayList<>();
List<String> gridNames = new ArrayList<>();
gridDTOs.forEach(gridDTO -> {
areaNames.add(gridDTO.getAreaName());
areaCodes.add(gridDTO.getAreaCode());
gridCodes.add(gridDTO.getGridCode());
gridNames.add(gridDTO.getGridCode());
});
Map<String, Object> data = new HashMap<>();
data.put("user_name", userDTO.getUserName());
data.put("user_phone", userDTO.getUserUsername());
data.put("user_avatar", userDTO.getUserAvatar());
data.put("area_name", WStringUtil.listToStr(areaNames, ","));
data.put("area_code", WStringUtil.listToStr(areaCodes, ","));
data.put("grid_code", WStringUtil.listToStr(gridCodes, ","));
data.put("grid_name", WStringUtil.listToStr(gridNames, ","));
SyncDataDTO<Map> syncDataDTO = new SyncDataDTO<>();
syncDataDTO.setUid(userDTO.getUserId());
syncDataDTO.setData(data);
syncDataDTO.setAction(ISmartCityUserCenterConsts.KAFKA_DATA_SAVE);
syncDataDTO.setTableNumber(ISmartCityUserCenterConsts.KAFKA_TABLE_SYNC_GRID_MEMBER);
kafkaTemplate.send(ISmartCityUserCenterConsts.KAFKA_TABLE_SYNC_TOPIC, JSONObject.toJSONString(syncDataDTO));
}
}

View File

@ -32,16 +32,16 @@ public class GridSaveAfterHandlerImpl implements IGridSaveAfterHandler {
}
SyncDataDTO<Map> syncDataDTO = new SyncDataDTO<>();
Map<String, String> gridMap = new HashMap<>();
gridMap.put("gridCode", gridCode);
gridMap.put("gridName", gridName);
gridMap.put("fillColor", fillColor);
gridMap.put("areaCode", areaCode);
gridMap.put("areaName", areaName);
gridMap.put("grid_code", gridCode);
gridMap.put("grid_name", gridName);
gridMap.put("grid_fill_color", fillColor);
gridMap.put("area_code", areaCode);
gridMap.put("area_name", areaName);
syncDataDTO.setUid(gridId);
syncDataDTO.setAction(ISmartCityUserCenterConsts.KAFKA_DATA_SAVE);
syncDataDTO.setData(gridMap);
syncDataDTO.setTableNumber(ISmartCityUserCenterConsts.KAFKA_TOPIC_GRID);
kafkaTemplate.send(ISmartCityUserCenterConsts.KAFKA_TOPIC_GRID, JSONObject.toJSONString(syncDataDTO));
syncDataDTO.setTableNumber(ISmartCityUserCenterConsts.KAFKA_TABLE_SYNC_GRID);
kafkaTemplate.send(ISmartCityUserCenterConsts.KAFKA_TABLE_SYNC_TOPIC, JSONObject.toJSONString(syncDataDTO));
}
}

View File

@ -35,13 +35,13 @@ public class GridUpdateAfterHandlerImpl implements IGridUpdateAfterHandler {
}
SyncDataDTO<Map> syncDataDTO = new SyncDataDTO<>();
Map<String, String> gridMap = new HashMap<>();
gridMap.put("gridName", gridName);
gridMap.put("fillColor", fillColor);
gridMap.put("grid_name", gridName);
gridMap.put("grid_fill_color", fillColor);
syncDataDTO.setUid(gridId);
syncDataDTO.setAction(ISmartCityUserCenterConsts.KAFKA_DATA_UPDATE);
syncDataDTO.setData(gridMap);
syncDataDTO.setTableNumber(ISmartCityUserCenterConsts.KAFKA_TOPIC_GRID);
kafkaTemplate.send(ISmartCityUserCenterConsts.KAFKA_TOPIC_GRID, JSONObject.toJSONString(syncDataDTO));
syncDataDTO.setTableNumber(ISmartCityUserCenterConsts.KAFKA_TABLE_SYNC_GRID);
kafkaTemplate.send(ISmartCityUserCenterConsts.KAFKA_TABLE_SYNC_TOPIC, JSONObject.toJSONString(syncDataDTO));
}
}

View File

@ -0,0 +1,51 @@
package cn.com.tenlion.usercenter.service.grid.impl;
import cn.com.tenlion.usercenter.consts.ISmartCityUserCenterConsts;
import cn.com.tenlion.usercenter.pojo.dtos.kafka.SyncDataDTO;
import com.alibaba.fastjson.JSONObject;
import ink.wgink.interfaces.user.IUserUpdateAfterHandler;
import ink.wgink.pojo.dtos.user.UserDTO;
import ink.wgink.service.user.service.IUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
/**
* @ClassName: UserUpdateAfterHandlerImpl
* @Description: 用户修改
* @Author: wanggeng
* @Date: 2021/12/24 3:58 PM
* @Version: 1.0
*/
@Service
public class UserUpdateAfterHandlerImpl implements IUserUpdateAfterHandler {
@Autowired
private KafkaTemplate kafkaTemplate;
@Autowired
private IUserService userService;
@Override
public void handle(String userId) {
if (StringUtils.isBlank(userId)) {
return;
}
UserDTO userDTO = userService.get(userId);
Map<String, Object> data = new HashMap<>();
data.put("user_name", userDTO.getUserName());
data.put("user_phone", userDTO.getUserUsername());
data.put("user_avatar", userDTO.getUserAvatar());
SyncDataDTO<Map> syncDataDTO = new SyncDataDTO<>();
syncDataDTO.setUid(userDTO.getUserId());
syncDataDTO.setData(data);
syncDataDTO.setAction(ISmartCityUserCenterConsts.KAFKA_DATA_UPDATE);
syncDataDTO.setTableNumber(ISmartCityUserCenterConsts.KAFKA_TABLE_SYNC_GRID_MEMBER);
kafkaTemplate.send(ISmartCityUserCenterConsts.KAFKA_TABLE_SYNC_TOPIC, JSONObject.toJSONString(syncDataDTO));
}
}