From 9e5c18158a1c965e05fb5b5486b104b9952c59f8 Mon Sep 17 00:00:00 2001
From: wenc000 <450292408@qq.com>
Date: Sun, 2 Aug 2020 23:26:43 +0800
Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=E5=8E=9F=E6=B5=8B=E8=AF=95?=
=?UTF-8?q?=E5=AE=A2=E6=88=B7=E7=AB=AF?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
cloud-common-socket-client/pom.xml | 23 ----
.../cm/socket/client/MessageSocketClient.java | 111 ------------------
.../config/MessageSocketClientConfig.java | 80 -------------
.../MessageClientChannelInitializer.java | 39 ------
.../client/handler/MessageClientHandler.java | 37 ------
.../handler/MessageClientPingHandler.java | 105 -----------------
.../service/AbstractMessageClientService.java | 47 --------
pom.xml | 1 -
8 files changed, 443 deletions(-)
delete mode 100644 cloud-common-socket-client/pom.xml
delete mode 100644 cloud-common-socket-client/src/main/java/com/cm/socket/client/MessageSocketClient.java
delete mode 100644 cloud-common-socket-client/src/main/java/com/cm/socket/client/config/MessageSocketClientConfig.java
delete mode 100644 cloud-common-socket-client/src/main/java/com/cm/socket/client/handler/MessageClientChannelInitializer.java
delete mode 100644 cloud-common-socket-client/src/main/java/com/cm/socket/client/handler/MessageClientHandler.java
delete mode 100644 cloud-common-socket-client/src/main/java/com/cm/socket/client/handler/MessageClientPingHandler.java
delete mode 100644 cloud-common-socket-client/src/main/java/com/cm/socket/client/service/AbstractMessageClientService.java
diff --git a/cloud-common-socket-client/pom.xml b/cloud-common-socket-client/pom.xml
deleted file mode 100644
index 3c8fbf2..0000000
--- a/cloud-common-socket-client/pom.xml
+++ /dev/null
@@ -1,23 +0,0 @@
-
-
-
- cm-cloud
- com.cm
- 1.0.1-SNAPSHOT
-
- 4.0.0
-
- cloud-common-socket-client
-
-
-
- com.cm
- cloud-common-socket
- 1.0.1-SNAPSHOT
-
-
-
-
-
\ No newline at end of file
diff --git a/cloud-common-socket-client/src/main/java/com/cm/socket/client/MessageSocketClient.java b/cloud-common-socket-client/src/main/java/com/cm/socket/client/MessageSocketClient.java
deleted file mode 100644
index 2934125..0000000
--- a/cloud-common-socket-client/src/main/java/com/cm/socket/client/MessageSocketClient.java
+++ /dev/null
@@ -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;
- }
-}
diff --git a/cloud-common-socket-client/src/main/java/com/cm/socket/client/config/MessageSocketClientConfig.java b/cloud-common-socket-client/src/main/java/com/cm/socket/client/config/MessageSocketClientConfig.java
deleted file mode 100644
index dab26c6..0000000
--- a/cloud-common-socket-client/src/main/java/com/cm/socket/client/config/MessageSocketClientConfig.java
+++ /dev/null
@@ -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();
- }
-}
diff --git a/cloud-common-socket-client/src/main/java/com/cm/socket/client/handler/MessageClientChannelInitializer.java b/cloud-common-socket-client/src/main/java/com/cm/socket/client/handler/MessageClientChannelInitializer.java
deleted file mode 100644
index 542a8f0..0000000
--- a/cloud-common-socket-client/src/main/java/com/cm/socket/client/handler/MessageClientChannelInitializer.java
+++ /dev/null
@@ -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 {
-
- 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));
- }
-
-}
diff --git a/cloud-common-socket-client/src/main/java/com/cm/socket/client/handler/MessageClientHandler.java b/cloud-common-socket-client/src/main/java/com/cm/socket/client/handler/MessageClientHandler.java
deleted file mode 100644
index ed11e74..0000000
--- a/cloud-common-socket-client/src/main/java/com/cm/socket/client/handler/MessageClientHandler.java
+++ /dev/null
@@ -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 {
-
- 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());
- }
-
-}
diff --git a/cloud-common-socket-client/src/main/java/com/cm/socket/client/handler/MessageClientPingHandler.java b/cloud-common-socket-client/src/main/java/com/cm/socket/client/handler/MessageClientPingHandler.java
deleted file mode 100644
index ff263b6..0000000
--- a/cloud-common-socket-client/src/main/java/com/cm/socket/client/handler/MessageClientPingHandler.java
+++ /dev/null
@@ -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 {
-
- 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();
- }
-}
diff --git a/cloud-common-socket-client/src/main/java/com/cm/socket/client/service/AbstractMessageClientService.java b/cloud-common-socket-client/src/main/java/com/cm/socket/client/service/AbstractMessageClientService.java
deleted file mode 100644
index 7688d05..0000000
--- a/cloud-common-socket-client/src/main/java/com/cm/socket/client/service/AbstractMessageClientService.java
+++ /dev/null
@@ -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;
- }
-}
diff --git a/pom.xml b/pom.xml
index 3464604..fdaf879 100644
--- a/pom.xml
+++ b/pom.xml
@@ -22,7 +22,6 @@
cloud-common-plugin-sensitive
cloud-common-freemarker
cloud-common-socket
- cloud-common-socket-client
cloud-central-control
cloud-central-control-client