一、上文我們已經(jīng)完成了創(chuàng)建common子模塊
接下來我們在里面創(chuàng)建返回實體類
新建 entry包
在包下面創(chuàng)建 Result類 用于控制器返回結(jié)果:
public class Result {
/**
* 是否成功
/
private boolean flag;
/*
* 返回碼
/
private Integer code;
/*
* 返回信息
/
private String message;
/*
* 返回數(shù)據(jù)
*/
private Object data;
public Result() {
}
public Result(boolean flag, Integer code, String message) {
this.flag = flag;
this.code = code;
this.message = message;
}
public Result(boolean flag, Integer code, String message, Object data) {
this.flag = flag;
this.code = code;
this.message = message;
this.data = data;
}
public boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
這里面我們使用很原始的set get 也可以使用lombook 通過注釋來完成 實體的一些操作微驶。
接下來創(chuàng)建分頁結(jié)果類:
public class PageResult <T>{
private Long total;
private List<T> rows;
public PageResult() {
}
public PageResult(Long total, List<T> rows) {
this.total = total;
this.rows = rows;
}
public Long getTotal() {
return total;
}
public void setTotal(Long total) {
this.total = total;
}
public List<T> getRows() {
return rows;
}
public void setRows(List<T> rows) {
this.rows = rows;
}
}
接下來 創(chuàng)建狀態(tài)碼實體類 :
public class StatusCode {
/**
* 成功
/
public static final int OK=20000;
/*
* 失敗
/
public static final int ERROR =20001;
/*
* 用戶名或密碼錯誤
/
public static final int LOGINERROR =20002;
/*
* 權(quán)限不足
/
public static final int ACCESSERROR =20003;
/*
* 遠(yuǎn)程調(diào)用失敗
/
public static final int REMOTEERROR =20004;
/*
* 重復(fù)操作
/
public static final int REPERROR =20005;
/*
* 驗證碼過期
*/
public static final int VERIFICATION =20006;
}
接下來我們需要解除一個新的分布式的ID生成器:
在 comnon包下創(chuàng)建 util包
IdWorker 代碼如下: 我們直接引用這個代碼 直接來使用:
package util;
import java.lang.management.ManagementFactory;
import java.net.InetAddress;
import java.net.NetworkInterface;
/**
<p>名稱:IdWorker.java</p>
<p>描述:分布式自增長ID</p>
<pre>
Twitter的 Snowflake JAVA實現(xiàn)方案
</pre>
核心代碼為其IdWorker這個類實現(xiàn)浪谴,其原理結(jié)構(gòu)如下,我分別用一個0表示一位因苹,用—分割開部分的作用:
1||0---0000000000 0000000000 0000000000 0000000000 0 --- 00000 ---00000 ---000000000000
在上面的字符串中苟耻,第一位為未使用(實際上也可作為long的符號位),接下來的41位為毫秒級時間扶檐,
然后5位datacenter標(biāo)識位凶杖,5位機器ID(并不算標(biāo)識符,實際是為線程標(biāo)識)款筑,
然后12位該毫秒內(nèi)的當(dāng)前毫秒內(nèi)的計數(shù)智蝠,加起來剛好64位,為一個Long型奈梳。
這樣的好處是杈湾,整體上按照時間自增排序,并且整個分布式系統(tǒng)內(nèi)不會產(chǎn)生ID碰撞(由datacenter和機器ID作區(qū)分)攘须,
并且效率較高漆撞,經(jīng)測試,snowflake每秒能夠產(chǎn)生26萬ID左右于宙,完全滿足需要浮驳。
<p>
64位ID (42(毫秒)+5(機器ID)+5(業(yè)務(wù)編碼)+12(重復(fù)累加))
-
@author Polim
/
public class IdWorker {
/*- 時間起始標(biāo)記點,作為基準(zhǔn)捞魁,一般取系統(tǒng)的最近時間(一旦確定不能變動)
/
private final static long twepoch = 1288834974657L;
/* - 機器標(biāo)識位數(shù)
/
private final static long workerIdBits = 5L;
/* - 數(shù)據(jù)中心標(biāo)識位數(shù)
/
private final static long datacenterIdBits = 5L;
/* - 機器ID最大值
/
private final static long maxWorkerId = -1L ^ (-1L << workerIdBits);
/* - 數(shù)據(jù)中心ID最大值
/
private final static long maxDatacenterId = -1L ^ (-1L << datacenterIdBits);
/* - 毫秒內(nèi)自增位
/
private final static long sequenceBits = 12L;
/* - 機器ID偏左移12位
/
private final static long workerIdShift = sequenceBits;
/* - 數(shù)據(jù)中心ID左移17位
/
private final static long datacenterIdShift = sequenceBits + workerIdBits;
/* - 時間毫秒左移22位
*/
private final static long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits;
private final static long sequenceMask = -1L ^ (-1L << sequenceBits);
/**- 上次生產(chǎn)id時間戳
/
private static long lastTimestamp = -1L;
/* - 0抹恳,并發(fā)控制
/
private long sequence = 0L;
/* - 工作機器ID
/
private final long workerId;
/* - 數(shù)據(jù)標(biāo)識id部分
*/
private final long datacenterId;
public IdWorker() {
this.datacenterId = getDatacenterId(maxDatacenterId);
this.workerId = getMaxWorkerId(datacenterId, maxWorkerId);
}/**
- @param workerId 工作機器ID
- @param datacenterId 序列號
*/
public IdWorker(long workerId, long datacenterId) {
if (workerId > maxWorkerId || workerId < 0) {
throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId));
}
if (datacenterId > maxDatacenterId || datacenterId < 0) {
throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId));
}
this.workerId = workerId;
this.datacenterId = datacenterId;
}
/**
獲取下一個ID
-
@return
*/
public synchronized long nextId() {
long timestamp = timeGen();
if (timestamp < lastTimestamp) {
throw new RuntimeException(String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp));
}if (lastTimestamp == timestamp) {
// 當(dāng)前毫秒內(nèi),則+1
sequence = (sequence + 1) & sequenceMask;
if (sequence == 0) {
// 當(dāng)前毫秒內(nèi)計數(shù)滿了署驻,則等待下一秒
timestamp = tilNextMillis(lastTimestamp);
}
} else {
sequence = 0L;
}
lastTimestamp = timestamp;
// ID偏移組合生成最終的ID奋献,并返回ID
long nextId = ((timestamp - twepoch) << timestampLeftShift)
| (datacenterId << datacenterIdShift)
| (workerId << workerIdShift) | sequence;return nextId;
}
private long tilNextMillis(final long lastTimestamp) {
long timestamp = this.timeGen();
while (timestamp <= lastTimestamp) {
timestamp = this.timeGen();
}
return timestamp;
}private long timeGen() {
return System.currentTimeMillis();
}/**
- <p>
- 獲取 maxWorkerId
- </p>
/
protected static long getMaxWorkerId(long datacenterId, long maxWorkerId) {
StringBuffer mpid = new StringBuffer();
mpid.append(datacenterId);
String name = ManagementFactory.getRuntimeMXBean().getName();
if (!name.isEmpty()) {
/
* GET jvmPid
/
mpid.append(name.split("@")[0]);
}
/- MAC + PID 的 hashcode 獲取16個低位
*/
return (mpid.toString().hashCode() & 0xffff) % (maxWorkerId + 1);
}
- MAC + PID 的 hashcode 獲取16個低位
/**
- <p>
- 數(shù)據(jù)標(biāo)識id部分
- </p>
*/
protected static long getDatacenterId(long maxDatacenterId) {
long id = 0L;
try {
InetAddress ip = InetAddress.getLocalHost();
NetworkInterface network = NetworkInterface.getByInetAddress(ip);
if (network == null) {
id = 1L;
} else {
byte[] mac = network.getHardwareAddress();
id = ((0x000000FF & (long) mac[mac.length - 1])
| (0x0000FF00 & (((long) mac[mac.length - 2]) << 8))) >> 6;
id = id % (maxDatacenterId + 1);
}
} catch (Exception e) {
System.out.println(" getDatacenterId: " + e.getMessage());
}
return id;
}
- 時間起始標(biāo)記點,作為基準(zhǔn)捞魁,一般取系統(tǒng)的最近時間(一旦確定不能變動)
}