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;
|
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));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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();
|
||||||
|
|
||||||
@ -42,28 +42,33 @@ public class NettyHttpServer implements Lifecycle {
|
|||||||
// start server
|
// start server
|
||||||
ServerBootstrap bootstrap = new ServerBootstrap();
|
ServerBootstrap bootstrap = new ServerBootstrap();
|
||||||
bootstrap.group(bossGroup, workerGroup)
|
bootstrap.group(bossGroup, workerGroup)
|
||||||
.channel(NioServerSocketChannel.class)
|
.channel(NioServerSocketChannel.class)
|
||||||
.option(ChannelOption.SO_BACKLOG, 128)
|
.option(ChannelOption.SO_BACKLOG, 128)
|
||||||
.childOption(ChannelOption.SO_KEEPALIVE, true)
|
.childOption(ChannelOption.SO_KEEPALIVE, true)
|
||||||
.childHandler(new ChannelInitializer<SocketChannel>() {
|
.childHandler(new ChannelInitializer<SocketChannel>() {
|
||||||
@Override
|
@Override
|
||||||
public void initChannel(SocketChannel channel) throws Exception {
|
public void initChannel(SocketChannel channel) throws Exception {
|
||||||
channel.pipeline()
|
channel.pipeline()
|
||||||
.addLast(new HttpServerCodec())
|
.addLast(new HttpServerCodec())
|
||||||
.addLast(new HttpObjectAggregator(5 * 1024 * 1024))
|
.addLast(new HttpObjectAggregator(5 * 1024 * 1024))
|
||||||
.addLast(new NettyHttpServerHandler());
|
.addLast(new NettyHttpServerHandler());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// 在特定端口绑定并启动服务器 默认是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以释放资源。
|
||||||
@ -73,12 +78,20 @@ 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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 {
|
||||||
|
@ -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);
|
||||||
|
@ -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 {
|
||||||
|
Loading…
Reference in New Issue
Block a user