-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2f44836
commit 4856a4e
Showing
2 changed files
with
34 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
groups=ghrc.io | ||
version=0.1.0 | ||
version=0.1.1 |
67 changes: 33 additions & 34 deletions
67
...annuus-apollo-client/src/main/java/io/ghcr/heliannuuthus/ApolloMetaServerInitializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,47 @@ | ||
package io.ghcr.heliannuuthus; | ||
|
||
import com.ctrip.framework.apollo.core.ApolloClientSystemConsts; | ||
import java.text.MessageFormat; | ||
import java.util.Arrays; | ||
import java.util.Optional; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.context.ApplicationContextInitializer; | ||
import org.springframework.context.ConfigurableApplicationContext; | ||
import org.springframework.core.Ordered; | ||
import org.springframework.core.env.ConfigurableEnvironment; | ||
|
||
import java.text.MessageFormat; | ||
import java.util.Arrays; | ||
import java.util.Optional; | ||
|
||
@Slf4j | ||
public class ApolloMetaServerInitializer | ||
implements ApplicationContextInitializer<ConfigurableApplicationContext>, Ordered { | ||
public static final int ORDER = -1; | ||
private static final String SCHEMA = "config"; | ||
private static final String SPRING_APPLICATION_NAME_KEY = "spring.application.name"; | ||
private static final String SPRING_PROFILES_ACTIVE_KEY = "spring.profiles.active"; | ||
private static final String DEFAULT_META_SERVER = "http://{0}.heliannuuthus.com"; | ||
private static final String DEFAULT_ENV = "dev"; | ||
private static final String PROD_ENV = "prod"; | ||
|
||
private static final String MIDDLE_LINE = "-"; | ||
implements ApplicationContextInitializer<ConfigurableApplicationContext>, Ordered { | ||
public static final int ORDER = -1; | ||
private static final String SCHEMA = "config"; | ||
private static final String SPRING_APPLICATION_NAME_KEY = "spring.application.name"; | ||
private static final String DEFAULT_META_SERVER = "http://{0}.heliannuuthus.org"; | ||
private static final String DEFAULT_ENV = "dev"; | ||
private static final String PROD_ENV = "prod"; | ||
private static final String MIDDLE_LINE = "-"; | ||
|
||
@Override | ||
public int getOrder() { | ||
return ORDER; | ||
} | ||
@Override | ||
public int getOrder() { | ||
return ORDER; | ||
} | ||
|
||
@Override | ||
public void initialize(ConfigurableApplicationContext applicationContext) { | ||
ConfigurableEnvironment environment = applicationContext.getEnvironment(); | ||
Optional.ofNullable(environment.getProperty(SPRING_APPLICATION_NAME_KEY)) | ||
.ifPresent( | ||
applicationName -> | ||
System.setProperty(ApolloClientSystemConsts.APP_ID, applicationName)); | ||
String env = | ||
Optional.of(environment.getActiveProfiles()) | ||
.flatMap(profiles -> Arrays.stream(profiles).findFirst()) | ||
.orElse(DEFAULT_ENV); | ||
System.setProperty("env", env); | ||
System.setProperty( | ||
ApolloClientSystemConsts.APOLLO_META, | ||
MessageFormat.format( | ||
DEFAULT_META_SERVER, PROD_ENV.equals(env) ? SCHEMA : SCHEMA + MIDDLE_LINE + env)); | ||
} | ||
@Override | ||
public void initialize(ConfigurableApplicationContext applicationContext) { | ||
ConfigurableEnvironment environment = applicationContext.getEnvironment(); | ||
Optional.ofNullable(environment.getProperty(SPRING_APPLICATION_NAME_KEY)) | ||
.ifPresent( | ||
applicationName -> | ||
System.setProperty(ApolloClientSystemConsts.APP_ID, applicationName)); | ||
String env = | ||
Optional.of(environment.getActiveProfiles()) | ||
.flatMap(profiles -> Arrays.stream(profiles).findFirst()) | ||
.orElse(DEFAULT_ENV); | ||
System.setProperty("env", env); | ||
System.setProperty( | ||
ApolloClientSystemConsts.APOLLO_META, | ||
MessageFormat.format( | ||
DEFAULT_META_SERVER, PROD_ENV.equals(env) ? SCHEMA : SCHEMA + MIDDLE_LINE + env)); | ||
} | ||
} |