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.
Net access documentation, please refer to Apollo.net Framework Integration
Apollo client relies on AppId
, Environment
and other environment information to work, so make sure to read the following instructions and do the correct configuration.
AppId is the identity of the application and is an important piece of information to get the configuration from the server.
Make sure you have the AppID configuration in app.config
or web.config
, where the content looks like
<?xml version="1.0"?>
<configuration>
<appSettings>
<! -- Change to the actual app id -->
<add key="Apollo.AppId" value="100004458"/>
</appSettings>
</configuration>
Note: app.id is a unique id used to identify the application identity in string format.
Apollo supports applications that have different configurations for different environments, so Environment is another important piece of information to get the configuration from the server.
Environment is specified through a configuration file in the location C:\opt\settings\server.properties
, with the contents of the file shaped like this
env=DEV
Currently, env
supports the following values (case-insensitive).
Apollo clients get their configuration from different servers for different environments, so make sure that the server address (Apollo.{ENV}.Meta) is correctly configured in app.config or web.config, where the content looks like
<?xml version="1.0"?>
<configuration>
<appSettings>
<! -- Change to the actual app id -->
<add key="Apollo.AppId" value="100004458"/>
<! -- Should change the apollo config service url for each environment -->
<add key="Apollo.DEV.Meta" value="http://dev-configservice:8080"/>
<add key="Apollo.FAT.Meta" value="http://fat-configservice:8080"/>
<add key="Apollo.UAT.Meta" value="http://uat-configservice:8080"/>
<Meta" value="http://pro-configservice:8080"/>
</appSettings>
</configuration>
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 C:\opt\data\{appId}\config-cache
, so please make sure that the C:\opt\data\
directory exists and the application has read/write access.
Cluster
Apollo supports configuration by cluster, meaning that for an appId and an environment, there can be different configurations for different clusters.
If you need to use this feature, you can specify the runtime clusters by.
via App Config
via configuration file
C:\opt\settings\server.properties
exists on the target machineidc=xxx
Cluster Precedence (cluster order)
If Apollo.Cluster
and idc
are both 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
)Net client project address is located at: https://github.com/ctripcorp/apollo.net.
Download the project locally, switch to the Release
configuration, compile the Solution and it will generate Framework.Apollo.Client.dll
in apollo.net\Apollo\bin\Release
.
Just reference Framework.Apollo.Client.dll
in your application.
.Net Core, you can refer to dotnet-core and nuget repository
Config config = ConfigService.GetAppConfig(); //config instance is singleton for each namespace and is never null
string someKey = "someKeyFromDefaultNamespace";
string someDefaultValue = "someDefaultValueForTheKey";
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.
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 call config.GetProperty as in the example above.
Config config = ConfigService.GetAppConfig(); //config instance is singleton for each namespace and is never null
config.ConfigChanged += new ConfigChangeEvent(OnChanged);
private void OnChanged(object sender, ConfigChangeEventArgs changeEvent)
{
Console.WriteLine("Changes for namespace {0}", changeEvent.Namespace);
foreach (string key in changeEvent.ChangedKeys)
{
ConfigChange change = changeEvent.GetChange(key);
Console.WriteLine("Change - key: {0}, oldValue: {1}, newValue: {2}, changeType: {3}", change.PropertyName, change, NewValue, change.ChangeType);
}
}
string somePublicNamespace = "CAT";
Config config = ConfigService.GetConfig(somePublicNamespace); //config instance is singleton for each namespace and is never null
string someKey = "someKeyFromPublicNamespace";
string someDefaultValue = "someDefaultValueForTheKey";
string value = config.GetProperty(someKey, someDefaultValue);
There is a sample client project in apollo.net project: ApolloDemo
, you can refer to 2.4 .Net sample client startup for more information.
Net client open source version will output logs directly to the Console by default, you can implement your own logging-related features.
See https://github.com/ctripcorp/apollo.net/tree/master/Apollo/Logging/Spi for more details .
The above diagram briefly describes the principle of Apollo client implementation.
Apollo.RefreshInterval
through App.config to override it in milliseconds.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.
Modify the C:\opt\settings\server.properties
file to set the env to Local:
env=Local
In local development mode, Apollo client will read files from local, so we need to prepare the configuration file beforehand.
The local configuration directory is located at: C:\opt\data{appId}\config-cache.
The appId is the appId of the application, e.g. 100004458.
Please make sure that the directory exists and the application has read access to the directory.
[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.
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}.json
namespace
used by the application, usually application
The content of the file is stored in json format, for example, if there are two keys, one is request.timeout, the other is batch, then the content of the file is the following format.
{
"request.timeout": "1000",
"batch": "2000"
}
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.