账单-雪花主键-mq-组织do和insertIgnore拆分
This commit is contained in:
@@ -8,7 +8,6 @@ import com.accompany.core.model.Users;
|
|||||||
import com.accompany.sharding.mapper.BillRecordMapper;
|
import com.accompany.sharding.mapper.BillRecordMapper;
|
||||||
import com.accompany.sharding.model.BillRecord;
|
import com.accompany.sharding.model.BillRecord;
|
||||||
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSON;
|
||||||
import com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator;
|
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.redisson.api.RMap;
|
import org.redisson.api.RMap;
|
||||||
@@ -37,59 +36,57 @@ public class BillMessageService implements InitializingBean {
|
|||||||
private UsersService usersService;
|
private UsersService usersService;
|
||||||
|
|
||||||
public void sendBillMessage(BillRecord billRecord){
|
public void sendBillMessage(BillRecord billRecord){
|
||||||
String messId = DefaultIdentifierGenerator.getInstance().nextUUID(null);
|
|
||||||
billRecord.setMessId(messId);
|
|
||||||
|
|
||||||
BillMessage message = new BillMessage();
|
BillMessage message = new BillMessage();
|
||||||
BeanUtils.copyProperties(billRecord, message);
|
BeanUtils.copyProperties(billRecord, message);
|
||||||
|
|
||||||
recordMessMap.fastPut(messId, message);
|
recordMessMap.fastPut(message.getMessId(), message);
|
||||||
|
|
||||||
rocketMQService.sendBillRecordMessage(message);
|
rocketMQService.sendBillRecordMessage(message);
|
||||||
};
|
};
|
||||||
|
|
||||||
public void handleBillMessage(BillMessage billMessage) {
|
public void handleBillMessage(BillMessage billMessage) {
|
||||||
|
|
||||||
BillRecord billRecord = insertBillRecordIgnore(billMessage);
|
BillRecord billRecord = buildBillRecord(billMessage);
|
||||||
log.info("【处理账单mq】 billRecord 插入成功 id:{} messId: {} mess:{}",
|
int row = insertBillRecordIgnore(billRecord);
|
||||||
billRecord.getBillId(), billMessage.getMessId(), JSON.toJSONString(billMessage));
|
if (row <= 0){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
recordMessMap.fastRemove(billMessage.getMessId());
|
recordMessMap.fastRemove(billMessage.getMessId());
|
||||||
}
|
}
|
||||||
|
|
||||||
private BillRecord insertBillRecordIgnore(BillMessage billMessage) {
|
private BillRecord buildBillRecord(BillMessage billMessage) {
|
||||||
long startTime = System.currentTimeMillis();
|
|
||||||
long copyPropertiesTime = 0;
|
|
||||||
long getUserTime = 0;
|
|
||||||
long insertTime = 0;
|
|
||||||
|
|
||||||
BillRecord billRecord = new BillRecord();
|
BillRecord billRecord = new BillRecord();
|
||||||
BeanUtils.copyProperties(billMessage, billRecord);
|
BeanUtils.copyProperties(billMessage, billRecord);
|
||||||
copyPropertiesTime = System.currentTimeMillis();
|
|
||||||
|
|
||||||
Users u = usersService.getUsersByUid(billMessage.getUid());
|
Users u = usersService.getUsersByUid(billMessage.getUid());
|
||||||
getUserTime = System.currentTimeMillis();
|
|
||||||
if (null != u){
|
if (null != u){
|
||||||
billRecord.setPartitionId(u.getPartitionId());
|
billRecord.setPartitionId(u.getPartitionId());
|
||||||
}
|
}
|
||||||
|
|
||||||
int insertRow = billRecordMapper.insertIgnore(billRecord);
|
return billRecord;
|
||||||
insertTime = System.currentTimeMillis();
|
}
|
||||||
|
|
||||||
|
private int insertBillRecordIgnore(BillRecord billRecord) {
|
||||||
|
long startTime = System.currentTimeMillis();
|
||||||
|
int insertRow = billRecordMapper.insertIgnore(billRecord);
|
||||||
long endTime = System.currentTimeMillis();
|
long endTime = System.currentTimeMillis();
|
||||||
|
|
||||||
log.info("insertBillRecordIgnore row {} performance - copy: {}ms, getUser: {}ms, insert: {}ms, total: {}ms",
|
log.info("insertBillRecordIgnore row {} performance - total: {}ms",
|
||||||
insertRow,
|
insertRow,
|
||||||
copyPropertiesTime - startTime,
|
|
||||||
getUserTime - copyPropertiesTime,
|
|
||||||
insertTime - getUserTime,
|
|
||||||
endTime - startTime);
|
endTime - startTime);
|
||||||
|
|
||||||
return billRecord;
|
if (insertRow > 0) {
|
||||||
|
log.info("【处理账单mq】 billRecord 插入成功 id:{} messId: {} billRecord:{}",
|
||||||
|
billRecord.getBillId(), billRecord.getMessId(), JSON.toJSONString(billRecord));
|
||||||
|
}
|
||||||
|
|
||||||
|
return insertRow;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void afterPropertiesSet() throws Exception {
|
public void afterPropertiesSet() throws Exception {
|
||||||
recordMessMap = redissonClient.getMap(RedisKey.bill_record_message.getKey());
|
recordMessMap = redissonClient.getMap(RedisKey.bill_record_message.getKey());
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
}
|
@@ -208,7 +208,7 @@ public class Lucky24GiftSendService {
|
|||||||
private void sendMq(Map<Long, Lucky24Record> recordMap) {
|
private void sendMq(Map<Long, Lucky24Record> recordMap) {
|
||||||
Map<String, Lucky24Message> caches = new HashMap<>(recordMap.size());
|
Map<String, Lucky24Message> caches = new HashMap<>(recordMap.size());
|
||||||
List<Lucky24Message> messageList = new ArrayList<>();
|
List<Lucky24Message> messageList = new ArrayList<>();
|
||||||
|
|
||||||
for (Lucky24Record record: recordMap.values()){
|
for (Lucky24Record record: recordMap.values()){
|
||||||
Long id = identifierGenerator.nextId(null).longValue();
|
Long id = identifierGenerator.nextId(null).longValue();
|
||||||
String messId = id.toString();
|
String messId = id.toString();
|
||||||
|
@@ -145,8 +145,12 @@ public class BillRecordService extends ServiceImpl<BillRecordMapper, BillRecord>
|
|||||||
}
|
}
|
||||||
Double beforeA = null != afterA? DoubleUtil.sub(afterA, a) : null;
|
Double beforeA = null != afterA? DoubleUtil.sub(afterA, a) : null;
|
||||||
|
|
||||||
|
Long billId = identifierGenerator.nextId(null).longValue();
|
||||||
|
String messId = billId.toString();
|
||||||
|
|
||||||
BillRecord billRecord = new BillRecord();
|
BillRecord billRecord = new BillRecord();
|
||||||
billRecord.setBillId(identifierGenerator.nextId(null).longValue());
|
billRecord.setBillId(billId);
|
||||||
|
billRecord.setMessId(messId);
|
||||||
billRecord.setBillType(billType.getValue());
|
billRecord.setBillType(billType.getValue());
|
||||||
billRecord.setUid(uid);
|
billRecord.setUid(uid);
|
||||||
billRecord.setTargetUid(targetUid);
|
billRecord.setTargetUid(targetUid);
|
||||||
|
Reference in New Issue
Block a user