提交 2c5a891e authored 作者: 陈世营's avatar 陈世营

Id leaf

上级 8525974d
......@@ -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
## 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
```
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";
}
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
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;
}
}
io.opensharding.keygen.leaf.LeafSegmentKeyGenerator
io.opensharding.keygen.leaf.LeafSnowflakeKeyGenerator
/*
* 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();
}
}
/*
* 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();
}
}
/*
* 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());
}
}
}
# 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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论