数据库主键id使用自动增长的坏处
1. 程序后台添加测试
2. 旧数据导入
3. 数据库迁移
4. 缓存 (如,两个库中id都为2时,缓存的时候会覆盖)以下为代码块
结论:一般在实际生产环境中,一般用程序来确保id的唯一性。
以下为UUID的生成工具类:
package com.cnksi.utils;
import java.net.InetAddress;
/**
* 在分布式系统中,需要生成全局UID的场合还是比较多的,twitter的snowflake解决了这种需求,
* 实现也还是很简单的,除去配置信息,核心代码就是毫秒级时间41位+机器ID 10位+毫秒内序列12位。
* 该项目地址为:https://github.com/twitter/snowflake是用Scala实现的。
* python版详见开源项目https://github.com/erans/pysnowflake。
*
* @author xiqiao
* @Date 2017-12-19
*/
public class IdWorker {
//根据具体机器环境提供
private final long workerId;
//滤波器,使时间变小,生成的总位数变小,一旦确定不能变动
private final static long twepoch = 1361753741828L;
private long sequence = 0L;
private final static long workerIdBits = 10L;
private final static long maxWorkerId = -1L ^ -