/** * Build the {@link StatusInfo}. General information are automatically * built here too. */ public StatusInfo build() { if (result.instanceInfo == null) { throw new IllegalStateException("instanceInfo can not be null"); } result.generalStats.put("server-uptime", getUpTime()); result.generalStats.put("environment", ConfigurationManager .getDeploymentContext().getDeploymentEnvironment()); Runtime runtime = Runtime.getRuntime(); int totalMem = (int) (runtime.totalMemory() / 1048576); int freeMem = (int) (runtime.freeMemory() / 1048576); int usedPercent = (int) (((float) totalMem - freeMem) / (totalMem) * 100.0); result.generalStats.put("num-of-cpus", String.valueOf(runtime.availableProcessors())); result.generalStats.put("total-avail-memory", String.valueOf(totalMem) + "mb"); result.generalStats.put("current-memory-usage", String.valueOf(totalMem - freeMem) + "mb" + " (" + usedPercent + "%)"); return result; } }
protected boolean isCloud(DeploymentContext deploymentContext) { logger.info("Deployment datacenter is {}", deploymentContext.getDeploymentDatacenter()); return CLOUD.equals(deploymentContext.getDeploymentDatacenter()); } }
ConfigurationManager.getDeploymentContext().setDeploymentEnvironment( "CHOP" ); LOG.info( "Setting environment to: CHOP" );
private void populateHeader(Map<String, Object> model) { model.put("currentTime", StatusResource.getCurrentTimeAsString()); model.put("upTime", StatusInfo.getUpTime()); model.put("environment", ConfigurationManager.getDeploymentContext() .getDeploymentEnvironment()); model.put("datacenter", ConfigurationManager.getDeploymentContext() .getDeploymentDatacenter()); PeerAwareInstanceRegistry registry = getRegistry(); model.put("registry", registry); model.put("isBelowRenewThresold", registry.isBelowRenewThresold() == 1); DataCenterInfo info = applicationInfoManager.getInfo().getDataCenterInfo(); if (info.getName() == DataCenterInfo.Name.Amazon) { AmazonInfo amazonInfo = (AmazonInfo) info; model.put("amazonInfo", amazonInfo); model.put("amiId", amazonInfo.get(AmazonInfo.MetaDataKey.amiId)); model.put("availabilityZone", amazonInfo.get(AmazonInfo.MetaDataKey.availabilityZone)); model.put("instanceId", amazonInfo.get(AmazonInfo.MetaDataKey.instanceId)); } }
@Override public void load() { String appNameToUse = appName; if (null == appNameToUse) { appNameToUse = ConfigurationManager.getDeploymentContext().getApplicationId(); } try { logger.info(String.format("Loading application properties with app id: %s and environment: %s", appNameToUse, ConfigurationManager.getDeploymentContext().getDeploymentEnvironment())); /** * This loads a property file with the name "appName".properties and "appName"-"env".properties, if found. */ ConfigurationManager.loadCascadedPropertiesFromResources(appNameToUse); } catch (IOException e) { logger.error(String.format( "Failed to load properties for application id: %s and environment: %s. This is ok, if you do not have application level properties.", appNameToUse, ConfigurationManager.getDeploymentContext().getDeploymentEnvironment()), e); } } }
@Override public String getValue(ContextKey key) { if (delegate == null) { System.out.println("Configuration not yet initialized. Returning 'null' for " + key); return null; } return delegate.getValue(key); }
final DeploymentContext config = ConfigurationManager.getDeploymentContext(); String stack = config.getDeploymentStack(); String env = config.getDeploymentEnvironment(); if (stack != null) { setApplicationName(stack);
@PostConstruct public void preprocess() { String zone = ConfigurationManager.getDeploymentContext() .getValue(ContextKey.zone); if (this.clientConfig != null && StringUtils.isEmpty(zone)) { if (this.approximateZoneFromHostname && this.eurekaConfig != null) { .extractApproximateZone(this.eurekaConfig.getHostName(false)); log.debug("Setting Zone To " + approxZone); ConfigurationManager.getDeploymentContext().setValue(ContextKey.zone, approxZone); ConfigurationManager.getDeploymentContext().setValue(ContextKey.zone, availabilityZone);
@Override protected AbstractConfiguration getConfiguration() { AbstractConfiguration config = ConfigurationManager.getConfigInstance(); boolean dynamic = config.getBoolean(DYNAMIC_CONFIG, true); if (!dynamic) { return new DynamicConfiguration(); } else { String appId = null; DeploymentContext context = ConfigurationManager.getDeploymentContext(); appId = context.getApplicationId(); if (appId == null) { throw new RuntimeException( "Archaius deployment context's applicationId not set (archaius.deployment.applicationId)"); } String consulHost = config.getString(CONSUL_HOST, CONSUL_DEFAULT_HOST); int consulPort = config.getInt(CONSUL_PORT, 8500); String consulAclToken = config.getString(CONSUL_TOKEN); ConsulWatchedConfigurationSource configSource = new ConsulWatchedConfigurationSource(appId, new ConsulClient(consulHost, consulPort), 30, TimeUnit.SECONDS, consulAclToken); // do the first update synchronously try { configSource.runOnce(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } configSource.startAsync(); return new DynamicWatchedConfiguration(configSource); } }
@WarmUp public void initialize() { try { RibbonConfig.setupAppInfo(); String stack = ConfigurationManager.getDeploymentContext().getDeploymentStack(); // Setup the default origin. String defaultClientName; if (stack != null && !stack.trim().isEmpty() && RibbonConfig.isAutodetectingBackendVips()) { defaultClientName = RibbonConfig.setupDefaultRibbonConfig(); } else { DynamicStringProperty DEFAULT_CLIENT = DynamicPropertyFactory.getInstance().getStringProperty(ZUUL_NIWS_DEFAULTCLIENT, null); defaultClientName = DEFAULT_CLIENT.get(); if (defaultClientName == null) { defaultClientName = stack; } ZuulApplicationInfo.setApplicationName(defaultClientName); } origins.put(defaultClientName, new RibbonOrigin(defaultClientName)); // Setup the other configured origins. String clientPropertyList = DynamicPropertyFactory.getInstance().getStringProperty(ZUUL_NIWS_CLIENTLIST, "").get(); String[] aClientList = clientPropertyList.split("\\|"); for (String client : aClientList) { initOrigin(RIBBON_NAMESPACE.get(), client); } } catch (ClientException e) { throw new ZuulException(e, "Error initializing Ribbon clients."); } }
private void populateHeader(Map<String, Object> model) { model.put("currentTime", StatusResource.getCurrentTimeAsString()); model.put("upTime", StatusInfo.getUpTime()); model.put("environment", ConfigurationManager.getDeploymentContext() .getDeploymentEnvironment()); model.put("datacenter", ConfigurationManager.getDeploymentContext() .getDeploymentDatacenter()); PeerAwareInstanceRegistry registry = getRegistry(); model.put("registry", registry); model.put("isBelowRenewThresold", registry.isBelowRenewThresold() == 1); DataCenterInfo info = applicationInfoManager.getInfo().getDataCenterInfo(); if (info.getName() == DataCenterInfo.Name.Amazon) { AmazonInfo amazonInfo = (AmazonInfo) info; model.put("amazonInfo", amazonInfo); model.put("amiId", amazonInfo.get(AmazonInfo.MetaDataKey.amiId)); model.put("availabilityZone", amazonInfo.get(AmazonInfo.MetaDataKey.availabilityZone)); model.put("instanceId", amazonInfo.get(AmazonInfo.MetaDataKey.instanceId)); } }
@Override public void load() { String appNameToUse = appName; if (null == appNameToUse) { appNameToUse = ConfigurationManager.getDeploymentContext().getApplicationId(); } try { logger.info(String.format("Loading application properties with app id: %s and environment: %s", appNameToUse, ConfigurationManager.getDeploymentContext().getDeploymentEnvironment())); /** * This loads a property file with the name "appName".properties and "appName"-"env".properties, if found. */ ConfigurationManager.loadCascadedPropertiesFromResources(appNameToUse); } catch (IOException e) { logger.error(String.format( "Failed to load properties for application id: %s and environment: %s. This is ok, if you do not have application level properties.", appNameToUse, ConfigurationManager.getDeploymentContext().getDeploymentEnvironment()), e); } } }
@Override public void initWithNiwsConfig(IClientConfig niwsClientConfig) { super.initWithNiwsConfig(niwsClientConfig); if (ConfigurationManager.getDeploymentContext() != null) { this.zone = ConfigurationManager.getDeploymentContext().getValue( ContextKey.zone); } }
private void populateHeader(NutMap result) { result.put("currentTime", StatusResource.getCurrentTimeAsString()); result.put("upTime", StatusInfo.getUpTime()); result.put("environment", ConfigurationManager.getDeploymentContext().getDeploymentEnvironment()); result.put("datacenter", ConfigurationManager.getDeploymentContext().getDeploymentDatacenter()); PeerAwareInstanceRegistry registry = getRegistry(); NutMap registryMap = NutMap.NEW() .setv("leaseExpirationEnabled", registry.isLeaseExpirationEnabled()) .setv("numOfRenewsPerMinThreshold", registry.getNumOfRenewsPerMinThreshold()) .setv("numOfRenewsInLastMin", registry.getNumOfRenewsInLastMin()) .setv("selfPreservationModeEnabled", registry.isSelfPreservationModeEnabled()); result.put("registry", registryMap); result.put("isBelowRenewThresold", registry.isBelowRenewThresold() == 1); DataCenterInfo info = getServerContext().getApplicationInfoManager() .getInfo() .getDataCenterInfo(); result.put("amazonInfo", false); if (info.getName() == DataCenterInfo.Name.Amazon) { AmazonInfo amazonInfo = (AmazonInfo) info; result.put("amazonInfo", true); result.put("amiId", amazonInfo.get(AmazonInfo.MetaDataKey.amiId)); result.put("availabilityZone", amazonInfo.get(AmazonInfo.MetaDataKey.availabilityZone)); result.put("instanceId", amazonInfo.get(AmazonInfo.MetaDataKey.instanceId)); } }
@Override public void load() { String appNameToUse = appName; if (null == appNameToUse) { appNameToUse = ConfigurationManager.getDeploymentContext().getApplicationId(); } try { logger.info(String.format("Loading application properties with app id: %s and environment: %s", appNameToUse, ConfigurationManager.getDeploymentContext().getDeploymentEnvironment())); /** * This loads a property file with the name "appName".properties and "appName"-"env".properties, if found. */ ConfigurationManager.loadCascadedPropertiesFromResources(appNameToUse); } catch (IOException e) { logger.error(String.format( "Failed to load properties for application id: %s and environment: %s. This is ok, if you do not have application level properties.", appNameToUse, ConfigurationManager.getDeploymentContext().getDeploymentEnvironment()), e); } } }
/** * Build the {@link StatusInfo}. General information are automatically * built here too. */ public StatusInfo build() { if (result.instanceInfo == null) { throw new IllegalStateException("instanceInfo can not be null"); } result.generalStats.put("server-uptime", getUpTime()); result.generalStats.put("environment", ConfigurationManager .getDeploymentContext().getDeploymentEnvironment()); Runtime runtime = Runtime.getRuntime(); int totalMem = (int) (runtime.totalMemory() / 1048576); int freeMem = (int) (runtime.freeMemory() / 1048576); int usedPercent = (int) (((float) totalMem - freeMem) / (totalMem) * 100.0); result.generalStats.put("num-of-cpus", String.valueOf(runtime.availableProcessors())); result.generalStats.put("total-avail-memory", String.valueOf(totalMem) + "mb"); result.generalStats.put("current-memory-usage", String.valueOf(totalMem - freeMem) + "mb" + " (" + usedPercent + "%)"); return result; } }
public static void setDeploymentContext(DeploymentContext context) { ConfigurationManager.context = context; if (getConfigInstance() == null) { return; } for (DeploymentContext.ContextKey key: DeploymentContext.ContextKey.values()) { String value = context.getValue(key); if (value != null) { instance.setProperty(key.getKey(), value); } } }
protected boolean isCloud(DeploymentContext deploymentContext) { logger.info("Deployment datacenter is {}", deploymentContext.getDeploymentDatacenter()); return CLOUD.equals(deploymentContext.getDeploymentDatacenter()); } }
@Override public void setDeploymentEnvironment(String env) { delegate.setDeploymentEnvironment(env); }
private static Properties loadCascadedProperties(String configName) throws IOException { String defaultConfigFileName = configName + ".properties"; if (instance == null) { instance = getConfigInstance(); } ClassLoader loader = Thread.currentThread().getContextClassLoader(); URL url = loader.getResource(defaultConfigFileName); if (url == null) { throw new IOException("Cannot locate " + defaultConfigFileName + " as a classpath resource."); } Properties props = getPropertiesFromFile(url); String environment = getDeploymentContext().getDeploymentEnvironment(); if (environment != null && environment.length() > 0) { String envConfigFileName = configName + "-" + environment + ".properties"; url = loader.getResource(envConfigFileName); if (url != null) { Properties envProps = getPropertiesFromFile(url); if (envProps != null) { props.putAll(envProps); } } } return props; }