创建maven项目引入依赖:
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-jdbc-core</artifactId>
<version>5.1.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.github.yitter</groupId>
<artifactId>yitter-idgenerator</artifactId>
<version>1.0.6</version>
</dependency>
编写主键生成类
public class CarautoKeyGen implements KeyGenerateAlgorithm {
final static short method = 1;
@Override
public Comparable<?> generateKey() {
return YitIdHelper.nextId();
}
@Override
public void init() {
IdGeneratorOptions options = new IdGeneratorOptions();
options.WorkerIdBitLength = 6;
options.SeqBitLength = 10;
options.BaseTime = System.currentTimeMillis();
options.Method = method;
options.WorkerId = 2;
YitIdHelper.setIdGenerator(options);
}
@Override
public String getType() {
return "CarautoKeyGen";
}
}
添加一个SPI配置
resources依次创建目录META-INF与services
在services目录下创建文件
org.apache.shardingsphere.sharding.spi.KeyGenerateAlgorithm
文件内容为您的Java类
我这里就只有一个 所以就写了
CarautoKeyGen
打包完成以后放到Sharding-Proxy程序目录下的Lib目录内并给予执行权限,
同时漂移ID的jar文件也要放入Lib目录内并给予执行权限
这篇文章还没有评论