Note: This document is intended for users of Apollo systems. If you are a developer/maintainer of Apollo systems in your company, it is recommended to refer to Apollo Development Guide first.

 

I. Preparation

1.1 Environment requirements

  • Java: 1.8+
    • To run in Java 1.7 runtime environment, please use 1.x version of apollo client, such as 1.9.1
  • Guava: 20.0+
    • The Apollo client will reference Guava 29 by default. If your project references other versions, make sure the version number is greater than or equal to 20.0

Note: For Apollo client, you can make a few code changes to downgrade to Java 1.6 if needed, see Issue 483 for details

1.2 Mandatory settings

Apollo client depends on AppId, Apollo Meta Server and other environment information to work, so please make sure to read the following instructions and do the correct configuration.

1.2.1 AppId

AppId is the identity of the application and is an important piece of information to get the configuration from the server.

There are several ways to set it, from highest to lowest priority, as follows.

  1. System Property

Apollo 0.7.0+ supports passing in app.id information via System Property, such as

  1. -Dapp.id=YOUR-APP-ID
  1. System Environment of the operating system

Apollo 1.4.0+ supports passing app.id information via the operating system’s System Environment APP_ID, as in

  1. APP_ID=YOUR-APP-ID
  1. Spring Boot application.properties

Apollo 1.0.0+ supports configuration via Spring Boot’s application.properties file, such as

  1. app.id=YOUR-APP-ID

This configuration does not work with multiple war packages deployed in the same tomcat

  1. app.properties

Make sure that the classpath:/META-INF/app.properties file exists and that its contents look like.

app.id=YOUR-APP-ID

The file location reference is as follows.

app-id-location

Note: app.id is a unique id used to identify the application identity in the format string.

1.2.2 Apollo Meta Server

Apollo supports applications with different configurations in different environments, so you need to provide the Apollo Meta Server information of the current environment to Apollo clients at runtime. By default, the meta server and config service are deployed in the same JVM process, so the address of the meta server is the address of the config service.

To achieve high availability of meta server, it is recommended to do dynamic load balancing by SLB (Software Load Balancer). meta server address can also be filled with IP, such as http://1.1.1.1:8080,http://2.2.2.2:8080, but it is still recommended for production environment Use the domain name (go SLB), because machine expansion, shrinkage, etc. may lead to changes in the IP list.

The following ways to configure apollo meta server information are supported since version 1.0.0, in descending order of priority

  1. Via the Java System Property apollo.meta
    • Can be specified via the Java System Property apollo.meta
    • Can be specified in the Java program startup script with -Dapollo.meta=http://config-service-url
      • If you are running a jar file, you need to note that the format is java -Dapollo.meta=http://config-service-url -jar xxx.jar
    • You can also specify it programmatically, e.g. System.setProperty("apollo.meta", "http://config-service-url");
  2. Through the Spring Boot configuration file
    • You can specify apollo.meta=http://config-service-url in application.properties or bootstrap.properties of Spring Boot

This configuration does not work with multiple war packages deployed in the same tomcat

  1. Through the operating system’s System Environment APOLLO_META
    • Can be specified by System Environment APOLLO_META of the operating system
    • Note that the key is all-caps and separated by _.
  2. Via server.properties configuration file
    • You can specify apollo.meta=http://config-service-url in the server.properties configuration file
    • For Mac/Linux, the default file location is /sopt/settings/server.properties.
    • For Windows, the default file location is C:\opt\settings\server.properties
  3. Via the app.properties configuration file
    • You can specify apollo.meta=http://config-service-url in classpath:/META-INF/app.properties
  4. Via Java system property ${env}_meta
    • If the current env is dev, then the user can configure -Ddev_meta=http://config-service-url
    • Using this configuration method, then the Environment must be configured correctly, see 1.2.4.1 Environment for details
  5. Via the OS System Environment ${ENV}_META (supported since version 1.2.0)
    • If the current env is dev, then the user can configure the OS System Environment DEV_META=http://config-service-url
    • Note that the key is all-caps
    • Using this configuration method, then the Environment must be configured correctly, see 1.2.4.1 Environment for details
  6. Via the apollo-env.properties file
    • The user can also create an apollo-env.properties and put it under the classpath of the application or under the config directory of the spring boot application
    • If you use this configuration, then you must configure the Environment correctly, see 1.2.4.1 Environment
    • The contents of the file look like this.
  1. dev.meta=http://1.1.1.1:8080
  2. fat.meta=http://apollo.fat.xxx.com
  3. uat.meta=http://apollo.uat.xxx.com
  4. pro.meta=http://apollo.xxx.com

If the Meta Server address cannot be obtained by any of the above means, Apollo will eventually fallback to http://apollo.meta as the Meta Server address

1.2.2.1 Customizing Apollo Meta Server Address Location Logic

In version 1.0.0, Apollo provides the MetaServerProvider SPI, which allows users to inject their own MetaServerProvider to customize the Meta Server address location logic.

Since we use the typical Java Service Loader pattern, it is still relatively simple to implement.

One thing to note is that apollo will iterate through all MetaServerProviders in order at runtime until a MetaServerProvider provides a non-empty Meta Server address, so users need to pay extra attention to the Order of the custom MetaServerProvider. The rule is that smaller Order has higher priority, so MetaServerProvider with Order=0 will be ranked ahead of MetaServerProvider with Order=1.

If your company has many applications that need to access Apollo, it is recommended to package a jar package and then provide a custom Apollo Meta Server positioning logic so that the applications that access Apollo can be used with zero configuration. For example, write your own xx-company-apollo-client, the jar package depends on apollo-client, define a custom MetaServerProvider implementation in the jar package by spi, and then the application directly depends on xx-company-apollo-client.

The implementation of MetaServerProvider can be found in LegacyMetaServerProvider and DefaultMetaServerProvider.

1.2.2.2 Skip Apollo Meta Server service discovery

For apollo-client version 0.11.0 and above

In general, it is recommended to use Apollo’s Meta Server mechanism to implement service discovery for Config Service, so that high availability of Config Service can be achieved. However, apollo-client also supports skipping Meta Server service discovery, which is mainly used in the following scenarios: 1.

  1. Config Service is deployed on the public cloud, and the address registered to Meta Server is an intranet address, so the local development environment cannot connect directly
    • If the Config Service is exposed through the public SLB, remember to set the IP whitelist to avoid data leakage.
  2. Config Service is deployed in docker environment, and the address registered to Meta Server is docker intranet address, so local development environment cannot connect to it directly.
  3. Config Service is deployed in kubernetes, and you want to use kubernetes’ own service discovery capability (Service).

For the above scenarios, you can skip Meta Server service discovery by directly specifying the Config Service address, in descending order of priority, as follows

  1. Via Java System Property apollo.config-service (1.9.0+) or apollo.configService (before 1.9.0)
    • Can be specified via Java’s System Property apollo.config-service (1.9.0+) or apollo.configService (before 1.9.0)
    • In the Java program startup script, you can specify -Dapollo.config-service=http://config-service-url:port
      • If you are running a jar file, you need to note that the format is java -Dapollo.configService=http://config-service-url:port -jar xxx.jar
    • It can also be specified programmatically, such as System.setProperty("apollo.config-service", "http://config-service-url:port");
  2. Through the operating system’s System Environment APOLLO_CONFIG_SERVICE (1.9.0+) or APOLLO_CONFIGSERVICE (before 1.9.0)
    • Can be specified by System Environment APOLLO_CONFIG_SERVICE(1.9.0+) or APOLLO_CONFIGSERVICE(before 1.9.0) of the operating system
    • Note that the key is all-caps and separated by _.
  3. Via server.properties configuration file
    • You can specify apollo.config-service=http://config-service-url:port(1.9.0+) or apollo.configService=http://config-service-url:port in the server.properties configuration file (before 1.9.0)
      • For Mac/Linux, the default file location is /opt/settings/server.properties
    • For Windows, the default file location is C:\opt\settings\server.properties

1.2.3 Local cache path

The Apollo client will cache a copy of the configuration obtained from the server in the local file system, so that if the service is unavailable or the network is down, the configuration can still be restored locally without affecting the normal operation of the application.

The local cache path is located in the following path by default, so please make sure /opt/data or C:\opt\data\ directory exists and the application has read/write permission.

  • Mac/Linux: /opt/data/{appId}/config-cache
  • Windows: C:\opt\data\{appId}\config-cache

The local configuration file will be placed under the local cache path in the following filename format.

{appId}+{cluster}+{namespace}.properties

  • AppId is the application’s own appId, e.g. 100004458
  • Cluster is the cluster used by the application, generally in local mode without configuration, it is default
  • Namespace is the configuration namespace used by the application, usually application client-local-cache

The content of the file is stored in properties format, for example, if there are two keys, one is request.timeout, and the other is batch, then the content of the file is in the following format.

  1. request.timeout=2000
  2. batch=2000

1.2.3.1 Customizing the cache path

The following custom cache paths are supported since version 1.0.0, in descending order of priority

  1. Via the Java System Property apollo.cache-dir (1.9.0+) or apollo.cacheDir (before 1.9.0)
    • Can be specified by Java’s System Property apollo.cache-dir(1.9.0+) or apollo.cacheDir(before 1.9.0)
    • In the Java program startup script, you can specify -Dapollo.cache-dir=/opt/data/some-cache-dir(1.9.0+) or apollo.cacheDir=/opt/data/some-cache-dir(before 1.9.0)
      • If you are running a jar file, note that the format is java -Dapollo.cache-dir=/opt/data/some-cache-dir -jar xxx.jar (1.9.0+) or java -Dapollo.cacheDir=/opt/data/some-cache-dir -jar xxx.jar(before 1.9.0)
    • Can also be specified programmatically, e.g. System.setProperty("apollo.cache-dir", "/opt/data/some-cache-dir");(1.9.0+) or System.setProperty("apollo.cacheDir", "/opt/data/some-cache-dir");(before 1.9.0)
  2. Via the Spring Boot configuration file
    • You can specify apollo.cache-dir=/opt/data/some-cache-dir (1.9.0+) or apollo. cacheDir=/opt/data/some-cache-dir(before 1.9.0)
  3. Via OS System Environment APOLLO_CACHE_DIR (1.9.0+) or APOLLO_CACHEDIR (before 1.9.0)
    • Can be specified by the OS System Environment APOLLO_CACHE_DIR(1.9.0+) or APOLLO_CACHEDIR(before 1.9.0)
    • Note that the key is all-caps and separated by _.
  4. Via server.properties configuration file
    • You can specify apollo.cache-dir=/opt/data/some-cache-dir(1.9.0+) or apollo.cacheDir=/opt/data/some-cache-dir(before 1.9. 0) in the server.properties configuration file. 0 or earlier)
    • For Mac/Linux, the default file location is /opt/settings/server.properties
    • For Windows, the default file location is C:\opt\settings\server.properties

Note: The local cache path can also be used for the disaster recovery directory. If the application needs to be expanded when all the config services are down, then the configuration can also be copied from the cache path on the existing machine to the same cache path on the new machine first

1.2.4 Optional settings

1.2.4.1 Environment

The Environment can be configured in any of the following 3 ways.

  1. Via Java System Property

    • Environment can be specified through Java’s System Property env.
    • In the Java program startup script, you can specify -Denv=YOUR-ENVIRONMENT
      • If you are running a jar file, note that the format is java -Denv=YOUR-ENVIRONMENT -jar xxx.jar
    • Note that the key is all lowercase
  2. Through the operating system’s System Environment

    • You can also specify the System Environment ENV of the operating system
    • Note that the key is all uppercase
  3. Through the configuration file

    • The last recommended way is to specify env=YOUR-ENVIRONMENT through the configuration file
    • For Mac/Linux, the default file location is /opt/settings/server.properties.
    • For Windows, the default file location is C:\opt\settings\server.properties.

The contents of the file look like :

  1. env=DEV

Currently, env supports the following values (case-insensitive).

  • DEV
    • Development environment
  • FAT
    • Feature Acceptance Test environment
  • UAT
    • User Acceptance Test environment
  • PRO
    • Production environment

For more environment definitions, you can refer to Env.java

1.2.4.2 Cluster

Apollo supports configuration by cluster, which means that for an appId and an environment, there can be different configurations for different clusters.

The following ways of clustering are supported since version 1.0.0, in descending order of priority.

  1. Via Java System Property apollo.cluster
    • can be specified via Java’s System Property apollo.cluster
    • Can be specified in the Java program startup script with -Dapollo.cluster=SomeCluster
      • If you are running a jar file, you need to note that the format is java -Dapollo.cluster=SomeCluster -jar xxx.jar
    • You can also specify it programmatically, e.g. System.setProperty("apollo.cluster", "SomeCluster");
  2. Through the Spring Boot configuration file
    • You can specify apollo.cluster=SomeCluster in application.properties or bootstrap.properties of Spring Boot
  3. Via Java System Property
    • You can specify the environment via Java’s System Property idc.
    • In the Java program startup script, you can specify -Didc=xxx
      • If you are running a jar file, you should note that the format is java -Didc=xxx -jar xxx.jar
    • Note that the key is all lowercase
  4. Through the operating system’s System Environment
    • Can also be specified by the operating system’s System Environment `IDC
    • Note that the key is all uppercase
  5. Through the server.properties configuration file
    • You can specify idc=xxx in the server.properties configuration file
    • For Mac/Linux, the default file location is /opt/settings/server.properties.
    • For Windows, the default file location is C:\opt\settings\server.properties

Cluster Precedence (cluster order)

  1. If apollo.cluster and idc are both specified.

    • It will first try to load the configuration from the cluster specified by apollo.cluster
    • If nothing is found, we try to load the configuration from the cluster specified by idc.
    • If nothing is found, we will load from the default cluster (default)
  2. If only apollo.cluster is specified.

    • We will first try to load the configuration from the cluster specified by apollo.cluster
    • If not found, it will be loaded from the default cluster (default)
  3. If only idc is specified.

    • We will first try to load the configuration from the cluster specified by idc
    • If not found, it will be loaded from the default cluster (default)
  4. If neither apollo.cluster nor idc is specified.

    • It will load the configuration from the default cluster (default)

1.2.4.3 Set whether the in-memory configuration items remain in the same order as they are on the page

For versions 1.6.0 and above

By default, the in-memory configuration of the apollo client is stored in Properties (Hashtable underneath) and is not intentionally kept in the same order as seen on the page, which has no effect on most scenarios. However, some scenarios will strongly rely on the order of configuration items (such as the routing rules of spring cloud zuul), for this case, you can turn on the OrderedProperties feature to make the in-memory configuration order consistent with what you see on the page.

The configuration methods, in descending order of priority, are

  1. Via the Java System Property `apollo.properties.order.enable
    • Can be specified via Java’s System Property apollo.property.order.enable
    • You can specify -Dapollo.property.order.enable=true in the Java program startup script
      • If you are running a jar file, you need to note that the format is java -Dapollo.property.order.enable=true -jar xxx.jar
    • You can also specify it programmatically, such as System.setProperty("apollo.property.order.enable", "true");
  2. Via the Spring Boot configuration file
    • You can specify apollo.properties.order.enable=true in Spring Boot’s application.properties or bootstrap.properties
  3. Via the app.properties configuration file
    • You can specify apollo.properties.order.enable=true in classpath:/META-INF/app.properties

1.2.4.4 Configuring access keys

For versions 1.6.0 and above

Apollo has added an access key mechanism since version 1.6.0 so that only authenticated clients can access sensitive configurations. If the application has access keys enabled, the client needs to configure the keys, otherwise the configuration cannot be accessed.

The configuration methods are as follows, in descending order of priority

  1. Via Java System Property apollo.access-key.secret (1.9.0+) or apollo.accessskey.secret (before 1.9.0)
    • Can be specified via Java’s System Property apollo.access-key.secret (1.9.0+) or apollo.accessskey.secret (before 1.9.0)
    • In the Java application startup script, you can specify -Dapollo.access-key.secret=1cf998c4e2ad4704b45a98a509d15719 (1.9.0+) or -Dapollo.accesskey.secret=1cf998c4e2ad4704b45a98a509d15719(before 1.9.0)
      • If running a jar file, note that the format is java -Dapollo.access-key.secret=1cf998c4e2ad4704b45a98a509d15719 -jar xxx.jar (1.9.0+) or java -Dapollo.accesskey.secret=1cf998c4e2ad4704b45a98a509d15719 -jar xxx.jar(before 1.9.0)
    • Can also be specified programmatically, such as System.setProperty("apollo.access-key.secret", "1cf998c4e2ad4704b45a98a509d15719");(1.9.0+) or System.setProperty("apollo.accesskey.secret", "1cf998c4e2ad4704b45a98a509d15719"); (before 1.9.0)
  2. Via the Spring Boot configuration file
    • You can specify apollo.access-key.secret=1cf998c4e2ad4704b45a98a509d15719 in application.properties or bootstrap.properties of Spring Boot (1.9.0 +) or apollo.accesskey.secret=1cf998c4e2ad4704b45a98a509d15719 (before 1.9.0)
  3. Through the operating system’s System Environment
    • Can also be specified through the OS System Environment APOLLO_ACCESS_KEY_SECRET(1.9.0+) or APOLLO_ACCESSKEY_SECRET(before 1.9.0)
    • Note that the key is all-caps
  4. Via app.properties configuration file
    • You can specify apollo.access-key.secret=1cf998c4e2ad4704b45a98a509d15719(1.9.0+) or apollo.accessskey.secret=1cf998c4e2ad4704b45a98a509d15719(before 1.9.0)

1.2.4.5 Custom server.properties path

for version 1.8.0 and above

The following ways to customize the server.properties path are supported since version 1.8.0, in descending order of priority

  1. Via the Java System Property apollo.path.server.properties
    • Can be specified via the Java System Property apollo.path.server.properties
    • You can specify -Dapollo.path.server.properties=/some-dir/some-file.properties in the Java program startup script
      • If you are running a jar file, you need to note that the format is java -Dapollo.path.server.properties=/some-dir/some-file.properties -jar xxx.jar
    • Can also be specified programmatically, e.g. System.setProperty("apollo.path.server.properties", "/some-dir/some-file.properties");
  2. Via the operating system’s System Environment APOLLO_PATH_SERVER_PROPERTIES
    • can be specified by System Environment APOLLO_PATH_SERVER_PROPERTIES of the operating system
    • Note that the key is all-caps and separated by _.

1.2.4.6 Enables propertyNames caching, which can significantly improve startup speed in a large number of configuration scenarios

For version 1.9.0 and above

In scenarios where @ConfigurationProperties is used and there are a large number of configuration items, the Spring container can be slow to start. This configuration can be turned on to significantly improve startup speed, and the cache will be automatically cleared when the configuration changes, default is false. See: issue 3800

The configuration methods, in descending order of priority, are

  1. via Java System Property apollo.property.names.cache.enable
    • can be specified via the Java System Property apollo.property.names.cache.enable
    • You can specify -Dapollo.property.names.cache.enable=true in the Java program startup script
      • If you are running a jar file, note that the format is java -Dapollo.property.names.cache.enable=true -jar xxx.jar
    • You can also specify it programmatically, such as System.setProperty("apollo.property.names.cache.enable", "true");
  2. via system environment variables
    • Configure the environment variable APOLLO_PROPERTY_NAMES_CACHE_ENABLE=true before starting the program to specify
    • Note that the key is all-caps and separated by _.
  3. via the Spring Boot configuration file
    • You can specify apollo.properties.names.cache.enable=true in Spring Boot’s application.properties or bootstrap.properties.
  4. via the app.properties configuration file
    • You can specify apollo.property.names.cache.enable=true in classpath:/META-INF/app.properties

1.2.4.7 Apollo-Label

ApolloLabel is the label information of the application, an important piece of information to get the configuration from the server side for the grayscale rules.

There are several ways to set it, from highest to lowest priority, as follows.

  1. System Property

Apollo 2.0.0+ supports passing in apollo.label information via System Property, such as

  1. -Dapollo.label=YOUR-APOLLO-LABEL
  1. System Environment of the operating system

Apollo 2.0.0+ supports passing apollo.label information through the operating system’s System Environment APP_LABEL, as in

  1. APOLLO_LABEL=YOUR-APOLLO-LABEL
  1. Spring boot application.properties

Apollo 2.0.0+ supports configuration via Spring Boot’s application.properties file, such as

  1. apollo.label=YOUR-APOLLO-LABEL

This configuration is not suitable for multiple war packages deployed in the same tomcat scenario

  1. app.properties

Make sure that the classpath:/META-INF/app.properties file exists and that its contents are shaped like.

apollo.label=YOUR-APOLLO-LABEL

The file location reference is as follows.

app-id-location

Note: apollo.label is a label used to identify the application identity in the format string.

II. Maven Dependency

Apollo’s client jar package has been uploaded to the central repository, the application only needs to be introduced in the following way when it is actually used.

  1. <dependency>
  2. <groupId>com.ctrip.framework.apollo</groupId>
  3. <artifactId>apollo-client</artifactId>
  4. <version>1.7.0</version>
  5. </dependency>

III. Client Usage

Apollo supports API approach and Spring integration approach, how to choose which one to use?

  • The API approach is flexible, fully functional, configuration values are updated in real time (hot release), and supports all Java environments.

  • Spring approach is easy to access and has N cool ways to play with Spring, such as

    • Placeholder way.
      • Direct use in the code, such as: @Value("${someKeyFromApollo:someDefaultValue}")
      • Replace the placeholder in the configuration file, e.g.: spring.datasource.url: ${someKeyFromApollo:someDefaultValue}
      • Directly hosting spring’s configuration, such as directly configuring spring.datasource.url=jdbc:mysql://localhost:3306/somedb?characterEncoding=utf8 in apollo
    • Spring boot’s @ConfigurationProperties method
    • Versions from v0.10.0 onwards support automatic update of placeholder at runtime, see PR #972 for details. (Versions prior to v0.10.0 do not re-inject after configuration changes and require a restart to update. If you need real-time updates of configuration values, you can refer to the subsequent description in 3.2.2 Use of Spring Placeholder
  • The Spring approach can also be used in combination with the API approach, such as injecting Apollo’s Config object, you can get the configuration as usual through the API approach:

    1. @ApolloConfig
    2. private Config config; //inject config for namespace application
  • For more interesting practical usage scenarios and sample code, please refer to apollo-use-cases

3.1 API Usage

The API approach is the easiest and most efficient way to use Apollo configuration without relying on the Spring Framework to use it.

3.1.1 Get the configuration of the default namespace (application)

  1. Config config = ConfigService.getAppConfig(); //config instance is singleton for each namespace and is never null
  2. String someKey = "someKeyFromDefaultNamespace";
  3. String someDefaultValue = "someDefaultValueForTheKey";
  4. String value = config.getProperty(someKey, someDefaultValue);

With the above config.getProperty you can get the real-time latest configuration value corresponding to someKey.

In addition, the configuration values are fetched from memory, so there is no need for the application to do its own caching.

3.1.2 Listening for configuration change events

Listening for configuration change events is only used when the application really cares about configuration changes and needs to be notified when the configuration changes, e.g. when the database connection string changes and the connection needs to be rebuilt, etc.

If you just want to fetch the latest configuration every time, just follow the example above and call config.getProperty.

  1. Config config = ConfigService.getAppConfig(); //config instance is singleton for each namespace and is never null
  2. config.addChangeListener(new ConfigChangeListener() {
  3. @Override
  4. public void onChange(ConfigChangeEvent changeEvent) {
  5. System.out.println("Changes for namespace " + changeEvent.getNamespace());
  6. for (String key : changeEvent.changedKeys()) {
  7. ConfigChange change = changeEvent.getChange(key);
  8. System.out.println(String.format("Found change - key: %s, oldValue: %s, newValue: %s, changeType: %s", change.getPropertyName(), change. getOldValue(), change.getNewValue(), change.getChangeType()));
  9. }
  10. }
  11. });

3.1.3 Get the configuration of the public Namespace

  1. String somePublicNamespace = "CAT";
  2. Config config = ConfigService.getConfig(somePublicNamespace); //config instance is singleton for each namespace and is never null
  3. String someKey = "someKeyFromPublicNamespace";
  4. String someDefaultValue = "someDefaultValueForTheKey";
  5. String value = config.getProperty(someKey, someDefaultValue);

3.1.4 Get the configuration of a non-properties format namespace

3.1.4.1 Namespace in yaml/yml format

Apollo-Client version 1.3.0 starts to make better support for yaml/yml, which is used in the same way as properties format.

  1. Config config = ConfigService.getConfig("application.yml");
  2. String someKey = "someKeyFromYmlNamespace";
  3. String someDefaultValue = "someDefaultValueForTheKey";
  4. String value = config.getProperty(someKey, someDefaultValue);

3.1.4.2 Namespace in non-yaml/yml format

To get it, you need to use the ConfigService.getConfigFile interface and specify the Format, such as ConfigFileFormat.XML.

  1. String someNamespace = "test";
  2. ConfigFile configFile = ConfigService.getConfigFile("test", ConfigFileFormat.XML);
  3. String content = configFile.getContent();

3.2 Spring integration approach

3.2.1 Configuration

Apollo also supports integration with Spring (Spring 3.1.1+), and only requires some simple configuration.

Apollo currently supports both the more traditional XML-based configuration and the currently more popular Java-based (recommended) configuration.

In case of Spring Boot environments, it is recommended to refer to 3.2.1.3 Spring Boot integration methods (recommended) for configuration.

Note that if you have previously used org.springframework.beans.factory.config.PropertyPlaceholderConfigurer, please replace it with org.springframework.context.support.PropertySourcesPlaceholderConfigurer. It is not recommended to use PropertyPlaceholderConfigurer after Spring 3.1, use PropertySourcesPlaceholderConfigurer instead.

If you have used <context:property-placeholder> before, please note that the spring-context.xsd version introduced in the xml needs to be 3.1 or higher (usually it will be upgraded automatically as long as no version is specified), and it is recommended to introduce it without the version number, e.g.: http://www.springframework.org/schema/context/spring-context.xsd

Note 1: namespace in yaml/yml format supports integration with Spring since version 1.3.0, when injecting you need to fill in the full name with a suffix, such as application.yml

Note 2: Non-properties, non-yaml/yml formatted namespace (e.g. xml, json, etc.) do not support integration with Spring at this time.

3.2.1.1 XML-based configuration

Note: You need to add apollo related xml namespace to the configuration file header, otherwise it will report xml syntax errors.

  1. Inject the default namespace configuration into Spring
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:apollo="http://www.ctrip.com/schema/apollo"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  6. http://www.ctrip.com/schema/apollo http://www.ctrip.com/schema/apollo.xsd">
  7. <! -- This is the simplest form of configuration, which is generally sufficient for applications, and is used to instruct Apollo to inject the configuration of the application namespace into the Spring environment -->
  8. <apollo:config/>
  9. <bean class="com.ctrip.framework.apollo.spring.TestXmlBean">
  10. <property name="timeout" value="${timeout:100}"/>
  11. <property name="batch" value="${batch:200}"/>
  12. </bean>
  13. </beans>
  1. Inject multiple namespace configuration into Spring
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:apollo="http://www.ctrip.com/schema/apollo"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  6. http://www.ctrip.com/schema/apollo http://www.ctrip.com/schema/apollo.xsd">
  7. <! -- This is the simplest form of configuration, which is generally sufficient for applications, and is used to instruct Apollo to inject the configuration of the application namespace into the Spring environment -->
  8. <apollo:config/>
  9. <! -- This is a slightly more complex form of configuration that instructs Apollo to inject the configuration of FX.apollo and application.yml namespace into the Spring environment -->
  10. <apollo:config namespaces="FX.apollo,application.yml"/>
  11. <bean class="com.ctrip.framework.apollo.spring.TestXmlBean">
  12. <property name="timeout" value="${timeout:100}"/>
  13. <property name="batch" value="${batch:200}"/>
  14. </bean>
  15. </beans>
  1. Inject multiple namespaces and specify the order

If multiple property sources have the same key, then the configuration with the highest order will take effect.

If does not specify an order, then the default is the lowest priority.

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:apollo="http://www.ctrip.com/schema/apollo"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  6. http://www.ctrip.com/schema/apollo http://www.ctrip.com/schema/apollo.xsd">
  7. <apollo:config order="2"/>
  8. <! -- This is the most complex form of configuration, instructing Apollo to inject the configuration of FX.apollo and application.yml namespace into the Spring environment, and in order before application -->
  9. <apollo:config namespaces="FX.apollo,application.yml" order="1"/>
  10. <bean class="com.ctrip.framework.apollo.spring.TestXmlBean">
  11. <property name="timeout" value="${timeout:100}"/>
  12. <property name="batch" value="${batch:200}"/>
  13. </bean>
  14. </beans>
  15. `

Java-based configuration is currently the more popular approach as opposed to XML-based configuration.

Note that @EnableApolloConfig should be used together with @Configuration, otherwise it will not take effect.

  1. Inject the default namespace configuration into Spring
  1. // This is the simplest form of configuration and is generally used by applications to instruct Apollo to inject the configuration of application namespace into the Spring environment
  2. @Configuration
  3. @EnableApolloConfig
  4. public class AppConfig {
  5. @Bean
  6. public TestJavaConfigBean javaConfigBean() {
  7. return new TestJavaConfigBean();
  8. }
  9. }
  10. `
  1. Inject multiple namespace configuration into Spring
  1. @Configuration
  2. @EnableApolloConfig
  3. public class SomeAppConfig {
  4. @Bean
  5. public TestJavaConfigBean javaConfigBean() {
  6. return new TestJavaConfigBean();
  7. }
  8. }
  9. // This is a slightly more complex form of configuration, instructing Apollo to inject the configuration of FX.apollo and application.yml namespace into the Spring environment
  10. @Configuration
  11. @EnableApolloConfig({"FX.apollo", "application.yml"})
  12. public class AnotherAppConfig {}
  1. Inject multiple namespaces and specify the order
  1. // This is the most complex form of configuration, instructing Apollo to inject the configuration of FX.apollo and application.yml namespace into the Spring environment, and in the order before application
  2. @Configuration
  3. @EnableApolloConfig(order = 2)
  4. public class SomeAppConfig {
  5. @Bean
  6. public TestJavaConfigBean javaConfigBean() {
  7. return new TestJavaConfigBean();
  8. }
  9. }
  10. @Configuration
  11. @EnableApolloConfig(value = {"FX.apollo", "application.yml"}, order = 1)
  12. public class AnotherAppConfig {}

Spring Boot supports the above two integration methods in addition to configuration via application.properties/bootstrap.properties, which enables configuration to be injected at an earlier stage, such as scenarios that use @ConditionalOnProperty or have some spring-boot-starter needs to read the configuration to do something in the startup phase (e.g. dubbo-spring-boot-project). So for Spring Boot environment it is recommended to access Apollo (requires version 0.10.0 and above) by the following way.

It is very simple to use, you just need to configure it in application.properties/bootstrap.properties according to the following sample.

  1. Example configuration with default application namespace injected
  1. # will inject 'application' namespace in bootstrap phase
  2. apollo.bootstrap.enabled = true
  1. Example configuration for injecting non-default application namespace or multiple namespaces
  1. apollo.bootstrap.enabled = true
  2. # will inject 'application', 'FX.apollo' and 'application.yml' namespaces in bootstrap phase
  3. apollo.bootstrap.namespaces = application,FX.apollo,application.yml
  1. Load Apollo configuration before initializing the logging system (1.2.0+)

Starting with version 1.2.0, if you wish to put logging-related configuration (such as logging.level.root=info or parameters in logback-spring.xml) in Apollo management as well, then you can additionally configure apollo.bootstrap.eagerLoad. enabled=true to put Apollo loading order before logging system loading, for more information you can refer to PR 1614. The reference configuration example is as follows.

  1. # will inject 'application' namespace in bootstrap phase
  2. apollo.bootstrap.enabled = true
  3. # put apollo initialization before logging system initialization
  4. apollo.bootstrap.eagerLoad.enabled = true

For Spring Boot 2.4+ versions there is also support for loading configurations via Config Data Loader mode

3.2.1.4.1 Adding maven dependencies

apollo-client-config-data already depends on apollo-client, so you only need to add this one dependency, not the apollo-client dependency

  1. <dependencies>
  2. <dependency>
  3. <groupId>com.ctrip.framework.apollo</groupId>
  4. <artifactId>apollo-client-config-data</artifactId>
  5. <version>1.9.0</version>
  6. </dependency
  7. </dependencies>
3.2.1.4.2 Configure app.id, env, apollo.meta (or apollo.config-service), apollo.cluster as described above
3.2.1.4.3 Configure application.properties or application.yml

Use the default namespace application

  1. # old way
  2. # apollo.bootstrap.enabled=true
  3. # do not configure apollo.bootstrap.namespaces
  4. # new way
  5. spring.config.import=apollo://

or

  1. # old way
  2. # apollo.bootstrap.enabled=true
  3. # apollo.bootstrap.namespaces=application
  4. # new way
  5. spring.config.import=apollo://application

Use custom namespace

  1. # old way
  2. # apollo.bootstrap.enabled=true
  3. # apollo.bootstrap.namespaces=your-namespace
  4. # new way
  5. spring.config.import=apollo://your-namespace

Using multiple namespaces
Note: spring.config.import loads the configuration from back to front, while apollo.bootstrap.namespaces loads it from back to front, just the opposite. To ensure consistency with the original logic, reverse the order of namespaces

  1. # old way
  2. # apollo.bootstrap.enabled=true
  3. # apollo.bootstrap.namespaces=namespace1,namespace2,namespace3
  4. # new way
  5. spring.config.import=apollo://namespace3, apollo://namespace2, apollo://namespace1

For Spring Boot version 2.4 and above, it also supports loading configuration through Config Data Loader mode Apollo’s Config Data Loader also provides a webClient-based http client to replace the original http client, so as to easily extend the http client

3.2.1.5.1 Add maven dependency

WebClient can be based on multiple implementations (reactor netty httpclient, jetty reactive httpclient, apache httpclient5), the dependencies to be added are as follows

Reactor netty httpclient
  1. <dependencies>
  2. <dependency>
  3. <groupId>com.ctrip.framework.apollo</groupId>
  4. <artifactId>apollo-client-config-data</artifactId>
  5. <version>1.9.0</version>
  6. </dependency>
  7. <!-- webclient -->
  8. <dependency>
  9. <groupId>org.springframework</groupId>
  10. <artifactId>spring-webflux</artifactId>
  11. </dependency>
  12. <!-- reactor netty httpclient -->
  13. <dependency>
  14. <groupId>io.projectreactor.netty</groupId>
  15. <artifactId>reactor-netty-http</artifactId>
  16. </dependency>
  17. </dependencies>
Jetty reactive httpclient
  1. <dependencies>
  2. <dependency>
  3. <groupId>com.ctrip.framework.apollo</groupId>
  4. <artifactId>apollo-client-config-data</artifactId>
  5. <version>1.9.0</version>
  6. </dependency>
  7. <!-- webclient -->
  8. <dependency>
  9. <groupId>org.springframework</groupId>
  10. <artifactId>spring-webflux</artifactId>
  11. </dependency>
  12. <!-- jetty reactive httpclient -->
  13. <dependency>
  14. <groupId>org.eclipse.jetty</groupId>
  15. <artifactId>jetty-reactive-httpclient</artifactId>
  16. </dependency>
  17. </dependencies>
Apache httpclient5

Spring boot does not specify the version of apache httpclient5, so you need to manually specify the version here

  1. <dependencies>
  2. <dependency>
  3. <groupId>com.ctrip.framework.apollo</groupId>
  4. <artifactId>apollo-client-config-data</artifactId>
  5. <version>1.9.0</version>
  6. </dependency>
  7. <!-- webclient -->
  8. <dependency>
  9. <groupId>org.springframework</groupId>
  10. <artifactId>spring-webflux</artifactId>
  11. </dependency>
  12. <!-- apache httpclient5 -->
  13. <dependency>
  14. <groupId>org.apache.httpcomponents.client5</groupId>
  15. <artifactId>httpclient5</artifactId>
  16. <version>5.1</version>
  17. </dependency>
  18. <dependency>
  19. <groupId>org.apache.httpcomponents.core5</groupId>
  20. <artifactId>httpcore5-reactive</artifactId>
  21. <version>5.1</version>
  22. </dependency>
  23. </dependencies>
3.2.1.5.2 Configure app.id, env, apollo.meta (or apollo.config-service), apollo.cluster as described above
3.2.1.5.3 Configuring application.properties or application.yml

The default namespace is used here as an example. Please refer to 3.2.1.4.3 for the configuration of namespace.

  1. spring.config.import=apollo://application
  2. apollo.client.extension.enabled=true
3.2.1.5.4 Provide implementation of spi

Provides a spi implementation of interface com.ctrip.framework.apollo.config.data.extension.webclient.customizer.spi.ApolloClientWebClientCustomizerFactory After configuring apollo.client.extension.enabled=true, Apollo’s Config Data Loader will try to load the spi’s implementation class to customize the webClient

3.2.2 Use of Spring Placeholder

Spring applications usually use Placeholder to inject configuration in the form of ${someKey:someDefaultValue}, such as ${timeout:100}. The key before the colon is the key, and the default value after the colon.

It is recommended to give the default value as much as possible in actual use, so as to avoid runtime errors due to the undefined key.

Versions starting from v0.10.0 support automatic update of placeholders at runtime, see PR #972.

If you need to turn off the automatic update function of placeholder at runtime, you can turn it off in the following two ways:

  1. By setting the System Property apollo.autoUpdateInjectedSpringProperties, such as passing in -Dapollo.autoUpdateInjectedSpringProperties=false at startup

  2. By setting the apollo.autoUpdateInjectedSpringProperties property in META-INF/app.properties, such as

  1. app.id=SampleApp
  2. apollo.autoUpdateInjectedSpringProperties=false

3.2.2.1 XML usage

Suppose I have a TestXmlBean with two configuration items that need to be injected:

  1. public class TestXmlBean {
  2. private int timeout;
  3. private int batch;
  4. public void setTimeout(int timeout) {
  5. this.timeout = timeout;
  6. }
  7. public void setBatch(int batch) {
  8. this.batch = batch;
  9. }
  10. public int getTimeout() {
  11. return timeout;
  12. }
  13. public int getBatch() {
  14. return batch;
  15. }
  16. }

Then, I will use the following way to define in XML (assuming that the default application namespace of the application has timeout and batch configuration items):

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:apollo="http://www.ctrip.com/schema/apollo"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  6. http://www.ctrip.com/schema/apollo http://www.ctrip.com/schema/apollo.xsd">
  7. <apollo:config/>
  8. <bean class="com.ctrip.framework.apollo.spring.TestXmlBean">
  9. <property name="timeout" value="${timeout:100}"/>
  10. <property name="batch" value="${batch:200}"/>
  11. </bean>
  12. </beans>

3.2.2.2 How to use Java Config

Suppose I have a TestJavaConfigBean, which can also be injected using @Value through Java Config:

  1. public class TestJavaConfigBean {
  2. @Value("${timeout:100}")
  3. private int timeout;
  4. private int batch;
  5. @Value("${batch:200}")
  6. public void setBatch(int batch) {
  7. this.batch = batch;
  8. }
  9. public int getTimeout() {
  10. return timeout;
  11. }
  12. public int getBatch() {
  13. return batch;
  14. }
  15. }

In the Configuration class, use it in the following way (assuming the default application namespace of the application has timeout and batch configuration items):

  1. @Configuration
  2. @EnableApolloConfig
  3. public class AppConfig {
  4. @Bean
  5. public TestJavaConfigBean javaConfigBean() {
  6. return new TestJavaConfigBean();
  7. }
  8. }

3.2.2.3 How to use ConfigurationProperties

Spring Boot provides @ConfigurationProperties to inject configuration into bean objects .

Apollo also supports this method. The following example will inject redis.cache.expireSeconds and redis.cache.commandTimeout into the expireSeconds and commandTimeout fields of SampleRedisConfig respectively.

  1. @ConfigurationProperties(prefix = "redis.cache")
  2. public class SampleRedisConfig {
  3. private int expireSeconds;
  4. private int commandTimeout;
  5. public void setExpireSeconds(int expireSeconds) {
  6. this.expireSeconds = expireSeconds;
  7. }
  8. public void setCommandTimeout(int commandTimeout) {
  9. this.commandTimeout = commandTimeout;
  10. }
  11. }

In the Configuration class, use it in the following way (assuming the default application namespace of the application has redis.cache.expireSeconds and redis.cache.commandTimeout configuration items):

  1. @Configuration
  2. @EnableApolloConfig
  3. public class AppConfig {
  4. @Bean
  5. public SampleRedisConfig sampleRedisConfig() {
  6. return new SampleRedisConfig();
  7. }
  8. }

It should be noted that if @ConfigurationProperties needs to automatically update the injected value when the Apollo configuration changes, you need to use EnvironmentChangeEvent or RefreshScope. For related code implementation, please refer to ZuulPropertiesRefresher.java and SampleRedisConfig.java and SpringBootApolloRefreshConfig.java

3.2.3 Spring Annotation Support

Apollo also adds several new Annotations to simplify usage in the Spring environment.

  1. @ApolloConfig
    • Used to automatically inject the Config object
  2. @ApolloConfigChangeListener
    • Used to automatically register ConfigChangeListener
  3. @ApolloJsonValue
    • Used to automatically inject the configured json string as an object

Example of usage is as follows:

  1. public class TestApolloAnnotationBean {
  2. @ApolloConfig
  3. private Config config; //inject config for namespace application
  4. @ApolloConfig("application")
  5. private Config anotherConfig; //inject config for namespace application
  6. @ApolloConfig("FX.apollo")
  7. private Config yetAnotherConfig; //inject config for namespace FX.apollo
  8. @ApolloConfig("application.yml")
  9. private Config ymlConfig; //inject config for namespace application.yml
  10. /**
  11. * ApolloJsonValue annotated on fields example, the default value is specified as empty list - []
  12. * <br />
  13. * jsonBeanProperty=[{"someString":"hello","someInt":100},{"someString":"world!","someInt":200}]
  14. */
  15. @ApolloJsonValue("${jsonBeanProperty:[]}")
  16. private List<JsonBean> anotherJsonBeans;
  17. @Value("${batch:100}")
  18. private int batch;
  19. //config change listener for namespace application
  20. @ApolloConfigChangeListener
  21. private void someOnChange(ConfigChangeEvent changeEvent) {
  22. //update injected value of batch if it is changed in Apollo
  23. if (changeEvent.isChanged("batch")) {
  24. batch = config.getIntProperty("batch", 100);
  25. }
  26. }
  27. //config change listener for namespace application
  28. @ApolloConfigChangeListener("application")
  29. private void anotherOnChange(ConfigChangeEvent changeEvent) {
  30. //do something
  31. }
  32. //config change listener for namespaces application, FX.apollo and application.yml
  33. @ApolloConfigChangeListener({"application", "FX.apollo", "application.yml"})
  34. private void yetAnotherOnChange(ConfigChangeEvent changeEvent) {
  35. //do something
  36. }
  37. //example of getting config from Apollo directly
  38. //this will always return the latest value of timeout
  39. public int getTimeout() {
  40. return config.getIntProperty("timeout", 200);
  41. }
  42. //example of getting config from injected value
  43. //the program needs to update the injected value when batch is changed in Apollo using @ApolloConfigChangeListener shown above
  44. public int getBatch() {
  45. return this.batch;
  46. }
  47. private static class JsonBean{
  48. private String someString;
  49. private int someInt;
  50. }
  51. }

Use it in the Configuration class as follows:

  1. @Configuration
  2. @EnableApolloConfig
  3. public class AppConfig {
  4. @Bean
  5. public TestApolloAnnotationBean testApolloAnnotationBean() {
  6. return new TestApolloAnnotationBean();
  7. }
  8. }

3.2.4 Existing configuration migration

In many cases, the application may already have a lot of configuration, such as Spring Boot application, there will be configurations such as bootstrap.properties/yml, application.properties/yml, etc.

After the application is connected to Apollo, these configurations can be easily migrated to Apollo. The specific steps are as follows:

  1. Create a new project for the application in Apollo
  2. Configure META-INF/app.properties in the application
  3. It is recommended to convert the original configuration to the properties format, and then paste it into the application namespace of the application through the text editing mode provided by Apollo, and publish the configuration
  4. If the original is yml and you want to continue to use yml to edit the configuration, you can create a private application.yml namespace, paste all the original configuration into it, and publish the configuration
    • Requires apollo-client to be version 1.3.0 and above
  5. Delete the original configuration files such as bootstrap.properties/yml, application.properties/yml from the project
    • If you need to keep the local configuration file, it should be noted that some configurations such as server.port must ensure that the configuration item has been deleted from the local file

Such as:

  1. spring.application.name=reservation-service
  2. server.port = 8080
  3. logging.level = ERROR
  4. eureka.client.serviceUrl.defaultZone = http://127.0.0.1:8761/eureka/
  5. eureka.client.healthcheck.enabled=true
  6. eureka.client.registerWithEureka = true
  7. eureka.client.fetchRegistry = true
  8. eureka.client.eurekaServiceUrlPollIntervalSeconds = 60
  9. eureka.instance.preferIpAddress = true

text-mode-spring-boot-config-sample

3.3 Demo

There is a sample client project in the project: apollo-demo, for details, please refer to 2.3 Java Sample Client Start in Apollo Development Guide section.

For more use case demos, please refer to Apollo usage scenarios and sample code.

IV. Client design

client-architecture

The above diagram briefly describes the principle of Apollo client implementation.

  1. The client and the server maintain a long connection so that it can get the first push of configuration updates. (achieved through Http Long Polling) .
  2. The client also regularly pulls the latest configuration of the application from the Apollo Configuration Center server.
    • This is a fallback mechanism to prevent the configuration from being updated due to the failure of the push mechanism.
    • The client will report the local version of the timed pull, so in general, for the timed pull operation, the server will return 304 - Not Modified.
    • Timing frequency defaults to pulling every 5 minutes. Clients can also override this by specifying System Property: apollo.refreshInterval at runtime, in minutes.
  3. After the client gets the latest configuration of the application from the Apollo Configuration Center server, it will be saved in memory.
  4. The client will cache a copy of the configuration obtained from the server in the local file system. In case of service unavailability or network failure, the configuration can still be restored locally.
  5. The application can get the latest configuration from the Apollo client, subscribe to configuration update notifications.

V. Local Development Mode

Apollo client also supports local development mode, which is mainly used when the development environment cannot connect to Apollo server, such as doing related function development on cruise ships or airplanes.

In local development mode, Apollo will only read configuration information from local files, not from Apollo server.

You can enable Apollo local development mode by following the steps below.

5.1 Modifying the environment

Modify the /opt/settings/server.properties (Mac/Linux) or C:\opt\settings\server.properties (Windows) file to set the env to Local:

  1. env=Local

For more ways to configure the environment, please refer to 1.2.4.1 Environment

5.2 Preparing local configuration files

In local development mode, Apollo client will read the files from local, so we need to prepare the configuration file beforehand.

5.2.1 Local configuration directory

The local configuration directory is located at.

  • Mac/Linux: /opt/data/{appId}/config-cache
  • Windows: C:\opt\data\{appId}\config-cache

The appId is the appId of the application, e.g. 100004458.

Please make sure the directory exists and the application has read access to it.

[Tip] The recommended way is to use Apollo in normal mode first, so that Apollo will automatically create the directory and generate the configuration file under it.

5.2.2 Local configuration files

Local configuration files need to be placed in the local configuration directory according to a certain file name format, which is as follows.

{appId}+{cluster}+{namespace}.properties

  • AppId is the application’s own appId, such as 100004458
  • Cluster is the cluster used by the application, generally in local mode without configuration, it is default
  • Namespace is the configuration namespace used by the application, usually application client-local-cache

The content of the file is stored in properties format, for example, if there are two keys, one is request.timeout and the other is batch, then the content of the file is in the following format.

  1. request.timeout=2000
  2. batch=2000

5.3 Modifying the configuration

In local development mode, Apollo does not monitor the file content for changes in real time, so if you modify the configuration, you need to restart the application to take effect.

VI. Test mode

The apollo-mockserver has been added since version 1.1.0, so that it can well support scenarios where mock configuration is required for unit testing, using the following methods.

6.1 Introducing pom dependencies

  1. <dependency>
  2. <groupId>com.ctrip.framework.apollo</groupId>
  3. <artifactId>apollo-mockserver</artifactId>
  4. <version>1.7.0</version>
  5. </dependency>

6.2 Placing mock data under test’s resources

The file name convention is mockdata-{namespace}.properties

image

6.3 Writing test classes

For more usage demos, see ApolloMockServerApiTest.java and ApolloMockServerSpringIntegrationTest.java.

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @SpringApplicationConfiguration(classes = TestConfiguration.class)
  3. public class SpringIntegrationTest {
  4. // startup apollo's mockserver
  5. @ClassRule
  6. public static EmbeddedApollo embeddedApollo = new EmbeddedApollo();
  7. @Test
  8. @DirtiesContext // This annotation is necessary because configuration injection can mess up the application context
  9. public void testPropertyInject(){
  10. assertEquals("value1", testBean.key1);
  11. assertEquals("value2", testBean.key2);
  12. }
  13. @Test
  14. @DirtiesContext
  15. public void testListenerTriggeredByAdd() throws InterruptedException, ExecutionException, TimeoutException {
  16. String otherNamespace = "othernamespace";
  17. embeddedApollo.addOrModifyPropery(otherNamespace,"someKey","someValue");
  18. ConfigChangeEvent changeEvent = testBean.futureData.get(5000, TimeUnit.MILLISECONDS);
  19. assertEquals(otherNamespace, changeEvent.getNamespace());
  20. assertEquals("someValue", changeEvent.getChange("someKey").getNewValue());
  21. }
  22. @EnableApolloConfig("application")
  23. @Configuration
  24. static class TestConfiguration{
  25. @Bean
  26. public TestBean testBean(){
  27. return new TestBean();
  28. }
  29. }
  30. static class TestBean{
  31. @Value("${key1:default}")
  32. String key1;
  33. @Value("${key2:default}")
  34. String key2;
  35. SettableFuture<ConfigChangeEvent> futureData = SettableFuture.create();
  36. @ApolloConfigChangeListener("othernamespace")
  37. private void onChange(ConfigChangeEvent changeEvent) {
  38. futureData.set(changeEvent);
  39. }
  40. }
  41. }