diff --git a/CHANGES.md b/CHANGES.md index f72dce3c0f6..81d5936d2bf 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -14,5 +14,7 @@ Apollo 2.4.0 * [Feature support the observe status access-key for pre-check and logging only](https://github.com/apolloconfig/apollo/pull/5236) * [Feature add limit for items count per namespace](https://github.com/apolloconfig/apollo/pull/5227) * [Feature: Add ConfigService cache record stats function](https://github.com/apolloconfig/apollo/pull/5247) +* [RefreshAdminServerAddressTask supports dynamic configuration of time interval](https://github.com/apolloconfig/apollo/pull/5248) + ------------------ All issues and pull requests are [here](https://github.com/apolloconfig/apollo/milestone/15?closed=1) diff --git a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/AdminServiceAddressLocator.java b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/AdminServiceAddressLocator.java index c8d4e72b22b..eaec49d941f 100644 --- a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/AdminServiceAddressLocator.java +++ b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/AdminServiceAddressLocator.java @@ -16,6 +16,7 @@ */ package com.ctrip.framework.apollo.portal.component; +import com.ctrip.framework.apollo.portal.component.config.PortalConfig; import com.ctrip.framework.apollo.portal.environment.PortalMetaDomainService; import com.ctrip.framework.apollo.core.dto.ServiceDTO; import com.ctrip.framework.apollo.portal.environment.Env; @@ -42,8 +43,6 @@ @Component public class AdminServiceAddressLocator { - private static final long NORMAL_REFRESH_INTERVAL = 5 * 60 * 1000; - private static final long OFFLINE_REFRESH_INTERVAL = 10 * 1000; private static final int RETRY_TIMES = 3; private static final String ADMIN_SERVICE_URL_PATH = "/services/admin"; private static final Logger logger = LoggerFactory.getLogger(AdminServiceAddressLocator.class); @@ -56,16 +55,19 @@ public class AdminServiceAddressLocator { private final PortalSettings portalSettings; private final RestTemplateFactory restTemplateFactory; private final PortalMetaDomainService portalMetaDomainService; + private final PortalConfig portalConfig; public AdminServiceAddressLocator( final HttpMessageConverters httpMessageConverters, final PortalSettings portalSettings, final RestTemplateFactory restTemplateFactory, - final PortalMetaDomainService portalMetaDomainService + final PortalMetaDomainService portalMetaDomainService, + final PortalConfig portalConfig ) { this.portalSettings = portalSettings; this.restTemplateFactory = restTemplateFactory; this.portalMetaDomainService = portalMetaDomainService; + this.portalConfig = portalConfig; } @PostConstruct @@ -105,10 +107,10 @@ public void run() { if (refreshSuccess) { refreshServiceAddressService - .schedule(new RefreshAdminServerAddressTask(), NORMAL_REFRESH_INTERVAL, TimeUnit.MILLISECONDS); + .schedule(new RefreshAdminServerAddressTask(), portalConfig.refreshAdminServerAddressTaskNormalIntervalSecond(), TimeUnit.SECONDS); } else { refreshServiceAddressService - .schedule(new RefreshAdminServerAddressTask(), OFFLINE_REFRESH_INTERVAL, TimeUnit.MILLISECONDS); + .schedule(new RefreshAdminServerAddressTask(), portalConfig.refreshAdminServerAddressTaskOfflineIntervalSecond(), TimeUnit.SECONDS); } } } diff --git a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/config/PortalConfig.java b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/config/PortalConfig.java index a5a4a9d81ea..982515be490 100644 --- a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/config/PortalConfig.java +++ b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/config/PortalConfig.java @@ -43,6 +43,9 @@ public class PortalConfig extends RefreshableConfig { private static final Logger logger = LoggerFactory.getLogger(PortalConfig.class); + private static final int DEFAULT_REFRESH_ADMIN_SERVER_ADDRESS_TASK_NORMAL_INTERVAL_IN_SECOND = 5 * 60; //5min + private static final int DEFAULT_REFRESH_ADMIN_SERVER_ADDRESS_TASK_OFFLINE_INTERVAL_IN_SECOND = 10; //10s + private static final Gson GSON = new Gson(); private static final Type ORGANIZATION = new TypeToken>() { }.getType(); @@ -193,6 +196,16 @@ public String portalAddress() { return getValue("apollo.portal.address"); } + public int refreshAdminServerAddressTaskNormalIntervalSecond() { + int interval = getIntProperty("refresh.admin.server.address.task.normal.interval.second", DEFAULT_REFRESH_ADMIN_SERVER_ADDRESS_TASK_NORMAL_INTERVAL_IN_SECOND); + return checkInt(interval, 5, Integer.MAX_VALUE, DEFAULT_REFRESH_ADMIN_SERVER_ADDRESS_TASK_NORMAL_INTERVAL_IN_SECOND); + } + + public int refreshAdminServerAddressTaskOfflineIntervalSecond() { + int interval = getIntProperty("refresh.admin.server.address.task.offline.interval.second", DEFAULT_REFRESH_ADMIN_SERVER_ADDRESS_TASK_OFFLINE_INTERVAL_IN_SECOND); + return checkInt(interval, 5, Integer.MAX_VALUE, DEFAULT_REFRESH_ADMIN_SERVER_ADDRESS_TASK_OFFLINE_INTERVAL_IN_SECOND); + } + public boolean isEmergencyPublishAllowed(Env env) { String targetEnv = env.getName(); @@ -304,4 +317,12 @@ public List getUserPasswordNotAllowList() { } return Arrays.asList(value); } + + private int checkInt(int value, int min, int max, int defaultValue) { + if (value >= min && value <= max) { + return value; + } + logger.warn("Configuration value '{}' is out of bounds [{} - {}]. Using default value '{}'.", value, min, max, defaultValue); + return defaultValue; + } }