1 Star 0 Fork 29

baxi84yy / melon-idfactory

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
LGPL-3.0

melon-idfactory

主键工厂,提供ID生成服务,保证ID的唯一性。

使用motan rpc + restful接口两种调用方式,简单配置,快速部署,使用方便。

目前提供3中ID服务:

  1. 提供唯一有序的,不重复的64位整数id生成服务(推荐使用)
  2. 提供自增长整数ID生产服务
  3. 提供32位UUID生产服务

使用说明

1.依赖安装

项目运行依赖JDK1.8和MongoDB,安装方法自行百度

2.发布版下载

获取最新版本的服务包,目前版本为v1.0.0,下载地址

解压melon-idfactory-server-assembly.tar.gz,目录如下:

  • bin 运行脚本,提供windows和linux两种运行方式
  • conf 配置文件
  • lib 项目库

3.配置说明

melon-idfactory的配置很简单,而且都提供默认配置,一般使用时关注少量配置即可

# conf.properties

# mongodb连接配置
mongo.host=127.0.0.1
mongo.port=27017   #默认为"27017"
mongo.databaseName=MelonIdFactory   #默认MelonMongoDbDefault

# uid-generator配置,与64位有序ID的功能有关,可以不做改动,全部使用默配置
#uid-generator配置详细介绍
uid.boostPower=3
uid.paddingFactor=50
uid.scheduleInterval=60
uid.timeBits=28
uid.workerBits=22
uid.seqBits=13
uid.epochStr=2017-10-1

# RPC远程调用配置,使用motan提供rpc服务和restful接口
motan.service.export=8002   # Java服务暴露端口,默认为"8002"
motan.restful.export=8004    # Restful接口暴露端口,默认为"8004"

# Java服务支持使用zookeeper或consul为注册中心,配置如下,默认为direct直连,可以不配置
motan.registry.regProtocol=zookeeper
motan.registry.address=127.0.0.1:2181

4.运行

进入server的bin目录,执行启动脚本

  • start.bat
  • start.sh
  • stop.sh

(windows运行start.bat,linux运行start.sh)

PS:
melon-idfactory-client.jar 为测试调用包,提供了motan rpc的调用封装,使用spring的小伙伴可以引用,
其他小伙伴可以直接运行server,调用restful接口即可。

调用说明

restful调用说明

client调用说明

引用melon-idfactory-client.jar

可自行编译源码或者直接下载JAR包,下载地址

目前只支持spring项目,可扩展,在你项目的spring配置文件中加入如下配置:

<context:component-scan base-package="com.fetech"/>

<bean id="propertyConfigurer" class="com.fetech.melon.context.property.MelonPropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath*:META-INF/melon/idfactory_conf.properties</value>
        </list>
    </property>
</bean>

# conf.properties

# RPC服务地址配置,无注册中心时使用
motan.referer.directUrl=127.0.0.1:8002
# 如服务使用注册中心发布,需要配置对应的注册中心地址
motan.registry.regProtocol=zookeeper
motan.registry.address=127.0.0.1:2181

代码中直接注入IdFactoryClient即可

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:META-INF/spring/spring-test-idfactory-client.xml"})
public class TestIF {

    @Resource
    private IdFactoryClient idFactoryClient;

    @Test
    public void uuid() {
        LogUtil.debug("uuid:");
        for (int i = 0; i < 10; i++) {
            LogUtil.debug(idFactoryClient.getUUID());
        }
    }

    @Test
    public void uid() {
        LogUtil.debug("64 uid:");
        for (int i = 0; i < 10; i++) {
            long id = idFactoryClient.get64Uid();
            LogUtil.debug(id + "");
            LogUtil.debug(idFactoryClient.parse64Uid(id));
        }
    }

    @Test
    public void incrementId() {
        LogUtil.debug("auto increment id:");
        for (int i = 0; i < 10; i++) {
            LogUtil.debug(idFactoryClient.getIncrementId("test_1") + "");
        }
    }

    @Test
    public void incrementId2() {
        LogUtil.debug("auto increment2 id:");
        boolean ret = idFactoryClient.initIncrementId("test_2", 10);
        Assert.assertTrue(ret);
        for (int i = 0; i < 10; i++) {
            LogUtil.debug(idFactoryClient.getIncrementId("test_2") + "");
        }
    }
}

IdFactoryClient接口说明

/**
 * 获取自增长的主键
 *
 * @param key 主键的key,一般可以使用表名
 * @return long exp:1,2,3...
 */
long getIncrementId(String key);

/**
 * 手动初始化自增长的主键,一般初始化一次即可,如没有初始化,默认getIncrementId从1开始
 *
 * @param key   主键的key,一般可以使用表名
 * @param start 组件的初始值,默认为0,则从1开始
 * @return boolean 初始化成功或失败
 */
boolean initIncrementId(String key, long start);

/**
 * 获取32位的UUID
 *
 * @return String exp:ea5846a348764fc4a7311d6a340e9d14,ae2653087ec84dd59b3adcf4c307cf97...
 */
String getUUID();

/**
 * 获取有序的64位的ID,推荐使用
 *
 * @return long exp:69728553533104128,69728553533104129,69728553533104130...
 */
long get64Uid();

/**
 * 解析有序的64位的ID
 *
 * @param uid 有序的64位的ID
 * @return json exp:
 * {
 * "UID": "69728553533104129",
 * "timestamp": "2017-11-08 11:42:48",
 * "workerId": "87",
 * "sequence": "1"
 * }
 */
String parse64Uid(long uid);

其他说明

  1. 64位有序ID使用了百度uid-generator,是对twitter的snowflake算法的实现
  1. 为什么要使用64d位的有序ID

特别说明,使用melon(甜瓜)开发框架

甜瓜系列不是发明创造,只是想把事情变得简单点,给使用者一点甜头。

melon传送门

// TODO 目前自增长整数ID不支持分布式部署,后续将改进。

GNU LESSER GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. [http://fsf.org/] Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below. 0. Additional Definitions. As used herein, "this License" refers to version 3 of the GNU Lesser General Public License, and the "GNU GPL" refers to version 3 of the GNU General Public License. "The Library" refers to a covered work governed by this License, other than an Application or a Combined Work as defined below. An "Application" is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library. A "Combined Work" is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the "Linked Version". The "Minimal Corresponding Source" for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version. The "Corresponding Application Code" for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work. 1. Exception to Section 3 of the GNU GPL. You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL. 2. Conveying Modified Versions. If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version: a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy. 3. Object Code Incorporating Material from Library Header Files. The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following: a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the object code with a copy of the GNU GPL and this license document. 4. Combined Works. You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following: a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the Combined Work with a copy of the GNU GPL and this license document. c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document. d) Do one of the following: 0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source. 1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version. e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.) 5. Combined Libraries. You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following: a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License. b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work. 6. Revised Versions of the GNU Lesser General Public License. The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation. If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library.

简介

id工厂 使用baidu的uid-generator,支持高并发 提供统一的有序,不重复的id生成服务 展开 收起
Java
LGPL-3.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/baxi84yy/melon-idfactory.git
git@gitee.com:baxi84yy/melon-idfactory.git
baxi84yy
melon-idfactory
melon-idfactory
master

搜索帮助

14c37bed 8189591 565d56ea 8189591