Thank you for your support of Capa!
This is the multi-page printable view of this section. Click here to print.
Getting Started
1 - Using Configuration API
Introduction
The configuration capabilities provided by CapaConfigurationClient require concrete implementation classes to adapt to different platforms (by inheriting the CapaConfigStoreSpi abstract class), such as the example DemoCapaConfigStore.
Loading of the concrete implementation class is achieved through the SPI mechanism. The specific configuration process is: add a capa-component-configuration.properties file under the project’s resources path.
Add a new property key: “group.rxcloud.capa.component.configstore.CapaConfigStore” with value: “full path of the implementation class”; add a new property key: “CONFIGURATION_COMPONENT_STORE_NAME” with value: “config store name”. Example:
//capa-component-configuration.properties file group.rxcloud.capa.component.configstore.CapaConfigStore=group.rxcloud.capa.spi.demo.configstore.DemoCapaConfigStore CONFIGURATION_COMPONENT_STORE_NAME=DEMO CONFIGCall the corresponding Configuration API for application-level configuration management.
API Usage Steps
Step 1: Build a singleton Configuration Client
public final class CapaConfigStoreClientProvider {
private static volatile CapaConfigurationClient client;
public static CapaConfigurationClient getClient() {
if (client == null) {
synchronized (CapaConfigStoreClientProvider.class) {
if (client == null) {
StoreConfig storeConfig = new StoreConfig();
storeConfig.setStoreName(Optional.ofNullable(CapaProperties.COMPONENT_PROPERTIES_SUPPLIER.apply("configuration").getProperty("CONFIGURATION_COMPONENT_STORE_NAME")).orElse("UN_CONFIGURED_STORE_CONFIG_NAME"));
client = new CapaConfigurationClientBuilder(storeConfig).build();
}
}
}
return client;
}
private CapaConfigStoreClientProvider() {
}
}
Step 2: Use the provided API to read/subscribe/delete/save configurations
- Read configuration operation (getConfiguration)
// Get singleton client
private static final CapaConfigurationClient client = CapaConfigStoreClientSingleton.getClient();
// getConfiguration() one of the overloaded methods
Mono<List<ConfigurationItem<User>>> configMono = client.getConfiguration(new ConfigurationRequestItem(), TypeRef.get(User.class));
// getConfiguration() another overloaded method
Mono<List<ConfigurationItem<User>>> configMono = client.getConfiguration("config",
SERVICE_APP_ID,
Lists.newArrayList("test.json"),
metaDataMap,
"group",
"label"
TypeRef.get(User.class));
// Block to get configuration result
List<ConfigurationItem<User>> config = configMono.block();
- Subscribe to configuration operation (subscribeConfiguration)
// Local variable to store configuration
private SubConfigurationResp<String> cur;
// subscribeConfiguration() one of the overloaded methods
Flux<SubConfigurationResp<User>> configFlux = client.subscribeConfiguration(new ConfigurationRequestItem(), TypeRef.get(User.class));
// subscribeConfiguration() another overloaded method
Flux<SubConfigurationResp<User>> configFlux = client.subscribeConfiguration("config",
SERVICE_APP_ID,
Lists.newArrayList("test.json"),
metaDataMap,
"group",
"label"
TypeRef.get(User.class));
// Subscribe to subsequent changes and update original data
configFlux.subscribe(resp -> cur.setItems(resp.getItems()));
- Save configuration operation (saveConfiguration)
Mono<Void> configFlux = client.saveConfiguration(new SaveConfigurationRequest());
- Delete configuration operation (deleteConfiguration)
Mono<Void> configFlux = client.deleteConfiguration(new ConfigurationRequestItem());
Note: The above APIs have overloaded methods. Click here to view the full API list.
2 - Using RPC API
Steps
- Build RPC Client
CapaRpcClient capaRpcClient = new CapaRpcClientBuilder().build();
- Invoke request
Mono<byte[]> responseMono = capaRpcClient.invokeMethod(SERVICE_APP_ID,
"hello",
"hello",
HttpExtension.POST,
null,
TypeRef.BYTE_ARRAY);
- Get invocation result
byte[] response = responseMono.block();
Explanation
- The RPC capabilities provided by CapaRpcClient require concrete implementation, as shown in the example DemoCapaHttp.
- After implementing DemoCapaHttp, it needs to be configured in capa-component-rpc.properties, and loaded through the SPI mechanism.