Commit ce3e7046 by xmh

部分开发

1 parent 87ad1849
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
<groupId>com.viontech</groupId> <groupId>com.viontech</groupId>
<artifactId>recv_data_longhua</artifactId> <artifactId>recv_data_longhua</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging> <packaging>jar</packaging>
<name>recv_data_longhua</name> <name>recv_data_longhua</name>
<description>recv_data_longhua</description> <description>recv_data_longhua</description>
...@@ -24,7 +24,22 @@ ...@@ -24,7 +24,22 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.viontech.keliu</groupId>
<artifactId>keliu-util</artifactId>
<version>6.0.7-SNAPSHOT</version>
<exclusions>
<exclusion>
<artifactId>maven-scm-provider-integrity</artifactId>
<groupId>org.apache.maven.scm</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.32.Final</version>
</dependency>
<dependency> <dependency>
<groupId>org.projectlombok</groupId> <groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId> <artifactId>lombok</artifactId>
...@@ -32,23 +47,23 @@ ...@@ -32,23 +47,23 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope> <scope>test</scope>
<exclusions> </dependency>
<exclusion> <dependency>
<groupId>org.junit.vintage</groupId> <groupId>commons-net</groupId>
<artifactId>junit-vintage-engine</artifactId> <artifactId>commons-net</artifactId>
</exclusion> <version>3.6</version>
</exclusions> </dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.5.0</version>
</dependency> </dependency>
</dependencies> </dependencies>
<build> <build>
<finalName>recv_data_longhua</finalName>
<plugins> <plugins>
<plugin> <plugin>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
......
...@@ -4,7 +4,7 @@ import lombok.extern.slf4j.Slf4j; ...@@ -4,7 +4,7 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication @SpringBootApplication(exclude = org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration.class)
@Slf4j @Slf4j
public class Application { public class Application {
...@@ -15,5 +15,4 @@ public class Application { ...@@ -15,5 +15,4 @@ public class Application {
log.error("error", e); log.error("error", e);
} }
} }
} }
package com.viontech.configuration;
import com.viontech.ftp.FTPClientFactory;
import com.viontech.ftp.FTPClientHelper;
import com.viontech.ftp.FTPClientPool;
import com.viontech.ftp.FtpPoolConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FtpConfiguration {
@Value("${ftp.host:127.0.0.1}")
private String host;//主机名
@Value("${ftp.port:21}")
private int port;//端口
@Value("${ftp.username:}")
private String username;//用户名
@Value("${ftp.password:}")
private String password;//密码
@Value("${ftp.tempPath:}")
private String tempPath;
@Bean("ftpPoolConfig")
@ConditionalOnProperty(name = "ftp.host")
public FtpPoolConfig ftpPoolConfig() {
FtpPoolConfig ftpPoolConfig = new FtpPoolConfig();
ftpPoolConfig.setHost(host);
ftpPoolConfig.setPort(port);
ftpPoolConfig.setUsername(username);
ftpPoolConfig.setPassword(password);
//ftpPoolConfig.setMaxWaitMillis(1000);
//ftpPoolConfig.setMaxIdle(2);
return ftpPoolConfig;
}
@Bean("ftpClientFactory")
@ConditionalOnBean(FtpPoolConfig.class)
public FTPClientFactory ftpClientFactory(FtpPoolConfig ftpPoolConfig) {
FTPClientFactory ftpClientFactory = new FTPClientFactory();
ftpClientFactory.setFtpPoolConfig(ftpPoolConfig);
return ftpClientFactory;
}
@Bean("ftpClientPool")
@ConditionalOnBean(FTPClientFactory.class)
public FTPClientPool ftpClientPool(FTPClientFactory ftpClientFactory) {
FTPClientPool ftpClientPool = new FTPClientPool(ftpClientFactory);
return ftpClientPool;
}
@Bean("ftpClientHelper")
@ConditionalOnBean(FTPClientPool.class)
public FTPClientHelper ftpClientHelper(FTPClientPool ftpClientPool) {
FTPClientHelper ftpClientHelper = new FTPClientHelper();
ftpClientHelper.setFtpClientPool(ftpClientPool);
if (!tempPath.trim().isEmpty())
ftpClientHelper.setTempPath(tempPath);
return ftpClientHelper;
}
}
package com.viontech.controller; package com.viontech.controller;
import org.springframework.stereotype.Controller; import com.viontech.process.Process;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
/** /**
...@@ -12,5 +14,12 @@ import org.springframework.web.bind.annotation.RestController; ...@@ -12,5 +14,12 @@ import org.springframework.web.bind.annotation.RestController;
@RestController @RestController
public class DataController { public class DataController {
@PostMapping("/data")
public Object receiveData(@RequestBody String dataStr) {
Process.process(dataStr);
return null;
}
} }
package com.viontech.ftp;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import java.io.IOException;
/**
* ftpclient 工厂
*/
@Slf4j
public class FTPClientFactory extends BasePooledObjectFactory<FTPClient> {
private FtpPoolConfig ftpPoolConfig;
public FtpPoolConfig getFtpPoolConfig() {
return ftpPoolConfig;
}
public void setFtpPoolConfig(FtpPoolConfig ftpPoolConfig) {
this.ftpPoolConfig = ftpPoolConfig;
}
/**
* 新建对象
*/
@Override
public FTPClient create() throws Exception {
FTPClient ftpClient = new FTPClient();
ftpClient.setConnectTimeout(ftpPoolConfig.getConnectTimeOut());
try {
log.info("连接ftp服务器:" + ftpPoolConfig.getHost() + ":" + ftpPoolConfig.getPort());
ftpClient.connect(ftpPoolConfig.getHost(), ftpPoolConfig.getPort());
int reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
log.error("FTPServer 拒绝连接");
return null;
}
boolean result = ftpClient.login(ftpPoolConfig.getUsername(), ftpPoolConfig.getPassword());
if (!result) {
throw new Exception("ftpClient登录失败! userName:" + ftpPoolConfig.getUsername() + ", password:"
+ ftpPoolConfig.getPassword());
}
ftpClient.setControlEncoding(ftpPoolConfig.getControlEncoding());
ftpClient.setBufferSize(ftpPoolConfig.getBufferSize());
ftpClient.setFileType(ftpPoolConfig.getFileType());
ftpClient.setDataTimeout(ftpPoolConfig.getDataTimeout());
if (ftpPoolConfig.isPassiveMode()) {
log.info("进入ftp被动模式");
ftpClient.enterLocalPassiveMode();//进入被动模式
}
} catch (IOException e) {
log.error("FTP连接失败:", e);
}
return ftpClient;
}
@Override
public PooledObject<FTPClient> wrap(FTPClient ftpClient) {
return new DefaultPooledObject<FTPClient>(ftpClient);
}
/**
* 销毁对象
*/
@Override
public void destroyObject(PooledObject<FTPClient> p) throws Exception {
FTPClient ftpClient = p.getObject();
try {
if (ftpClient != null && ftpClient.isConnected()) {
ftpClient.logout();
}
} catch (IOException io) {
io.printStackTrace();
} finally {
// 注意,一定要在finally代码中断开连接,否则会导致占用ftp连接情况
try {
ftpClient.disconnect();
} catch (IOException io) {
io.printStackTrace();
}
}
}
/**
* 验证对象
*/
@Override
public boolean validateObject(PooledObject<FTPClient> p) {
FTPClient ftpClient = p.getObject();
boolean connect = false;
try {
connect = ftpClient.sendNoOp();
} catch (IOException e) {
e.printStackTrace();
}
return connect;
}
/**
* No-op.
*
* @param p ignored
*/
@Override
public void activateObject(PooledObject<FTPClient> p) throws Exception {
FTPClient ftpClient = p.getObject();
if (!validateObject(p)) {
ftpClient.reinitialize();
}
if (!validateObject(p)) {
throw new RuntimeException("ftp已经失效");
}
}
/**
* No-op.
*
* @param p ignored
*/
@Override
public void passivateObject(PooledObject<FTPClient> p) throws Exception {
}
}
\ No newline at end of file \ No newline at end of file
package com.viontech.ftp;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.pool2.impl.GenericObjectPool;
import java.io.IOException;
/**
* FTP 客户端连接池
*/
@Slf4j
public class FTPClientPool {
/**
* ftp客户端连接池
*/
private GenericObjectPool<FTPClient> pool;
/**
* ftp客户端工厂
*/
private FTPClientFactory clientFactory;
/**
* 构造函数中 注入一个bean
*
* @param clientFactory
*/
public FTPClientPool(FTPClientFactory clientFactory) {
this.clientFactory = clientFactory;
pool = new GenericObjectPool<FTPClient>(clientFactory, clientFactory.getFtpPoolConfig());
}
public FTPClientFactory getClientFactory() {
return clientFactory;
}
public GenericObjectPool<FTPClient> getPool() {
return pool;
}
/**
* 借 获取一个连接对象
*
* @return
*/
public FTPClient borrowObject() throws Exception {
FTPClient client = pool.borrowObject();
boolean valid = true;
try {
if (client.isConnected()) {
valid = client.sendNoOp();
} else {
valid = false;
}
} catch (IOException e) {
log.error("什么情况 刚刚获取的对象就不可用,", e);
e.printStackTrace();
valid = false;
}
if (!valid) {
//使池中的对象无效
try {
client.logout();
client.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
pool.invalidateObject(client);
}
return client;
}
/**
* 还 归还一个连接对象
*
* @param ftpClient
*/
public void returnObject(FTPClient ftpClient) {
if (ftpClient != null) {
try {
pool.returnObject(ftpClient);
} catch (Exception e) {
log.error("将FTP归还到FTP池中时发生异常", e);
}
}
}
}
\ No newline at end of file \ No newline at end of file
package com.viontech.ftp;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
/**
* ftp配置参数对象 继承自GenericObjectPoolConfig
*/
@Getter
@Setter
public class FtpPoolConfig extends GenericObjectPoolConfig {
private String host;
private int port;
private String username;
private String password;
/** ftp 连接超时时间 毫秒 */
private int connectTimeOut = 6000000;
private String controlEncoding = "utf-8";
/** 缓冲区大小 */
private int bufferSize = 1024;
/** 传输数据格式 2表binary二进制数据 */
private int fileType = 2;
private int dataTimeout = 120000;
private boolean useEPSVwithIPv4 = false;
/** 是否启用被动模式 */
private boolean passiveMode = true;
private String tempPath = System.getProperty("java.io.tmpdir");
}
package com.viontech.model;
import io.netty.buffer.ByteBuf;
import lombok.Getter;
import lombok.Setter;
/**
* .
*
* @author 谢明辉
* @date 2020/8/20
*/
@Getter
@Setter
public abstract class BaseModel {
protected Long empty;
protected Long length;
protected Long firstInt;
protected Long secondInt;
protected byte[] data;
/**
* 将byteBuf 转换为 model
*
* @param byteBuf byteBuf
*
* @return model
*/
abstract public BaseModel decode(ByteBuf byteBuf);
/**
* 将 model 转换为 byteBuf
*
* @return byteBuf
*/
abstract public ByteBuf encode();
}
package com.viontech.model;
import io.netty.buffer.ByteBuf;
import lombok.Getter;
import lombok.Setter;
/**
* .
*
* @author 谢明辉
* @date 2020/8/20
*/
@Getter
@Setter
public class LoginData extends BaseModel {
private String username;
private String password;
@Override
public BaseModel decode(ByteBuf byteBuf) {
return null;
}
@Override
public ByteBuf encode() {
return null;
}
}
package com.viontech.netty;
import io.netty.bootstrap.ServerBootstrap;
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.LengthFieldBasedFrameDecoder;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
/**
* .
*
* @author 谢明辉
* @date 2020/8/18
*/
@Component
@Slf4j
public class NettyServer implements CommandLineRunner {
@Value("${netty.port:30001}")
private int port;
@Override
public void run(String[] args) {
EventLoopGroup workerGroup = new NioEventLoopGroup(30);
EventLoopGroup bossGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b = b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) {
ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO));
ch.pipeline().addLast(new LengthFieldBasedFrameDecoder(1024 * 1024 * 1000, 4, 4, -16, 0));
}
})
.option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true);
log.info("通道构建完毕");
b.bind(port).addListener(future -> {
if (future.isSuccess()) {
log.info("端口[{}]绑定成功", port);
} else {
log.info("端口[{}]绑定失败", port);
}
});
} catch (Exception e) {
log.error("", e);
}
}
}
package com.viontech.process;
/**
* .
*
* @author 谢明辉
* @date 2020/8/20
*/
public class Process {
public static void process(String jsonStr) {
}
}
netty.port=30001
ftp.host=
ftp.port=
ftp.username=
ftp.password=
\ No newline at end of file \ No newline at end of file
spring:
profiles:
active: config
application:
name: revc_data_longhua
jackson:
time-zone: GMT+8
date-format: yyyy-MM-dd HH:mm:ss
server:
port: 30000
\ No newline at end of file \ No newline at end of file
package com.viontech;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class RecvDataLonghuaApplicationTests {
@Test
void contextLoads() {
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!