This commit is contained in:
pader.zhang 2019-03-02 11:50:59 +08:00
parent 6290f8e12e
commit 2f0c503da1
5 changed files with 34 additions and 7 deletions

View File

@ -70,6 +70,8 @@
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>

View File

@ -16,6 +16,7 @@
package com.alibaba.nacos.config.server.service;
import com.alibaba.nacos.config.server.monitor.MetricsMonitor;
import com.alibaba.nacos.core.utils.InetUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@ -31,6 +32,8 @@ import com.alibaba.nacos.config.server.utils.PropertyUtil;
import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.dao.DataAccessException;
@ -52,7 +55,10 @@ import static com.alibaba.nacos.config.server.utils.LogUtil.fatalLog;
*/
@Service("basicDataSourceService")
public class BasicDataSourceServiceImpl implements DataSourceService {
private static final String JDBC_DRIVER_NAME = "com.mysql.jdbc.Driver";
private static final Logger log = LoggerFactory.getLogger(InetUtils.class); private static final String DEFAULT_MYSQL_DRIVER = "com.mysql.jdbc.Driver";
private static final String MYSQL_HIGH_LEVEL_DRIVER = "com.mysql.cj.jdbc.Driver";
private static String JDBC_DRIVER_NAME;
/**
* JDBC执行超时时间, 单位秒
@ -76,9 +82,22 @@ public class BasicDataSourceServiceImpl implements DataSourceService {
private volatile int masterIndex;
private static Pattern ipPattern = Pattern.compile("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");
@Autowired
private Environment env;
static {
try {
Class.forName(MYSQL_HIGH_LEVEL_DRIVER);
JDBC_DRIVER_NAME = MYSQL_HIGH_LEVEL_DRIVER;
log.info("Use Mysql 8 as the driver");
} catch (ClassNotFoundException e) {
log.info("Use Mysql as the driver");
JDBC_DRIVER_NAME = DEFAULT_MYSQL_DRIVER;
}
}
@PostConstruct
public void init() {
queryTimeout = NumberUtils.toInt(System.getProperty("QUERYTIMEOUT"), 3);
@ -242,7 +261,7 @@ public class BasicDataSourceServiceImpl implements DataSourceService {
if (ds == null) {
return StringUtils.EMPTY;
}
BasicDataSource bds = (BasicDataSource)ds;
BasicDataSource bds = (BasicDataSource) ds;
return bds.getUrl();
}
@ -282,6 +301,7 @@ public class BasicDataSourceServiceImpl implements DataSourceService {
}
class SelectMasterTask implements Runnable {
@Override
public void run() {
defaultLog.info("check master db.");
@ -294,7 +314,8 @@ public class BasicDataSourceServiceImpl implements DataSourceService {
testMasterJT.setQueryTimeout(queryTimeout);
try {
testMasterJT
.update("DELETE FROM config_info WHERE data_id='com.alibaba.nacos.testMasterDB'");
.update(
"DELETE FROM config_info WHERE data_id='com.alibaba.nacos.testMasterDB'");
if (jt.getDataSource() != ds) {
fatalLog.warn("[master-db] {}", ds.getUrl());
}
@ -317,6 +338,7 @@ public class BasicDataSourceServiceImpl implements DataSourceService {
@SuppressWarnings("PMD.ClassNamingShouldBeCamelRule")
class CheckDBHealthTask implements Runnable {
@Override
public void run() {
defaultLog.info("check db health.");
@ -329,9 +351,11 @@ public class BasicDataSourceServiceImpl implements DataSourceService {
isHealthList.set(i, Boolean.TRUE);
} catch (DataAccessException e) {
if (i == masterIndex) {
fatalLog.error("[db-error] master db {} down.", getIpFromUrl(dataSourceList.get(i).getUrl()));
fatalLog.error("[db-error] master db {} down.",
getIpFromUrl(dataSourceList.get(i).getUrl()));
} else {
fatalLog.error("[db-error] slave db {} down.", getIpFromUrl(dataSourceList.get(i).getUrl()));
fatalLog.error("[db-error] slave db {} down.",
getIpFromUrl(dataSourceList.get(i).getUrl()));
}
isHealthList.set(i, Boolean.FALSE);

View File

@ -94,10 +94,10 @@ fi
JAVA_MAJOR_VERSION=$($JAVA -version 2>&1 | sed -E -n 's/.* version "([0-9]*).*$/\1/p')
if [[ "$JAVA_MAJOR_VERSION" -ge "9" ]] ; then
JAVA_OPT="${JAVA_OPT} -cp .:${BASE_DIR}/plugins/cmdb/*.jar"
JAVA_OPT="${JAVA_OPT} -cp .:${BASE_DIR}/plugins/cmdb/*.jar:${BASE_DIR}/plugins/mysql/*.jar"
JAVA_OPT="${JAVA_OPT} -Xlog:gc*:file=${BASE_DIR}/logs/nacos_gc.log:time,tags:filecount=10,filesize=102400"
else
JAVA_OPT="${JAVA_OPT} -Djava.ext.dirs=${JAVA_HOME}/jre/lib/ext:${JAVA_HOME}/lib/ext:${BASE_DIR}/plugins/cmdb"
JAVA_OPT="${JAVA_OPT} -Djava.ext.dirs=${JAVA_HOME}/jre/lib/ext:${JAVA_HOME}/lib/ext:${BASE_DIR}/plugins/cmdb:${BASE_DIR}/plugins/mysql"
JAVA_OPT="${JAVA_OPT} -Xloggc:${BASE_DIR}/logs/nacos_gc.log -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=10 -XX:GCLogFileSize=100M"
fi

View File

@ -131,6 +131,7 @@
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>provided</scope>
</dependency>
<!-- log -->