main
黄海 9 months ago
parent 691c5b15b5
commit 027c12d130

@ -104,38 +104,16 @@ public class BaseApplication extends JFinalConfig {
} }
/**
*
*/
private String connectionTestQuery = "select 1";
// 连接池中允许的最大连接数。缺省值10推荐的公式((core_count * 2) + effective_spindle_count)
private int maxPoolSize = 10;
// 一个连接 idle 状态的最大时长(毫秒),超时则被释放(retired),缺省:10分钟
private long idleTimeoutMs = 600000;
private long maxLifetimeMs = 1800000;
// 等待连接池分配连接的最大时长(毫秒),超过这个时长还没可用的连接则发生 SQLException 缺省:30秒
private long connectionTimeoutMs = 30000;
@SneakyThrows @SneakyThrows
@Override @Override
public void configPlugin(Plugins me) { public void configPlugin(Plugins me) {
HikariCpPlugin masterPlugin = new HikariCpPlugin(PropKit.get("mysql.jdbcUrl"), PropKit.get("mysql.user"), HikariCpPlugin masterPlugin = new HikariCpPlugin(PropKit.get("mysql.jdbcUrl"), PropKit.get("mysql.user"),
PropKit.get("mysql.password").trim(), PropKit.get("mysql.driverClassName")); PropKit.get("mysql.password").trim(), PropKit.get("mysql.driverClassName"));
masterPlugin.setConnectionTestQuery(connectionTestQuery);
masterPlugin.setConnectionTimeout(connectionTimeoutMs);
masterPlugin.setIdleTimeout(idleTimeoutMs);
masterPlugin.setMaxLifetime(maxLifetimeMs);
masterPlugin.setMaximumPoolSize(maxPoolSize);
String jdbcUrlSlave = PropKit.get("mysql.jdbcUrlSlave"); String jdbcUrlSlave = PropKit.get("mysql.jdbcUrlSlave");
if (StrKit.isBlank(jdbcUrlSlave)) jdbcUrlSlave = PropKit.get("mysql.jdbcUrl"); if (StrKit.isBlank(jdbcUrlSlave)) jdbcUrlSlave = PropKit.get("mysql.jdbcUrl");
HikariCpPlugin slavePlugin = new HikariCpPlugin(jdbcUrlSlave, PropKit.get("mysql.user"), HikariCpPlugin slavePlugin = new HikariCpPlugin(jdbcUrlSlave, PropKit.get("mysql.user"),
PropKit.get("mysql.password").trim(), PropKit.get("mysql.driverClassName")); PropKit.get("mysql.password").trim(), PropKit.get("mysql.driverClassName"));
slavePlugin.setConnectionTestQuery(connectionTestQuery);
slavePlugin.setConnectionTimeout(connectionTimeoutMs);
slavePlugin.setIdleTimeout(idleTimeoutMs);
slavePlugin.setMaxLifetime(maxLifetimeMs);
slavePlugin.setMaximumPoolSize(maxPoolSize);
me.add(masterPlugin); me.add(masterPlugin);
me.add(slavePlugin); me.add(slavePlugin);

@ -16,8 +16,11 @@ import org.slf4j.LoggerFactory;
import java.io.*; import java.io.*;
import java.net.JarURLConnection; import java.net.JarURLConnection;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.security.CodeSource;
import java.security.ProtectionDomain;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.*; import java.util.*;
@ -52,30 +55,37 @@ public class CommonUtil {
* @return * @return
* @throws IOException * @throws IOException
*/ */
public static String txt2String(String path) { public static String txt2String(String path) {
InputStream is = CommonUtil.class.getClassLoader().getResourceAsStream(path); InputStream is = CommonUtil.class.getClassLoader().getResourceAsStream(path);
return IoUtil.read(is, "UTF-8"); return IoUtil.read(is, "UTF-8");
} }
/** /**
* sqllist * sqllist
* *
* @return * @return
*/ */
public static List<String> getAllSql() throws IOException { public static List<String> getAllSql() throws IOException, URISyntaxException {
List<String> list = new ArrayList<>(); List<String> list = new ArrayList<>();
if (isRunInJar()) { if (isRunInJar()) {
URL url = CommonUtil.class.getClassLoader().getResource("Sql/"); // 获取当前类的ProtectionDomain
String jarPath = url.toString().substring(0, url.toString().indexOf("!/") + 2); ProtectionDomain domain = CommonUtil.class.getProtectionDomain();
URL jarURL = new URL(jarPath); // 获取CodeSource
JarURLConnection jarCon = (JarURLConnection) jarURL.openConnection(); CodeSource source = domain.getCodeSource();
JarFile jarFile = jarCon.getJarFile(); // 获取Jar文件的URL
Enumeration<JarEntry> jarEntrys = jarFile.entries(); URL jarUrl = source.getLocation();
while (jarEntrys.hasMoreElements()) { // 获取Jar文件的路径
JarEntry entry = jarEntrys.nextElement(); String jarPath = jarUrl.toURI().getSchemeSpecificPart();
String name = entry.getName(); // 打开Jar文件
if (name.startsWith("Sql/") && !entry.isDirectory()) { JarFile jarFile = new JarFile(jarPath);
list.add(name); // 遍历Jar中的所有条目
Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
// 检查条目是否是SQL文件
if (entry.getName().startsWith("Sql/") && entry.getName().toLowerCase().endsWith(".sql")) {
list.add(entry.getName());
} }
} }
} else {//如果运行在文件系统中直接加载sql文件 } else {//如果运行在文件系统中直接加载sql文件
@ -86,7 +96,7 @@ public class CommonUtil {
File[] sqlFiles = sqlDir.listFiles(); File[] sqlFiles = sqlDir.listFiles();
for (File sqlFile : sqlFiles != null ? sqlFiles : new File[0]) { for (File sqlFile : sqlFiles != null ? sqlFiles : new File[0]) {
if (sqlFile.getName().indexOf(".sql") > 0) {//只加载.sql文件 if (sqlFile.getName().indexOf(".sql") > 0) {//只加载.sql文件
list.add("Sql/"+sqlFile.getName()); list.add("Sql/" + sqlFile.getName());
} }
} }
} }

@ -10,7 +10,7 @@ mysql:
driverClassName: com.mysql.cj.jdbc.Driver driverClassName: com.mysql.cj.jdbc.Driver
user: ylt user: ylt
password: Ycharge666 password: Ycharge666
jdbcUrl : jdbc:mysql://rm-bp1ux6tuk49er80t9.mysql.rds.aliyuncs.com:3306/ds_db?useUnicode=true&characterEncoding=UTF-8 jdbcUrl : jdbc:mysql://rm-bp1ux6tuk49er80t9.mysql.rds.aliyuncs.com:3306/ds_db?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false
redis: redis:

@ -10,7 +10,7 @@ mysql:
driverClassName: com.mysql.cj.jdbc.Driver driverClassName: com.mysql.cj.jdbc.Driver
user: root user: root
password: DsideaL147258369 password: DsideaL147258369
jdbcUrl : jdbc:mysql://10.10.14.210:22066/ds_db?rewriteBatchedStatements=true&useUnicode=true&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai jdbcUrl : jdbc:mysql://10.10.14.210:22066/ds_db?rewriteBatchedStatements=true&useUnicode=true&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&autoReconnect=true&failOverReadOnly=false
redis: redis:

@ -10,7 +10,7 @@ mysql:
driverClassName: com.mysql.cj.jdbc.Driver driverClassName: com.mysql.cj.jdbc.Driver
user: ylt user: ylt
password: Ycharge666 password: Ycharge666
jdbcUrl : jdbc:mysql://rm-bp1ux6tuk49er80t9.mysql.rds.aliyuncs.com:3306/ds_db?useUnicode=true&characterEncoding=UTF-8 jdbcUrl : jdbc:mysql://rm-bp1ux6tuk49er80t9.mysql.rds.aliyuncs.com:3306/ds_db?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false
redis: redis:

Loading…
Cancel
Save