From 4c1d36ac0dd856b7476efee7ca42f61b58f6cd3a Mon Sep 17 00:00:00 2001 From: byteblogs168 <598092184@qq.com> Date: Mon, 31 Jul 2023 11:04:16 +0800 Subject: [PATCH] =?UTF-8?q?feat:=202.1.0=201.=20=E4=BC=98=E5=8C=96netty?= =?UTF-8?q?=E5=90=AF=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/EasyRetryServerApplication.java | 15 +++++ .../retry/server/server/NettyHttpServer.java | 55 ++++++++++++------- .../handler/ConfigVersionSyncHandler.java | 2 +- .../support/handler/ServerNodeBalance.java | 2 +- .../support/register/ClientRegister.java | 2 +- 5 files changed, 52 insertions(+), 24 deletions(-) diff --git a/easy-retry-server/src/main/java/com/aizuda/easy/retry/server/EasyRetryServerApplication.java b/easy-retry-server/src/main/java/com/aizuda/easy/retry/server/EasyRetryServerApplication.java index 46cf1a311..c3939ad9a 100644 --- a/easy-retry-server/src/main/java/com/aizuda/easy/retry/server/EasyRetryServerApplication.java +++ b/easy-retry-server/src/main/java/com/aizuda/easy/retry/server/EasyRetryServerApplication.java @@ -1,8 +1,13 @@ package com.aizuda.easy.retry.server; +import com.aizuda.easy.retry.server.server.NettyHttpServer; 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.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.scheduling.TaskScheduler; import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; @@ -28,4 +33,14 @@ public class EasyRetryServerApplication { 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)); + } + }; + } } diff --git a/easy-retry-server/src/main/java/com/aizuda/easy/retry/server/server/NettyHttpServer.java b/easy-retry-server/src/main/java/com/aizuda/easy/retry/server/server/NettyHttpServer.java index 4360633f7..f525277b2 100644 --- a/easy-retry-server/src/main/java/com/aizuda/easy/retry/server/server/NettyHttpServer.java +++ b/easy-retry-server/src/main/java/com/aizuda/easy/retry/server/server/NettyHttpServer.java @@ -1,7 +1,7 @@ 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.exception.EasyRetryServerException; import com.aizuda.easy.retry.server.support.Lifecycle; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelFuture; @@ -26,15 +26,15 @@ import org.springframework.stereotype.Component; */ @Component @Slf4j -public class NettyHttpServer implements Lifecycle { +public class NettyHttpServer implements Runnable, Lifecycle { @Autowired private SystemProperties systemProperties; + private Thread thread = null; + private volatile boolean started = false; - /*** - * 服务端启动 - */ - public void runServer() { + @Override + public void run() { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); @@ -42,28 +42,33 @@ public class NettyHttpServer implements Lifecycle { // start server ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(bossGroup, workerGroup) - .channel(NioServerSocketChannel.class) - .option(ChannelOption.SO_BACKLOG, 128) - .childOption(ChannelOption.SO_KEEPALIVE, true) - .childHandler(new ChannelInitializer() { - @Override - public void initChannel(SocketChannel channel) throws Exception { - channel.pipeline() - .addLast(new HttpServerCodec()) - .addLast(new HttpObjectAggregator(5 * 1024 * 1024)) - .addLast(new NettyHttpServerHandler()); - } - }); + .channel(NioServerSocketChannel.class) + .option(ChannelOption.SO_BACKLOG, 128) + .childOption(ChannelOption.SO_KEEPALIVE, true) + .childHandler(new ChannelInitializer() { + @Override + public void initChannel(SocketChannel channel) throws Exception { + channel.pipeline() + .addLast(new HttpServerCodec()) + .addLast(new HttpObjectAggregator(5 * 1024 * 1024)) + .addLast(new NettyHttpServerHandler()); + } + }); // 在特定端口绑定并启动服务器 默认是1788 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(); + } catch (InterruptedException e) { + log.info("--------> easy-retry remoting server stop."); } catch (Exception e) { log.error("--------> easy-retry remoting server error.", e); + started = false; throw new EasyRetryServerException("easy-retry server start error"); } finally { // 当服务器正常关闭时,关闭EventLoopGroups以释放资源。 @@ -73,12 +78,20 @@ public class NettyHttpServer implements Lifecycle { } @Override - public void start() { - runServer(); + public void start() { + thread = new Thread(this); + thread.setDaemon(true); + thread.start(); } @Override public void close() { + if (thread != null && thread.isAlive()) { + thread.interrupt(); + } + } + public boolean isStarted() { + return started; } } diff --git a/easy-retry-server/src/main/java/com/aizuda/easy/retry/server/support/handler/ConfigVersionSyncHandler.java b/easy-retry-server/src/main/java/com/aizuda/easy/retry/server/support/handler/ConfigVersionSyncHandler.java index d1910c752..b7691dd84 100644 --- a/easy-retry-server/src/main/java/com/aizuda/easy/retry/server/support/handler/ConfigVersionSyncHandler.java +++ b/easy-retry-server/src/main/java/com/aizuda/easy/retry/server/support/handler/ConfigVersionSyncHandler.java @@ -98,7 +98,7 @@ public class ConfigVersionSyncHandler implements Lifecycle, Runnable { syncVersion(pair.getKey()); } } catch (InterruptedException e) { - LogUtils.error(log, "[{}] thread interrupt.", Thread.currentThread().getName()); + LogUtils.info(log, "[{}] thread stop.", Thread.currentThread().getName()); } catch (Exception e) { LogUtils.error(log, "client refresh expireAt error.", e); } finally { diff --git a/easy-retry-server/src/main/java/com/aizuda/easy/retry/server/support/handler/ServerNodeBalance.java b/easy-retry-server/src/main/java/com/aizuda/easy/retry/server/support/handler/ServerNodeBalance.java index 47553c31f..5c466dc77 100644 --- a/easy-retry-server/src/main/java/com/aizuda/easy/retry/server/support/handler/ServerNodeBalance.java +++ b/easy-retry-server/src/main/java/com/aizuda/easy/retry/server/support/handler/ServerNodeBalance.java @@ -250,7 +250,7 @@ public class ServerNodeBalance implements Lifecycle, Runnable { } } catch (InterruptedException e) { - LogUtils.error(log, "check balance interrupt"); + LogUtils.info(log, "check balance stop"); Thread.currentThread().interrupt(); } catch (Exception e) { LogUtils.error(log, "check balance error", e); diff --git a/easy-retry-server/src/main/java/com/aizuda/easy/retry/server/support/register/ClientRegister.java b/easy-retry-server/src/main/java/com/aizuda/easy/retry/server/support/register/ClientRegister.java index 10817ecf6..b89178584 100644 --- a/easy-retry-server/src/main/java/com/aizuda/easy/retry/server/support/register/ClientRegister.java +++ b/easy-retry-server/src/main/java/com/aizuda/easy/retry/server/support/register/ClientRegister.java @@ -93,7 +93,7 @@ public class ClientRegister extends AbstractRegister implements Runnable { } } }catch (InterruptedException e) { - LogUtils.error(log, "[{}] thread interrupt.", Thread.currentThread().getName()); + LogUtils.info(log, "[{}] thread stop.", Thread.currentThread().getName()); } catch (Exception e) { LogUtils.error(log, "client refresh expireAt error."); } finally {