Code Monkey home page Code Monkey logo

idleaf's Introduction

idleaf

简介

对于 Leaf——美团点评分布式ID生成系统 中介绍的 Leaf-segment数据库方案 生成唯一orderId的方案的一个实现。 在实现中使用双buffer优化,在第一个buffer使用50%的时候去加载另一个buffer的数据,这里分同步与异步两种方式,默认是同步加载。对于异步增加参数asynLoadingSegment 设为true. 在第一个buffer使用完毕之后,切换到另一个buffer,需要去验证该buffer是否加载完成数据,然后进行切换(对于异步加载出了异常则同步加载数据,然后再切换,此时会产生发号的阻塞)。

使用示例

一种业务id使用方式

<bean id="idLeafService" 	class="com.z.idleaf.support.MysqlIdLeafServiceImpl">
	<property name="jdbcTemplate" ref="jdbcTemplate" />
	<property name="asynLoadingSegment" value="true" />
	<property name="bizTag" value="order"></property>
</bean>

Long id=idLeafService.getId()

多个业务id使用方式

<bean id="parentIdLeafService"  abstract="true" 	class="com.z.idleaf.support.MysqlIdLeafServiceImpl">
	<property name="jdbcTemplate" ref="jdbcTemplate" />
	<property name="asynLoadingSegment" value="true" />
	
</bean>

<bean id="productIdLeafService"  parent="parentIdLeafService">
	<property name="bizTag" value="product"></property>
</bean>

<bean id="orderIdLeafService"  parent="parentIdLeafService">
	<property name="bizTag" value="order"></property>
</bean>




Long orderId=orderIdLeafService.getId();
Long productId=productIdLeafService.getId();

spring提供的关于主键生成的策略

spring 框架是无所不能,关于主键的生成它也提供了类似的功能,相应的类为org.springframework.jdbc.support.incrementer.MySQLMaxValueIncrementer 但是这个类只保证单jvm的唯一性,在集群环中它会有并发更新的问题,所以我在此写了一个ExtendMySQLMaxValueIncrementer。思路还是沿袭上面的。

使用示例

<bean id="extendMysqlMaxValueIncrementer" abstract="true"
	class="com.z.idleaf.support.ExtendMySQLMaxValueIncrementer">
	<property name="dataSource" ref="testDataSource" />
	<property name="asynLoadingSegment" value="true"></property>
	<property name="incrementerName" value="id_segment" />
	<property name="columnName" value="max_id" />
	<property name="stepField" value="p_step" />
	<property name="bizField" value="biz_tag"></property>
	<property name="lastUpdateTimeField" value="last_update_time" />
	<property name="updateTimeField" value="current_update_time" />
	<property name="paddingLength" value="6"></property>

</bean>

<bean id="orderIncrementer" parent="extendMysqlMaxValueIncrementer">
	<property name="bizTag" value="order" />
</bean>

<bean id="productNoIncrementer" parent="extendMysqlMaxValueIncrementer">
	<property name="bizTag" value="productNo" />
</bean>

 @Autowired
@Qualifier("productNoIncrementer")
private DataFieldMaxValueIncrementer incrementer;

@Test
public void test() {
    int i = 0;
    while (i < 10) {
        System.out.println("long id=" + incrementer.nextLongValue());
        System.out.println("int id=" + incrementer.nextIntValue());
        System.out.println("string id=" + incrementer.nextStringValue());
        i++;
    }
}

spring 帮我们提供了三个接口,分别为获取int,long,string 三种数据类型。当然这种方式需要深度使用spring-jdbc框架.

带基因法id生成

基因法生成id,主要思路来自架构师之路 实现接口com.z.idleaf.WithGeneIdLeafService 使用示例

<bean id="idLeafService"    class="com.z.idleaf.support.MysqlIdLeafServiceImpl">
    <property name="jdbcTemplate" ref="jdbcTemplate" />
    <property name="asynLoadingSegment" value="true" />
    <property name="bizTag" value="order"></property>
</bean>
<bean id="withGeneIdLeafService" class="com.z.idleaf.support.WithGeneIdLeafServiceImpl">
	 <property name="idLeafService" ref="idLeafService" />
    <property name="dbSize" value="32" />
</bean>
Long orderId=withGeneIdLeafService.getId(10000L)

idleaf's People

Contributors

zhuzhong avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

idleaf's Issues

1、是否要加上 init-method="init"?2、能否产生时间序列那种ID

  • 1 是否要加上 init-method="init"
<bean id="orderIdLeafService"  parent="parentIdLeafService">
	<property name="bizTag" value="order"></property>
</bean>

稳定性欠缺

从数据库获取序列的最大的主键时的条件为:
1、当前序列已使用50%。
2、当前序列已经使用完毕。
则如果50%时获取序列失败时,则需要等使用完毕才可以加载;递归加载在极端情况下,可能进入死循环。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.