Commit 2285bd9b by liuyf

使用雪花算法优化

git-svn-id: svn://192.168.3.233/frms/code/car-service/trunk/car-service@32482 aeb6102e-e053-11e5-9fec-f9ebc45caf4b
1 parent e98d562a
......@@ -2,8 +2,8 @@ package com.glsx.ordercenter.config;
import com.glsx.ordercenter.utils.SnowflakeWorker;
import com.glsx.plat.common.utils.SnowFlake;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
......@@ -18,13 +18,12 @@ import org.springframework.context.annotation.Configuration;
@Configuration
public class SnowFlakeWorkerConfig {
@Autowired
private SnowFlakeProperties snowFlakeConfig;
@Bean("snowflakeWorker")
public SnowflakeWorker getSnowFlakeWorker() {
String workerId = snowFlakeConfig.getWorkerId();
String dataCenterId = snowFlakeConfig.getDatacenterId();
return new SnowflakeWorker(Long.parseLong(workerId),Long.parseLong(dataCenterId));
Long nodeId = SnowFlake.getLocalNodeId();
long workerId = nodeId & 31L;
long datacenterId = nodeId % 31L;
return new SnowflakeWorker(workerId, datacenterId);
}
}
package com.glsx.ordercenter.service.impl;
import cn.hutool.core.lang.Snowflake;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
......@@ -31,6 +30,7 @@ import com.glsx.ordercenter.strategy.DeviceVerifyStrategy;
import com.glsx.ordercenter.strategy.OrderSyncStrategy;
import com.glsx.ordercenter.thread.OrderStatusNotifyTask;
import com.glsx.ordercenter.utils.OrderUtil;
import com.glsx.ordercenter.utils.SnowflakeWorker;
import com.glsx.ordercenter.utils.SpringContextUtils;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
......@@ -183,6 +183,9 @@ public class OrderServiceImpl implements IOrderService {
@Autowired
private SysSubOrderMapper subOrderMapper;
@Autowired
private SnowflakeWorker snowflakeWorker;
@Override
public ResultObject deleteByPrimaryKey(Integer id) {
SysOrder record = orderMapper.selectByPrimaryKey(id);
......@@ -2203,7 +2206,7 @@ public class OrderServiceImpl implements IOrderService {
public String getOrderSn(int orderType) {
StringBuffer buffer = new StringBuffer();
buffer.append(OrderTypeEnum.getOrderNoPrefix(orderType));
String snowId = String.valueOf(new Snowflake().nextId());
String snowId = String.valueOf(snowflakeWorker.nextId());
int len = snowId.length();
if (len == 16) { //长度16直接返回
return buffer.append(snowId).toString();
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!