im.shs.tick.sequence.builder.RedisSeqBuilder Maven / Gradle / Ivy
package im.shs.tick.sequence.builder;
import im.shs.tick.sequence.range.BizName;
import im.shs.tick.sequence.range.impl.redis.RedisSeqRangeMgr;
import im.shs.tick.sequence.seq.Sequence;
import im.shs.tick.sequence.seq.format.GenerateNoFormat;
import im.shs.tick.sequence.seq.impl.DefaultRangeSequence;
import im.shs.tick.core.props.TickProperties;
/**
* 基于redis取步长,序列号生成器构建者
*/
public class RedisSeqBuilder implements SeqBuilder {
private TickProperties tickProperties;
/**
* 连接redis的IP[必选]
*/
private String ip;
/**
* 连接redis的port[必选]
*/
private int port;
/**
* 业务名称[必选]
*/
private BizName bizName;
/**
* 认证权限,看redis是否配置了需要密码auth[可选]
*/
private String auth;
/**
* 获取range步长[可选,默认:1000]
*/
private int step = 1000;
/**
* 序列号分配起始值[可选:默认:0]
*/
private long stepStart = 0;
/**
* 格式
*/
private GenerateNoFormat format = GenerateNoFormat.DATE;
/**
* 前缀
*/
private String bizPrefix = "";
/**
* 是否添加环境参数
*/
private boolean addEnv;
/**
* 添加随机数位数
*/
private int addRandom = 0;
public static RedisSeqBuilder create() {
RedisSeqBuilder builder = new RedisSeqBuilder();
return builder;
}
@Override
public Sequence build() {
//利用Redis获取区间管理器
RedisSeqRangeMgr redisSeqRangeMgr = new RedisSeqRangeMgr();
redisSeqRangeMgr.setTickProperties(this.tickProperties);
redisSeqRangeMgr.setIp(this.ip);
redisSeqRangeMgr.setPort(this.port);
redisSeqRangeMgr.setAuth(this.auth);
redisSeqRangeMgr.setStep(this.step);
redisSeqRangeMgr.setStepStart(stepStart);
redisSeqRangeMgr.init();
//构建序列号生成器
DefaultRangeSequence sequence = new DefaultRangeSequence();
sequence.setName(this.bizName);
sequence.setFormat(this.format);
sequence.setBizPrefix(this.bizPrefix);
sequence.setAddEnv(this.addEnv);
sequence.setAddRandom(this.addRandom);
sequence.setSeqRangeMgr(redisSeqRangeMgr);
return sequence;
}
public RedisSeqBuilder global(TickProperties tickProperties) {
this.tickProperties = tickProperties;
return this;
}
public RedisSeqBuilder ip(String ip) {
this.ip = ip;
return this;
}
public RedisSeqBuilder port(int port) {
this.port = port;
return this;
}
public RedisSeqBuilder auth(String auth) {
this.auth = auth;
return this;
}
public RedisSeqBuilder step(int step) {
this.step = step;
return this;
}
public RedisSeqBuilder bizName(BizName bizName) {
this.bizName = bizName;
return this;
}
public RedisSeqBuilder stepStart(long stepStart) {
this.stepStart = stepStart;
return this;
}
public RedisSeqBuilder format(GenerateNoFormat format) {
this.format = format;
return this;
}
public RedisSeqBuilder bizPrefix(String bizPrefix) {
this.bizPrefix = bizPrefix;
return this;
}
public RedisSeqBuilder addEnv(boolean addEnv) {
this.addEnv = addEnv;
return this;
}
public RedisSeqBuilder addRandom(int addRandom) {
this.addRandom = addRandom;
return this;
}
}