Skip to content

Commit

Permalink
feat(): refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Rawven committed Sep 7, 2024
1 parent 2ec4630 commit f45a452
Show file tree
Hide file tree
Showing 51 changed files with 750 additions and 1,326 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,13 @@

import com.ctrip.framework.apollo.build.ApolloInjector;
import com.ctrip.framework.apollo.core.utils.ClassLoaderUtil;
import com.ctrip.framework.apollo.monitor.api.ConfigMonitor;
import com.ctrip.framework.apollo.monitor.internal.DefaultConfigMonitor;
import com.ctrip.framework.apollo.monitor.internal.exporter.AbstractApolloClientMetricsExporter;
import com.ctrip.framework.apollo.monitor.internal.jmx.ApolloClientJmxMBeanRegister;
import com.ctrip.framework.apollo.monitor.internal.listener.ApolloClientMonitorEventListener;
import com.ctrip.framework.apollo.monitor.internal.listener.ApolloClientMonitorEventListenerManager;
import com.ctrip.framework.apollo.monitor.internal.ApolloClientMonitorContext;
import com.ctrip.framework.apollo.monitor.internal.listener.impl.DefaultApolloClientBootstrapArgsApi;
import com.ctrip.framework.apollo.monitor.internal.listener.impl.DefaultApolloClientExceptionApi;
import com.ctrip.framework.apollo.monitor.internal.listener.impl.DefaultApolloClientNamespaceApi;
import com.ctrip.framework.apollo.monitor.internal.listener.impl.DefaultApolloClientThreadPoolApi;
import com.ctrip.framework.apollo.monitor.internal.listener.DefaultApolloClientMonitorEventListenerManager;
import com.ctrip.framework.apollo.monitor.internal.exporter.ApolloClientMetricsExporter;
import com.ctrip.framework.apollo.monitor.internal.exporter.ApolloClientMetricsExporterFactory;
import com.ctrip.framework.apollo.monitor.internal.tracer.ApolloClientMonitorMessageProducer;
import com.ctrip.framework.apollo.monitor.internal.tracer.ApolloClientMessageProducerComposite;
Expand All @@ -41,91 +36,72 @@
import com.ctrip.framework.apollo.tracer.spi.MessageProducer;
import com.ctrip.framework.apollo.util.ConfigUtil;
import com.ctrip.framework.foundation.internals.ServiceBootstrap;
import com.google.common.collect.Lists;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* ConfigMonitorInitializer initializes the Apollo Config Monitor.
*/
public class ConfigMonitorInitializer {

private static final Logger logger = LoggerFactory.getLogger(ConfigMonitorInitializer.class);
protected static boolean hasInitialized = false;
private static ConfigUtil CONFIG_UTIL = ApolloInjector.getInstance(ConfigUtil.class);
private static ConfigUtil m_configUtil = ApolloInjector.getInstance(ConfigUtil.class);
private static ApolloClientMonitorContext monitorContext = ApolloInjector.getInstance(
ApolloClientMonitorContext.class);

public static void initialize() {
if (CONFIG_UTIL.getClientMonitorEnabled() && !hasInitialized) {
hasInitialized = true;
logger.debug("Initializing ConfigMonitor");
DefaultApolloClientMonitorEventListenerManager manager = initializeMetricsEventListenerManager();
List<ApolloClientMonitorEventListener> collectors = initializeCollectors(manager);
ApolloClientMetricsExporter metricsExporter = initializeMetricsExporter(collectors);
initializeJmxMonitoring(collectors);
initializeConfigMonitor(collectors, metricsExporter);
logger.debug("ConfigMonitor initialized successfully.");
if (m_configUtil.isClientMonitorEnabled() && !hasInitialized) {
synchronized (ConfigMonitorInitializer.class) {
if (!hasInitialized) {
doInit();
hasInitialized = true;
}
}
}
}

protected static DefaultApolloClientMonitorEventListenerManager initializeMetricsEventListenerManager() {
return (DefaultApolloClientMonitorEventListenerManager) ApolloInjector.getInstance(
ApolloClientMonitorEventListenerManager.class);
private static void doInit() {
initializeMetricsEventListener();
initializeMetricsExporter();
initializeJmxMonitoring();
hasInitialized = true;
}

protected static void initializeJmxMonitoring(List<ApolloClientMonitorEventListener> collectors) {
if (CONFIG_UTIL.getClientMonitorJmxEnabled()) {
collectors.forEach(metricsCollector ->


private static void initializeJmxMonitoring() {
if (m_configUtil.isClientMonitorJmxEnabled()) {
monitorContext.getCollectors().forEach(metricsCollector ->
ApolloClientJmxMBeanRegister.register(
MBEAN_NAME + metricsCollector.mBeanName(), metricsCollector)
MBEAN_NAME + metricsCollector.getName(), metricsCollector)
);
}
}

protected static List<ApolloClientMonitorEventListener> initializeCollectors(
DefaultApolloClientMonitorEventListenerManager manager) {

private static void initializeMetricsEventListener() {
DefaultConfigManager configManager = (DefaultConfigManager) ApolloInjector.getInstance(
ConfigManager.class);

List<ApolloClientMonitorEventListener> collectors = Lists.newArrayList(
new DefaultApolloClientExceptionApi(),
new DefaultApolloClientNamespaceApi(configManager.m_configs, configManager.m_configFiles),
new DefaultApolloClientThreadPoolApi(RemoteConfigRepository.m_executorService,
AbstractConfig.m_executorService, AbstractConfigFile.m_executorService,
AbstractApolloClientMetricsExporter.m_executorService),
new DefaultApolloClientBootstrapArgsApi(CONFIG_UTIL)
);

manager.setCollectors(collectors);
return collectors;
monitorContext.setApolloClientBootstrapArgsMonitorApi(new DefaultApolloClientBootstrapArgsApi(
m_configUtil));
monitorContext.setApolloClientExceptionMonitorApi(new DefaultApolloClientExceptionApi());
monitorContext.setApolloClientNamespaceMonitorApi(new DefaultApolloClientNamespaceApi(
configManager.m_configs, configManager.m_configFiles));
monitorContext.setApolloClientThreadPoolMonitorApi(new DefaultApolloClientThreadPoolApi(
RemoteConfigRepository.m_executorService,
AbstractConfig.m_executorService, AbstractConfigFile.m_executorService,
AbstractApolloClientMetricsExporter.m_executorService));
}

protected static ApolloClientMetricsExporter initializeMetricsExporter(
List<ApolloClientMonitorEventListener> collectors) {
private static void initializeMetricsExporter(
) {
ApolloClientMetricsExporterFactory exporterFactory = ApolloInjector.getInstance(
ApolloClientMetricsExporterFactory.class);
return exporterFactory.getMetricsReporter(collectors);
}

protected static void initializeConfigMonitor(List<ApolloClientMonitorEventListener> collectors,
ApolloClientMetricsExporter metricsExporter) {

DefaultConfigMonitor configMonitor = (DefaultConfigMonitor) ApolloInjector.getInstance(
ConfigMonitor.class);
configMonitor.init(
(DefaultApolloClientNamespaceApi) collectors.get(1),
(DefaultApolloClientThreadPoolApi) collectors.get(2),
(DefaultApolloClientExceptionApi) collectors.get(0),
(DefaultApolloClientBootstrapArgsApi) collectors.get(3),
metricsExporter
);
monitorContext.setApolloClientMetricsExporter(
exporterFactory.getMetricsReporter(monitorContext.getCollectors()));
}

public static ApolloClientMessageProducerComposite initializeMessageProducerComposite() {
List<MessageProducer> producers = ServiceBootstrap.loadAllOrdered(MessageProducer.class);

if (CONFIG_UTIL.getClientMonitorEnabled()) {
if (m_configUtil.isClientMonitorEnabled()) {
producers.add(new ApolloClientMonitorMessageProducer());
}

Expand All @@ -139,11 +115,11 @@ public static ApolloClientMessageProducerComposite initializeMessageProducerComp

return new ApolloClientMessageProducerComposite(producers);
}

// for test only
protected static void reset() {
hasInitialized = false;
CONFIG_UTIL = ApolloInjector.getInstance(ConfigUtil.class);
m_configUtil = ApolloInjector.getInstance(ConfigUtil.class);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
import com.ctrip.framework.apollo.monitor.api.ConfigMonitor;
import com.ctrip.framework.apollo.monitor.internal.DefaultConfigMonitor;
import com.ctrip.framework.apollo.monitor.internal.exporter.impl.DefaultApolloClientMetricsExporterFactory;
import com.ctrip.framework.apollo.monitor.internal.listener.ApolloClientMonitorEventListenerManager;
import com.ctrip.framework.apollo.monitor.internal.listener.DefaultApolloClientMonitorEventListenerManager;
import com.ctrip.framework.apollo.monitor.internal.ApolloClientMonitorContext;
import com.ctrip.framework.apollo.monitor.internal.exporter.ApolloClientMetricsExporterFactory;
import com.ctrip.framework.apollo.spi.ApolloInjectorCustomizer;
import com.ctrip.framework.apollo.spi.ConfigFactory;
Expand Down Expand Up @@ -113,8 +112,7 @@ protected void configure() {
bind(YamlParser.class).in(Singleton.class);
bind(PropertiesFactory.class).to(DefaultPropertiesFactory.class).in(Singleton.class);
bind(ConfigMonitor.class).to(DefaultConfigMonitor.class).in(Singleton.class);
bind(ApolloClientMonitorEventListenerManager.class).to(
DefaultApolloClientMonitorEventListenerManager.class).in(Singleton.class);
bind(ApolloClientMonitorContext.class).in(Singleton.class);
bind(ApolloClientMetricsExporterFactory.class).to(DefaultApolloClientMetricsExporterFactory.class).in(Singleton.class);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public Properties getConfig() {
if (m_configCache.get() == null) {
long start = System.currentTimeMillis();
this.sync();
Tracer.logEvent(APOLLO_CLIENT_NAMESPACE_FIRST_LOAD_SPEND+m_namespace,
Tracer.logEvent(APOLLO_CLIENT_NAMESPACE_FIRST_LOAD_SPEND+":"+m_namespace,
String.valueOf(System.currentTimeMillis() - start));
}
return transformApolloConfigToProperties(m_configCache.get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,73 +16,119 @@
*/
package com.ctrip.framework.apollo.monitor.api;

import static com.ctrip.framework.apollo.core.ApolloClientSystemConsts.*;
import static com.ctrip.framework.apollo.core.ConfigConsts.APOLLO_AUTO_UPDATE_INJECTED_SPRING_PROPERTIES;
import static com.ctrip.framework.apollo.monitor.internal.ApolloClientMonitorConstant.*;
import static com.ctrip.framework.apollo.spring.config.PropertySourcesConstants.*;

import java.util.Collections;
import java.util.Map;

/**
* @author Rawven
*/
public interface ApolloClientBootstrapArgsMonitorApi {

/**
* get bootstrap args map
*/
Map<String, String> getBootstrapArgs();

/**
* get startup params by key
*/
String getStartupParams(String key);

/**
* config service url
*/
String getConfigServiceUrl();
default Object getStartupArg(String key) {
return getBootstrapArgs().get(key);
}

/**
* access key secret
*/
String getAccessKeySecret();

/**
* auto update injected spring properties
* get bootstrap args map
*/
Boolean getAutoUpdateInjectedSpringProperties();

Boolean getBootstrapEnabled();

String getBootstrapNamespaces();

Boolean getBootstrapEagerLoadEnabled();

Boolean getOverrideSystemProperties();

String getCacheDir();

String getCluster();

String getConfigService();

Boolean getClientMonitorEnabled();

Boolean getClientMonitorJmxEnabled();

String getClientMonitorExternalForm();

long getClientMonitorExternalExportPeriod();

int getClientMonitorExceptionSaveSize();

String getApolloMeta();

String getMetaLatestFreshTime();

Boolean getPropertyNamesCacheEnable();

Boolean getPropertyOrderEnable();

String getVersion();

String getEnv();

String getAppId();
}
default Map<String, Object> getBootstrapArgs() {
return Collections.emptyMap();
}

default String getConfigServiceUrl() {
return (String) getBootstrapArgs().getOrDefault(CONFIG_SERVICE_URL, "");
}

default String getAccessKeySecret() {
return (String) getBootstrapArgs().getOrDefault(APOLLO_ACCESS_KEY_SECRET, "");
}

default Boolean getAutoUpdateInjectedSpringProperties() {
return (Boolean) getBootstrapArgs().getOrDefault(APOLLO_AUTO_UPDATE_INJECTED_SPRING_PROPERTIES,
false);
}

default Boolean isBootstrapEnabled() {
return (Boolean) getBootstrapArgs().getOrDefault(APOLLO_BOOTSTRAP_ENABLED, false);
}

default String getBootstrapNamespaces() {
return (String) getBootstrapArgs().getOrDefault(APOLLO_BOOTSTRAP_NAMESPACES, "");
}

default Boolean isBootstrapEagerLoadEnabled() {
return (Boolean) getBootstrapArgs().getOrDefault(APOLLO_BOOTSTRAP_EAGER_LOAD_ENABLED, false);
}

default Boolean isOverrideSystemProperties() {
return (Boolean) getBootstrapArgs().getOrDefault(APOLLO_OVERRIDE_SYSTEM_PROPERTIES, false);
}

default String getCacheDir() {
return (String) getBootstrapArgs().getOrDefault(APOLLO_CACHE_DIR, "");
}

default String getCluster() {
return (String) getBootstrapArgs().getOrDefault(APOLLO_CLUSTER, "");
}

default String getConfigService() {
return (String) getBootstrapArgs().getOrDefault(APOLLO_CONFIG_SERVICE, "");
}

default String getClientMonitorExternalForm() {
return (String) getBootstrapArgs().getOrDefault(APOLLO_CLIENT_MONITOR_EXTERNAL_TYPE, "");
}

default Boolean isClientMonitorEnabled() {
return (Boolean) getBootstrapArgs().getOrDefault(APOLLO_CLIENT_MONITOR_ENABLED, false);
}

default Boolean isClientMonitorJmxEnabled() {
return (Boolean) getBootstrapArgs().getOrDefault(APOLLO_CLIENT_MONITOR_JMX_ENABLED, false);
}

default long getClientMonitorExternalExportPeriod() {
return (Long) getBootstrapArgs().getOrDefault(APOLLO_CLIENT_MONITOR_EXTERNAL_EXPORT_PERIOD, 0L);
}

default int getClientMonitorExceptionSaveSize() {
return (Integer) getBootstrapArgs().getOrDefault(APOLLO_CLIENT_MONITOR_EXCEPTION_QUEUE_SIZE, 0);
}

default String getApolloMeta() {
return (String) getBootstrapArgs().getOrDefault(APOLLO_META, "");
}

default String getMetaLatestFreshTime() {
return (String) getBootstrapArgs().getOrDefault(META_FRESH, "");
}

default Boolean isPropertyNamesCacheEnable() {
return (Boolean) getBootstrapArgs().getOrDefault(APOLLO_PROPERTY_NAMES_CACHE_ENABLE, false);
}

default Boolean isPropertyOrderEnable() {
return (Boolean) getBootstrapArgs().getOrDefault(APOLLO_PROPERTY_ORDER_ENABLE, false);
}

default String getVersion() {
return (String) getBootstrapArgs().getOrDefault(VERSION, "");
}

default String getEnv() {
return (String) getBootstrapArgs().getOrDefault(ENV, "");
}

default String getAppId() {
return (String) getBootstrapArgs().getOrDefault(APP_ID, "");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@
public interface ApolloClientExceptionMonitorApi {

/**
* get ApolloConfigException details
* Get exception information the number is limited and can be configured through
* apollo.client.monitor.exception-queue-size
*/
List<Exception> getApolloConfigExceptionList();

Integer getExceptionCountFromStartup();

}
Loading

0 comments on commit f45a452

Please sign in to comment.