优化netty代码

This commit is contained in:
seagull 2020-02-26 16:10:51 +08:00
parent a3c743eac2
commit 69b2a352f9
6 changed files with 66 additions and 37 deletions

17
pom.xml
View File

@ -314,7 +314,20 @@
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--netty-->
<!--netty -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
</dependency>
</dependencies>
<!--排除版本管理警告-->
<dependencyManagement>
<dependencies>
<!--netty -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
@ -325,6 +338,6 @@
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>

View File

@ -0,0 +1,4 @@
谷歌浏览器驱动下载链接http://chromedriver.storage.googleapis.com/index.html
火狐浏览器驱动下载链接https://github.com/mozilla/geckodriver/releases/
Edge浏览器驱动下载链接https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
Edge浏览器驱动下载链接http://selenium-release.storage.googleapis.com/index.html

View File

@ -1,27 +1,29 @@
package luckyclient.netty;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.EventLoop;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import luckyclient.utils.config.SysConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
public class ClientHandler extends ChannelInboundHandlerAdapter {
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.EventLoop;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import luckyclient.utils.config.SysConfig;
public class ClientHandler extends ChannelHandlerAdapter {
//从application.properties文件中获取用到的参数;
private static Resource resource = new ClassPathResource("application.properties");
@ -37,8 +39,6 @@ public class ClientHandler extends ChannelInboundHandlerAdapter {
private static String port = props.getProperty("server.port");
private int counter;
static final String ECHO_REQ = "$_";
private static final Logger log = LoggerFactory.getLogger(ClientHandler.class);
@ -86,6 +86,7 @@ public class ClientHandler extends ChannelInboundHandlerAdapter {
String tmpResult="";
if("get".equals(getOrPost))
{
@SuppressWarnings("unchecked")
Map<String,Object> jsonparams = (Map<String,Object>)json.get("data");
//get方法
try {
@ -117,7 +118,7 @@ public class ClientHandler extends ChannelInboundHandlerAdapter {
String fileName=json.get("fileName").toString();
//发出http请求下载文件
try {
boolean isSuccess=HttpRequest.downLoadFromUrl("http://"+SERVER_IP+":"+SERVER_PORT+"/common/download?fileName="+fileName,fileName,path);
HttpRequest.downLoadFromUrl("http://"+SERVER_IP+":"+SERVER_PORT+"/common/download?fileName="+fileName,fileName,path);
//返回请求
JSONObject re=new JSONObject();
Result result=new Result(1,"上传成功",json.get("uuid").toString());

View File

@ -1,9 +1,22 @@
package luckyclient.netty;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoop;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
@ -12,19 +25,13 @@ import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.timeout.IdleStateHandler;
import luckyclient.utils.config.SysConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.concurrent.TimeUnit;
public class NettyClient {
private static final String NETTY_SERVER_IP= SysConfig.getConfiguration().getProperty("netty.server.ip");
private static final int NETTY_SERVER_PORT=Integer.parseInt(SysConfig.getConfiguration().getProperty("netty.server.port"));
private static Channel channel;
protected static Channel channel;
private static ChannelFuture connect;

View File

@ -3,6 +3,10 @@ package luckyclient.netty;
import java.io.Serializable;
public class Result implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private int code;
private Object message;
private String uniId;

View File

@ -2,16 +2,16 @@ package springboot;
import java.io.File;
import java.net.InetAddress;
import java.net.UnknownHostException;
import luckyclient.netty.NettyClient;
import luckyclient.utils.config.SysConfig;
import org.apache.log4j.PropertyConfigurator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import luckyclient.netty.NettyClient;
import luckyclient.utils.config.SysConfig;
/**
* =================================================================
* 这是一个受限制的自由软件您不能在任何未经允许的前提下对程序代码进行修改和用于商业用途也不允许对程序代码修改后以任何形式任何目的的再发布
@ -39,7 +39,7 @@ public class RunService {
if(NETTY_SERVER_IP==null)
HttpImpl.checkHostNet();
try {
log.error("##################客户端netty开启#################");
log.info("##################客户端netty开启#################");
NettyClient.start();
} catch (Exception e) {
log.error("连接服务端netty异常......");