1. h2数据库文件优化, 取消h2server启动

2. 项目结构优化, pom版本统一管理
3. core的beanutils依赖升级为commons-beanutils2版本, 修复之前版本的安全风险.
4. 此版本打包部署需要替换之前所有依赖
This commit is contained in:
QAIU
2024-06-06 18:06:33 +08:00
parent aaae7ab9a6
commit 7663320a55
13 changed files with 90 additions and 127 deletions

View File

@@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>netdisk-fast-download</artifactId> <artifactId>netdisk-fast-download</artifactId>
<groupId>cn.qaiu</groupId> <groupId>cn.qaiu</groupId>
<version>0.1.7</version> <version>${revision}</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
@@ -23,7 +23,6 @@
<dependency> <dependency>
<groupId>cn.qaiu</groupId> <groupId>cn.qaiu</groupId>
<artifactId>core</artifactId> <artifactId>core</artifactId>
<version>1.0.8</version>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/com.h2database/h2 --> <!-- https://mvnrepository.com/artifact/com.h2database/h2 -->

View File

@@ -1,5 +1,6 @@
package cn.qaiu.db.ddl; package cn.qaiu.db.ddl;
import cn.qaiu.db.pool.JDBCType;
import cn.qaiu.vx.core.util.ReflectionUtil; import cn.qaiu.vx.core.util.ReflectionUtil;
import io.vertx.codegen.format.CamelCase; import io.vertx.codegen.format.CamelCase;
import io.vertx.codegen.format.Case; import io.vertx.codegen.format.Case;
@@ -65,7 +66,9 @@ public class CreateTable {
} }
} }
public static String getCreateTableSQL(Class<?> clz) { public static String getCreateTableSQL(Class<?> clz, JDBCType type) {
String quotationMarks = type == JDBCType.H2DB ? "\"" : "`";
String endStr = type == JDBCType.H2DB ? ");" : ")ENGINE=InnoDB DEFAULT CHARSET=utf8;";
// 判断类上是否有次注解 // 判断类上是否有次注解
String primaryKey = null; // 主键 String primaryKey = null; // 主键
String tableName = null; // 表名 String tableName = null; // 表名
@@ -93,7 +96,7 @@ public class CreateTable {
int[] decimalSize = {22, 2}; int[] decimalSize = {22, 2};
int varcharSize = 255; int varcharSize = 255;
StringBuilder sb = new StringBuilder(50); StringBuilder sb = new StringBuilder(50);
sb.append("CREATE TABLE IF NOT EXISTS \"").append(tableName).append("\" ( \r\n "); sb.append("CREATE TABLE IF NOT EXISTS ").append(quotationMarks).append(tableName).append(quotationMarks).append(" ( \r\n ");
boolean firstId = true; boolean firstId = true;
for (Field f : fields) { for (Field f : fields) {
Class<?> paramType = f.getType(); Class<?> paramType = f.getType();
@@ -114,7 +117,7 @@ public class CreateTable {
decimalSize = fieldAnnotation.decimalSize(); decimalSize = fieldAnnotation.decimalSize();
varcharSize = fieldAnnotation.varcharSize(); varcharSize = fieldAnnotation.varcharSize();
} }
sb.append("\"").append(column).append("\""); sb.append(quotationMarks).append(column).append(quotationMarks);
sb.append(" ").append(sqlType); sb.append(" ").append(sqlType);
// 添加类型长度 // 添加类型长度
if (sqlType.equals("DECIMAL")) { if (sqlType.equals("DECIMAL")) {
@@ -155,17 +158,20 @@ public class CreateTable {
//去掉最后一个逗号 //去掉最后一个逗号
int lastIndex = sql.lastIndexOf(","); int lastIndex = sql.lastIndexOf(",");
sql = sql.substring(0, lastIndex) + sql.substring(lastIndex + 1); sql = sql.substring(0, lastIndex) + sql.substring(lastIndex + 1);
return sql.substring(0, sql.length() - 1) + ");\r\n"; return sql.substring(0, sql.length() - 1) + endStr;
} }
public static void createTable(JDBCPool pool) { public static void createTable(JDBCPool pool, JDBCType type) {
Set<Class<?>> tableClassList = ReflectionUtil.getReflections().getTypesAnnotatedWith(Table.class); Set<Class<?>> tableClassList = ReflectionUtil.getReflections().getTypesAnnotatedWith(Table.class);
if (tableClassList.isEmpty()) LOGGER.info("Table model class not fount"); if (tableClassList.isEmpty()) LOGGER.info("Table model class not fount");
tableClassList.forEach(clazz -> { tableClassList.forEach(clazz -> {
String createTableSQL = getCreateTableSQL(clazz); String createTableSQL = getCreateTableSQL(clazz, type);
pool.query(createTableSQL).execute().onSuccess( pool.query(createTableSQL).execute().onSuccess(
rs -> LOGGER.info("\n" + createTableSQL + "create table --> ok") rs -> LOGGER.info("table auto generate:\n" + createTableSQL)
).onFailure(Throwable::printStackTrace); ).onFailure(e -> {
LOGGER.error(e.getMessage() + " SQL: \n" + createTableSQL);
});
}); });
} }
} }

View File

@@ -1,20 +1,13 @@
package cn.qaiu.db.pool; package cn.qaiu.db.pool;
import cn.qaiu.db.ddl.CreateTable; import cn.qaiu.db.ddl.CreateTable;
import cn.qaiu.db.server.H2ServerHolder;
import cn.qaiu.vx.core.util.VertxHolder; import cn.qaiu.vx.core.util.VertxHolder;
import io.vertx.core.Promise;
import io.vertx.core.Vertx; import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject; import io.vertx.core.json.JsonObject;
import io.vertx.jdbcclient.JDBCPool; import io.vertx.jdbcclient.JDBCPool;
import org.h2.tools.Server;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
/** /**
* 初始化JDBC * 初始化JDBC
* <br>Create date 2021/8/10 12:04 * <br>Create date 2021/8/10 12:04
@@ -28,12 +21,14 @@ public class JDBCPoolInit {
JsonObject dbConfig; JsonObject dbConfig;
Vertx vertx = VertxHolder.getVertxInstance(); Vertx vertx = VertxHolder.getVertxInstance();
String url; String url;
private JDBCType type;
private static JDBCPoolInit instance; private static JDBCPoolInit instance;
public JDBCPoolInit(Builder builder) { public JDBCPoolInit(Builder builder) {
this.dbConfig = builder.dbConfig; this.dbConfig = builder.dbConfig;
this.url = builder.url; this.url = builder.url;
this.type = builder.type;
} }
public static Builder builder() { public static Builder builder() {
@@ -47,10 +42,12 @@ public class JDBCPoolInit {
public static class Builder { public static class Builder {
private JsonObject dbConfig; private JsonObject dbConfig;
private String url; private String url;
private JDBCType type;
public Builder config(JsonObject dbConfig) { public Builder config(JsonObject dbConfig) {
this.dbConfig = dbConfig; this.dbConfig = dbConfig;
this.url = dbConfig.getString("jdbcUrl"); this.url = dbConfig.getString("jdbcUrl");
this.type = JDBCUtil.getJDBCType(dbConfig.getString("driverClassName"));
return this; return this;
} }
@@ -73,67 +70,16 @@ public class JDBCPoolInit {
return; return;
} }
// 异步启动H2服务
vertx.createSharedWorkerExecutor("h2-server", 1, Long.MAX_VALUE)
.executeBlocking(this::h2serverExecute)
.onSuccess(res->{
LOGGER.info(res);
// 初始化数据库连接 // 初始化数据库连接
vertx.createSharedWorkerExecutor("sql-pool-init") vertx.createSharedWorkerExecutor("sql-pool-init")
.executeBlocking(this::poolInitExecute) .executeBlocking(() -> {
.onSuccess(LOGGER::info)
.onFailure(Throwable::printStackTrace);
})
.onFailure(Throwable::printStackTrace);
}
private void poolInitExecute(Promise<String> promise) {
// 初始化连接池 // 初始化连接池
pool = JDBCPool.pool(vertx, dbConfig); pool = JDBCPool.pool(vertx, dbConfig);
CreateTable.createTable(pool); CreateTable.createTable(pool, type);
promise.complete("init jdbc pool success"); return "数据库连接初始化: URL=" + url;
})
} .onSuccess(LOGGER::info)
.onFailure(Throwable::printStackTrace);
private void checkOrCreateDBFile() {
LOGGER.info("init sql start");
String[] path = url.split("\\./");
path[1] = path[1].split(";")[0];
path[1] += ".mv.db";
File file = new File(path[1]);
if (!file.exists()) {
if (!file.getParentFile().exists()) {
if (file.getParentFile().mkdirs()) {
LOGGER.info("mkdirs -> {}", file.getParentFile().getAbsolutePath());
}
}
try {
if (file.createNewFile()) {
LOGGER.info("create file -> {}", file.getAbsolutePath());
}
} catch (IOException e) {
LOGGER.error(e.getMessage());
throw new RuntimeException("file create failed");
}
}
}
private void h2serverExecute(Promise<String> promise) {
// 初始化H2db, 创建本地db文件
checkOrCreateDBFile();
try {
String url = dbConfig.getString("jdbcUrl");
String[] portStr = url.split(":");
String port = portStr[portStr.length - 1].split("[/\\\\]")[0];
LOGGER.info("H2server listen port to {}", port);
H2ServerHolder.init(Server.createTcpServer("-tcp", "-tcpAllowOthers", "-tcpPort", port).start());
promise.complete("Start h2Server success");
} catch (SQLException e) {
throw new RuntimeException("Start h2Server failed: " + e.getMessage());
}
} }
/** /**

View File

@@ -5,28 +5,15 @@
<parent> <parent>
<artifactId>netdisk-fast-download</artifactId> <artifactId>netdisk-fast-download</artifactId>
<groupId>cn.qaiu</groupId> <groupId>cn.qaiu</groupId>
<version>0.1.7</version> <version>${revision}</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<version>1.0.8</version>
<artifactId>core</artifactId> <artifactId>core</artifactId>
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> </properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-dependencies</artifactId>
<version>${vertx.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies> <dependencies>
<!--logback日志实现--> <!--logback日志实现-->
<dependency> <dependency>
@@ -73,11 +60,13 @@
<artifactId>commons-lang3</artifactId> <artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version> <version>${commons-lang3.version}</version>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/com.melloware/commons-beanutils2 -->
<dependency> <dependency>
<groupId>commons-beanutils</groupId> <groupId>com.melloware</groupId>
<artifactId>commons-beanutils</artifactId> <artifactId>commons-beanutils2</artifactId>
<version>1.9.4</version> <version>${commons-beanutils2.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.fasterxml.jackson.core</groupId> <groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId> <artifactId>jackson-databind</artifactId>

View File

@@ -3,8 +3,8 @@ package cn.qaiu.vx.core.util;
import cn.qaiu.vx.core.annotaions.HandleSortFilter; import cn.qaiu.vx.core.annotaions.HandleSortFilter;
import io.vertx.core.buffer.Buffer; import io.vertx.core.buffer.Buffer;
import io.vertx.core.json.JsonObject; import io.vertx.core.json.JsonObject;
import org.apache.commons.beanutils.ConvertUtils; import org.apache.commons.beanutils2.ConvertUtils;
import org.apache.commons.beanutils.Converter; import org.apache.commons.beanutils2.Converter;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;

View File

@@ -1,7 +1,7 @@
package cn.qaiu.vx.core.util; package cn.qaiu.vx.core.util;
import io.vertx.core.MultiMap; import io.vertx.core.MultiMap;
import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.beanutils2.BeanUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;

View File

@@ -5,12 +5,13 @@ import javassist.bytecode.AccessFlag;
import javassist.bytecode.CodeAttribute; import javassist.bytecode.CodeAttribute;
import javassist.bytecode.LocalVariableAttribute; import javassist.bytecode.LocalVariableAttribute;
import javassist.bytecode.MethodInfo; import javassist.bytecode.MethodInfo;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils; import org.apache.commons.lang3.time.DateUtils;
import org.apache.commons.lang3.tuple.Pair; import org.apache.commons.lang3.tuple.Pair;
import org.reflections.Reflections; import org.reflections.Reflections;
import org.reflections.scanners.*; import org.reflections.scanners.MemberUsageScanner;
import org.reflections.scanners.MethodParameterNamesScanner;
import org.reflections.scanners.Scanners;
import org.reflections.util.ClasspathHelper; import org.reflections.util.ClasspathHelper;
import org.reflections.util.ConfigurationBuilder; import org.reflections.util.ConfigurationBuilder;
import org.reflections.util.FilterBuilder; import org.reflections.util.FilterBuilder;
@@ -185,10 +186,10 @@ public final class ReflectionUtil {
return DateUtils.parseDate(value, fmt); return DateUtils.parseDate(value, fmt);
} catch (ParseException e) { } catch (ParseException e) {
e.printStackTrace(); e.printStackTrace();
throw new ConversionException("无法将格式化日期"); throw new RuntimeException("无法将格式化日期");
} }
default: default:
throw new ConversionException("无法将String类型" + value + "转为[" + name + "]"); throw new RuntimeException("无法将String类型" + value + "转为[" + name + "]");
} }
} }
@@ -200,7 +201,7 @@ public final class ReflectionUtil {
* @return Array * @return Array
*/ */
public static Object conversionArray(CtClass ctClass, String value) { public static Object conversionArray(CtClass ctClass, String value) {
if (!isBasicTypeArray(ctClass)) throw new ConversionException("无法解析数组"); if (!isBasicTypeArray(ctClass)) throw new RuntimeException("无法解析数组");
String[] strArr = value.split(","); String[] strArr = value.split(",");
List<Object> obj = new ArrayList<>(); List<Object> obj = new ArrayList<>();
Arrays.stream(strArr).forEach(v -> obj.add(conversion(ctClass, v, null))); Arrays.stream(strArr).forEach(v -> obj.add(conversion(ctClass, v, null)));

View File

@@ -2,13 +2,12 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" <project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent> <parent>
<artifactId>netdisk-fast-download</artifactId> <artifactId>netdisk-fast-download</artifactId>
<groupId>cn.qaiu</groupId> <groupId>cn.qaiu</groupId>
<version>0.1.7</version> <version>${revision}</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>parser</artifactId> <artifactId>parser</artifactId>
<properties> <properties>

33
pom.xml
View File

@@ -7,7 +7,7 @@
<groupId>cn.qaiu</groupId> <groupId>cn.qaiu</groupId>
<artifactId>netdisk-fast-download</artifactId> <artifactId>netdisk-fast-download</artifactId>
<packaging>pom</packaging> <packaging>pom</packaging>
<version>0.1.7</version> <version>${revision}</version>
<modules> <modules>
<module>core</module> <module>core</module>
@@ -17,6 +17,7 @@
</modules> </modules>
<properties> <properties>
<revision>0.1.7</revision>
<java.version>17</java.version> <java.version>17</java.version>
<maven.compiler.source>17</maven.compiler.source> <maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target> <maven.compiler.target>17</maven.compiler.target>
@@ -30,10 +31,40 @@
<lombok.version>1.18.12</lombok.version> <lombok.version>1.18.12</lombok.version>
<slf4j.version>2.0.5</slf4j.version> <slf4j.version>2.0.5</slf4j.version>
<commons-lang3.version>3.12.0</commons-lang3.version> <commons-lang3.version>3.12.0</commons-lang3.version>
<commons-beanutils2.version>2.0.0</commons-beanutils2.version>
<jackson.version>2.14.2</jackson.version> <jackson.version>2.14.2</jackson.version>
<logback.version>1.4.12</logback.version> <logback.version>1.4.12</logback.version>
</properties> </properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-dependencies</artifactId>
<version>${vertx.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>cn.qaiu</groupId>
<artifactId>core</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>cn.qaiu</groupId>
<artifactId>core-database</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>cn.qaiu</groupId>
<artifactId>parser</artifactId>
<version>${revision}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build> <build>
<plugins> <plugins>
<plugin> <plugin>

View File

@@ -5,10 +5,9 @@
<parent> <parent>
<artifactId>netdisk-fast-download</artifactId> <artifactId>netdisk-fast-download</artifactId>
<groupId>cn.qaiu</groupId> <groupId>cn.qaiu</groupId>
<version>0.1.7</version> <version>${revision}</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<version>0.1.7</version>
<artifactId>web-service</artifactId> <artifactId>web-service</artifactId>
<properties> <properties>
@@ -22,7 +21,14 @@
<dependency> <dependency>
<groupId>cn.qaiu</groupId> <groupId>cn.qaiu</groupId>
<artifactId>core</artifactId> <artifactId>core</artifactId>
<version>1.0.8</version> </dependency>
<dependency>
<groupId>cn.qaiu</groupId>
<artifactId>core-database</artifactId>
</dependency>
<dependency>
<groupId>cn.qaiu</groupId>
<artifactId>parser</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.projectlombok</groupId> <groupId>org.projectlombok</groupId>
@@ -47,16 +53,6 @@
<artifactId>slf4j-api</artifactId> <artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version> <version>${slf4j.version}</version>
</dependency> </dependency>
<dependency>
<groupId>cn.qaiu</groupId>
<artifactId>core-database</artifactId>
<version>0.1.7</version>
</dependency>
<dependency>
<groupId>cn.qaiu</groupId>
<artifactId>parser</artifactId>
<version>0.1.7</version>
</dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>

View File

@@ -35,7 +35,7 @@ custom:
# 数据源配置 # 数据源配置
dataSource: dataSource:
provider_class: io.vertx.ext.jdbc.spi.impl.HikariCPDataSourceProvider provider_class: io.vertx.ext.jdbc.spi.impl.HikariCPDataSourceProvider
jdbcUrl: jdbc:h2:tcp://127.0.0.1:9095/./db/myData;MODE=MySQL;DATABASE_TO_UPPER=FALSE jdbcUrl: jdbc:h2:file:./db/nfdData;MODE=MySQL;DATABASE_TO_UPPER=FALSE
driverClassName: org.h2.Driver driverClassName: org.h2.Driver
username: root username: root
password: '123456' password: '123456'

View File

@@ -83,14 +83,10 @@ GET http://127.0.0.1:6400/parser?url=https://share.feijipan.com/s/nMtCOXL
### 小飞机盘 https://share.feijipan.com/s/laUshYGk ### 小飞机盘 https://share.feijipan.com/s/laUshYGk
# @no-redirect # @no-redirect
GET http://127.0.0.1:6400/parser?url=https://share.feijipan.com/s/laUshYGk GET http://127.0.0.1:6400/parser?url=https://share.feijipan.com/s/laUshYGk
### 小飞机盘 ### 小飞机盘 tIfhRqH
GET http://127.0.0.1:6400/json/fj/tIfhRqH GET http://127.0.0.1:6400/json/fj/tIfhRqH
### 小飞机盘 https://share.feijipan.com/s/7jy0zlv ### 小飞机盘 https://share.feijipan.com/s/7jy0zlv
GET http://127.0.0.1:6400/json/fj/7jy0zlv GET http://127.0.0.1:6400/json/fj/7jy0zlv
### 小飞机盘
# @no-redirect
GET http://127.0.0.1:6400/fj/tIfhRqH
### 小飞机盘 https://share.feijipan.com/s/nMtCOXL ### 小飞机盘 https://share.feijipan.com/s/nMtCOXL
# @no-redirect # @no-redirect
GET http://127.0.0.1:6400/fj/nMtCOXL GET http://127.0.0.1:6400/fj/nMtCOXL

View File

@@ -1,9 +1,9 @@
package cn.qaiu.web.test; package cn.qaiu.web.test;
import io.vertx.ext.web.RoutingContext; import io.vertx.ext.web.RoutingContext;
import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.beanutils2.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils; import org.apache.commons.beanutils2.ConvertUtils;
import org.apache.commons.beanutils.Converter; import org.apache.commons.beanutils2.Converter;
import org.apache.commons.lang3.time.DateFormatUtils; import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils; import org.apache.commons.lang3.time.DateUtils;
import org.junit.Test; import org.junit.Test;