This action will force synchronization from fetech-platform/melon-idfactory, which will overwrite any changes that you have made since you forked the repository, and can not be recovered!!!
Synchronous operation will process in the background and will refresh the page when finishing processing. Please be patient.
主键工厂,提供ID生成服务,保证ID的唯一性。
使用motan rpc + restful接口两种调用方式,简单配置,快速部署,使用方便。
项目运行依赖JDK1.8和MongoDB,安装方法自行百度
获取最新版本的服务包,目前版本为v1.0.0,下载地址
解压melon-idfactory-server-assembly.tar.gz,目录如下:
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
进入server的bin目录,执行启动脚本
(windows运行start.bat,linux运行start.sh)
PS:
melon-idfactory-client.jar 为测试调用包,提供了motan rpc的调用封装,使用spring的小伙伴可以引用,
其他小伙伴可以直接运行server,调用restful接口即可。
引用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
@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") + "");
}
}
}
/**
* 获取自增长的主键
*
* @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);
甜瓜系列不是发明创造,只是想把事情变得简单点,给使用者一点甜头。
// TODO 目前自增长整数ID不支持分布式部署,后续将改进。
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。