删除原测试客户端

This commit is contained in:
wenc000 2020-08-02 23:26:43 +08:00
parent bb4ff95a3e
commit 9e5c18158a
8 changed files with 0 additions and 443 deletions

View File

@ -1,23 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>cm-cloud</artifactId>
<groupId>com.cm</groupId>
<version>1.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-common-socket-client</artifactId>
<dependencies>
<dependency>
<groupId>com.cm</groupId>
<artifactId>cloud-common-socket</artifactId>
<version>1.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>

View File

@ -1,111 +0,0 @@
package com.cm.socket.client;
import com.cm.socket.client.config.MessageSocketClientConfig;
import com.cm.socket.client.handler.MessageClientChannelInitializer;
import com.cm.socket.client.service.AbstractMessageClientService;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
/**
* When you feel like quitting. Think about why you started
* 当你想要放弃的时候想想当初你为何开始
*
* @ClassName: MessageSocketClient
* @Description: 消息客户端
* @Author: WangGeng
* @Date: 2020/7/5 10:03 下午
* @Version: 1.0
**/
public class MessageSocketClient implements Runnable {
private static final MessageSocketClient messageSocketClient = MessageSocketClientBuilder.messageSocketClient;
private AbstractMessageClientService abstractMessageClientService;
private MessageSocketClientConfig messageSocketClientConfig;
private int currentReconnectCount = 1;
private int currentReconnectTime = 1;
EventLoopGroup eventLoopGroup;
Bootstrap bootstrap;
private MessageSocketClient() {
this.eventLoopGroup = new NioEventLoopGroup();
this.bootstrap = new Bootstrap();
this.bootstrap.group(eventLoopGroup);
}
public static MessageSocketClient getInstance() {
return messageSocketClient;
}
public void initClient(AbstractMessageClientService abstractMessageClientService, MessageSocketClientConfig messageSocketClientConfig) {
this.messageSocketClientConfig = messageSocketClientConfig;
this.abstractMessageClientService = abstractMessageClientService;
this.bootstrap.channel(NioSocketChannel.class);
this.bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
this.bootstrap.handler(new MessageClientChannelInitializer(this.abstractMessageClientService, this.messageSocketClientConfig));
}
@Override
public void run() {
ChannelFuture channelFuture = this.bootstrap.connect(messageSocketClientConfig.getHost(), messageSocketClientConfig.getPort());
channelFuture.addListener((ChannelFutureListener) future -> {
if (!future.isSuccess()) {
future.channel().pipeline().fireChannelInactive();
}
});
channelFuture.channel().closeFuture();
}
private static class MessageSocketClientBuilder {
public static final MessageSocketClient messageSocketClient = new MessageSocketClient();
}
public static void main(String[] args) {
MessageSocketClientConfig messageSocketClientConfig = new MessageSocketClientConfig();
messageSocketClientConfig.setHost("127.0.0.1");
messageSocketClientConfig.setPort(8888);
messageSocketClientConfig.setDelayPingSeconds(3);
messageSocketClientConfig.setMaxReconnectCount(5);
messageSocketClientConfig.setReconnectTimeStep(1);
AbstractMessageClientService abstractMessageClientService = new AbstractMessageClientService() {
@Override
public void readMessage(String message) {
System.out.println("读取数据:" + message);
}
};
MessageSocketClient messageSocketClient = MessageSocketClient.getInstance();
messageSocketClient.initClient(abstractMessageClientService, messageSocketClientConfig);
messageSocketClient.run();
new Thread(() -> {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
abstractMessageClientService.sendMessage("我是客户端");
}).run();
}
public int getCurrentReconnectCount() {
return currentReconnectCount <= 1 ? 1 : currentReconnectCount;
}
public void setCurrentReconnectCount(int currentReconnectCount) {
this.currentReconnectCount = currentReconnectCount;
}
public int getCurrentReconnectTime() {
return currentReconnectTime <= 1 ? 1 : currentReconnectTime;
}
public void setCurrentReconnectTime(int currentReconnectTime) {
this.currentReconnectTime = currentReconnectTime;
}
}

View File

@ -1,80 +0,0 @@
package com.cm.socket.client.config;
import org.springframework.stereotype.Component;
/**
* When you feel like quitting. Think about why you started
* 当你想要放弃的时候想想当初你为何开始
*
* @ClassName: MessageSocketClientConfig
* @Description: 消息客户端配置
* @Author: WangGeng
* @Date: 2020/7/7 11:04 下午
* @Version: 1.0
**/
@Component
public class MessageSocketClientConfig {
private String host;
private int port;
private int maxReconnectCount;
private int reconnectTimeStep;
private int delayPingSeconds;
public String getHost() {
return host == null ? "" : host.trim();
}
public void setHost(String host) {
this.host = host;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public int getMaxReconnectCount() {
return maxReconnectCount <= 0 ? 20 : maxReconnectCount;
}
public void setMaxReconnectCount(int maxReconnectCount) {
this.maxReconnectCount = maxReconnectCount;
}
public int getReconnectTimeStep() {
return reconnectTimeStep <= 0 ? 0 : reconnectTimeStep;
}
public void setReconnectTimeStep(int reconnectTimeStep) {
this.reconnectTimeStep = reconnectTimeStep;
}
public int getDelayPingSeconds() {
return delayPingSeconds <= 0 ? 3 : delayPingSeconds;
}
public void setDelayPingSeconds(int delayPingSeconds) {
this.delayPingSeconds = delayPingSeconds;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("{");
sb.append("\"host\":")
.append("\"").append(host).append("\"");
sb.append(",\"port\":")
.append(port);
sb.append(",\"maxReconnectCount\":")
.append(maxReconnectCount);
sb.append(",\"reconnectTimeStep\":")
.append(reconnectTimeStep);
sb.append(",\"delayPingSeconds\":")
.append(delayPingSeconds);
sb.append('}');
return sb.toString();
}
}

View File

@ -1,39 +0,0 @@
package com.cm.socket.client.handler;
import com.cm.socket.client.MessageSocketClient;
import com.cm.socket.client.config.MessageSocketClientConfig;
import com.cm.socket.client.service.AbstractMessageClientService;
import com.cm.socket.decoder.MessageDecoder;
import com.cm.socket.encoder.MessageEncoder;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
/**
* When you feel like quitting. Think about why you started
* 当你想要放弃的时候想想当初你为何开始
*
* @ClassName: MessageClientChannelInitializer
* @Description: 消息客户端初始化
* @Author: WangGeng
* @Date: 2020/7/6 9:32 上午
* @Version: 1.0
**/
public class MessageClientChannelInitializer extends ChannelInitializer<SocketChannel> {
private AbstractMessageClientService abstractMessageClientService;
private MessageSocketClientConfig messageSocketClientConfig;
public MessageClientChannelInitializer(AbstractMessageClientService abstractMessageClientService, MessageSocketClientConfig messageSocketClientConfig) {
this.abstractMessageClientService = abstractMessageClientService;
this.messageSocketClientConfig = messageSocketClientConfig;
}
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new MessageEncoder());
ch.pipeline().addLast(new MessageDecoder());
ch.pipeline().addLast(new MessageClientPingHandler(MessageSocketClient.getInstance(), messageSocketClientConfig));
ch.pipeline().addLast(new MessageClientHandler(abstractMessageClientService));
}
}

View File

@ -1,37 +0,0 @@
package com.cm.socket.client.handler;
import com.cm.socket.client.service.AbstractMessageClientService;
import com.cm.socket.pojo.Message;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
/**
* When you feel like quitting. Think about why you started
* 当你想要放弃的时候想想当初你为何开始
*
* @ClassName: MessageClientHandler
* @Description: 消息客户端处理器
* @Author: WangGeng
* @Date: 2020/7/6 9:34 上午
* @Version: 1.0
**/
public class MessageClientHandler extends SimpleChannelInboundHandler<Message> {
private AbstractMessageClientService abstractMessageClientService;
public MessageClientHandler(AbstractMessageClientService abstractMessageClientService) {
this.abstractMessageClientService = abstractMessageClientService;
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
super.channelActive(ctx);
abstractMessageClientService.setChannelHandlerContext(ctx);
}
@Override
protected void channelRead0(ChannelHandlerContext ctx, Message msg) throws Exception {
abstractMessageClientService.readMessage(msg.getContent());
}
}

View File

@ -1,105 +0,0 @@
package com.cm.socket.client.handler;
import com.cm.socket.client.MessageSocketClient;
import com.cm.socket.client.config.MessageSocketClientConfig;
import com.cm.socket.enums.SocketMessageEnum;
import com.cm.socket.enums.SocketTypeMessageEnum;
import com.cm.socket.pojo.Message;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
/**
* When you feel like quitting. Think about why you started
* 当你想要放弃的时候想想当初你为何开始
*
* @ClassName: MessageClientPingHandler
* @Description: 客户端ping
* @Author: WangGeng
* @Date: 2020/7/6 9:43 上午
* @Version: 1.0
**/
public class MessageClientPingHandler extends SimpleChannelInboundHandler<Message> {
private MessageSocketClientConfig messageSocketClientConfig;
private MessageSocketClient messageSocketClient;
public MessageClientPingHandler(MessageSocketClient messageSocketClient, MessageSocketClientConfig messageSocketClientConfig) {
this.messageSocketClient = messageSocketClient;
this.messageSocketClientConfig = messageSocketClientConfig;
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println("服务器连接成功");
messageSocketClient.setCurrentReconnectCount(1);
messageSocketClient.setCurrentReconnectTime(1);
super.channelActive(ctx);
ping(ctx.channel());
}
@Override
protected void channelRead0(ChannelHandlerContext ctx, Message msg) throws Exception {
if (msg.getStart() != SocketTypeMessageEnum.MESSAGE_TYPE_START.getType()) {
return;
}
if (msg.getType() == SocketTypeMessageEnum.MESSAGE_TYPE_PONG.getType()) {
System.out.println("server pong");
return;
}
ctx.fireChannelRead(msg);
}
/**
* ping
*
* @param channel
*/
private void ping(Channel channel) {
ScheduledFuture<?> scheduledFuture = channel.eventLoop().schedule(() -> {
if (channel.isActive()) {
Message ping = new Message();
ping.setStart(SocketTypeMessageEnum.MESSAGE_TYPE_START.getType());
ping.setType(SocketTypeMessageEnum.MESSAGE_TYPE_PING.getType());
ping.setContent(SocketMessageEnum.MESSAGE_PING.getMessage());
channel.writeAndFlush(ping);
} else {
channel.closeFuture();
throw new RuntimeException();
}
}, messageSocketClientConfig.getDelayPingSeconds(), TimeUnit.SECONDS);
scheduledFuture.addListener(future -> {
if (future.isSuccess()) {
ping(channel);
}
});
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
System.err.println("连接断开");
int currentReconnectCount = messageSocketClient.getCurrentReconnectCount();
int waitTime = messageSocketClient.getCurrentReconnectTime();
if (messageSocketClient.getCurrentReconnectCount() > this.messageSocketClientConfig.getMaxReconnectCount()) {
System.out.println("服务器重连失败");
throw new RuntimeException();
}
System.out.println(waitTime + "s后进行第" + currentReconnectCount + "次重连");
ctx.channel().eventLoop().schedule(() -> {
MessageSocketClient.getInstance().run();
}, waitTime, TimeUnit.SECONDS);
messageSocketClient.setCurrentReconnectCount(++currentReconnectCount);
if (messageSocketClientConfig.getReconnectTimeStep() > 0) {
messageSocketClient.setCurrentReconnectTime(waitTime + messageSocketClientConfig.getReconnectTimeStep());
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
}

View File

@ -1,47 +0,0 @@
package com.cm.socket.client.service;
import com.cm.socket.enums.SocketTypeMessageEnum;
import com.cm.socket.pojo.Message;
import io.netty.channel.ChannelHandlerContext;
/**
* When you feel like quitting. Think about why you started
* 当你想要放弃的时候想想当初你为何开始
*
* @ClassName: IMessageClientService
* @Description: 客户端消息业务接口
* @Author: WangGeng
* @Date: 2020/7/6 10:50 上午
* @Version: 1.0
**/
public abstract class AbstractMessageClientService {
protected ChannelHandlerContext channelHandlerContext;
/**
* 发送消息
*
* @param sendMessage
*/
public void sendMessage(String sendMessage) {
if (sendMessage == null || sendMessage.isEmpty()) {
return;
}
Message message = new Message();
message.setStart(SocketTypeMessageEnum.MESSAGE_TYPE_START.getType());
message.setType(SocketTypeMessageEnum.MESSAGE_TYPE_MESSAGE.getType());
message.setContent(sendMessage);
channelHandlerContext.writeAndFlush(message);
}
/**
* 读取消息
*
* @param message
*/
public abstract void readMessage(String message);
public void setChannelHandlerContext(ChannelHandlerContext channelHandlerContext) {
this.channelHandlerContext = channelHandlerContext;
}
}

View File

@ -22,7 +22,6 @@
<module>cloud-common-plugin-sensitive</module>
<module>cloud-common-freemarker</module>
<module>cloud-common-socket</module>
<module>cloud-common-socket-client</module>
<module>cloud-central-control</module>
<module>cloud-central-control-client</module>
</modules>