feat: 2.1.0

1. 优化netty启动
This commit is contained in:
byteblogs168 2023-07-31 11:04:16 +08:00
parent 3a73a53e0e
commit 4c1d36ac0d
5 changed files with 52 additions and 24 deletions

View File

@ -1,8 +1,13 @@
package com.aizuda.easy.retry.server; package com.aizuda.easy.retry.server;
import com.aizuda.easy.retry.server.server.NettyHttpServer;
import org.mybatis.spring.annotation.MapperScan; import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.TaskScheduler; import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
@ -28,4 +33,14 @@ public class EasyRetryServerApplication {
SpringApplication.run(EasyRetryServerApplication.class, args); SpringApplication.run(EasyRetryServerApplication.class, args);
} }
@Bean
public ApplicationRunner nettyStartupChecker(NettyHttpServer nettyHttpServer, ServletWebServerFactory serverFactory) {
return args -> {
if (!nettyHttpServer.isStarted()) {
// Netty启动失败停止Web服务和Spring Boot应用程序
serverFactory.getWebServer().stop();
SpringApplication.exit(SpringApplication.run(EasyRetryServerApplication.class));
}
};
}
} }

View File

@ -1,7 +1,7 @@
package com.aizuda.easy.retry.server.server; package com.aizuda.easy.retry.server.server;
import com.aizuda.easy.retry.server.exception.EasyRetryServerException;
import com.aizuda.easy.retry.server.config.SystemProperties; import com.aizuda.easy.retry.server.config.SystemProperties;
import com.aizuda.easy.retry.server.exception.EasyRetryServerException;
import com.aizuda.easy.retry.server.support.Lifecycle; import com.aizuda.easy.retry.server.support.Lifecycle;
import io.netty.bootstrap.ServerBootstrap; import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFuture;
@ -26,15 +26,15 @@ import org.springframework.stereotype.Component;
*/ */
@Component @Component
@Slf4j @Slf4j
public class NettyHttpServer implements Lifecycle { public class NettyHttpServer implements Runnable, Lifecycle {
@Autowired @Autowired
private SystemProperties systemProperties; private SystemProperties systemProperties;
private Thread thread = null;
private volatile boolean started = false;
/*** @Override
* 服务端启动 public void run() {
*/
public void runServer() {
EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup();
@ -58,12 +58,17 @@ public class NettyHttpServer implements Lifecycle {
// 在特定端口绑定并启动服务器 默认是1788 // 在特定端口绑定并启动服务器 默认是1788
ChannelFuture future = bootstrap.bind(systemProperties.getNettyPort()).sync(); ChannelFuture future = bootstrap.bind(systemProperties.getNettyPort()).sync();
log.info("------> easy-retry remoting server start success, nettype = {}, port = {}", NettyHttpServer.class.getName(), systemProperties.getNettyPort()); log.info("------> easy-retry remoting server start success, nettype = {}, port = {}",
NettyHttpServer.class.getName(), systemProperties.getNettyPort());
started = true;
future.channel().closeFuture().sync(); future.channel().closeFuture().sync();
} catch (InterruptedException e) {
log.info("--------> easy-retry remoting server stop.");
} catch (Exception e) { } catch (Exception e) {
log.error("--------> easy-retry remoting server error.", e); log.error("--------> easy-retry remoting server error.", e);
started = false;
throw new EasyRetryServerException("easy-retry server start error"); throw new EasyRetryServerException("easy-retry server start error");
} finally { } finally {
// 当服务器正常关闭时关闭EventLoopGroups以释放资源 // 当服务器正常关闭时关闭EventLoopGroups以释放资源
@ -74,11 +79,19 @@ public class NettyHttpServer implements Lifecycle {
@Override @Override
public void start() { public void start() {
runServer(); thread = new Thread(this);
thread.setDaemon(true);
thread.start();
} }
@Override @Override
public void close() { public void close() {
if (thread != null && thread.isAlive()) {
thread.interrupt();
}
}
public boolean isStarted() {
return started;
} }
} }

View File

@ -98,7 +98,7 @@ public class ConfigVersionSyncHandler implements Lifecycle, Runnable {
syncVersion(pair.getKey()); syncVersion(pair.getKey());
} }
} catch (InterruptedException e) { } catch (InterruptedException e) {
LogUtils.error(log, "[{}] thread interrupt.", Thread.currentThread().getName()); LogUtils.info(log, "[{}] thread stop.", Thread.currentThread().getName());
} catch (Exception e) { } catch (Exception e) {
LogUtils.error(log, "client refresh expireAt error.", e); LogUtils.error(log, "client refresh expireAt error.", e);
} finally { } finally {

View File

@ -250,7 +250,7 @@ public class ServerNodeBalance implements Lifecycle, Runnable {
} }
} catch (InterruptedException e) { } catch (InterruptedException e) {
LogUtils.error(log, "check balance interrupt"); LogUtils.info(log, "check balance stop");
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
} catch (Exception e) { } catch (Exception e) {
LogUtils.error(log, "check balance error", e); LogUtils.error(log, "check balance error", e);

View File

@ -93,7 +93,7 @@ public class ClientRegister extends AbstractRegister implements Runnable {
} }
} }
}catch (InterruptedException e) { }catch (InterruptedException e) {
LogUtils.error(log, "[{}] thread interrupt.", Thread.currentThread().getName()); LogUtils.info(log, "[{}] thread stop.", Thread.currentThread().getName());
} catch (Exception e) { } catch (Exception e) {
LogUtils.error(log, "client refresh expireAt error."); LogUtils.error(log, "client refresh expireAt error.");
} finally { } finally {