2019-09-05修改版

This commit is contained in:
qidaijie
2019-09-05 17:26:02 +08:00
parent a5070711f6
commit a8af7eea66
8 changed files with 146 additions and 30 deletions

View File

@@ -60,7 +60,7 @@ public class LogFlowWriteTopology {
builder.setSpout("LogFlowWriteSpout", new CustomizedKafkaSpout(), FlowWriteConfig.SPOUT_PARALLELISM);
builder.setBolt("ConnCompletionBolt", new ConnCompletionBolt(), FlowWriteConfig.DATACENTER_BOLT_PARALLELISM).localOrShuffleGrouping("LogFlowWriteSpout");
builder.setBolt("NtcLogSendBolt", new NtcLogSendBolt(), FlowWriteConfig.KAFKA_BOLT_PARALLELISM).localOrShuffleGrouping("ConnCompletionBolt");
builder.setBolt("SummaryBolt", new SummaryBolt(), 1).localOrShuffleGrouping("NtcLogSendBolt");
// builder.setBolt("SummaryBolt", new SummaryBolt(), 1).localOrShuffleGrouping("NtcLogSendBolt");
}
public static void main(String[] args) throws Exception {

View File

@@ -114,5 +114,10 @@ public class TransFormUtils {
return "";
}
public static void main(String[] args) {
String s = ipLookup.countryLookup("192.168.10.207");
System.out.println(s);
}
}

View File

@@ -105,11 +105,12 @@ public class SnowflakeId {
if (tmpWorkerId > maxWorkerId || tmpWorkerId < 0) {
throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId));
}
if (FlowWriteConfig.DATA_CENTER_ID_NUM > maxDataCenterId || FlowWriteConfig.DATA_CENTER_ID_NUM < 0) {
int dataCenterId = FlowWriteConfig.DATA_CENTER_ID_NUM;
if (dataCenterId > maxDataCenterId || dataCenterId < 0) {
throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDataCenterId));
}
this.workerId = tmpWorkerId;
this.dataCenterId = FlowWriteConfig.DATA_CENTER_ID_NUM;
this.dataCenterId = dataCenterId;
}
// ==============================Methods==========================================

View File

@@ -196,9 +196,13 @@ public class DistributedLock implements Lock, Watcher {
public void run() {
DistributedLock lock = null;
try {
// lock = new DistributedLock(FlowWriteConfig.ZOOKEEPER_SERVERS, "disLocks1");
// lock.lock();
System.out.println(SnowflakeId.generateId());
lock = new DistributedLock(FlowWriteConfig.ZOOKEEPER_SERVERS, "disLocks1");
lock.lock();
// System.out.println(SnowflakeId.generateId());
System.out.println(1);
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
if (lock != null) {
lock.unlock();

View File

@@ -37,16 +37,17 @@ public class ZookeeperUtils implements Watcher {
* @param path 节点路径
*/
public int modifyNode(String path) {
createNode("/Snowflake", null, ZooDefs.Ids.OPEN_ACL_UNSAFE);
createNode("/Snowflake/" + FlowWriteConfig.KAFKA_TOPIC, "0".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE);
int workerId = 0;
int workerId;
try {
connectZookeeper(FlowWriteConfig.ZOOKEEPER_SERVERS);
connectZookeeper();
Stat stat = zookeeper.exists(path, true);
workerId = Integer.parseInt(getNodeDate(path));
if (workerId > 55){
if (workerId > 55) {
workerId = 0;
zookeeper.setData(path, "1".getBytes(), stat.getVersion());
}else {
} else {
String result = String.valueOf(workerId + 1);
if (stat != null) {
zookeeper.setData(path, result.getBytes(), stat.getVersion());
@@ -67,11 +68,10 @@ public class ZookeeperUtils implements Watcher {
/**
* 连接zookeeper
*
* @param host 地址
*/
public void connectZookeeper(String host) {
private void connectZookeeper() {
try {
zookeeper = new ZooKeeper(host, SESSION_TIME_OUT, this);
zookeeper = new ZooKeeper(FlowWriteConfig.ZOOKEEPER_SERVERS, SESSION_TIME_OUT, this);
countDownLatch.await();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
@@ -81,7 +81,7 @@ public class ZookeeperUtils implements Watcher {
/**
* 关闭连接
*/
public void closeConn() {
private void closeConn() {
try {
if (zookeeper != null) {
zookeeper.close();
@@ -97,7 +97,7 @@ public class ZookeeperUtils implements Watcher {
* @param path 节点路径
* @return 内容/异常null
*/
public String getNodeDate(String path) {
private String getNodeDate(String path) {
String result = null;
Stat stat = new Stat();
try {
@@ -115,9 +115,9 @@ public class ZookeeperUtils implements Watcher {
* @param date 节点所存储的数据的byte[]
* @param acls 控制权限策略
*/
public void createNode(String path, byte[] date, List<ACL> acls) {
private void createNode(String path, byte[] date, List<ACL> acls) {
try {
connectZookeeper(FlowWriteConfig.ZOOKEEPER_SERVERS);
connectZookeeper();
Stat exists = zookeeper.exists(path, true);
if (exists == null) {
zookeeper.create(path, date, acls, CreateMode.PERSISTENT);