|
@@ -1,11 +1,11 @@
|
|
|
package com.its.common.cluster.slave;
|
|
|
|
|
|
-import com.its.common.cluster.utils.ClusterMessage;
|
|
|
-import com.its.common.cluster.utils.ClusterMessageData;
|
|
|
-import com.its.common.cluster.utils.ClusterSlaveBootstrapFactory;
|
|
|
-import com.its.common.cluster.utils.ClusterUtils;
|
|
|
-import com.its.common.cluster.vo.HaClusterConfig;
|
|
|
-import com.its.common.cluster.vo.HaClusterInfo;
|
|
|
+import com.its.common.cluster.utils.HaClusterMessage;
|
|
|
+import com.its.common.cluster.utils.AbstractHaClusterMessageData;
|
|
|
+import com.its.common.cluster.utils.HaClusterSlaveBootstrapFactory;
|
|
|
+import com.its.common.cluster.utils.HaUtils;
|
|
|
+import com.its.common.cluster.vo.AbstractHaClusterConfig;
|
|
|
+import com.its.common.cluster.vo.HaInfo;
|
|
|
import com.its.common.cluster.vo.HaNET;
|
|
|
import io.netty.channel.Channel;
|
|
|
import io.netty.channel.ChannelFuture;
|
|
@@ -13,7 +13,6 @@ import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.slf4j.MDC;
|
|
|
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
|
import java.text.SimpleDateFormat;
|
|
@@ -25,20 +24,21 @@ import java.util.concurrent.ScheduledFuture;
|
|
|
|
|
|
@Slf4j
|
|
|
@RequiredArgsConstructor
|
|
|
-public class ClusterSlaveService {
|
|
|
+public abstract class AbstractHaClusterSlaveService {
|
|
|
|
|
|
private final ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
|
|
|
- private ScheduledFuture<?> taskFuture;
|
|
|
+ private final ExecutorService executorService= Executors.newFixedThreadPool(1);
|
|
|
+ private final List<HaClusterSlave> clientTasks = Collections.synchronizedList(new ArrayList<>());
|
|
|
|
|
|
- private final HaClusterConfig clusterConfig;
|
|
|
- private ClusterSlaveBootstrapFactory bootstrapFactory;
|
|
|
+ private final AbstractHaClusterConfig clusterConfig;
|
|
|
+
|
|
|
+ private ScheduledFuture<?> taskFuture;
|
|
|
+ private HaClusterSlaveBootstrapFactory bootstrapFactory;
|
|
|
|
|
|
- private final ExecutorService executorService= Executors.newFixedThreadPool(1);
|
|
|
- private final List<ClusterSlave> clientTasks = Collections.synchronizedList(new ArrayList<>());
|
|
|
|
|
|
@PostConstruct
|
|
|
void init() {
|
|
|
- this.bootstrapFactory = new ClusterSlaveBootstrapFactory(1, 5);
|
|
|
+ this.bootstrapFactory = new HaClusterSlaveBootstrapFactory(1, 5);
|
|
|
this.taskScheduler.setPoolSize(1);
|
|
|
this.taskScheduler.initialize();
|
|
|
}
|
|
@@ -49,12 +49,12 @@ public class ClusterSlaveService {
|
|
|
/**
|
|
|
* Cluster 접속
|
|
|
*/
|
|
|
- for (Map.Entry<Integer, HaClusterInfo> entry : this.clusterConfig.getClusterMap().entrySet()) {
|
|
|
- HaClusterInfo cluster = entry.getValue();
|
|
|
+ for (Map.Entry<Integer, HaInfo> entry : this.clusterConfig.getClusterMap().entrySet()) {
|
|
|
+ HaInfo cluster = entry.getValue();
|
|
|
if (cluster.getServerId() == this.clusterConfig.getServerId()) {
|
|
|
continue;
|
|
|
}
|
|
|
- ClusterSlave slaveClient = new ClusterSlave(this, clusterConfig, cluster, this.bootstrapFactory);
|
|
|
+ HaClusterSlave slaveClient = new HaClusterSlave(this, clusterConfig, cluster, this.bootstrapFactory);
|
|
|
this.clientTasks.add(slaveClient);
|
|
|
}
|
|
|
|
|
@@ -77,8 +77,8 @@ public class ClusterSlaveService {
|
|
|
}
|
|
|
this.taskScheduler.shutdown();
|
|
|
|
|
|
- for (Map.Entry<Integer, HaClusterInfo> entry : this.clusterConfig.getClusterMap().entrySet()) {
|
|
|
- HaClusterInfo cluster = entry.getValue();
|
|
|
+ for (Map.Entry<Integer, HaInfo> entry : this.clusterConfig.getClusterMap().entrySet()) {
|
|
|
+ HaInfo cluster = entry.getValue();
|
|
|
if (cluster.getServerId() == this.clusterConfig.getServerId()) {
|
|
|
continue;
|
|
|
}
|
|
@@ -108,8 +108,12 @@ public class ClusterSlaveService {
|
|
|
Date dtLog = new Date();
|
|
|
return sdfDate.format(dtLog);
|
|
|
}
|
|
|
- private ClusterMessage getClusterMessage() {
|
|
|
- List<ClusterMessageData> details = new ArrayList<>();
|
|
|
+
|
|
|
+ public abstract List<AbstractHaClusterMessageData> getClusterMessageData();
|
|
|
+
|
|
|
+ private HaClusterMessage getClusterMessage() {
|
|
|
+// List<HaClusterMessageData> details = new ArrayList<>();
|
|
|
+ List<AbstractHaClusterMessageData> details = getClusterMessageData();
|
|
|
// List<String> keySet = new ArrayList<>(ApplicationRepository.CENTER_MAP.keySet());
|
|
|
// Collections.sort(keySet);
|
|
|
// for (String key : keySet) {
|
|
@@ -119,7 +123,7 @@ public class ClusterSlaveService {
|
|
|
// }
|
|
|
//// details.add(region.getClusterData());
|
|
|
// }
|
|
|
- return ClusterMessage.builder()
|
|
|
+ return HaClusterMessage.builder()
|
|
|
.serverId(this.clusterConfig.getServerId())
|
|
|
.serverTime(getSysTime())
|
|
|
.infos(details)
|
|
@@ -129,9 +133,9 @@ public class ClusterSlaveService {
|
|
|
private void dataSyncSchedule() {
|
|
|
log.info("ClusterSlaveService:dataSyncSchedule: {} seconds.", this.clusterConfig.getSyncSeconds());
|
|
|
this.taskFuture = this.taskScheduler.scheduleAtFixedRate(() -> {
|
|
|
- ClusterMessage clusterMsg = getClusterMessage();
|
|
|
- for (Map.Entry<Integer, HaClusterInfo> entry : this.clusterConfig.getClusterMap().entrySet()) {
|
|
|
- HaClusterInfo cluster = entry.getValue();
|
|
|
+ HaClusterMessage clusterMsg = getClusterMessage();
|
|
|
+ for (Map.Entry<Integer, HaInfo> entry : this.clusterConfig.getClusterMap().entrySet()) {
|
|
|
+ HaInfo cluster = entry.getValue();
|
|
|
if (cluster.getServerId() == this.clusterConfig.getServerId()) {
|
|
|
continue;
|
|
|
}
|
|
@@ -139,11 +143,12 @@ public class ClusterSlaveService {
|
|
|
sendSyncData(cluster, cluster.getSyncState().getChannel(), clusterMsg);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
}, this.clusterConfig.getSyncSeconds() * 1000L);
|
|
|
|
|
|
}
|
|
|
|
|
|
- public void sendSyncData(final HaClusterInfo cluster, final Channel channel, ClusterMessage clusterMsg) {
|
|
|
+ public void sendSyncData(final HaInfo cluster, final Channel channel, HaClusterMessage clusterMsg) {
|
|
|
if (null == clusterMsg) {
|
|
|
clusterMsg = getClusterMessage();
|
|
|
}
|
|
@@ -153,16 +158,16 @@ public class ClusterSlaveService {
|
|
|
f.awaitUninterruptibly();
|
|
|
if (f.isDone() || f.isSuccess()) {
|
|
|
log.info("ClusterSlaveService.sendSyncData: [{}], {}, [--TO: serverId: {}, serverTime: {}, infos: {}]",
|
|
|
- this.clusterConfig.getServerId(), ClusterUtils.getTcpAddress(channel),
|
|
|
+ this.clusterConfig.getServerId(), HaUtils.getTcpAddress(channel),
|
|
|
clusterMsg.getServerId(), clusterMsg.getServerTime(), clusterMsg.getInfos().size());
|
|
|
}
|
|
|
}
|
|
|
catch (Exception e) {
|
|
|
log.info("ClusterSlaveService.sendSyncData: [{}], {}, Failed: [--TO: serverId: {}, serverTime: {}, infos: {}]",
|
|
|
- this.clusterConfig.getServerId(), ClusterUtils.getTcpAddress(channel),
|
|
|
+ this.clusterConfig.getServerId(), HaUtils.getTcpAddress(channel),
|
|
|
clusterMsg.getServerId(), clusterMsg.getServerTime(), clusterMsg.getInfos().size());
|
|
|
log.info("ClusterSlaveService.sendSyncData: [{}], {}, Failed: {}",
|
|
|
- this.clusterConfig.getServerId(), ClusterUtils.getTcpAddress(channel), e.getMessage());
|
|
|
+ this.clusterConfig.getServerId(), HaUtils.getTcpAddress(channel), e.getMessage());
|
|
|
}
|
|
|
finally {
|
|
|
MDC.remove(cluster.getLogKey());
|