feat: 3.1.1
1. 优化客户端重连逻辑 2. 新增netty重连事件、系统启动开始事件、系统启动完成事件、系统开始关闭事件、系统关闭完成事件
This commit is contained in:
parent
dacae6ebb7
commit
479d5c5ac5
@ -0,0 +1,15 @@
|
||||
package com.aizuda.easy.retry.client.common.event;
|
||||
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
|
||||
/**
|
||||
* @author xiaowoniu
|
||||
* @date 2024-03-14 21:17:55
|
||||
* @since 3.1.1
|
||||
*/
|
||||
public class ChannelReconnectEvent extends ApplicationEvent {
|
||||
private static final String SOURCE = "ChannelReconnect";
|
||||
public ChannelReconnectEvent() {
|
||||
super(SOURCE);
|
||||
}
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package com.aizuda.easy.retry.client.common.event;
|
||||
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
|
||||
/**
|
||||
* @author xiaowoniu
|
||||
* @date 2024-03-14 21:23:29
|
||||
* @since 3.1.0
|
||||
*/
|
||||
public class EasyRetryClosedEvent extends ApplicationEvent {
|
||||
private static final String SOURCE = "EasyRetryClosed";
|
||||
|
||||
public EasyRetryClosedEvent() {
|
||||
super(SOURCE);
|
||||
}
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package com.aizuda.easy.retry.client.common.event;
|
||||
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
|
||||
/**
|
||||
* @author xiaowoniu
|
||||
* @date 2024-03-14 21:23:29
|
||||
* @since 3.1.0
|
||||
*/
|
||||
public class EasyRetryClosingEvent extends ApplicationEvent {
|
||||
private static final String SOURCE = "EasyRetryClosing";
|
||||
|
||||
public EasyRetryClosingEvent() {
|
||||
super(SOURCE);
|
||||
}
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package com.aizuda.easy.retry.client.common.event;
|
||||
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
|
||||
/**
|
||||
* @author xiaowoniu
|
||||
* @date 2024-03-14 21:23:29
|
||||
* @since 3.1.0
|
||||
*/
|
||||
public class EasyRetryStartedEvent extends ApplicationEvent {
|
||||
private static final String SOURCE = "EasyRetryStarted";
|
||||
|
||||
public EasyRetryStartedEvent() {
|
||||
super(SOURCE);
|
||||
}
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package com.aizuda.easy.retry.client.common.event;
|
||||
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
|
||||
/**
|
||||
* @author xiaowoniu
|
||||
* @date 2024-03-14 21:23:29
|
||||
* @since 3.1.0
|
||||
*/
|
||||
public class EasyRetryStartingEvent extends ApplicationEvent {
|
||||
private static final String SOURCE = "EasyRetryStarting";
|
||||
|
||||
public EasyRetryStartingEvent() {
|
||||
super(SOURCE);
|
||||
}
|
||||
}
|
@ -1,6 +1,10 @@
|
||||
package com.aizuda.easy.retry.client.common.init;
|
||||
|
||||
import com.aizuda.easy.retry.client.common.Lifecycle;
|
||||
import com.aizuda.easy.retry.client.common.event.EasyRetryClosedEvent;
|
||||
import com.aizuda.easy.retry.client.common.event.EasyRetryClosingEvent;
|
||||
import com.aizuda.easy.retry.client.common.event.EasyRetryStartingEvent;
|
||||
import com.aizuda.easy.retry.common.core.context.SpringContext;
|
||||
import com.aizuda.easy.retry.common.core.util.EasyRetryVersion;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@ -19,7 +23,7 @@ import java.util.List;
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
public class EasyRetryEndListener implements ApplicationListener<ContextClosedEvent> {
|
||||
public class EasyRetryCloseListener implements ApplicationListener<ContextClosedEvent> {
|
||||
|
||||
@Autowired
|
||||
private List<Lifecycle> lifecycleList;
|
||||
@ -27,7 +31,9 @@ public class EasyRetryEndListener implements ApplicationListener<ContextClosedEv
|
||||
@Override
|
||||
public void onApplicationEvent(ContextClosedEvent event) {
|
||||
log.info("Easy-Retry client about to shutdown v{}", EasyRetryVersion.getVersion());
|
||||
SpringContext.CONTEXT.publishEvent(new EasyRetryClosingEvent());
|
||||
lifecycleList.forEach(Lifecycle::close);
|
||||
SpringContext.CONTEXT.publishEvent(new EasyRetryClosedEvent());
|
||||
log.info("Easy-Retry client closed successfully v{}", EasyRetryVersion.getVersion());
|
||||
}
|
||||
}
|
@ -1,10 +1,12 @@
|
||||
package com.aizuda.easy.retry.client.common.init;
|
||||
|
||||
import com.aizuda.easy.retry.client.common.Lifecycle;
|
||||
import com.aizuda.easy.retry.client.common.event.EasyRetryStartedEvent;
|
||||
import com.aizuda.easy.retry.client.common.event.EasyRetryStartingEvent;
|
||||
import com.aizuda.easy.retry.common.core.constant.SystemConstants;
|
||||
import com.aizuda.easy.retry.common.core.context.SpringContext;
|
||||
import com.aizuda.easy.retry.common.core.util.EasyRetryVersion;
|
||||
import com.aizuda.easy.retry.common.log.EasyRetryLog;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.slf4j.helpers.MessageFormatter;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
@ -30,7 +32,9 @@ public class EasyRetryStartListener implements ApplicationRunner {
|
||||
System.out.println(MessageFormatter.format(SystemConstants.LOGO, EasyRetryVersion.getVersion()).getMessage());
|
||||
|
||||
EasyRetryLog.LOCAL.info("Easy-Retry client is preparing to start... v{}", EasyRetryVersion.getVersion());
|
||||
SpringContext.CONTEXT.publishEvent(new EasyRetryStartingEvent());
|
||||
lifecycleList.forEach(Lifecycle::start);
|
||||
SpringContext.CONTEXT.publishEvent(new EasyRetryStartedEvent());
|
||||
EasyRetryLog.LOCAL.info("Easy-Retry client started successfully v{}", EasyRetryVersion.getVersion());
|
||||
}
|
||||
|
||||
|
@ -31,20 +31,71 @@ import java.util.Optional;
|
||||
*/
|
||||
@Slf4j
|
||||
public class NettyChannel {
|
||||
|
||||
private NettyChannel() {
|
||||
}
|
||||
|
||||
private static Channel CHANNEL;
|
||||
|
||||
/**
|
||||
* 服务端端口
|
||||
*/
|
||||
private static final String EASY_RETRY_SERVER_PORT = "easy-retry.server.port";
|
||||
/**
|
||||
* 服务端host
|
||||
*/
|
||||
private static final String EASY_RETRY_SERVER_HOST = "easy-retry.server.host";
|
||||
|
||||
/**
|
||||
* 客户端端口
|
||||
*/
|
||||
private static final String EASY_RETRY_CLIENT_PORT = "easy-retry.port";
|
||||
/**
|
||||
* 客户端host
|
||||
*/
|
||||
private static final String EASY_RETRY_CLIENT_HOST = "easy-retry.host";
|
||||
|
||||
private static final String HOST_ID = IdUtil.getSnowflake().nextIdStr();
|
||||
private static final int PORT;
|
||||
private static final String HOST;
|
||||
|
||||
static {
|
||||
PORT = Integer.parseInt(System.getProperty("easy-retry.port", String.valueOf(8080)));
|
||||
HOST = System.getProperty("easy-retry.host", NetUtil.getLocalIpStr());
|
||||
PORT = Integer.parseInt(System.getProperty(EASY_RETRY_CLIENT_PORT, String.valueOf(8080)));
|
||||
HOST = System.getProperty(EASY_RETRY_CLIENT_HOST, NetUtil.getLocalIpStr());
|
||||
}
|
||||
|
||||
private static Channel CHANNEL;
|
||||
/**
|
||||
* 获取服务端端口
|
||||
*
|
||||
* @return port
|
||||
*/
|
||||
public static int getServerPort() {
|
||||
EasyRetryProperties easyRetryProperties = SpringContext.CONTEXT.getBean(EasyRetryProperties.class);
|
||||
EasyRetryProperties.ServerConfig serverConfig = easyRetryProperties.getServer();
|
||||
|
||||
String port = System.getProperty(EASY_RETRY_SERVER_PORT);
|
||||
if (StrUtil.isBlank(port)) {
|
||||
System.setProperty(EASY_RETRY_SERVER_PORT, String.valueOf(serverConfig.getPort()));
|
||||
}
|
||||
|
||||
return Integer.parseInt(System.getProperty(EASY_RETRY_SERVER_PORT));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取服务端host
|
||||
*
|
||||
* @return host
|
||||
*/
|
||||
public static String getServerHost() {
|
||||
EasyRetryProperties easyRetryProperties = SpringContext.CONTEXT.getBean(EasyRetryProperties.class);
|
||||
EasyRetryProperties.ServerConfig serverConfig = easyRetryProperties.getServer();
|
||||
|
||||
String host = System.getProperty(EASY_RETRY_SERVER_HOST);
|
||||
if (StrUtil.isBlank(host)) {
|
||||
System.setProperty(EASY_RETRY_SERVER_HOST, serverConfig.getHost());
|
||||
}
|
||||
|
||||
return System.getProperty(EASY_RETRY_SERVER_HOST);
|
||||
}
|
||||
|
||||
public static void setChannel(Channel channel) {
|
||||
NettyChannel.CHANNEL = channel;
|
||||
|
@ -1,8 +1,10 @@
|
||||
package com.aizuda.easy.retry.client.common.netty;
|
||||
|
||||
import com.aizuda.easy.retry.client.common.event.ChannelReconnectEvent;
|
||||
import com.aizuda.easy.retry.client.common.proxy.RequestBuilder;
|
||||
import com.aizuda.easy.retry.client.common.NettyClient;
|
||||
import com.aizuda.easy.retry.common.core.constant.SystemConstants.BEAT;
|
||||
import com.aizuda.easy.retry.common.core.context.SpringContext;
|
||||
import com.aizuda.easy.retry.common.log.EasyRetryLog;
|
||||
import com.aizuda.easy.retry.common.core.model.NettyResult;
|
||||
import com.aizuda.easy.retry.common.core.util.JsonUtil;
|
||||
@ -64,6 +66,8 @@ public class NettyHttpClientHandler extends SimpleChannelInboundHandler<FullHttp
|
||||
EasyRetryLog.LOCAL.debug("channelUnregistered");
|
||||
ctx.channel().eventLoop().schedule(() -> {
|
||||
try {
|
||||
// 抛出重连事件
|
||||
SpringContext.CONTEXT.publishEvent(new ChannelReconnectEvent());
|
||||
nettyHttpConnectClient.reconnect();
|
||||
} catch (Exception e) {
|
||||
EasyRetryLog.LOCAL.error("reconnect error ", e);
|
||||
|
@ -42,13 +42,10 @@ public class NettyHttpConnectClient implements Lifecycle, ApplicationContextAwar
|
||||
public void start() {
|
||||
|
||||
try {
|
||||
EasyRetryProperties easyRetryProperties = applicationContext.getBean(EasyRetryProperties.class);
|
||||
|
||||
EasyRetryProperties.ServerConfig server = easyRetryProperties.getServer();
|
||||
final NettyHttpConnectClient thisClient = this;
|
||||
bootstrap.group(nioEventLoopGroup)
|
||||
.channel(NioSocketChannel.class)
|
||||
.remoteAddress(server.getHost(), server.getPort())
|
||||
.remoteAddress(NettyChannel.getServerHost(), NettyChannel.getServerPort())
|
||||
.handler(new ChannelInitializer<SocketChannel>() {
|
||||
@Override
|
||||
public void initChannel(SocketChannel channel) throws Exception {
|
||||
@ -108,7 +105,9 @@ public class NettyHttpConnectClient implements Lifecycle, ApplicationContextAwar
|
||||
* 重连
|
||||
*/
|
||||
public void reconnect() {
|
||||
ChannelFuture channelFuture = bootstrap.connect();
|
||||
ChannelFuture channelFuture = bootstrap
|
||||
.remoteAddress(NettyChannel.getServerHost(), NettyChannel.getServerPort())
|
||||
.connect();
|
||||
channelFuture.addListener((ChannelFutureListener) future -> {
|
||||
Throwable cause = future.cause();
|
||||
if (cause != null) {
|
||||
|
2
pom.xml
2
pom.xml
@ -21,7 +21,7 @@
|
||||
<java.version>17</java.version>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<revision>3.1.0</revision>
|
||||
<revision>3.1.1-SNAPSHOT</revision>
|
||||
<dingding-talk.version>1.0.0</dingding-talk.version>
|
||||
<netty-all.version>4.1.94.Final</netty-all.version>
|
||||
<hutool-all.version>5.8.25</hutool-all.version>
|
||||
|
Loading…
Reference in New Issue
Block a user