Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

Commit

Permalink
Add ability to set region specific JDBC urls (#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
fieldju authored Apr 15, 2019
1 parent bbd8d1e commit 9488754
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 18 deletions.
2 changes: 1 addition & 1 deletion debugShadowJar.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/sh

java -jar -D@appId=cms -D@environment=local $* -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 build/libs/*.jar
java -jar -D@appId=cms -D@environment=local $* -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005 build/libs/*.jar
2 changes: 1 addition & 1 deletion gradle/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ repositories {
def riposteVersion = '0.13.0'
def logbackVersion = '1.1.7'
// Request version 4 for full java 8 support
def guiceVersion = '4.0'
def guiceVersion = '4.2.2'
def awsSdkVersion = '1.11.160'
def groovyVersion = '2.3.9'

Expand Down
28 changes: 12 additions & 16 deletions src/main/java/com/nike/cerberus/server/config/CmsConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,11 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.*;
import com.google.inject.util.Modules;
import com.nike.backstopper.handler.riposte.config.guice.BackstopperRiposteConfigGuiceModule;
import com.nike.cerberus.server.config.guice.AwsStsGuiceModule;
import com.nike.cerberus.server.config.guice.CerberusBackstopperRiposteGuiceModule;
import com.nike.cerberus.server.config.guice.CmsFlywayModule;
import com.nike.cerberus.server.config.guice.CmsGuiceModule;
import com.nike.cerberus.server.config.guice.CmsMyBatisModule;
import com.nike.cerberus.server.config.guice.GuiceProvidedServerConfigValues;
import com.nike.cerberus.server.config.guice.MetricsGuiceModule;
import com.nike.cerberus.server.config.guice.OneLoginGuiceModule;
import com.nike.cerberus.server.config.guice.*;
import com.nike.cerberus.service.ConfigService;
import com.nike.cerberus.util.ArchaiusUtils;
import com.nike.cerberus.util.JobsInitializerUtils;
import com.nike.guice.PropertiesRegistrationGuiceModule;
Expand All @@ -48,12 +41,12 @@
import com.nike.riposte.server.http.filter.RequestAndResponseFilter;
import com.nike.riposte.server.logging.AccessLogger;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigException;
import io.netty.handler.ssl.SslContext;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import javax.inject.Named;
import javax.inject.Singleton;
import java.util.*;
import java.util.concurrent.CompletableFuture;

/**
Expand Down Expand Up @@ -96,7 +89,10 @@ protected CmsConfig(Config appConfig, PropertiesRegistrationGuiceModule properti

// Create a Guice Injector for this app.
List<Module> appGuiceModules = new ArrayList<>();
appGuiceModules.add(propertiesRegistrationGuiceModule);
appGuiceModules.add(
Modules.override(propertiesRegistrationGuiceModule)
.with(new JdbcPropertiesModule()) // Allow us to dynamically set JDBC.url
);
appGuiceModules.addAll(Arrays.asList(
new CmsMyBatisModule(),
new BackstopperRiposteConfigGuiceModule(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.nike.cerberus.server.config.guice;

import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.nike.cerberus.service.ConfigService;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigException;

import javax.inject.Named;
import javax.inject.Singleton;
import java.util.Optional;

public class JdbcPropertiesModule extends AbstractModule {

/**
* Attempts to fetch the region specific JDBC URL first, if not present fall back to the globally defined
* JDBC url (The write cluster for the primary region).
*
* @return The resolved jdbc url
*/
@Provides
@Singleton
@Named("JDBC.url")
public String jdbcUrl() {
ConfigService configService = ConfigService.getInstance();
Config config = configService.getAppConfigMergedWithCliGeneratedProperties();
String regionKey = Optional.ofNullable(configService.getSetRegion()).orElse("region_env_var_not_set");
try {
return config.getString(String.format("JDBC.%s.url", regionKey));
} catch (ConfigException e) {
return config.getString("JDBC.url");
}
}
}
4 changes: 4 additions & 0 deletions src/main/java/com/nike/cerberus/service/ConfigService.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,8 @@ public Set<String> getEnabledEventProcessors() {
}
return new HashSet<>();
}

public String getSetRegion() {
return System.getenv(REGION_KEY);
}
}

0 comments on commit 9488754

Please sign in to comment.