feat: 2.1.0
1. 优化netty启动
This commit is contained in:
parent
3a73a53e0e
commit
4c1d36ac0d
@ -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));
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -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<SocketChannel>() {
|
||||
@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<SocketChannel>() {
|
||||
@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;
|
||||
}
|
||||
}
|
||||
|
@ -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 {
|
||||
|
@ -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);
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user