Commit 2887e583 authored by zhiguo.liu's avatar zhiguo.liu

# 添加在 ID 生成器中通过 value 注解注入 dataCenterId .

parent 3e966f73
......@@ -2,6 +2,7 @@ package cn.quantgroup.xyqb.Utils;
import com.google.common.base.Stopwatch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
......@@ -14,7 +15,11 @@ import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
/**
* Created by liuzhiguo1 on 2017/5/18.
* 注意事项:
* 1. 使用前在配置文件内配置 DATA_CENTER_ID
*
*
* Created by zhiguo.liu on 2017/5/18.
*/
@Component
public class IDGenerator {
......@@ -41,10 +46,10 @@ public class IDGenerator {
// data center 字符串
private static String DATA_CENTER_STR;
@Autowired
private StringRedisTemplate redis;
@Value("${data.center.id}")
public static void setDataCenterId(int dataCenterId) {
DATA_CENTER_ID = dataCenterId;
}
......@@ -83,26 +88,4 @@ public class IDGenerator {
return prefix + CURRENT_TIME_STR + DATA_CENTER_STR + WORKER_ID_STR + String.format("%06d", ++COUNTER);
}
public static void main(String[] args) throws InterruptedException {
WORKER_ID = 1;
WORKER_ID_STR = String.format("%04d", WORKER_ID);
DATA_CENTER_STR = String.format("%03d", DATA_CENTER_ID);
Stopwatch watch = Stopwatch.createStarted();
CountDownLatch countDownLatch = new CountDownLatch(100);
ConcurrentHashMap hashMap = new ConcurrentHashMap();
for (int i = 0; i < 100; i++) {
new Thread(() -> {
for (int j = 0; j < 1000; j++) {
hashMap.put(getId("BT"), "xx");
}
countDownLatch.countDown();
}).start();
}
countDownLatch.await();
watch.stop();
System.out.println(hashMap.size() + ", " + watch.elapsed(TimeUnit.MILLISECONDS) + "ms");
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment