From c0a25c148a97027a5704c374d4d0853a73be0f1f Mon Sep 17 00:00:00 2001 From: mercyblitz Date: Mon, 1 Oct 2018 22:54:37 +0800 Subject: [PATCH] Polish alibaba/nacos#105 --- .../{SystemUtil.java => SystemUtils.java} | 13 +- .../ClusterDataSourceConfiguration.java | 2 +- .../NacosConfigConfiguration.java | 29 ++ .../server/controller/ConfigServletInner.java | 13 +- .../{WebFilter.java => NacosWebFilter.java} | 5 +- .../service/BasicDataSourceServiceImpl.java | 4 +- .../config/server/service/ConfigService.java | 22 +- .../server/service/DynamicDataSource.java | 5 +- .../service/LocalDataSourceServiceImpl.java | 5 +- .../server/service/ServerListService.java | 3 +- .../capacity/GroupCapacityPersistService.java | 4 +- .../TenantCapacityPersistService.java | 4 +- .../server/service/dump/DumpService.java | 3 +- .../config/server/utils/PaginationHelper.java | 12 +- .../config/server/utils/PropertyUtil.java | 393 +++++++++--------- .../nacos/naming/misc/NamingProxy.java | 11 +- .../nacos/naming/misc/UtilsAndCommons.java | 2 - .../alibaba/nacos/naming/raft/PeerSet.java | 9 +- .../alibaba/nacos/naming/raft/RaftCore.java | 4 +- .../alibaba/nacos/naming/web/ApiCommands.java | 14 +- 20 files changed, 297 insertions(+), 260 deletions(-) rename common/src/main/java/com/alibaba/nacos/common/util/{SystemUtil.java => SystemUtils.java} (91%) create mode 100644 config/src/main/java/com/alibaba/nacos/config/server/configuration/NacosConfigConfiguration.java rename config/src/main/java/com/alibaba/nacos/config/server/filter/{WebFilter.java => NacosWebFilter.java} (93%) diff --git a/common/src/main/java/com/alibaba/nacos/common/util/SystemUtil.java b/common/src/main/java/com/alibaba/nacos/common/util/SystemUtils.java similarity index 91% rename from common/src/main/java/com/alibaba/nacos/common/util/SystemUtil.java rename to common/src/main/java/com/alibaba/nacos/common/util/SystemUtils.java index 54f2f9181..49a964226 100644 --- a/common/src/main/java/com/alibaba/nacos/common/util/SystemUtil.java +++ b/common/src/main/java/com/alibaba/nacos/common/util/SystemUtils.java @@ -16,18 +16,23 @@ package com.alibaba.nacos.common.util; +import com.sun.management.OperatingSystemMXBean; +import org.apache.commons.lang3.StringUtils; + import java.lang.management.ManagementFactory; -import java.sql.SQLException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import org.apache.commons.lang3.StringUtils; -import com.sun.management.OperatingSystemMXBean; /** * @author nacos */ -public class SystemUtil { +public class SystemUtils { + + /** + * Standalone mode or not + */ + public static final boolean STANDALONE_MODE = Boolean.getBoolean("nacos.standalone"); private static OperatingSystemMXBean operatingSystemMXBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean(); diff --git a/config/src/main/java/com/alibaba/nacos/config/server/configuration/ClusterDataSourceConfiguration.java b/config/src/main/java/com/alibaba/nacos/config/server/configuration/ClusterDataSourceConfiguration.java index c4e44cde1..c4a42d710 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/configuration/ClusterDataSourceConfiguration.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/configuration/ClusterDataSourceConfiguration.java @@ -104,7 +104,7 @@ // * 事务的超时时间需要与普通操作区分开 // */ // tjt.setTimeout(TRANSACTION_QUERY_TIMEOUT); -// if (!PropertyUtil.isStandaloneMode()) { +// if (!STANDALONE_MODE) { // try { // reload(); // } catch (IOException e) { diff --git a/config/src/main/java/com/alibaba/nacos/config/server/configuration/NacosConfigConfiguration.java b/config/src/main/java/com/alibaba/nacos/config/server/configuration/NacosConfigConfiguration.java new file mode 100644 index 000000000..383379b0a --- /dev/null +++ b/config/src/main/java/com/alibaba/nacos/config/server/configuration/NacosConfigConfiguration.java @@ -0,0 +1,29 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.nacos.config.server.configuration; + +import org.springframework.context.annotation.Configuration; + +/** + * Nacos Config {@link Configuration} includes required Spring components. + * + * @author Mercy + * @since 0.2.2 + */ +@Configuration +public class NacosConfigConfiguration { +} diff --git a/config/src/main/java/com/alibaba/nacos/config/server/controller/ConfigServletInner.java b/config/src/main/java/com/alibaba/nacos/config/server/controller/ConfigServletInner.java index 1d4782bd0..cd8883eba 100755 --- a/config/src/main/java/com/alibaba/nacos/config/server/controller/ConfigServletInner.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/controller/ConfigServletInner.java @@ -41,6 +41,7 @@ import java.nio.charset.StandardCharsets; import java.util.List; import java.util.Map; +import static com.alibaba.nacos.common.util.SystemUtils.STANDALONE_MODE; import static com.alibaba.nacos.config.server.utils.LogUtil.pullLog; /** @@ -133,7 +134,7 @@ public class ConfigServletInner { if (isBeta) { md5 = cacheItem.getMd54Beta(); lastModified = cacheItem.getLastModifiedTs4Beta(); - if (PropertyUtil.isStandaloneMode()) { + if (STANDALONE_MODE) { configInfoBase = persistService.findConfigInfo4Beta(dataId, group,tenant); } else { file = DiskUtil.targetBetaFile(dataId, group, tenant); @@ -150,7 +151,7 @@ public class ConfigServletInner { lastModified = cacheItem.tagLastModifiedTs.get(autoTag); } } - if (PropertyUtil.isStandaloneMode()) { + if (STANDALONE_MODE) { configInfoBase = persistService.findConfigInfo4Tag(dataId, group, tenant, autoTag); } else { file = DiskUtil.targetTagFile(dataId, group, tenant, autoTag); @@ -161,7 +162,7 @@ public class ConfigServletInner { } else { md5 = cacheItem.getMd5(); lastModified = cacheItem.getLastModifiedTs(); - if (PropertyUtil.isStandaloneMode()) { + if (STANDALONE_MODE) { configInfoBase = persistService.findConfigInfo(dataId, group, tenant); } else { file = DiskUtil.targetFile(dataId, group, tenant); @@ -193,7 +194,7 @@ public class ConfigServletInner { } } } - if (PropertyUtil.isStandaloneMode()) { + if (STANDALONE_MODE) { configInfoBase = persistService.findConfigInfo4Tag(dataId, group, tenant, tag); } else { file = DiskUtil.targetTagFile(dataId, group, tenant, tag); @@ -222,7 +223,7 @@ public class ConfigServletInner { response.setHeader("Pragma", "no-cache"); response.setDateHeader("Expires", 0); response.setHeader("Cache-Control", "no-cache,no-store"); - if (PropertyUtil.isStandaloneMode()) { + if (STANDALONE_MODE) { response.setDateHeader("Last-Modified", lastModified); } else { fis = new FileInputStream(file); @@ -230,7 +231,7 @@ public class ConfigServletInner { } - if (PropertyUtil.isStandaloneMode()) { + if (STANDALONE_MODE) { out = response.getWriter(); out.print(configInfoBase.getContent()); out.flush(); diff --git a/config/src/main/java/com/alibaba/nacos/config/server/filter/WebFilter.java b/config/src/main/java/com/alibaba/nacos/config/server/filter/NacosWebFilter.java similarity index 93% rename from config/src/main/java/com/alibaba/nacos/config/server/filter/WebFilter.java rename to config/src/main/java/com/alibaba/nacos/config/server/filter/NacosWebFilter.java index 60539714a..86a882ac4 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/filter/WebFilter.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/filter/NacosWebFilter.java @@ -19,6 +19,7 @@ import com.alibaba.nacos.config.server.constant.Constants; import org.springframework.core.annotation.Order; import javax.servlet.*; +import javax.servlet.annotation.WebFilter; import java.io.IOException; import static com.alibaba.nacos.config.server.utils.LogUtil.defaultLog; @@ -30,8 +31,8 @@ import static com.alibaba.nacos.config.server.utils.LogUtil.defaultLog; * */ @Order(1) -@javax.servlet.annotation.WebFilter(filterName = "webFilter", urlPatterns = "/*") -public class WebFilter implements Filter { +@WebFilter(filterName = "webFilter", urlPatterns = "/*") +public class NacosWebFilter implements Filter { static private String webRootPath; diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/BasicDataSourceServiceImpl.java b/config/src/main/java/com/alibaba/nacos/config/server/service/BasicDataSourceServiceImpl.java index f004580af..a5ef71ecb 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/BasicDataSourceServiceImpl.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/BasicDataSourceServiceImpl.java @@ -15,7 +15,6 @@ */ 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; @@ -37,6 +36,7 @@ import java.util.concurrent.TimeUnit; import java.util.regex.Matcher; import java.util.regex.Pattern; +import static com.alibaba.nacos.common.util.SystemUtils.STANDALONE_MODE; import static com.alibaba.nacos.config.server.service.PersistService.CONFIG_INFO4BETA_ROW_MAPPER; import static com.alibaba.nacos.config.server.utils.LogUtil.defaultLog; import static com.alibaba.nacos.config.server.utils.LogUtil.fatalLog; @@ -105,7 +105,7 @@ public class BasicDataSourceServiceImpl implements DataSourceService { * 事务的超时时间需要与普通操作区分开 */ tjt.setTimeout(TRANSACTION_QUERY_TIMEOUT); - if (!PropertyUtil.isStandaloneMode()) { + if (!STANDALONE_MODE) { try { reload(); } catch (IOException e) { diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/ConfigService.java b/config/src/main/java/com/alibaba/nacos/config/server/service/ConfigService.java index 95f209ab8..3b5c6178d 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/ConfigService.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/ConfigService.java @@ -21,7 +21,6 @@ import com.alibaba.nacos.config.server.model.ConfigInfoBase; 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; @@ -33,6 +32,7 @@ import java.util.*; import java.util.Map.Entry; import java.util.concurrent.ConcurrentHashMap; +import static com.alibaba.nacos.common.util.SystemUtils.STANDALONE_MODE; import static com.alibaba.nacos.config.server.utils.LogUtil.*; /** @@ -74,7 +74,7 @@ public class ConfigService { "[dump-ignore] ignore to save cache file. groupKey={}, md5={}, lastModifiedOld={}, " + "lastModifiedNew={}", groupKey, md5, ConfigService.getLastModifiedTs(groupKey), lastModifiedTs); - } else if (!PropertyUtil.isStandaloneMode()) { + } else if (!STANDALONE_MODE) { DiskUtil.saveToDisk(dataId, group, tenant, content); } updateMd5(groupKey, md5, lastModifiedTs); @@ -118,7 +118,7 @@ public class ConfigService { "[dump-beta-ignore] ignore to save cache file. groupKey={}, md5={}, lastModifiedOld={}, " + "lastModifiedNew={}", groupKey, md5, ConfigService.getLastModifiedTs(groupKey), lastModifiedTs); - } else if (!PropertyUtil.isStandaloneMode()) { + } else if (!STANDALONE_MODE) { DiskUtil.saveBetaToDisk(dataId, group, tenant, content); } String[] betaIpsArr = betaIps.split(","); @@ -156,7 +156,7 @@ public class ConfigService { "[dump-tag-ignore] ignore to save cache file. groupKey={}, md5={}, lastModifiedOld={}, " + "lastModifiedNew={}", groupKey, md5, ConfigService.getLastModifiedTs(groupKey), lastModifiedTs); - } else if (!PropertyUtil.isStandaloneMode()) { + } else if (!STANDALONE_MODE) { DiskUtil.saveTagToDisk(dataId, group, tenant, tag, content); } @@ -188,7 +188,7 @@ public class ConfigService { try { final String md5 = MD5.getInstance().getMD5String(content); - if (!PropertyUtil.isStandaloneMode()) { + if (!STANDALONE_MODE) { String loacalMd5 = DiskUtil.getLocalConfigMd5(dataId, group, tenant); if(md5.equals(loacalMd5)) { dumpLog.warn( @@ -214,7 +214,7 @@ public class ConfigService { { String aggreds = null; try { - if (PropertyUtil.isStandaloneMode()) { + if (STANDALONE_MODE) { ConfigInfoBase config = persistService.findConfigInfoBase(AggrWhitelist.AGGRIDS_METADATA, "DEFAULT_GROUP"); if (config != null) { aggreds = config.getContent(); @@ -232,7 +232,7 @@ public class ConfigService { String clientIpWhitelist = null; try { - if (PropertyUtil.isStandaloneMode()) { + if (STANDALONE_MODE) { ConfigInfoBase config = persistService.findConfigInfoBase(ClientIpWhiteList.CLIENT_IP_WHITELIST_METADATA, "DEFAULT_GROUP"); if (config != null) { clientIpWhitelist = config.getContent(); @@ -251,7 +251,7 @@ public class ConfigService { String switchContent= null; try { - if (PropertyUtil.isStandaloneMode()) { + if (STANDALONE_MODE) { ConfigInfoBase config = persistService.findConfigInfoBase(SwitchService.SWITCH_META_DATAID, "DEFAULT_GROUP"); if (config != null) { switchContent = config.getContent(); @@ -318,7 +318,7 @@ public class ConfigService { } try { - if (!PropertyUtil.isStandaloneMode()) { + if (!STANDALONE_MODE) { DiskUtil.removeConfigInfo(dataId, group, tenant); } CACHE.remove(groupKey); @@ -351,7 +351,7 @@ public class ConfigService { } try { - if (!PropertyUtil.isStandaloneMode()) { + if (!STANDALONE_MODE) { DiskUtil.removeConfigInfo4Beta(dataId, group, tenant); } EventDispatcher.fireEvent(new LocalDataChangeEvent(groupKey, true, CACHE.get(groupKey).getIps4Beta())); @@ -386,7 +386,7 @@ public class ConfigService { } try { - if (!PropertyUtil.isStandaloneMode()) { + if (!STANDALONE_MODE) { DiskUtil.removeConfigInfo4Tag(dataId, group, tenant, tag); } diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/DynamicDataSource.java b/config/src/main/java/com/alibaba/nacos/config/server/service/DynamicDataSource.java index b720aaf3e..74d0593f9 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/DynamicDataSource.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/DynamicDataSource.java @@ -15,11 +15,12 @@ */ package com.alibaba.nacos.config.server.service; -import com.alibaba.nacos.config.server.utils.PropertyUtil; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; +import static com.alibaba.nacos.common.util.SystemUtils.STANDALONE_MODE; + /** * datasource adapter * @author Nacos @@ -40,7 +41,7 @@ public class DynamicDataSource implements ApplicationContextAware { public DataSourceService getDataSource() { DataSourceService dataSourceService = null; - if (PropertyUtil.isStandaloneMode()) { + if (STANDALONE_MODE) { dataSourceService = (DataSourceService)applicationContext.getBean("localDataSourceService"); } else { dataSourceService = (DataSourceService)applicationContext.getBean("basicDataSourceService"); diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/LocalDataSourceServiceImpl.java b/config/src/main/java/com/alibaba/nacos/config/server/service/LocalDataSourceServiceImpl.java index f12008b44..abc105de5 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/LocalDataSourceServiceImpl.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/LocalDataSourceServiceImpl.java @@ -17,7 +17,6 @@ package com.alibaba.nacos.config.server.service; 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; @@ -39,6 +38,8 @@ import java.util.ArrayList; import java.util.List; import java.util.concurrent.TimeUnit; +import static com.alibaba.nacos.common.util.SystemUtils.STANDALONE_MODE; + /** * local data source * @@ -88,7 +89,7 @@ public class LocalDataSourceServiceImpl implements DataSourceService { tm.setDataSource(ds); tjt.setTimeout(5000); - if (PropertyUtil.isStandaloneMode()) { + if (STANDALONE_MODE) { reload(); } } diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/ServerListService.java b/config/src/main/java/com/alibaba/nacos/config/server/service/ServerListService.java index cff097f9e..49c0f26cf 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/ServerListService.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/ServerListService.java @@ -47,6 +47,7 @@ import java.util.*; import java.util.concurrent.ThreadFactory; import java.util.concurrent.TimeUnit; +import static com.alibaba.nacos.common.util.SystemUtils.STANDALONE_MODE; import static com.alibaba.nacos.config.server.utils.LogUtil.defaultLog; import static com.alibaba.nacos.config.server.utils.LogUtil.fatalLog; @@ -240,7 +241,7 @@ public class ServerListService implements ApplicationListener getCapacityList4CorrectUsage(long lastId, int pageSize) { String sql = "select id, group_id from group_capacity where id>? limit ?"; - if (PropertyUtil.isStandaloneMode()) { + if (STANDALONE_MODE) { sql = "select id, group_id from group_capacity where id>? OFFSET 0 ROWS FETCH NEXT ? ROWS ONLY"; } try { diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/capacity/TenantCapacityPersistService.java b/config/src/main/java/com/alibaba/nacos/config/server/service/capacity/TenantCapacityPersistService.java index a435e9746..01c3b06bc 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/capacity/TenantCapacityPersistService.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/capacity/TenantCapacityPersistService.java @@ -18,7 +18,6 @@ package com.alibaba.nacos.config.server.service.capacity; 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; @@ -34,6 +33,7 @@ import javax.annotation.PostConstruct; import java.sql.*; import java.util.List; +import static com.alibaba.nacos.common.util.SystemUtils.STANDALONE_MODE; import static com.alibaba.nacos.config.server.utils.LogUtil.fatalLog; /** @@ -229,7 +229,7 @@ public class TenantCapacityPersistService { public List getCapacityList4CorrectUsage(long lastId, int pageSize) { String sql = "select id, tenant_id from tenant_capacity where id>? limit ?"; - if (PropertyUtil.isStandaloneMode()) { + if (STANDALONE_MODE) { sql = "select id, tenant_id from tenant_capacity where id>? OFFSET 0 ROWS FETCH NEXT ? ROWS ONLY"; } diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/dump/DumpService.java b/config/src/main/java/com/alibaba/nacos/config/server/service/dump/DumpService.java index 87a385b5f..40f770088 100755 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/dump/DumpService.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/dump/DumpService.java @@ -46,6 +46,7 @@ import java.util.Random; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; +import static com.alibaba.nacos.common.util.SystemUtils.STANDALONE_MODE; import static com.alibaba.nacos.config.server.utils.LogUtil.fatalLog; /** @@ -152,7 +153,7 @@ public class DumpService { throw new RuntimeException( "Nacos Server did not start because dumpservice bean construction failure :\n" + e.getMessage()); } - if (!PropertyUtil.isStandaloneMode()) { + if (!STANDALONE_MODE) { Runnable heartbeat = new Runnable() { @Override public void run() { diff --git a/config/src/main/java/com/alibaba/nacos/config/server/utils/PaginationHelper.java b/config/src/main/java/com/alibaba/nacos/config/server/utils/PaginationHelper.java index a4b42554b..7a96a2f1d 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/utils/PaginationHelper.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/utils/PaginationHelper.java @@ -21,6 +21,8 @@ import org.springframework.jdbc.core.RowMapper; import java.util.List; +import static com.alibaba.nacos.common.util.SystemUtils.STANDALONE_MODE; + /** * 分页辅助类 @@ -87,7 +89,7 @@ public class PaginationHelper { final int startRow = (pageNo - 1) * pageSize; String selectSQL = ""; - if (PropertyUtil.isStandaloneMode()) { + if (STANDALONE_MODE) { 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; @@ -131,7 +133,7 @@ public class PaginationHelper { } String selectSQL = sqlFetchRows; - if (PropertyUtil.isStandaloneMode()) { + if (STANDALONE_MODE) { selectSQL = selectSQL.replaceAll("(?i)LIMIT \\?,\\?", "OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"); } @@ -171,7 +173,7 @@ public class PaginationHelper { } String selectSQL = sqlFetchRows; - if (PropertyUtil.isStandaloneMode()) { + if (STANDALONE_MODE) { selectSQL = selectSQL.replaceAll("(?i)LIMIT \\?,\\?", "OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"); } @@ -191,7 +193,7 @@ public class PaginationHelper { final Page page = new Page(); String selectSQL = sqlFetchRows; - if (PropertyUtil.isStandaloneMode()) { + if (STANDALONE_MODE) { selectSQL = selectSQL.replaceAll("(?i)LIMIT \\?,\\?", "OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"); } @@ -205,7 +207,7 @@ public class PaginationHelper { public void updateLimit(final JdbcTemplate jt, final String sql, final Object args[]) { String sqlUpdate = sql; - if (PropertyUtil.isStandaloneMode()) { + if (STANDALONE_MODE) { sqlUpdate = sqlUpdate.replaceAll("limit \\?", "OFFSET 0 ROWS FETCH NEXT ? ROWS ONLY"); } diff --git a/config/src/main/java/com/alibaba/nacos/config/server/utils/PropertyUtil.java b/config/src/main/java/com/alibaba/nacos/config/server/utils/PropertyUtil.java index e2507aac9..d588ddbc8 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/utils/PropertyUtil.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/utils/PropertyUtil.java @@ -22,255 +22,246 @@ import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; +import static com.alibaba.nacos.common.util.SystemUtils.STANDALONE_MODE; + /** * properties utils - * - * @author Nacos * + * @author Nacos */ @Component public class PropertyUtil { - private final static Logger logger = LogUtil.defaultLog; - - private static int notifyConnectTimeout = 100; - private static int notifySocketTimeout = 200; - private static int maxHealthCheckFailCount = 12; - private static boolean isHealthCheck = true; - private static int maxContent = 10 * 1024 * 1024; + private final static Logger logger = LogUtil.defaultLog; - /** - * 是否开启容量管理 - */ - private static boolean isManageCapacity = true; - /** - * 是否开启容量管理的限制检验功能,包括配置个数上限、配置内容大小限制等 - */ - private static boolean isCapacityLimitCheck = false; - /** - * 集群默认容量上限 - */ - private static int defaultClusterQuota = 100000; - /** - * 每个Group默认容量上限 - */ - private static int defaultGroupQuota = 200; - /** - * 每个Tenant默认容量上限 - */ - private static int defaultTenantQuota = 200; - /** - * 单个配置中content的最大大小,单位为字节 - */ - private static int defaultMaxSize = 100 * 1024; - /** - * 聚合数据子配置最大个数 - */ - private static int defaultMaxAggrCount = 10000; - /** - * 聚合数据单个子配置中content的最大大小,单位为字节 - */ - private static int defaultMaxAggrSize = 1024; - /** - * 初始化容量信息记录时,发现已经到达限额时的扩容百分比 - */ - private static int initialExpansionPercent = 100; - /** - * 修正容量信息表使用量(usage)的时间间隔,单位为秒 - */ - private static int correctUsageDelay = 10 * 60; + private static int notifyConnectTimeout = 100; + private static int notifySocketTimeout = 200; + private static int maxHealthCheckFailCount = 12; + private static boolean isHealthCheck = true; + private static int maxContent = 10 * 1024 * 1024; - private static boolean standaloneMode = false; + /** + * 是否开启容量管理 + */ + private static boolean isManageCapacity = true; + /** + * 是否开启容量管理的限制检验功能,包括配置个数上限、配置内容大小限制等 + */ + private static boolean isCapacityLimitCheck = false; + /** + * 集群默认容量上限 + */ + private static int defaultClusterQuota = 100000; + /** + * 每个Group默认容量上限 + */ + private static int defaultGroupQuota = 200; + /** + * 每个Tenant默认容量上限 + */ + private static int defaultTenantQuota = 200; + /** + * 单个配置中content的最大大小,单位为字节 + */ + private static int defaultMaxSize = 100 * 1024; + /** + * 聚合数据子配置最大个数 + */ + private static int defaultMaxAggrCount = 10000; + /** + * 聚合数据单个子配置中content的最大大小,单位为字节 + */ + private static int defaultMaxAggrSize = 1024; + /** + * 初始化容量信息记录时,发现已经到达限额时的扩容百分比 + */ + private static int initialExpansionPercent = 100; + /** + * 修正容量信息表使用量(usage)的时间间隔,单位为秒 + */ + private static int correctUsageDelay = 10 * 60; - @Autowired - private Environment env; + @Autowired + private Environment env; - static { - setStandaloneMode(Boolean.parseBoolean(System.getProperty("nacos.standalone", "false"))); - } + @PostConstruct + public void init() { + try { - @PostConstruct - public void init() { - try { + setNotifyConnectTimeout(Integer.parseInt(env.getProperty("notifyConnectTimeout", "100"))); + logger.info("notifyConnectTimeout:{}", notifyConnectTimeout); + setNotifySocketTimeout(Integer.parseInt(env.getProperty("notifySocketTimeout", "200"))); + logger.info("notifySocketTimeout:{}", notifySocketTimeout); + setHealthCheck(Boolean.valueOf(env.getProperty("isHealthCheck", "true"))); + logger.info("isHealthCheck:{}", isHealthCheck); + setMaxHealthCheckFailCount(Integer.parseInt(env.getProperty("maxHealthCheckFailCount", "12"))); + logger.info("maxHealthCheckFailCount:{}", maxHealthCheckFailCount); + setMaxContent(Integer.parseInt(env.getProperty("maxContent", String.valueOf(maxContent)))); + logger.info("maxContent:{}", maxContent); + // 容量管理 + setManageCapacity(getBoolean("isManageCapacity", isManageCapacity)); + setCapacityLimitCheck(getBoolean("isCapacityLimitCheck", isCapacityLimitCheck)); + setDefaultClusterQuota(getInt("defaultClusterQuota", defaultClusterQuota)); + setDefaultGroupQuota(getInt("defaultGroupQuota", defaultGroupQuota)); + setDefaultTenantQuota(getInt("defaultTenantQuota", defaultTenantQuota)); + setDefaultMaxSize(getInt("defaultMaxSize", defaultMaxSize)); + setDefaultMaxAggrCount(getInt("defaultMaxAggrCount", defaultMaxAggrCount)); + setDefaultMaxAggrSize(getInt("defaultMaxAggrSize", defaultMaxAggrSize)); + setCorrectUsageDelay(getInt("correctUsageDelay", correctUsageDelay)); + setInitialExpansionPercent(getInt("initialExpansionPercent", initialExpansionPercent)); - setNotifyConnectTimeout(Integer.parseInt(env.getProperty("notifyConnectTimeout", "100"))); - logger.info("notifyConnectTimeout:{}", notifyConnectTimeout); - setNotifySocketTimeout(Integer.parseInt(env.getProperty("notifySocketTimeout", "200"))); - logger.info("notifySocketTimeout:{}", notifySocketTimeout); - setHealthCheck(Boolean.valueOf(env.getProperty("isHealthCheck", "true"))); - logger.info("isHealthCheck:{}", isHealthCheck); - setMaxHealthCheckFailCount(Integer.parseInt(env.getProperty("maxHealthCheckFailCount", "12"))); - logger.info("maxHealthCheckFailCount:{}", maxHealthCheckFailCount); - setMaxContent(Integer.parseInt(env.getProperty("maxContent", String.valueOf(maxContent)))); - logger.info("maxContent:{}", maxContent); - // 容量管理 - setManageCapacity(getBoolean("isManageCapacity", isManageCapacity)); - setCapacityLimitCheck(getBoolean("isCapacityLimitCheck", isCapacityLimitCheck)); - setDefaultClusterQuota(getInt("defaultClusterQuota", defaultClusterQuota)); - setDefaultGroupQuota(getInt("defaultGroupQuota", defaultGroupQuota)); - setDefaultTenantQuota(getInt("defaultTenantQuota", defaultTenantQuota)); - setDefaultMaxSize(getInt("defaultMaxSize", defaultMaxSize)); - setDefaultMaxAggrCount(getInt("defaultMaxAggrCount", defaultMaxAggrCount)); - setDefaultMaxAggrSize(getInt("defaultMaxAggrSize", defaultMaxAggrSize)); - setCorrectUsageDelay(getInt("correctUsageDelay", correctUsageDelay)); - setInitialExpansionPercent(getInt("initialExpansionPercent", initialExpansionPercent)); - - } catch (Exception e) { - logger.error("read application.properties failed", e); - } - } + } catch (Exception e) { + logger.error("read application.properties failed", e); + } + } - public static int getNotifyConnectTimeout() { - return notifyConnectTimeout; - } + public static int getNotifyConnectTimeout() { + return notifyConnectTimeout; + } - public static int getNotifySocketTimeout() { - return notifySocketTimeout; - } + public static int getNotifySocketTimeout() { + return notifySocketTimeout; + } - public static int getMaxHealthCheckFailCount() { - return maxHealthCheckFailCount; - } + public static int getMaxHealthCheckFailCount() { + return maxHealthCheckFailCount; + } - public static boolean isHealthCheck() { - return isHealthCheck; - } + public static boolean isHealthCheck() { + return isHealthCheck; + } - private boolean getBoolean(String key, boolean defaultValue) { - return Boolean.valueOf(getString(key, String.valueOf(defaultValue))); - } + private boolean getBoolean(String key, boolean defaultValue) { + return Boolean.valueOf(getString(key, String.valueOf(defaultValue))); + } - private int getInt(String key, int defaultValue) { - return Integer.parseInt(getString(key, String.valueOf(defaultValue))); - } + private int getInt(String key, int defaultValue) { + return Integer.parseInt(getString(key, String.valueOf(defaultValue))); + } - private String getString(String key, String defaultValue) { - String value = env.getProperty(key); - if (value == null) { - return defaultValue; - } - logger.info("{}:{}", key, value); - return value; - } + private String getString(String key, String defaultValue) { + String value = env.getProperty(key); + if (value == null) { + return defaultValue; + } + logger.info("{}:{}", key, value); + return value; + } - public String getProperty(String key) { - return env.getProperty(key); - } + public String getProperty(String key) { + return env.getProperty(key); + } - public String getProperty(String key, String defaultValue) { - return env.getProperty(key, defaultValue); - } + public String getProperty(String key, String defaultValue) { + return env.getProperty(key, defaultValue); + } - public static int getMaxContent() { - return maxContent; - } + public static int getMaxContent() { + return maxContent; + } - public static boolean isManageCapacity() { - return isManageCapacity; - } + public static boolean isManageCapacity() { + return isManageCapacity; + } - public static int getDefaultClusterQuota() { - return defaultClusterQuota; - } + public static int getDefaultClusterQuota() { + return defaultClusterQuota; + } - public static boolean isCapacityLimitCheck() { - return isCapacityLimitCheck; - } + public static boolean isCapacityLimitCheck() { + return isCapacityLimitCheck; + } - public static int getDefaultGroupQuota() { - return defaultGroupQuota; - } + public static int getDefaultGroupQuota() { + return defaultGroupQuota; + } - public static int getDefaultTenantQuota() { - return defaultTenantQuota; - } + public static int getDefaultTenantQuota() { + return defaultTenantQuota; + } - public static int getInitialExpansionPercent() { - return initialExpansionPercent; - } + public static int getInitialExpansionPercent() { + return initialExpansionPercent; + } - public static int getDefaultMaxSize() { - return defaultMaxSize; - } + public static int getDefaultMaxSize() { + return defaultMaxSize; + } - public static int getDefaultMaxAggrCount() { - return defaultMaxAggrCount; - } + public static int getDefaultMaxAggrCount() { + return defaultMaxAggrCount; + } - public static int getDefaultMaxAggrSize() { - return defaultMaxAggrSize; - } + public static int getDefaultMaxAggrSize() { + return defaultMaxAggrSize; + } - public static int getCorrectUsageDelay() { - return correctUsageDelay; - } + public static int getCorrectUsageDelay() { + return correctUsageDelay; + } - public static boolean isStandaloneMode() { - return standaloneMode; - } + public static boolean isStandaloneMode() { + return STANDALONE_MODE; + } - public static void setStandaloneMode(boolean standaloneMode) { - PropertyUtil.standaloneMode = standaloneMode; - } + public static void setNotifyConnectTimeout(int notifyConnectTimeout) { + PropertyUtil.notifyConnectTimeout = notifyConnectTimeout; + } - public static void setNotifyConnectTimeout(int notifyConnectTimeout) { - PropertyUtil.notifyConnectTimeout = notifyConnectTimeout; - } + public static void setNotifySocketTimeout(int notifySocketTimeout) { + PropertyUtil.notifySocketTimeout = notifySocketTimeout; + } - public static void setNotifySocketTimeout(int notifySocketTimeout) { - PropertyUtil.notifySocketTimeout = notifySocketTimeout; - } + public static void setMaxHealthCheckFailCount(int maxHealthCheckFailCount) { + PropertyUtil.maxHealthCheckFailCount = maxHealthCheckFailCount; + } - public static void setMaxHealthCheckFailCount(int maxHealthCheckFailCount) { - PropertyUtil.maxHealthCheckFailCount = maxHealthCheckFailCount; - } + public static void setHealthCheck(boolean isHealthCheck) { + PropertyUtil.isHealthCheck = isHealthCheck; + } - public static void setHealthCheck(boolean isHealthCheck) { - PropertyUtil.isHealthCheck = isHealthCheck; - } + public static void setMaxContent(int maxContent) { + PropertyUtil.maxContent = maxContent; + } - public static void setMaxContent(int maxContent) { - PropertyUtil.maxContent = maxContent; - } + public static void setManageCapacity(boolean isManageCapacity) { + PropertyUtil.isManageCapacity = isManageCapacity; + } - public static void setManageCapacity(boolean isManageCapacity) { - PropertyUtil.isManageCapacity = isManageCapacity; - } + public static void setCapacityLimitCheck(boolean isCapacityLimitCheck) { + PropertyUtil.isCapacityLimitCheck = isCapacityLimitCheck; + } - public static void setCapacityLimitCheck(boolean isCapacityLimitCheck) { - PropertyUtil.isCapacityLimitCheck = isCapacityLimitCheck; - } + public static void setDefaultClusterQuota(int defaultClusterQuota) { + PropertyUtil.defaultClusterQuota = defaultClusterQuota; + } - public static void setDefaultClusterQuota(int defaultClusterQuota) { - PropertyUtil.defaultClusterQuota = defaultClusterQuota; - } + public static void setDefaultGroupQuota(int defaultGroupQuota) { + PropertyUtil.defaultGroupQuota = defaultGroupQuota; + } - public static void setDefaultGroupQuota(int defaultGroupQuota) { - PropertyUtil.defaultGroupQuota = defaultGroupQuota; - } + public static void setDefaultTenantQuota(int defaultTenantQuota) { + PropertyUtil.defaultTenantQuota = defaultTenantQuota; + } - public static void setDefaultTenantQuota(int defaultTenantQuota) { - PropertyUtil.defaultTenantQuota = defaultTenantQuota; - } + public static void setDefaultMaxSize(int defaultMaxSize) { + PropertyUtil.defaultMaxSize = defaultMaxSize; + } - public static void setDefaultMaxSize(int defaultMaxSize) { - PropertyUtil.defaultMaxSize = defaultMaxSize; - } + public static void setDefaultMaxAggrCount(int defaultMaxAggrCount) { + PropertyUtil.defaultMaxAggrCount = defaultMaxAggrCount; + } - public static void setDefaultMaxAggrCount(int defaultMaxAggrCount) { - PropertyUtil.defaultMaxAggrCount = defaultMaxAggrCount; - } + public static void setDefaultMaxAggrSize(int defaultMaxAggrSize) { + PropertyUtil.defaultMaxAggrSize = defaultMaxAggrSize; + } - public static void setDefaultMaxAggrSize(int defaultMaxAggrSize) { - PropertyUtil.defaultMaxAggrSize = defaultMaxAggrSize; - } + public static void setInitialExpansionPercent(int initialExpansionPercent) { + PropertyUtil.initialExpansionPercent = initialExpansionPercent; + } - public static void setInitialExpansionPercent(int initialExpansionPercent) { - PropertyUtil.initialExpansionPercent = initialExpansionPercent; - } + public static void setCorrectUsageDelay(int correctUsageDelay) { + PropertyUtil.correctUsageDelay = correctUsageDelay; + } - public static void setCorrectUsageDelay(int correctUsageDelay) { - PropertyUtil.correctUsageDelay = correctUsageDelay; - } - } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/misc/NamingProxy.java b/naming/src/main/java/com/alibaba/nacos/naming/misc/NamingProxy.java index 55ac393e9..ee87dd582 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/misc/NamingProxy.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/misc/NamingProxy.java @@ -15,13 +15,12 @@ */ package com.alibaba.nacos.naming.misc; -import com.alibaba.nacos.common.util.SystemUtil; +import com.alibaba.nacos.common.util.SystemUtils; import com.alibaba.nacos.naming.boot.RunningConfig; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.StringUtils; -import java.io.File; import java.io.IOException; import java.net.HttpURLConnection; import java.net.InetAddress; @@ -32,6 +31,8 @@ import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit; +import static com.alibaba.nacos.common.util.SystemUtils.STANDALONE_MODE; + /** * @author nacos */ @@ -62,7 +63,7 @@ public class NamingProxy { private static String jmenv; public static String getJmenv() { - jmenv = SystemUtil.getSystemEnv("nacos_jmenv_domain"); + jmenv = SystemUtils.getSystemEnv("nacos_jmenv_domain"); if (StringUtils.isEmpty(jmenv)) { jmenv = System.getProperty("com.alibaba.nacos.naming.jmenv", "jmenv.tbsite.net"); @@ -106,7 +107,7 @@ public class NamingProxy { return; } - if (UtilsAndCommons.STANDALONE_MODE) { + if (STANDALONE_MODE) { servers = new ArrayList<>(); servers.add(InetAddress.getLocalHost().getHostAddress() + ":" + RunningConfig.getServerPort()); return; @@ -165,7 +166,7 @@ public class NamingProxy { //use system env if (CollectionUtils.isEmpty(result)) { - result = SystemUtil.getIPsBySystemEnv(UtilsAndCommons.SELF_SERVICE_CLUSTER_ENV); + result = SystemUtils.getIPsBySystemEnv(UtilsAndCommons.SELF_SERVICE_CLUSTER_ENV); Loggers.DEBUG_LOG.debug("REFRESH-SERVER-LIST4: " + result); } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/misc/UtilsAndCommons.java b/naming/src/main/java/com/alibaba/nacos/naming/misc/UtilsAndCommons.java index f0b4b3b70..33f8ce7dd 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/misc/UtilsAndCommons.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/misc/UtilsAndCommons.java @@ -80,8 +80,6 @@ public class UtilsAndCommons { public static final String SELF_SERVICE_CLUSTER_ENV = "naming_self_service_cluster_ips"; - public static final boolean STANDALONE_MODE = Boolean.parseBoolean(System.getProperty("nacos.standalone", "false")); - public static final String CACHE_KEY_SPLITER = "@@@@"; public static final String LOCAL_HOST_IP = "127.0.0.1"; diff --git a/naming/src/main/java/com/alibaba/nacos/naming/raft/PeerSet.java b/naming/src/main/java/com/alibaba/nacos/naming/raft/PeerSet.java index 97af12c79..c67454dfa 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/raft/PeerSet.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/raft/PeerSet.java @@ -19,7 +19,6 @@ import com.alibaba.fastjson.JSON; import com.alibaba.nacos.naming.misc.HttpClient; import com.alibaba.nacos.naming.misc.Loggers; import com.alibaba.nacos.naming.misc.NetUtils; -import com.alibaba.nacos.naming.misc.UtilsAndCommons; import com.ning.http.client.AsyncCompletionHandler; import com.ning.http.client.Response; import org.apache.commons.collections.SortedBag; @@ -30,6 +29,8 @@ import org.apache.commons.lang3.StringUtils; import java.net.HttpURLConnection; import java.util.*; +import static com.alibaba.nacos.common.util.SystemUtils.STANDALONE_MODE; + /** * @author nacos */ @@ -45,7 +46,7 @@ public class PeerSet { } public RaftPeer getLeader() { - if (UtilsAndCommons.STANDALONE_MODE) { + if (STANDALONE_MODE) { return local(); } return leader; @@ -63,7 +64,7 @@ public class PeerSet { peers.put(server, peer); } - if (UtilsAndCommons.STANDALONE_MODE) { + if (STANDALONE_MODE) { RaftPeer local = local(); local.state = RaftPeer.State.LEADER; local.voteFor = NetUtils.localIP(); @@ -83,7 +84,7 @@ public class PeerSet { } public boolean isLeader(String ip) { - if (UtilsAndCommons.STANDALONE_MODE) { + if (STANDALONE_MODE) { return true; } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/raft/RaftCore.java b/naming/src/main/java/com/alibaba/nacos/naming/raft/RaftCore.java index 43af2a0db..76b539dc7 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/raft/RaftCore.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/raft/RaftCore.java @@ -37,6 +37,8 @@ import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; import java.util.zip.GZIPOutputStream; +import static com.alibaba.nacos.common.util.SystemUtils.STANDALONE_MODE; + /** * @author nacos */ @@ -525,7 +527,7 @@ public class RaftCore { public static void sendBeat() throws IOException, InterruptedException { RaftPeer local = peers.local(); - if (local.state != RaftPeer.State.LEADER && !UtilsAndCommons.STANDALONE_MODE) { + if (local.state != RaftPeer.State.LEADER && !STANDALONE_MODE) { return; } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/web/ApiCommands.java b/naming/src/main/java/com/alibaba/nacos/naming/web/ApiCommands.java index 57e637426..615b6359e 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/web/ApiCommands.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/web/ApiCommands.java @@ -19,9 +19,8 @@ import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.TypeReference; -import com.alibaba.nacos.common.util.IoUtils; import com.alibaba.nacos.common.util.Md5Utils; -import com.alibaba.nacos.common.util.SystemUtil; +import com.alibaba.nacos.common.util.SystemUtils; import com.alibaba.nacos.naming.boot.RunningConfig; import com.alibaba.nacos.naming.core.*; import com.alibaba.nacos.naming.exception.NacosException; @@ -50,7 +49,10 @@ import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; -import java.io.*; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.InetAddress; import java.net.InetSocketAddress; @@ -2002,9 +2004,9 @@ public class ApiCommands { result.put("ipCount", ipCount); result.put("responsibleDomCount", responsibleDomCount); result.put("responsibleIPCount", responsibleIPCount); - result.put("cpu", SystemUtil.getCPU()); - result.put("load", SystemUtil.getLoad()); - result.put("mem", SystemUtil.getMem()); + result.put("cpu", SystemUtils.getCPU()); + result.put("load", SystemUtils.getLoad()); + result.put("mem", SystemUtils.getMem()); return result; }