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

【修改】 leafFactory 和 zookeeper 节点因为tryLock 导致定时被删除。

上级 165962c1
...@@ -47,5 +47,10 @@ ...@@ -47,5 +47,10 @@
<artifactId>curator-test</artifactId> <artifactId>curator-test</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
</dependencies> </dependencies>
</project> </project>
\ No newline at end of file
...@@ -11,7 +11,6 @@ import lombok.Getter; ...@@ -11,7 +11,6 @@ import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import java.util.Properties; import java.util.Properties;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
...@@ -19,17 +18,16 @@ import java.util.concurrent.SynchronousQueue; ...@@ -19,17 +18,16 @@ import java.util.concurrent.SynchronousQueue;
/** /**
* Key generator implemented by leaf segment algorithms. * Key generator implemented by leaf segment algorithms.
*
*/ */
public final class LeafSegmentKeyGenerator implements LeafKeyGenerator { public final class LeafSegmentKeyGenerator implements LeafKeyGenerator {
private static final String DEFAULT_NAMESPACE = "leaf_segment"; protected static final String DEFAULT_NAMESPACE = "leaf_segment";
private static final String DEFAULT_STEP = "10000"; protected static final String DEFAULT_STEP = "10000";
private static final String DEFAULT_INITIAL_VALUE = "1"; protected static final String DEFAULT_INITIAL_VALUE = "1";
private static final String DEFAULT_REGISTRY_CENTER = "zookeeper"; protected static final String DEFAULT_REGISTRY_CENTER = "zookeeper";
private static final String SLANTING_BAR = "/"; private static final String SLANTING_BAR = "/";
...@@ -96,7 +94,7 @@ public final class LeafSegmentKeyGenerator implements LeafKeyGenerator { ...@@ -96,7 +94,7 @@ public final class LeafSegmentKeyGenerator implements LeafKeyGenerator {
} }
private void initializeLeafKeyInCenter(final String leafKey, final long id, final long step) { private void initializeLeafKeyInCenter(final String leafKey, final long id, final long step) {
leafRegistryCenter.initLock(leafKey); leafRegistryCenter.initLock(leafKey + "_lock");
while (!leafRegistryCenter.tryLock()) { while (!leafRegistryCenter.tryLock()) {
continue; continue;
} }
......
package com.loit.component.keygen.leaf;
import org.apache.commons.lang3.StringUtils;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;
/**
* leafKey 获取不同的keyGenerator
*/
public class LeafSegmentKeyGeneratorFactory {
private Map<String, LeafSegmentKeyGenerator> keyGeneratorMap = new LinkedHashMap<>();
private Properties globleProperties = new Properties();
/**
* 根据 leafKey 标识不同的keyGenerator
*/
public synchronized LeafSegmentKeyGenerator getKeyGenerator(String leafKey) {
if (StringUtils.isEmpty(leafKey)) {
return null;
}
LeafSegmentKeyGenerator leafSegmentKeyGenerator = keyGeneratorMap.get(leafKey);
if (leafSegmentKeyGenerator == null) {
leafSegmentKeyGenerator = new LeafSegmentKeyGenerator();
Properties properties = new Properties();
properties.setProperty("leaf.namespace", globleProperties.getProperty("leaf.namespace", LeafSegmentKeyGenerator.DEFAULT_NAMESPACE));
properties.setProperty("server.list", globleProperties.getProperty("server.list"));
properties.setProperty("leaf.segment.id.initial.value", globleProperties.getProperty("leaf.segment.id.initial.value", LeafSegmentKeyGenerator.DEFAULT_INITIAL_VALUE));
properties.setProperty("leaf.segment.step", globleProperties.getProperty("leaf.segment.step", LeafSegmentKeyGenerator.DEFAULT_STEP));
properties.setProperty("leaf.key", leafKey);
properties.setProperty("registry.center.type", globleProperties.getProperty("registry.center.type", LeafSegmentKeyGenerator.DEFAULT_REGISTRY_CENTER));
leafSegmentKeyGenerator.setProperties(properties);
keyGeneratorMap.put(leafKey, leafSegmentKeyGenerator);
}
return leafSegmentKeyGenerator;
}
public Properties getGlobleProperties() {
return globleProperties;
}
public void setGlobleProperties(Properties globleProperties) {
this.globleProperties = globleProperties;
}
}
package com.loit.component.keygen.leaf;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import java.util.Properties;
@FixMethodOrder(value = MethodSorters.NAME_ASCENDING)
public class LeafSegmentKeyGeneratorFactoryTest {
LeafSegmentKeyGeneratorFactory leafSegmentKeyGeneratorFactory = new LeafSegmentKeyGeneratorFactory();
@Test
public void assertGenerateKeyWithSingleThread() {
Properties properties = new Properties();
properties.setProperty("leaf.namespace", "leaf-segment-loit-test");
properties.setProperty("server.list", "127.0.0.1:2181");
properties.setProperty("leaf.segment.id.initial.value", "3");
properties.setProperty("leaf.segment.step", "100");
leafSegmentKeyGeneratorFactory.setGlobleProperties(properties);
for (int i = 0; i < 12; i++) {
Comparable<?> t_order_item = leafSegmentKeyGeneratorFactory.getKeyGenerator("t_order_item").generateKey();
System.out.println(t_order_item);
}
}
}
...@@ -36,16 +36,17 @@ public final class LeafSegmentKeyGeneratorTest { ...@@ -36,16 +36,17 @@ public final class LeafSegmentKeyGeneratorTest {
properties.setProperty("leaf.namespace","leaf-segment-loit-test"); properties.setProperty("leaf.namespace","leaf-segment-loit-test");
properties.setProperty("server.list", "127.0.0.1:2181"); properties.setProperty("server.list", "127.0.0.1:2181");
properties.setProperty("leaf.segment.id.initial.value", "100001"); properties.setProperty("leaf.segment.id.initial.value", "100001");
properties.setProperty("leaf.segment.step", "5"); properties.setProperty("leaf.segment.step", "100");
properties.setProperty("leaf.key", "test_table_1"); properties.setProperty("leaf.key", "test_table_1");
properties.setProperty("registry.center.type", "zookeeper"); properties.setProperty("registry.center.type", "zookeeper");
leafSegmentKeyGenerator.setProperties(properties); leafSegmentKeyGenerator.setProperties(properties);
List<Comparable<?>> expected = Arrays.<Comparable<?>>asList(100001L, 100002L, 100003L, 100004L, 100005L, 100006L, 100007L, 100008L, 100009L, 100010L); List<Comparable<?>> expected = Arrays.<Comparable<?>>asList(100001L, 100002L, 100003L, 100004L, 100005L, 100006L, 100007L, 100008L, 100009L, 100010L);
List<Comparable<?>> actual = new ArrayList<>(); List<Comparable<?>> actual = new ArrayList<>();
for (int i = 0; i < 10; i++) { for (int i = 0; i < 3; i++) {
actual.add(leafSegmentKeyGenerator.generateKey()); //actual.add(leafSegmentKeyGenerator.generateKey());
System.out.println(leafSegmentKeyGenerator.generateKey());
} }
assertThat(actual, is(expected)); //assertThat(actual, is(expected));
} }
@Test @Test
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论