14、ShardingJDBC实战:一种orderID生成策略

之前沈剑老师介绍过,从订单中心到多对多业务模式设计。

常见两种方案:

1维护映射关系查询表。

2、 基因分库法:orderid包含userid;

有个类似的业务场景。贴个demo来验证下。

*

整体算法类似于snowflake方法,有所调整,加入分库基因。

public class Test {
	  static Charset charset = Charset.forName("utf-8");  
	public Test(long preid,long uuid){  
		uuid = Math.abs(uuid);
        System.out.println("=============低7位清0,针对orderid非自行生成57位case ===========");  
        System.out.println("移位前:i= "+preid+" = "+Long.toBinaryString(preid)+"(B)");     
        long i1=((preid>>7)<<7);  
        System.out.println("移位后:i= "+i1+" = "+Long.toBinaryString(i1)+"(B)");        
        System.out.println("--------------低7位保留-------------------");     
        System.out.println("移位前:uuid= "+uuid+" = "+Long.toBinaryString(uuid)+"(B)");  
        long j1=uuid&127;  
        System.out.println("移位后:uuid= "+j1+" = "+Long.toBinaryString(j1)+"(B)");  
        System.out.println("==============生成新id >>> =============");  
        long m=i1|j1;  
        System.out.println("移位后:orderid= "+m+" = "+Long.toBinaryString(m)+"(B)");  
       
        System.out.println("==============移位符号的取模===============");  
        
        System.out.println("uuid:= "+uuid+" =%16 "+Math.abs(uuid%16)+"(B)");   
        System.out.println("新id="+m+" =%16 " +Math.abs(m%16)+"(B)");  
        System.out.println("\n");
    }  
     
    public static void main(String[] args){  
    	
    	IdWorker idWorker = new IdWorker(5);
    	Random ran = new Random();
    	for(int i=1;i<=10;i++){
    		
         new Test(idWorker.nextId(),ran.nextLong());
       }
    }  
}*
输出:
=============低7位清0,针对orderid非自行生成57位case ===========
移位前:i= 313408231558893568 = 10001011001011100110010000001000111110000000101000000000000(B)
移位后:i= 313408231558893568 = 10001011001011100110010000001000111110000000101000000000000(B)
--------------低7位保留-------------------
移位前:uuid= 8027758321443996164 = 110111101101000010100111010100100010100010110110000001000000100(B)
移位后:uuid= 4 = 100(B)
==============生成新id >>> =============
移位后:orderid= 313408231558893572 = 10001011001011100110010000001000111110000000101000000000100(B)
==============移位符号的取模===============
uuid:= 8027758321443996164 =%16 4(B)
新id=313408231558893572 =%16 4(B)
=============低7位清0,针对orderid非自行生成57位case ===========
移位前:i= 313408231563087873 = 10001011001011100110010000001001000000000000101000000000001(B)
移位后:i= 313408231563087872 = 10001011001011100110010000001001000000000000101000000000000(B)
--------------低7位保留-------------------
移位前:uuid= 5717100056439315503 = 100111101010111001110000011010111111001011011100101110000101111(B)
移位后:uuid= 47 = 101111(B)
==============生成新id >>> =============
移位后:orderid= 313408231563087919 = 10001011001011100110010000001001000000000000101000000101111(B)
==============移位符号的取模===============
uuid:= 5717100056439315503 =%16 15(B)
新id=313408231563087919 =%16 15(B)
=============低7位清0,针对orderid非自行生成57位case ===========
移位前:i= 313408231563087874 = 10001011001011100110010000001001000000000000101000000000010(B)
移位后:i= 313408231563087872 = 10001011001011100110010000001001000000000000101000000000000(B)
--------------低7位保留-------------------
移位前:uuid= 5077385597686742855 = 100011001110110011111110100110011001001110011010111111101000111(B)
移位后:uuid= 71 = 1000111(B)
==============生成新id >>> =============
移位后:orderid= 313408231563087943 = 10001011001011100110010000001001000000000000101000001000111(B)
==============移位符号的取模===============
uuid:= 5077385597686742855 =%16 7(B)
新id=313408231563087943 =%16 7(B)
=============低7位清0,针对orderid非自行生成57位case ===========
移位前:i= 313408231567282179 = 10001011001011100110010000001001000010000000101000000000011(B)
移位后:i= 313408231567282176 = 10001011001011100110010000001001000010000000101000000000000(B)
--------------低7位保留-------------------
移位前:uuid= 5310546049355588095 = 100100110110010110110010111111011010011001100011010000111111111(B)
移位后:uuid= 127 = 1111111(B)
==============生成新id >>> =============
移位后:orderid= 313408231567282303 = 10001011001011100110010000001001000010000000101000001111111(B)
==============移位符号的取模===============
uuid:= 5310546049355588095 =%16 15(B)
新id=313408231567282303 =%16 15(B)
=============低7位清0,针对orderid非自行生成57位case ===========
移位前:i= 313408231567282180 = 10001011001011100110010000001001000010000000101000000000100(B)
移位后:i= 313408231567282176 = 10001011001011100110010000001001000010000000101000000000000(B)
--------------低7位保留-------------------
移位前:uuid= 9171840745071641936 = 111111101001000111011001001000000101010011100110000010101010000(B)
移位后:uuid= 80 = 1010000(B)
==============生成新id >>> =============
移位后:orderid= 313408231567282256 = 10001011001011100110010000001001000010000000101000001010000(B)
==============移位符号的取模===============
uuid:= 9171840745071641936 =%16 0(B)
新id=313408231567282256 =%16 0(B)
=============低7位清0,针对orderid非自行生成57位case ===========
移位前:i= 313408231575670789 = 10001011001011100110010000001001000110000000101000000000101(B)
移位后:i= 313408231575670784 = 10001011001011100110010000001001000110000000101000000000000(B)
--------------低7位保留-------------------
移位前:uuid= 1013830710403261307 = 111000010001110110011010100011011011111101111110001101111011(B)
移位后:uuid= 123 = 1111011(B)
==============生成新id >>> =============
移位后:orderid= 313408231575670907 = 10001011001011100110010000001001000110000000101000001111011(B)
==============移位符号的取模===============
uuid:= 1013830710403261307 =%16 11(B)
新id=313408231575670907 =%16 11(B)
=============低7位清0,针对orderid非自行生成57位case ===========
移位前:i= 313408231575670790 = 10001011001011100110010000001001000110000000101000000000110(B)
移位后:i= 313408231575670784 = 10001011001011100110010000001001000110000000101000000000000(B)
--------------低7位保留-------------------
移位前:uuid= 6363984744013950644 = 101100001010001011010100110100000100110111101001110111010110100(B)
移位后:uuid= 52 = 110100(B)
==============生成新id >>> =============
移位后:orderid= 313408231575670836 = 10001011001011100110010000001001000110000000101000000110100(B)
==============移位符号的取模===============
uuid:= 6363984744013950644 =%16 4(B)
新id=313408231575670836 =%16 4(B)
=============低7位清0,针对orderid非自行生成57位case ===========
移位前:i= 313408231579865095 = 10001011001011100110010000001001001000000000101000000000111(B)
移位后:i= 313408231579865088 = 10001011001011100110010000001001001000000000101000000000000(B)
--------------低7位保留-------------------
移位前:uuid= 3314939005102391508 = 10111000000001000001110111011000111011001101110000000011010100(B)
移位后:uuid= 84 = 1010100(B)
==============生成新id >>> =============
移位后:orderid= 313408231579865172 = 10001011001011100110010000001001001000000000101000001010100(B)
==============移位符号的取模===============
uuid:= 3314939005102391508 =%16 4(B)
新id=313408231579865172 =%16 4(B)
=============低7位清0,针对orderid非自行生成57位case ===========
移位前:i= 313408231579865096 = 10001011001011100110010000001001001000000000101000000001000(B)
移位后:i= 313408231579865088 = 10001011001011100110010000001001001000000000101000000000000(B)
--------------低7位保留-------------------
移位前:uuid= 2241333329336378670 = 1111100011010110100001100100110110110010101101010010100101110(B)
移位后:uuid= 46 = 101110(B)
==============生成新id >>> =============
移位后:orderid= 313408231579865134 = 10001011001011100110010000001001001000000000101000000101110(B)
==============移位符号的取模===============
uuid:= 2241333329336378670 =%16 14(B)
新id=313408231579865134 =%16 14(B)
=============低7位清0,针对orderid非自行生成57位case ===========
移位前:i= 313408231584059401 = 10001011001011100110010000001001001010000000101000000001001(B)
移位后:i= 313408231584059392 = 10001011001011100110010000001001001010000000101000000000000(B)
--------------低7位保留-------------------
移位前:uuid= 6738151064093185198 = 101110110000010101110001011000101001010001100011101010010101110(B)
移位后:uuid= 46 = 101110(B)
==============生成新id >>> =============
移位后:orderid= 313408231584059438 = 10001011001011100110010000001001001010000000101000000101110(B)
==============移位符号的取模===============
uuid:= 6738151064093185198 =%16 14(B)
新id=313408231584059438 =%16 14(B)

可见模拟分库是保持一致的。

优点:不用维护映射关系表。

缺点:传入的id可能不均匀,需要业务保证。

好忧伤。。。。

版权声明:本文不是「本站」原创文章,版权归原作者所有 | 原文地址: