From 3989bd9c5fbb9b112907630baf28e454d9405230 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90Li?= <15040126243@163.com> Date: Wed, 20 Mar 2024 20:08:59 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E4=BF=AE=E5=A4=8D=20easyretry=20?= =?UTF-8?q?=E6=97=A0=E6=B3=95=E7=9B=91=E6=8E=A7=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../starter/server/NettyHttpServer.java | 102 ++++++++++++++++++ .../src/main/resources/application.yml | 3 + .../src/main/resources/logback-common.xml | 97 +++++++++++++++++ 3 files changed, 202 insertions(+) create mode 100644 ruoyi-visual/ruoyi-easyretry-server/src/main/java/com/aizuda/easy/retry/server/starter/server/NettyHttpServer.java create mode 100644 ruoyi-visual/ruoyi-easyretry-server/src/main/resources/logback-common.xml diff --git a/ruoyi-visual/ruoyi-easyretry-server/src/main/java/com/aizuda/easy/retry/server/starter/server/NettyHttpServer.java b/ruoyi-visual/ruoyi-easyretry-server/src/main/java/com/aizuda/easy/retry/server/starter/server/NettyHttpServer.java new file mode 100644 index 00000000..94cbe224 --- /dev/null +++ b/ruoyi-visual/ruoyi-easyretry-server/src/main/java/com/aizuda/easy/retry/server/starter/server/NettyHttpServer.java @@ -0,0 +1,102 @@ +package com.aizuda.easy.retry.server.starter.server; + +import com.aizuda.easy.retry.common.log.EasyRetryLog; +import com.aizuda.easy.retry.server.common.config.SystemProperties; +import com.aizuda.easy.retry.server.common.exception.EasyRetryServerException; +import com.aizuda.easy.retry.server.common.Lifecycle; +import io.netty.bootstrap.ServerBootstrap; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.ChannelOption; +import io.netty.channel.EventLoopGroup; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.SocketChannel; +import io.netty.channel.socket.nio.NioServerSocketChannel; +import io.netty.handler.codec.http.HttpObjectAggregator; +import io.netty.handler.codec.http.HttpServerCodec; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; +import org.springframework.stereotype.Component; + +/** + * netty server + * + * @author: www.byteblogs.com + * @date : 2022-03-07 15:54 + * @since 1.0.0 + */ +@Component +@Order(Ordered.HIGHEST_PRECEDENCE) +public class NettyHttpServer implements Runnable, Lifecycle { + + @Autowired + private SystemProperties systemProperties; + private Thread thread = null; + private volatile boolean started = false; + + @Override + public void run() { + EventLoopGroup bossGroup = new NioEventLoopGroup(); + EventLoopGroup workerGroup = new NioEventLoopGroup(); + + try { + // 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()); + } + }); + + // 在特定端口绑定并启动服务器 默认是1788 + ChannelFuture future = bootstrap.bind(systemProperties.getNettyPort()).sync(); + + EasyRetryLog.LOCAL.info("------> easy-retry remoting server start success, nettype = {}, port = {}", + NettyHttpServer.class.getName(), systemProperties.getNettyPort()); + + started = true; + future.channel().closeFuture().sync(); + + } catch (InterruptedException e) { + EasyRetryLog.LOCAL.info("--------> easy-retry remoting server stop."); + } catch (Exception e) { + EasyRetryLog.LOCAL.error("--------> easy-retry remoting server error.", e); + started = false; + throw new EasyRetryServerException("easy-retry server start error"); + } finally { + // 当服务器正常关闭时,关闭EventLoopGroups以释放资源。 + workerGroup.shutdownGracefully(); + bossGroup.shutdownGracefully(); + } + } + + @Override + public void start() { + if (isStarted()) { + return; + } + 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/ruoyi-visual/ruoyi-easyretry-server/src/main/resources/application.yml b/ruoyi-visual/ruoyi-easyretry-server/src/main/resources/application.yml index f8ea995e..eb268f05 100644 --- a/ruoyi-visual/ruoyi-easyretry-server/src/main/resources/application.yml +++ b/ruoyi-visual/ruoyi-easyretry-server/src/main/resources/application.yml @@ -28,6 +28,9 @@ logging: config: classpath:logback-plus.xml management: + # 解决 er 服务有 context-path 无法监控问题 + server: + port: 8801 endpoints: web: exposure: diff --git a/ruoyi-visual/ruoyi-easyretry-server/src/main/resources/logback-common.xml b/ruoyi-visual/ruoyi-easyretry-server/src/main/resources/logback-common.xml new file mode 100644 index 00000000..89eaa97e --- /dev/null +++ b/ruoyi-visual/ruoyi-easyretry-server/src/main/resources/logback-common.xml @@ -0,0 +1,97 @@ + + + + + + + + + ${log.path}/console.log + + + ${log.path}/console.%d{yyyy-MM-dd}.log + + 1 + + + ${log.pattern} + utf-8 + + + + INFO + + + + + + ${log.path}/info.log + + + + ${log.path}/info.%d{yyyy-MM-dd}.log + + 60 + + + ${log.pattern} + + + + INFO + + ACCEPT + + DENY + + + + + ${log.path}/error.log + + + + ${log.path}/error.%d{yyyy-MM-dd}.log + + 60 + + + ${log.pattern} + + + + ERROR + + ACCEPT + + DENY + + + + + + + 0 + + 512 + + + + + + + + 0 + + 512 + + + + + + + + + + +