This is the multi-page printable view of this section. Click here to print.

Return to the regular view of this page.

Getting Started

Get started with Capa quickly.

Thank you for your support of Capa!

1 - Using Configuration API

Using Configuration API for application-level configuration management.

Introduction

  1. 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.

  2. 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 CONFIG
    
  3. Call the corresponding Configuration API for application-level configuration management.

API Usage Steps

Demo Example

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

  1. 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();
  1. 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()));
  1. Save configuration operation (saveConfiguration)
Mono<Void> configFlux = client.saveConfiguration(new SaveConfigurationRequest());
  1. 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

Using RPC API for remote calls.

Steps

Example

  1. Build RPC Client
CapaRpcClient capaRpcClient = new CapaRpcClientBuilder().build();
  1. Invoke request
Mono<byte[]> responseMono = capaRpcClient.invokeMethod(SERVICE_APP_ID,
        "hello",
        "hello",
        HttpExtension.POST,
        null,
        TypeRef.BYTE_ARRAY);
  1. Get invocation result
byte[] response = responseMono.block();

Explanation

  1. The RPC capabilities provided by CapaRpcClient require concrete implementation, as shown in the example DemoCapaHttp.
  2. After implementing DemoCapaHttp, it needs to be configured in capa-component-rpc.properties, and loaded through the SPI mechanism.