Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
L
loit-build-common
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
loit-Infrastructure
loit-build-common
Commits
2c5a891e
提交
2c5a891e
authored
2月 25, 2020
作者:
陈世营
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Id leaf
上级
8525974d
全部展开
隐藏空白字符变更
内嵌
并排
正在显示
11 个修改的文件
包含
350 行增加
和
0 行删除
+350
-0
pom.xml
loit-build-component/pom.xml
+2
-0
README.md
loit-build-component/sharding-keygen-leaf/README.md
+45
-0
pom.xml
loit-build-component/sharding-keygen-leaf/pom.xml
+0
-0
LeafPropertiesConstant.java
...a/io/opensharding/keygen/leaf/LeafPropertiesConstant.java
+12
-0
LeafSegmentKeyGenerator.java
.../io/opensharding/keygen/leaf/LeafSegmentKeyGenerator.java
+83
-0
LeafSnowflakeKeyGenerator.java
...o/opensharding/keygen/leaf/LeafSnowflakeKeyGenerator.java
+51
-0
org.apache.shardingsphere.spi.keygen.ShardingKeyGenerator
...org.apache.shardingsphere.spi.keygen.ShardingKeyGenerator
+2
-0
LeafSegmentKeyGeneratorTest.java
...opensharding/keygen/leaf/LeafSegmentKeyGeneratorTest.java
+44
-0
LeafSnowflakeKeyGeneratorTest.java
...ensharding/keygen/leaf/LeafSnowflakeKeyGeneratorTest.java
+44
-0
UniqueLeafKeyGeneratorTest.java
.../opensharding/keygen/leaf/UniqueLeafKeyGeneratorTest.java
+56
-0
leaf.properties
...t/sharding-keygen-leaf/src/test/resources/leaf.properties
+11
-0
没有找到文件。
loit-build-component/pom.xml
浏览文件 @
2c5a891e
...
...
@@ -14,6 +14,7 @@
<modules>
<module>
loit-seata-mybatis-mysql-suport
</module>
<module>
loit-component-jetcache-client
</module>
<module>
sharding-keygen-leaf
</module>
</modules>
</project>
\ No newline at end of file
loit-build-component/sharding-keygen-leaf/README.md
0 → 100644
浏览文件 @
2c5a891e
## Build & Test
For using Leaf-Segment key generator, this SQL should executed in MySQL first:
```
$mysql
CREATE DATABASE leaf;
CREATE TABLE `leaf_alloc` (
`biz_tag` varchar(128) NOT NULL DEFAULT '', -- your biz unique name
`max_id` bigint(20) NOT NULL DEFAULT '1',
`step` int(11) NOT NULL,
`description` varchar(256) DEFAULT NULL,
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`biz_tag`)
) ENGINE=InnoDB;
insert into leaf_alloc(biz_tag, max_id, step, description) values('sstest', 1, 2000, 'Test leaf Segment Mode Get Id');
```
## Examples
In tests of this project, you can see:
1.
Configuration parameters in
`leaf.properties`
;
2.
Example codes in test classes.
## Using in ShardingSphere config file
Step1: You just need refer dependency in you pom.xml:
```
$xml
<dependency>
<groupId>io.opensharding</groupId>
<artifactId>sharding-keygen-leaf</artifactId>
<version>5.0.0-RC1-SNAPSHOT</version>
</dependency>
```
Step2: And then config this parameter in your rule,
Using Leaf Segment via Database:
```
$yaml
keyGenerator: LEAF_SEGMENT
```
Or using Leaf Snowflake via zookeeper:
```
$yaml
keyGenerator: LEAF_SNOWFLAKE
```
loit-build-component/sharding-keygen-leaf/pom.xml
0 → 100644
浏览文件 @
2c5a891e
差异被折叠。
点击展开。
loit-build-component/sharding-keygen-leaf/src/main/java/io/opensharding/keygen/leaf/LeafPropertiesConstant.java
0 → 100644
浏览文件 @
2c5a891e
package
io
.
opensharding
.
keygen
.
leaf
;
public
final
class
LeafPropertiesConstant
{
// for keyGenerator
public
final
static
String
LEAF_KEY
=
"leaf.key"
;
// for LeafSegment
public
final
static
String
LEAF_JDBC_URL
=
"leaf.jdbc.url"
;
public
final
static
String
LEAF_JDBC_USERNAME
=
"leaf.jdbc.username"
;
public
final
static
String
LEAF_JDBC_PASSWORD
=
"leaf.jdbc.password"
;
// for LeafSnowflake
public
final
static
String
LEAF_ZK_LIST
=
"leaf.zk.list"
;
}
loit-build-component/sharding-keygen-leaf/src/main/java/io/opensharding/keygen/leaf/LeafSegmentKeyGenerator.java
0 → 100644
浏览文件 @
2c5a891e
package
io
.
opensharding
.
keygen
.
leaf
;
import
com.alibaba.druid.pool.DruidDataSource
;
import
com.sankuai.inf.leaf.IDGen
;
import
com.sankuai.inf.leaf.common.Result
;
import
com.sankuai.inf.leaf.segment.SegmentIDGenImpl
;
import
com.sankuai.inf.leaf.segment.dao.IDAllocDao
;
import
com.sankuai.inf.leaf.segment.dao.impl.IDAllocDaoImpl
;
import
lombok.Getter
;
import
lombok.Setter
;
import
lombok.SneakyThrows
;
import
org.apache.shardingsphere.spi.keygen.ShardingKeyGenerator
;
import
java.util.Properties
;
/**
* Key generator implemented by leaf segment algorithms.
*/
public
final
class
LeafSegmentKeyGenerator
implements
ShardingKeyGenerator
{
public
final
static
String
TYPE
=
"LEAF_SEGMENT"
;
@Getter
@Setter
private
Properties
properties
;
private
IDGen
idGen
;
private
DruidDataSource
dataSource
;
@Override
public
Comparable
<?>
generateKey
()
{
if
(
this
.
dataSource
==
null
){
initDataSourceAndIDGen
(
this
.
properties
);
}
Result
result
=
this
.
idGen
.
get
(
properties
.
getProperty
(
LeafPropertiesConstant
.
LEAF_KEY
));
return
result
.
getId
();
}
@SneakyThrows
public
synchronized
void
initDataSourceAndIDGen
(
Properties
properties
){
if
(
this
.
properties
==
null
){
this
.
setProperties
(
properties
);
}
synchronized
(
this
){
if
(
this
.
dataSource
==
null
)
{
DruidDataSource
dataSource
=
new
DruidDataSource
();
dataSource
.
setUrl
(
properties
.
getProperty
(
LeafPropertiesConstant
.
LEAF_JDBC_URL
));
dataSource
.
setUsername
(
properties
.
getProperty
(
LeafPropertiesConstant
.
LEAF_JDBC_USERNAME
));
dataSource
.
setPassword
(
properties
.
getProperty
(
LeafPropertiesConstant
.
LEAF_JDBC_PASSWORD
));
dataSource
.
init
();
IDAllocDao
dao
=
new
IDAllocDaoImpl
(
dataSource
);
this
.
idGen
=
new
SegmentIDGenImpl
();
((
SegmentIDGenImpl
)
this
.
idGen
).
setDao
(
dao
);
this
.
idGen
.
init
();
this
.
dataSource
=
dataSource
;
}
}
}
@Override
public
String
getType
()
{
return
TYPE
;
}
@Override
public
Properties
getProperties
()
{
return
this
.
properties
;
}
@Override
public
void
setProperties
(
Properties
properties
)
{
this
.
properties
=
properties
;
}
@Override
protected
void
finalize
()
throws
Throwable
{
super
.
finalize
();
if
(
this
.
dataSource
!=
null
)
{
this
.
dataSource
.
close
();
}
}
}
\ No newline at end of file
loit-build-component/sharding-keygen-leaf/src/main/java/io/opensharding/keygen/leaf/LeafSnowflakeKeyGenerator.java
0 → 100644
浏览文件 @
2c5a891e
package
io
.
opensharding
.
keygen
.
leaf
;
import
com.sankuai.inf.leaf.IDGen
;
import
com.sankuai.inf.leaf.snowflake.SnowflakeIDGenImpl
;
import
lombok.Getter
;
import
lombok.Setter
;
import
org.apache.shardingsphere.spi.keygen.ShardingKeyGenerator
;
import
java.util.Properties
;
/**
* Key generator implemented by leaf snowflake algorithms.
*/
public
final
class
LeafSnowflakeKeyGenerator
implements
ShardingKeyGenerator
{
public
final
static
String
TYPE
=
"LEAF_SNOWFLAKE"
;
@Getter
@Setter
private
Properties
properties
;
private
IDGen
idGen
;
@Override
public
Comparable
<?>
generateKey
()
{
if
(
this
.
idGen
==
null
)
{
synchronized
(
this
)
{
if
(
this
.
idGen
==
null
)
{
IDGen
idGen
=
new
SnowflakeIDGenImpl
(
properties
.
getProperty
(
LeafPropertiesConstant
.
LEAF_ZK_LIST
),
8089
);
this
.
idGen
=
idGen
;
}
}
}
return
this
.
idGen
.
get
(
properties
.
getProperty
(
LeafPropertiesConstant
.
LEAF_KEY
)).
getId
();
}
@Override
public
String
getType
()
{
return
TYPE
;
}
@Override
public
Properties
getProperties
()
{
return
this
.
properties
;
}
@Override
public
void
setProperties
(
Properties
properties
)
{
this
.
properties
=
properties
;
}
}
loit-build-component/sharding-keygen-leaf/src/main/resources/META-INF/services/org.apache.shardingsphere.spi.keygen.ShardingKeyGenerator
0 → 100644
浏览文件 @
2c5a891e
io.opensharding.keygen.leaf.LeafSegmentKeyGenerator
io.opensharding.keygen.leaf.LeafSnowflakeKeyGenerator
loit-build-component/sharding-keygen-leaf/src/test/java/io/opensharding/keygen/leaf/LeafSegmentKeyGeneratorTest.java
0 → 100644
浏览文件 @
2c5a891e
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
io
.
opensharding
.
keygen
.
leaf
;
import
org.junit.Before
;
import
org.junit.Test
;
import
java.io.IOException
;
import
java.sql.SQLException
;
public
class
LeafSegmentKeyGeneratorTest
extends
UniqueLeafKeyGeneratorTest
{
@Override
public
String
getLeafType
()
{
return
LeafSegmentKeyGenerator
.
TYPE
;
}
@Before
@Override
public
void
before
()
throws
IOException
,
SQLException
{
super
.
before
();
}
@Test
@Override
public
void
testGetId
()
{
super
.
testGetId
();
}
}
loit-build-component/sharding-keygen-leaf/src/test/java/io/opensharding/keygen/leaf/LeafSnowflakeKeyGeneratorTest.java
0 → 100644
浏览文件 @
2c5a891e
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
io
.
opensharding
.
keygen
.
leaf
;
import
org.junit.Before
;
import
org.junit.Test
;
import
java.io.IOException
;
import
java.sql.SQLException
;
public
class
LeafSnowflakeKeyGeneratorTest
extends
UniqueLeafKeyGeneratorTest
{
@Override
public
String
getLeafType
()
{
return
LeafSnowflakeKeyGenerator
.
TYPE
;
}
@Before
@Override
public
void
before
()
throws
IOException
,
SQLException
{
super
.
before
();
}
@Test
@Override
public
void
testGetId
()
{
super
.
testGetId
();
}
}
loit-build-component/sharding-keygen-leaf/src/test/java/io/opensharding/keygen/leaf/UniqueLeafKeyGeneratorTest.java
0 → 100644
浏览文件 @
2c5a891e
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
io
.
opensharding
.
keygen
.
leaf
;
import
com.sankuai.inf.leaf.common.PropertyFactory
;
import
org.apache.shardingsphere.spi.algorithm.keygen.ShardingKeyGeneratorServiceLoader
;
import
org.apache.shardingsphere.spi.keygen.ShardingKeyGenerator
;
import
org.junit.Assert
;
import
java.io.IOException
;
import
java.sql.SQLException
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Properties
;
public
abstract
class
UniqueLeafKeyGeneratorTest
{
ShardingKeyGenerator
keyGenerator
;
public
abstract
String
getLeafType
();
public
void
before
()
throws
IOException
,
SQLException
{
if
(
this
.
getLeafType
()
!=
null
)
{
Properties
properties
=
PropertyFactory
.
getProperties
();
keyGenerator
=
new
ShardingKeyGeneratorServiceLoader
().
newService
(
this
.
getLeafType
(),
properties
);
}
}
public
void
testGetId
()
{
if
(
this
.
getLeafType
()
!=
null
)
{
List
<
Object
>
list
=
new
ArrayList
<>();
for
(
int
i
=
0
;
i
<
100
;
++
i
)
{
Object
r
=
keyGenerator
.
generateKey
();
System
.
out
.
println
(
r
);
if
(((
Long
)
r
).
longValue
()
>
0
)
list
.
add
(
r
);
}
Assert
.
assertEquals
(
100
,
list
.
size
());
}
}
}
loit-build-component/sharding-keygen-leaf/src/test/resources/leaf.properties
0 → 100644
浏览文件 @
2c5a891e
# for keyGenerator key
leaf.key
=
loittest
# for LeafSegment
leaf.jdbc.url
=
jdbc:mysql://39.98.202.173:3306/leaf?serverTimezone=UTC&useSSL=false
leaf.jdbc.username
=
root
leaf.jdbc.password
=
abcd1234A!
# for LeafSnowflake
leaf.zk.list
=
localhost:2181
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论