Vesta發(fā)號(hào)器源碼解析——AbstractIdServiceImpl
這個(gè)類是Id生成的實(shí)現(xiàn)類的抽象類,主要的入口就在AbstractIdServiceImpl的系列類里面
字段
//日志記錄
protected final Logger log = LoggerFactory.getLogger(this.getClass());
protected long machineId = -1;
protected long genMethod = 0;
protected long version = 0;
//Id類型
protected IdType idType;
//Id元數(shù)據(jù)
protected IdMeta idMeta;
//Id轉(zhuǎn)換器
protected IdConverter idConverter;
//MachineId存儲(chǔ)
protected MachineIdProvider machineIdProvider;
//timer字段
protected Timer timer;
構(gòu)造方法
//默認(rèn)構(gòu)造方法
public AbstractIdServiceImpl() {
idType = IdType.SECONDS;
}
//按照給定的名稱初始化
public AbstractIdServiceImpl(String type) {
idType = IdType.parse(type);
}
//按照給定的類型值初始化
public AbstractIdServiceImpl(long type) {
idType = IdType.parse(type);
}
//按照給定的枚舉類初始化
public AbstractIdServiceImpl(IdType type) {
idType = type;
}
初始化方法
public void init() {
//如果id元數(shù)據(jù)沒有初始化,利用工廠初始化id元數(shù)據(jù)
if (this.idMeta == null) {
setIdMeta(IdMetaFactory.getIdMeta(idType));
}
//Id轉(zhuǎn)換器如果沒有初始化按照默認(rèn)的初始化
if (this.idConverter == null) {
setIdConverter(new IdConverterImpl());
}
//timer如果沒有初始化,按默認(rèn)的實(shí)例化
if (this.timer == null) {
setTimer(new SimpleTimer());
}
//初始化,元數(shù)據(jù)和類型
this.timer.init(idMeta, idType);
//初始化machineID
this.machineId = machineIdProvider.getMachineId();
//校驗(yàn)machineid
validateMachineId(this.machineId);
}
校驗(yàn)MachineId
方法主要用于校驗(yàn)MachineId
public void validateMachineId(long machineId){
//MachineId小于0,說明沒有配置,停止啟動(dòng)拋出異常記錄日志
if (machineId < 0) {
log.error("The machine ID is not configured properly (" + machineId + " < 0) so that Vesta Service refuses to start.");
throw new IllegalStateException(
"The machine ID is not configured properly (" + machineId + " < 0) so that Vesta Service refuses to start.");
//machineId超過了最大的MachineId的標(biāo)示范圍,停止啟動(dòng),拋出異常
} else if (machineId >= (1 << this.idMeta.getMachineBits())) {
log.error("The machine ID is not configured properly ("
+ machineId + " >= " + (1 << this.idMeta.getMachineBits()) + ") so that Vesta Service refuses to start.");
throw new IllegalStateException("The machine ID is not configured properly ("
+ machineId + " >= " + (1 << this.idMeta.getMachineBits()) + ") so that Vesta Service refuses to start.");
}
}
核心方法 生成id
public long genId() {
Id id = new Id();
//對(duì)ID進(jìn)行基本信息配置
id.setMachine(machineId);
id.setGenMethod(genMethod);
id.setType(idType.value());
id.setVersion(version);
//生成變化字段,一個(gè)是時(shí)間一個(gè)是序列
populateId(id);
//將Id對(duì)象轉(zhuǎn)換成long型的
long ret = idConverter.convert(id, this.idMeta);
// 記錄日志
if (log.isTraceEnabled())
log.trace(String.format("Id: %s => %d", id, ret));
return ret;
}
其他方法
具體作用詳見各方法注釋
//Id主要變化生成方法,實(shí)現(xiàn)類中實(shí)現(xiàn)
protected abstract void populateId(Id id);
//轉(zhuǎn)換時(shí)間,將long時(shí)間轉(zhuǎn)換為date
public Date transTime(final long time) {
return timer.transTime(time);
}
//解析Id,本質(zhì)上是調(diào)用轉(zhuǎn)換方法
//將long轉(zhuǎn)換為Id對(duì)象
public Id expId(long id) {
return idConverter.convert(id, this.idMeta);
}
//構(gòu)建一個(gè)指定的ID,主要參數(shù)是時(shí)間和序列號(hào)
public long makeId(long time, long seq) {
return makeId(time, seq, machineId);
}
//構(gòu)建一個(gè)指定的ID,主要參數(shù)是時(shí)間和序列號(hào)以及machineid
public long makeId(long time, long seq, long machine) {
return makeId(genMethod, time, seq, machine);
}
//構(gòu)建一個(gè)指定的ID,主要參數(shù)是生成方法、時(shí)間和序列號(hào)以及machineid
public long makeId(long genMethod, long time, long seq, long machine) {
return makeId(idType.value(), genMethod, time, seq, machine);
}
//構(gòu)建一個(gè)指定的ID,主要參數(shù)是類型、生成方法、時(shí)間和序列號(hào)以及machineid
public long makeId(long type, long genMethod, long time,
long seq, long machine) {
return makeId(version, type, genMethod, time, seq, machine);
}
//構(gòu)建一個(gè)指定的ID,主要參數(shù)是版本、類型、生成方法、時(shí)間和序列號(hào)以及machineid
public long makeId(long version, long type, long genMethod,
long time, long seq, long machine) {
Id id = new Id(machine, seq, time, genMethod, type, version);
return idConverter.convert(id, this.idMeta);
}
//下面是相關(guān)字段的get和set方法
public void setMachineId(long machineId) {
this.machineId = machineId;
}
public void setGenMethod(long genMethod) {
this.genMethod = genMethod;
}
public void setVersion(long version) {
this.version = version;
}
public void setIdConverter(IdConverter idConverter) {
this.idConverter = idConverter;
}
public void setIdMeta(IdMeta idMeta) {
this.idMeta = idMeta;
}
public void setMachineIdProvider(MachineIdProvider machineIdProvider) {
this.machineIdProvider = machineIdProvider;
}
public void setTimer(Timer timer) {
this.timer = timer;
}