提交 2493526c authored 作者: 陈世营's avatar 陈世营

【修改】 优化 共用 zookeeper client

上级 87f4f6dc
...@@ -43,6 +43,11 @@ public final class LeafSegmentKeyGenerator implements LeafKeyGenerator { ...@@ -43,6 +43,11 @@ public final class LeafSegmentKeyGenerator implements LeafKeyGenerator {
private long step; private long step;
/**
* 是否第一次加载
*/
private Boolean isInit = true;
@Getter @Getter
@Setter @Setter
private Properties properties = new Properties(); private Properties properties = new Properties();
...@@ -60,17 +65,36 @@ public final class LeafSegmentKeyGenerator implements LeafKeyGenerator { ...@@ -60,17 +65,36 @@ public final class LeafSegmentKeyGenerator implements LeafKeyGenerator {
@Override @Override
public synchronized Comparable<?> generateKey() { public synchronized Comparable<?> generateKey() {
String leafKey = getLeafKey(); String leafKey = getLeafKey();
if (null == leafRegistryCenter) { if (isInit) {
initLeafSegmentKeyGenerator(leafKey); initLeafSegmentKeyGenerator(leafKey);
} else { } else {
increaseIdWhenLeafKeyStoredInCenter(leafKey); increaseIdWhenLeafKeyStoredInCenter(leafKey);
} }
isInit = false;
return id; return id;
} }
private void initLeafSegmentKeyGenerator(final String leafKey) { /**
* 构建zookeeper注册中心
*/
public RegistryCenter buidLeafRegistryCenter() {
RegistryCenterConfiguration leafConfiguration = getRegistryCenterConfiguration(); RegistryCenterConfiguration leafConfiguration = getRegistryCenterConfiguration();
leafRegistryCenter = new RegistryCenterServiceLoader().load(leafConfiguration); leafRegistryCenter = new RegistryCenterServiceLoader().load(leafConfiguration);
return leafRegistryCenter;
}
/**
* 设置zookeeper注册中心(共用注册中心)
*/
public void setLeafRegistryCenter(RegistryCenter registryCenter) {
this.leafRegistryCenter = registryCenter;
}
private void initLeafSegmentKeyGenerator(final String leafKey) {
buidLeafRegistryCenter();
step = getStep(); step = getStep();
id = initializeId(leafKey); id = initializeId(leafKey);
initializeLeafKeyInCenter(leafKey, id, step); initializeLeafKeyInCenter(leafKey, id, step);
......
package com.loit.component.keygen.leaf; package com.loit.component.keygen.leaf;
import com.loit.component.keygen.leaf.core.reg.RegistryCenter;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
...@@ -15,6 +16,8 @@ public class LeafSegmentKeyGeneratorFactory { ...@@ -15,6 +16,8 @@ public class LeafSegmentKeyGeneratorFactory {
private Properties globleProperties = new Properties(); private Properties globleProperties = new Properties();
private RegistryCenter registryCenter;
/** /**
* 根据 leafKey 标识不同的keyGenerator * 根据 leafKey 标识不同的keyGenerator
*/ */
...@@ -36,6 +39,12 @@ public class LeafSegmentKeyGeneratorFactory { ...@@ -36,6 +39,12 @@ public class LeafSegmentKeyGeneratorFactory {
properties.setProperty("registry.center.type", globleProperties.getProperty("registry.center.type", LeafSegmentKeyGenerator.DEFAULT_REGISTRY_CENTER)); properties.setProperty("registry.center.type", globleProperties.getProperty("registry.center.type", LeafSegmentKeyGenerator.DEFAULT_REGISTRY_CENTER));
leafSegmentKeyGenerator.setProperties(properties); leafSegmentKeyGenerator.setProperties(properties);
if (registryCenter == null) {
registryCenter = leafSegmentKeyGenerator.buidLeafRegistryCenter();
} else {
leafSegmentKeyGenerator.setLeafRegistryCenter(registryCenter);
}
keyGeneratorMap.put(leafKey, leafSegmentKeyGenerator); keyGeneratorMap.put(leafKey, leafSegmentKeyGenerator);
} }
......
...@@ -22,9 +22,11 @@ public class LeafSegmentKeyGeneratorFactoryTest { ...@@ -22,9 +22,11 @@ public class LeafSegmentKeyGeneratorFactoryTest {
properties.setProperty("leaf.segment.step", "100"); properties.setProperty("leaf.segment.step", "100");
leafSegmentKeyGeneratorFactory.setGlobleProperties(properties); leafSegmentKeyGeneratorFactory.setGlobleProperties(properties);
for (int i = 0; i < 12; i++) { for (int i = 0; i < 20; i++) {
Comparable<?> t_order_item = leafSegmentKeyGeneratorFactory.getKeyGenerator("t_order_item").generateKey(); for (int j = 0; j < 50; j++) {
System.out.println(t_order_item); Comparable<?> t_order_item = leafSegmentKeyGeneratorFactory.getKeyGenerator("t_order_item" + i).generateKey();
System.out.println("t_order_item" + i + ": " + t_order_item);
}
} }
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论