Skip to content

Commit

Permalink
Merge pull request alibaba#458 from alibaba/feature_stanalone_mysql
Browse files Browse the repository at this point in the history
  • Loading branch information
hxy1991 authored Dec 13, 2018
2 parents 198c6a7 + 505252c commit e623384
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public String doGetConfig(HttpServletRequest request, HttpServletResponse respon
if (isBeta) {
md5 = cacheItem.getMd54Beta();
lastModified = cacheItem.getLastModifiedTs4Beta();
if (STANDALONE_MODE) {
if (STANDALONE_MODE && !PropertyUtil.isStandaloneUseMysql()) {
configInfoBase = persistService.findConfigInfo4Beta(dataId, group, tenant);
} else {
file = DiskUtil.targetBetaFile(dataId, group, tenant);
Expand All @@ -154,7 +154,7 @@ public String doGetConfig(HttpServletRequest request, HttpServletResponse respon
lastModified = cacheItem.tagLastModifiedTs.get(autoTag);
}
}
if (STANDALONE_MODE) {
if (STANDALONE_MODE && !PropertyUtil.isStandaloneUseMysql()) {
configInfoBase = persistService.findConfigInfo4Tag(dataId, group, tenant, autoTag);
} else {
file = DiskUtil.targetTagFile(dataId, group, tenant, autoTag);
Expand All @@ -165,7 +165,7 @@ public String doGetConfig(HttpServletRequest request, HttpServletResponse respon
} else {
md5 = cacheItem.getMd5();
lastModified = cacheItem.getLastModifiedTs();
if (STANDALONE_MODE) {
if (STANDALONE_MODE && !PropertyUtil.isStandaloneUseMysql()) {
configInfoBase = persistService.findConfigInfo(dataId, group, tenant);
} else {
file = DiskUtil.targetFile(dataId, group, tenant);
Expand Down Expand Up @@ -197,7 +197,7 @@ public String doGetConfig(HttpServletRequest request, HttpServletResponse respon
}
}
}
if (STANDALONE_MODE) {
if (STANDALONE_MODE && !PropertyUtil.isStandaloneUseMysql()) {
configInfoBase = persistService.findConfigInfo4Tag(dataId, group, tenant, tag);
} else {
file = DiskUtil.targetTagFile(dataId, group, tenant, tag);
Expand Down Expand Up @@ -227,14 +227,14 @@ public String doGetConfig(HttpServletRequest request, HttpServletResponse respon
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0);
response.setHeader("Cache-Control", "no-cache,no-store");
if (STANDALONE_MODE) {
if (STANDALONE_MODE && !PropertyUtil.isStandaloneUseMysql()) {
response.setDateHeader("Last-Modified", lastModified);
} else {
fis = new FileInputStream(file);
response.setDateHeader("Last-Modified", file.lastModified());
}

if (STANDALONE_MODE) {
if (STANDALONE_MODE && !PropertyUtil.isStandaloneUseMysql()) {
out = response.getWriter();
out.print(configInfoBase.getContent());
out.flush();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.alibaba.nacos.config.server.service;

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;
Expand Down Expand Up @@ -50,6 +51,9 @@
public class BasicDataSourceServiceImpl implements DataSourceService {
private static final String JDBC_DRIVER_NAME = "com.mysql.jdbc.Driver";

@Autowired
private PropertyUtil propertyUtil;

/**
* JDBC执行超时时间, 单位秒
*/
Expand Down Expand Up @@ -105,7 +109,7 @@ public void init() {
* 事务的超时时间需要与普通操作区分开
*/
tjt.setTimeout(TRANSACTION_QUERY_TIMEOUT);
if (!STANDALONE_MODE) {
if (!STANDALONE_MODE || propertyUtil.isStandaloneUseMysql()) {
try {
reload();
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.alibaba.nacos.config.server.utils.GroupKey;
import com.alibaba.nacos.config.server.utils.GroupKey2;
import com.alibaba.nacos.config.server.utils.MD5;
import com.alibaba.nacos.config.server.utils.PropertyUtil;
import com.alibaba.nacos.config.server.utils.event.EventDispatcher;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
Expand Down Expand Up @@ -74,7 +75,7 @@ static public boolean dump(String dataId, String group, String tenant, String co
"[dump-ignore] ignore to save cache file. groupKey={}, md5={}, lastModifiedOld={}, "
+ "lastModifiedNew={}",
groupKey, md5, ConfigService.getLastModifiedTs(groupKey), lastModifiedTs);
} else if (!STANDALONE_MODE) {
} else if (!STANDALONE_MODE || PropertyUtil.isStandaloneUseMysql()) {
DiskUtil.saveToDisk(dataId, group, tenant, content);
}
updateMd5(groupKey, md5, lastModifiedTs);
Expand Down Expand Up @@ -119,7 +120,7 @@ static public boolean dumpBeta(String dataId, String group, String tenant, Strin
"[dump-beta-ignore] ignore to save cache file. groupKey={}, md5={}, lastModifiedOld={}, "
+ "lastModifiedNew={}",
groupKey, md5, ConfigService.getLastModifiedTs(groupKey), lastModifiedTs);
} else if (!STANDALONE_MODE) {
} else if (!STANDALONE_MODE || PropertyUtil.isStandaloneUseMysql()) {
DiskUtil.saveBetaToDisk(dataId, group, tenant, content);
}
String[] betaIpsArr = betaIps.split(",");
Expand Down Expand Up @@ -158,7 +159,7 @@ static public boolean dumpTag(String dataId, String group, String tenant, String
"[dump-tag-ignore] ignore to save cache file. groupKey={}, md5={}, lastModifiedOld={}, "
+ "lastModifiedNew={}",
groupKey, md5, ConfigService.getLastModifiedTs(groupKey), lastModifiedTs);
} else if (!STANDALONE_MODE) {
} else if (!STANDALONE_MODE || PropertyUtil.isStandaloneUseMysql()) {
DiskUtil.saveTagToDisk(dataId, group, tenant, tag, content);
}

Expand Down Expand Up @@ -190,7 +191,7 @@ static public boolean dumpChange(String dataId, String group, String tenant, Str

try {
final String md5 = MD5.getInstance().getMD5String(content);
if (!STANDALONE_MODE) {
if (!STANDALONE_MODE || PropertyUtil.isStandaloneUseMysql()) {
String loacalMd5 = DiskUtil.getLocalConfigMd5(dataId, group, tenant);
if (md5.equals(loacalMd5)) {
dumpLog.warn(
Expand All @@ -215,7 +216,7 @@ static public boolean dumpChange(String dataId, String group, String tenant, Str
static public void reloadConfig() {
String aggreds = null;
try {
if (STANDALONE_MODE) {
if (STANDALONE_MODE && !PropertyUtil.isStandaloneUseMysql()) {
ConfigInfoBase config = persistService.findConfigInfoBase(AggrWhitelist.AGGRIDS_METADATA,
"DEFAULT_GROUP");
if (config != null) {
Expand All @@ -234,7 +235,7 @@ static public void reloadConfig() {

String clientIpWhitelist = null;
try {
if (STANDALONE_MODE) {
if (STANDALONE_MODE && !PropertyUtil.isStandaloneUseMysql()) {
ConfigInfoBase config = persistService.findConfigInfoBase(
ClientIpWhiteList.CLIENT_IP_WHITELIST_METADATA, "DEFAULT_GROUP");
if (config != null) {
Expand All @@ -254,7 +255,7 @@ static public void reloadConfig() {

String switchContent = null;
try {
if (STANDALONE_MODE) {
if (STANDALONE_MODE && !PropertyUtil.isStandaloneUseMysql()) {
ConfigInfoBase config = persistService.findConfigInfoBase(SwitchService.SWITCH_META_DATAID,
"DEFAULT_GROUP");
if (config != null) {
Expand Down Expand Up @@ -322,7 +323,7 @@ static public boolean remove(String dataId, String group, String tenant) {
}

try {
if (!STANDALONE_MODE) {
if (!STANDALONE_MODE || PropertyUtil.isStandaloneUseMysql()) {
DiskUtil.removeConfigInfo(dataId, group, tenant);
}
CACHE.remove(groupKey);
Expand Down Expand Up @@ -356,7 +357,7 @@ static public boolean removeBeta(String dataId, String group, String tenant) {
}

try {
if (!STANDALONE_MODE) {
if (!STANDALONE_MODE || PropertyUtil.isStandaloneUseMysql()) {
DiskUtil.removeConfigInfo4Beta(dataId, group, tenant);
}
EventDispatcher.fireEvent(new LocalDataChangeEvent(groupKey, true, CACHE.get(groupKey).getIps4Beta()));
Expand Down Expand Up @@ -391,7 +392,7 @@ static public boolean removeTag(String dataId, String group, String tenant, Stri
}

try {
if (!STANDALONE_MODE) {
if (!STANDALONE_MODE || PropertyUtil.isStandaloneUseMysql()) {
DiskUtil.removeConfigInfo4Tag(dataId, group, tenant, tag);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.alibaba.nacos.config.server.service;

import com.alibaba.nacos.config.server.utils.PropertyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
Expand All @@ -28,6 +30,10 @@
*/
@Component
public class DynamicDataSource implements ApplicationContextAware {

@Autowired
private PropertyUtil propertyUtil;

private ApplicationContext applicationContext;

public void setApplicationContext(ApplicationContext applicationContext) {
Expand All @@ -41,7 +47,7 @@ public ApplicationContext getApplicationContext() {
public DataSourceService getDataSource() {
DataSourceService dataSourceService = null;

if (STANDALONE_MODE) {
if (STANDALONE_MODE && !propertyUtil.isStandaloneUseMysql()) {
dataSourceService = (DataSourceService)applicationContext.getBean("localDataSourceService");
} else {
dataSourceService = (DataSourceService)applicationContext.getBean("basicDataSourceService");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@

import com.alibaba.nacos.config.server.constant.Constants;
import com.alibaba.nacos.config.server.utils.LogUtil;
import com.alibaba.nacos.config.server.utils.PropertyUtil;
import com.alibaba.nacos.config.server.utils.StringUtils;
import org.apache.commons.dbcp.BasicDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -60,6 +62,9 @@ public class LocalDataSourceServiceImpl implements DataSourceService {
private JdbcTemplate jt;
private TransactionTemplate tjt;

@Autowired
private PropertyUtil propertyUtil;

@PostConstruct
public void init() {
BasicDataSource ds = new BasicDataSource();
Expand All @@ -85,7 +90,7 @@ public void init() {
tm.setDataSource(ds);
tjt.setTimeout(5000);

if (STANDALONE_MODE) {
if (STANDALONE_MODE && !propertyUtil.isStandaloneUseMysql()) {
reload();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.alibaba.nacos.config.server.model.capacity.GroupCapacity;
import com.alibaba.nacos.config.server.service.DataSourceService;
import com.alibaba.nacos.config.server.service.DynamicDataSource;
import com.alibaba.nacos.config.server.utils.PropertyUtil;
import com.alibaba.nacos.config.server.utils.TimeUtils;
import com.google.common.collect.Lists;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
Expand Down Expand Up @@ -281,7 +282,7 @@ public boolean correctUsage(String group, Timestamp gmtModified) {
public List<GroupCapacity> getCapacityList4CorrectUsage(long lastId, int pageSize) {
String sql = "SELECT id, group_id FROM group_capacity WHERE id>? LIMIT ?";

if (STANDALONE_MODE) {
if (STANDALONE_MODE && !PropertyUtil.isStandaloneUseMysql()) {
sql = "SELECT id, group_id FROM group_capacity WHERE id>? OFFSET 0 ROWS FETCH NEXT ? ROWS ONLY";
}
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.alibaba.nacos.config.server.model.capacity.TenantCapacity;
import com.alibaba.nacos.config.server.service.DataSourceService;
import com.alibaba.nacos.config.server.service.DynamicDataSource;
import com.alibaba.nacos.config.server.utils.PropertyUtil;
import com.alibaba.nacos.config.server.utils.TimeUtils;
import com.google.common.collect.Lists;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
Expand Down Expand Up @@ -231,7 +232,7 @@ public boolean correctUsage(String tenant, Timestamp gmtModified) {
public List<TenantCapacity> getCapacityList4CorrectUsage(long lastId, int pageSize) {
String sql = "SELECT id, tenant_id FROM tenant_capacity WHERE id>? LIMIT ?";

if (STANDALONE_MODE) {
if (STANDALONE_MODE && !PropertyUtil.isStandaloneUseMysql()) {
sql = "SELECT id, tenant_id FROM tenant_capacity WHERE id>? OFFSET 0 ROWS FETCH NEXT ? ROWS ONLY";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public Page<E> fetchPage(final JdbcTemplate jt, final String sqlCountRows, final

final int startRow = (pageNo - 1) * pageSize;
String selectSQL = "";
if (STANDALONE_MODE) {
if (STANDALONE_MODE && !PropertyUtil.isStandaloneUseMysql()) {
selectSQL = sqlFetchRows + " OFFSET " + startRow + " ROWS FETCH NEXT " + pageSize + " ROWS ONLY";
} else if (lastMaxId != null) {
selectSQL = sqlFetchRows + " and id > " + lastMaxId + " order by id asc" + " limit " + 0 + "," + pageSize;
Expand Down Expand Up @@ -127,7 +127,7 @@ public Page<E> fetchPageLimit(final JdbcTemplate jt, final String sqlCountRows,
}

String selectSQL = sqlFetchRows;
if (STANDALONE_MODE) {
if (STANDALONE_MODE && !PropertyUtil.isStandaloneUseMysql()) {
selectSQL = selectSQL.replaceAll("(?i)LIMIT \\?,\\?", "OFFSET ? ROWS FETCH NEXT ? ROWS ONLY");
}

Expand Down Expand Up @@ -169,7 +169,7 @@ public Page<E> fetchPageLimit(final JdbcTemplate jt, final String sqlCountRows,
}

String selectSQL = sqlFetchRows;
if (STANDALONE_MODE) {
if (STANDALONE_MODE && !PropertyUtil.isStandaloneUseMysql()) {
selectSQL = selectSQL.replaceAll("(?i)LIMIT \\?,\\?", "OFFSET ? ROWS FETCH NEXT ? ROWS ONLY");
}

Expand All @@ -190,7 +190,7 @@ public Page<E> fetchPageLimit(final JdbcTemplate jt, final String sqlFetchRows,
final Page<E> page = new Page<E>();

String selectSQL = sqlFetchRows;
if (STANDALONE_MODE) {
if (STANDALONE_MODE && !PropertyUtil.isStandaloneUseMysql()) {
selectSQL = selectSQL.replaceAll("(?i)LIMIT \\?,\\?", "OFFSET ? ROWS FETCH NEXT ? ROWS ONLY");
}

Expand All @@ -204,7 +204,7 @@ public Page<E> fetchPageLimit(final JdbcTemplate jt, final String sqlFetchRows,
public void updateLimit(final JdbcTemplate jt, final String sql, final Object args[]) {
String sqlUpdate = sql;

if (STANDALONE_MODE) {
if (STANDALONE_MODE && !PropertyUtil.isStandaloneUseMysql()) {
sqlUpdate = sqlUpdate.replaceAll("limit \\?", "OFFSET 0 ROWS FETCH NEXT ? ROWS ONLY");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ public class PropertyUtil {
* 修正容量信息表使用量(usage)的时间间隔,单位为秒
*/
private static int correctUsageDelay = 10 * 60;
/**
* 单机模式使用db
*/
private static boolean standaloneUseMysql = false;


@Autowired
private Environment env;
Expand Down Expand Up @@ -109,6 +114,7 @@ public void init() {
setDefaultMaxAggrSize(getInt("defaultMaxAggrSize", defaultMaxAggrSize));
setCorrectUsageDelay(getInt("correctUsageDelay", correctUsageDelay));
setInitialExpansionPercent(getInt("initialExpansionPercent", initialExpansionPercent));
setStandaloneUseMysql(getString("spring.datasource.platform", "").equals("mysql"));

} catch (Exception e) {
logger.error("read application.properties failed", e);
Expand Down Expand Up @@ -204,6 +210,10 @@ public static boolean isStandaloneMode() {
return STANDALONE_MODE;
}

public static boolean isStandaloneUseMysql() {
return standaloneUseMysql;
}

public static void setNotifyConnectTimeout(int notifyConnectTimeout) {
PropertyUtil.notifyConnectTimeout = notifyConnectTimeout;
}
Expand Down Expand Up @@ -263,5 +273,7 @@ public static void setInitialExpansionPercent(int initialExpansionPercent) {
public static void setCorrectUsageDelay(int correctUsageDelay) {
PropertyUtil.correctUsageDelay = correctUsageDelay;
}

public static void setStandaloneUseMysql(boolean standaloneUseMysql) {
PropertyUtil.standaloneUseMysql = standaloneUseMysql;
}
}
Loading

0 comments on commit e623384

Please sign in to comment.