客户端增加断线重连支持

This commit is contained in:
aoshiguchen
2022-10-29 23:40:05 +08:00
parent b13dbc687b
commit 41a9ded54a
3 changed files with 46 additions and 1 deletions
@@ -36,6 +36,13 @@ import java.util.function.Consumer;
* @date: 2022/6/28
*/
public class DateUtil {
public static final long SECOND_LONG = 1000L;
public static final long MINUTE_LONG = 60 * SECOND_LONG;
public static final long HOUR_LONG = 60 * MINUTE_LONG;
public static final long DAY_LONG = DAY_2_HOUR * HOUR_LONG;
public static final long MONTH_LONG = 30 * DAY_LONG;
public static final long YEAR_LONG = 365 * DAY_LONG;
private static final Cache<String, SimpleDateFormat> sdfCache = new MemoryCache<>();
private static SimpleDateFormat getSimpleDateFormat(String format) {
try {
@@ -26,6 +26,7 @@ import fun.asgc.neutrino.core.annotation.Autowired;
import fun.asgc.neutrino.core.annotation.Bean;
import fun.asgc.neutrino.core.annotation.Component;
import fun.asgc.neutrino.core.annotation.NonIntercept;
import fun.asgc.neutrino.core.base.CustomThreadFactory;
import fun.asgc.neutrino.core.context.Environment;
import fun.asgc.neutrino.core.util.FileUtil;
import fun.asgc.neutrino.core.util.StringUtil;
@@ -49,6 +50,9 @@ import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import java.io.InputStream;
import java.security.KeyStore;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
* 客户端服务
@@ -69,6 +73,27 @@ public class ProxyClientService {
@Autowired
private Environment environment;
private volatile Channel channel;
/**
* 重连间隔(秒)
*/
private static final long RECONNECT_INTERVAL_SECONDS = 5;
/**
* 重连次数
*/
private volatile int reconnectCount = 0;
/**
* 启用重连服务
*/
private volatile boolean reconnectServiceEnable = true;
/**
* 重连服务执行器
*/
private static final ScheduledExecutorService reconnectExecutor = Executors.newSingleThreadScheduledExecutor(new CustomThreadFactory("ClientReconnect"));
public void ProxyClientService() {
this.reconnectExecutor.scheduleWithFixedDelay(this::reconnect, 0, RECONNECT_INTERVAL_SECONDS, TimeUnit.SECONDS);
}
public void start() {
if (StringUtil.isEmpty(proxyConfig.getLicenseKey())) {
@@ -125,6 +150,8 @@ public class ProxyClientService {
ProxyUtil.setCmdChannel(future.channel());
future.channel().writeAndFlush(ProxyMessage.buildAuthMessage(proxyConfig.getLicenseKey()));
log.info("连接代理服务成功. channelId:{}", future.channel().id().asLongText());
reconnectCount = 0;
} else {
log.info("连接代理服务失败!");
System.exit(-1);
@@ -156,6 +183,17 @@ public class ProxyClientService {
return null;
}
protected synchronized void reconnect() {
if (!reconnectServiceEnable) {
return;
}
if (channel.isActive()) {
return;
}
log.info("客户端重连 seq:{}", ++reconnectCount);
connectProxyServer();
}
@Bean
public Bootstrap bootstrap() {
return new Bootstrap();
@@ -26,7 +26,7 @@
</encoder>
</appender>
<root level="INFO">
<root level="DEBUG">
<appender-ref ref="console"/>
<appender-ref ref="file"/>
</root>