nobodyiam 3a8e92c9c2 version to 1.1.2 | 6 سال پیش | |
---|---|---|
.. | ||
doc | 8 سال پیش | |
src | 6 سال پیش | |
README.md | 6 سال پیش | |
pom.xml | 6 سال پیش |
Apollo client requires AppId
and Environment
information available to function properly, so please read the following and configure them properly:
AppId is the identity for the application, which is a key information to retrieve the config from server.
AppId information should be put in classpath:/META-INF/app.properties
with its key as app.id
.
For example, you could place the file as the following screenshot:
And config the file as:
app.id=YOUR-APP-ID
Apollo supports config by multiple environments, so environment is another key information to retrieve the config from server.
Environment could be configured in 3 ways:
As Java System Property
env
-Denv=YOUR-ENVIRONMENT
As OS System Environment
ENV
As Property File
/opt/settings/server.properties
on the target machineenv=YOUR-ENVIRONMENT
Currently, env
allows the following values (case-insensitive):
Apollo supports config separated by clusters, which means for one appId and one environment, you could have different configs.
If you need this functionality, you could specify the cluster as follows:
apollo.cluster
-Dapollo.cluster=xxx
/opt/settings/server.properties
on the target machineidc=xxx
If both apollo.cluster
and idc
are specified:
apollo.cluster
idc
default
If only apollo.cluster
is specified:
apollo.cluster
default
If only idc
is specified:
idc
default
If neither apollo.cluster
nor idc
is specified:
default
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>1.1.2</version>
</dependency>
Config config = ConfigService.getAppConfig();
String someKey = "someKeyFromDefaultNamespace";
String someDefaultValue = "someDefaultValueForTheKey";
System.out.println(String.format("Value for key %s is %s", someKey, config.getProperty(someKey, someDefaultValue)));
Config config = ConfigService.getAppConfig();
config.addChangeListener(new ConfigChangeListener() {
@Override
public void onChange(ConfigChangeEvent changeEvent) {
System.out.println("Changes for namespace " + changeEvent.getNamespace());
for (String key : changeEvent.changedKeys()) {
ConfigChange change = changeEvent.getChange(key);
System.out.println(String.format("Found change - key: %s, oldValue: %s, newValue: %s, changeType: %s", change.getPropertyName(), change.getOldValue(), change.getNewValue(), change.getChangeType()));
}
}
});
String somePublicNamespace = "CAT";
Config config = ConfigService.getConfig(somePublicNamespace);
String someKey = "someKeyFromPublicNamespace";
String someDefaultValue = "someDefaultValueForTheKey";
System.out.println(String.format("Value for key %s is %s", someKey, config.getProperty(someKey, someDefaultValue)));