From 0c7c3415ce95003ba1e6fa7166f78304d8eb4852 Mon Sep 17 00:00:00 2001 From: Terry Lam <36256759+TerryLam2010@users.noreply.github.com> Date: Sat, 14 Dec 2024 14:40:01 +0800 Subject: [PATCH] Add usage instructions for multiple appid in the Apollo client (#5295) * add multiple appid dosc add multiple appid dosc * fix some problem * Apply suggestions from code review --------- Co-authored-by: Jason Song --- docs/en/client/java-sdk-user-guide.md | 23 +++++++++++++++++++++++ docs/zh/client/java-sdk-user-guide.md | 22 ++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/docs/en/client/java-sdk-user-guide.md b/docs/en/client/java-sdk-user-guide.md index eb8e11dbb44..276d59a27c7 100644 --- a/docs/en/client/java-sdk-user-guide.md +++ b/docs/en/client/java-sdk-user-guide.md @@ -611,6 +611,17 @@ ConfigFile configFile = ConfigService.getConfigFile("test", ConfigFileFormat.XML String content = configFile.getContent(); ``` +### 3.1.5 Read the configuration corresponding to multiple appid and their namespaces.(added in version 2.4.0) +Specify the corresponding appid and namespace to retrieve the config, and then obtain the properties. +```java +String someAppId = "Animal"; +String somePublicNamespace = "CAT"; +Config config = ConfigService.getConfig(someAppId, somePublicNamespace); +String someKey = "someKeyFromPublicNamespace"; +String someDefaultValue = "someDefaultValueForTheKey"; +String value = config.getProperty(someKey, someDefaultValue); +``` + ## 3.2 Spring integration approach ### 3.2.1 Configuration @@ -749,6 +760,18 @@ public class SomeAppConfig { public class AnotherAppConfig {} ``` +4.Support for multiple appid (added in version 2.4.0) +```java +// Added support for loading multiple appid their corresponding namespaces. +// Note that when using multiple appid, if there are keys that are the same, +// only the key from the prioritized loaded appid will be retrieved +@Configuration +@EnableApolloConfig(value = {"FX.apollo", "application.yml"}, + multipleConfigs = {@MultipleConfig(appid = "ORDER_SERVICE", namespaces = {"ORDER.apollo"})} +) +public class SomeAppConfig {} +``` + #### 3.2.1.3 Spring Boot integration methods (recommended) 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](https://github.com/apache/incubator-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. diff --git a/docs/zh/client/java-sdk-user-guide.md b/docs/zh/client/java-sdk-user-guide.md index fa12873ad89..f3314696228 100644 --- a/docs/zh/client/java-sdk-user-guide.md +++ b/docs/zh/client/java-sdk-user-guide.md @@ -583,6 +583,17 @@ ConfigFile configFile = ConfigService.getConfigFile("test", ConfigFileFormat.XML String content = configFile.getContent(); ``` +### 3.1.5 读取多AppId对应namespace的配置 +指定对应的AppId和namespace来获取Config,再获取属性 +```java +String someAppId = "Animal"; +String somePublicNamespace = "CAT"; +Config config = ConfigService.getConfig(someAppId, somePublicNamespace); +String someKey = "someKeyFromPublicNamespace"; +String someDefaultValue = "someDefaultValueForTheKey"; +String value = config.getProperty(someKey, someDefaultValue); +``` + ## 3.2 Spring整合方式 ### 3.2.1 配置 @@ -719,6 +730,17 @@ public class SomeAppConfig { public class AnotherAppConfig {} ``` +4.多appId的支持(新增于2.4.0版本) +```java +// 新增支持了多appId和对应namespace的加载,注意使用多appId的情况下,key相同的情况,只会取优先加载appId的那一个key +@Configuration +@EnableApolloConfig(value = {"FX.apollo", "application.yml"}, + multipleConfigs = {@MultipleConfig(appid = "ORDER_SERVICE", namespaces = {"ORDER.apollo"})} +) +public class SomeAppConfig {} +``` + + #### 3.2.1.3 Spring Boot集成方式(推荐) Spring Boot除了支持上述两种集成方式以外,还支持通过application.properties/bootstrap.properties来配置,该方式能使配置在更早的阶段注入,比如使用`@ConditionalOnProperty`的场景或者是有一些spring-boot-starter在启动阶段就需要读取配置做一些事情(如[dubbo-spring-boot-project](https://github.com/apache/incubator-dubbo-spring-boot-project)),所以对于Spring Boot环境建议通过以下方式来接入Apollo(需要0.10.0及以上版本)。