Skip to content

Commit

Permalink
feat: configuration module
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinten10 committed Sep 27, 2021
1 parent fb6dcd4 commit 535c88f
Show file tree
Hide file tree
Showing 27 changed files with 953 additions and 68 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ For a Maven project, add the following to your pom.xml file:
<dependency>
<groupId>group.rxcloud</groupId>
<artifactId>capa-sdk</artifactId>
<version>1.0.1.RELEASE</version>
<version>1.0.2.SNAPSHOT</version>
</dependency>
...
</dependencies>
Expand All @@ -93,7 +93,7 @@ Sample implementation library:
<dependency>
<groupId>group.rxcloud</groupId>
<artifactId>capa-sdk-spi-demo</artifactId>
<version>1.0.1.RELEASE</version>
<version>1.0.2.SNAPSHOT</version>
</dependency>
...
</dependencies>
Expand Down
2 changes: 1 addition & 1 deletion examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<artifactId>capa-parent</artifactId>
<groupId>group.rxcloud</groupId>
<version>1.0.1.RELEASE</version>
<version>1.0.2.SNAPSHOT</version>
</parent>

<artifactId>capa-examples</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,49 @@
package group.rxcloud.capa.examples.configuration;

import group.rxcloud.capa.component.configstore.StoreConfig;
import group.rxcloud.capa.configuration.CapaConfigurationClient;
import group.rxcloud.capa.configuration.CapaConfigurationClientBuilder;
import group.rxcloud.cloudruntimes.domain.core.configuration.ConfigurationItem;
import group.rxcloud.cloudruntimes.domain.core.configuration.ConfigurationRequestItem;
import group.rxcloud.cloudruntimes.domain.core.configuration.SubConfigurationResp;
import group.rxcloud.cloudruntimes.utils.TypeRef;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.Collections;
import java.util.List;

public class DemoConfigurationClient {

public static void main(String[] args) throws InterruptedException {
StoreConfig storeConfig = new StoreConfig();
storeConfig.setStoreName("config");

CapaConfigurationClient capaConfigurationClient = new CapaConfigurationClientBuilder(storeConfig).build();

ConfigurationRequestItem configurationRequestItem = new ConfigurationRequestItem();
configurationRequestItem.setAppId("test");
configurationRequestItem.setStoreName("config");
configurationRequestItem.setKeys(Collections.singletonList("test.json"));

// get
Mono<List<ConfigurationItem<String>>> configuration =
capaConfigurationClient.getConfiguration(configurationRequestItem, TypeRef.STRING);

List<ConfigurationItem<String>> block = configuration.block();

for (ConfigurationItem<String> item : block) {
System.out.println(item);
}

// subscribe
Flux<SubConfigurationResp<String>> subConfigurationRespFlux =
capaConfigurationClient.subscribeConfiguration(configurationRequestItem, TypeRef.STRING);

subConfigurationRespFlux.subscribe(resp -> {
System.out.println(resp);
});

Thread.sleep(1000 * 10);
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>group.rxcloud</groupId>
<artifactId>capa-parent</artifactId>
<packaging>pom</packaging>
<version>1.0.1.RELEASE</version>
<version>1.0.2.SNAPSHOT</version>
<name>capa-sdk-parent</name>
<description>SDK for Capa.</description>
<url>https://github.com/reactivegroup</url>
Expand Down
2 changes: 1 addition & 1 deletion sdk-component/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>group.rxcloud</groupId>
<artifactId>capa-parent</artifactId>
<version>1.0.1.RELEASE</version>
<version>1.0.2.SNAPSHOT</version>
</parent>

<artifactId>capa-sdk-component</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@


import group.rxcloud.capa.infrastructure.serializer.CapaObjectSerializer;
import group.rxcloud.cloudruntimes.utils.TypeRef;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.List;

/**
* The Abstract ConfigStore Client. Extend this and provide your specific impl.
*/
public abstract class CapaConfigStore implements AutoCloseable {

/**
Expand All @@ -16,11 +24,61 @@ public abstract class CapaConfigStore implements AutoCloseable {
protected final CapaObjectSerializer objectSerializer;

/**
* Instantiates a new Capa configuration.
* Init init the configuration store.
*/
private String storeName;

/**
* Instantiates a new Capa ConfigStore.
*
* @param objectSerializer Serializer for transient request/response objects.
*/
public CapaConfigStore(CapaObjectSerializer objectSerializer) {
this.objectSerializer = objectSerializer;
}

/**
* Init init the configuration store.
*/
public void init(StoreConfig storeConfig) {
this.storeName = storeConfig.getStoreName();
this.doInit(storeConfig);
}

/**
* Init init the configuration store.
*/
protected abstract void doInit(StoreConfig storeConfig);

/**
* Gets store name.
*/
public String getStoreName() {
return this.storeName;
}

/**
* GetSpecificKeysValue get specific key value.
*/
public abstract <T> Mono<List<ConfigurationItem<T>>> get(GetRequest getRequest, TypeRef<T> type);

/**
* Subscribe subscribe the configurations updates.
*/
public abstract <T> Flux<SubscribeResp<T>> subscribe(SubscribeReq subscribeReq, TypeRef<T> type);

/**
* StopSubscribe stop subs
*/
public abstract String stopSubscribe();

/**
* GetDefaultGroup returns default group.This method will be invoked if a request doesn't specify the group field
*/
public abstract String getDefaultGroup();

/**
* GetDefaultLabel returns default label
*/
public abstract String getDefaultLabel();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package group.rxcloud.capa.component.configstore;


import group.rxcloud.capa.infrastructure.config.CapaProperties;
import group.rxcloud.capa.infrastructure.serializer.CapaObjectSerializer;
import group.rxcloud.capa.infrastructure.serializer.DefaultObjectSerializer;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Properties;

/**
* A builder for the {@link CapaConfigStore} implementor.
*/
public class CapaConfigStoreBuilder {

/**
* Serializer used for request and response objects in CapaClient.
*/
private CapaObjectSerializer objectSerializer;

private final StoreConfig storeConfig;

/**
* Creates a constructor for CapaConfigStore.
* <p>
* {@link DefaultObjectSerializer} is used for object and state serializers by default but is not recommended
* for production scenarios.
*/
public CapaConfigStoreBuilder(StoreConfig storeConfig) {
this.objectSerializer = new DefaultObjectSerializer();
this.storeConfig = storeConfig;
}

/**
* Sets the serializer for objects to be sent and received from Capa.
* See {@link DefaultObjectSerializer} as possible serializer for non-production scenarios.
*
* @param objectSerializer Serializer for objects to be sent and received from Capa.
* @return This instance.
*/
public CapaConfigStoreBuilder withObjectSerializer(CapaObjectSerializer objectSerializer) {
if (objectSerializer == null) {
throw new IllegalArgumentException("Object serializer is required");
}
if (objectSerializer.getContentType() == null
|| objectSerializer.getContentType().isEmpty()) {
throw new IllegalArgumentException("Content Type should not be null or empty");
}
this.objectSerializer = objectSerializer;
return this;
}

/**
* Build an instance of the client based on the provided setup.
*
* @return an instance of {@link CapaConfigStore}
* @throws IllegalStateException if any required field is missing
*/
public CapaConfigStore build() {
CapaConfigStore capaConfigStore = buildCapaConfigStore();
capaConfigStore.init(this.storeConfig);
return capaConfigStore;
}

/**
* Creates an instance of the {@link CapaConfigStore} implementor.
*
* @return Instance of {@link CapaConfigStore} implementor
*/
private CapaConfigStore buildCapaConfigStore() {
// load spi capa config store impl
try {
Properties properties = CapaProperties.COMPONENT_PROPERTIES_SUPPLIER.get();
String capaConfigStoreClassPath = properties.getProperty(CapaConfigStore.class.getName());
Class<? extends CapaConfigStore> aClass = (Class<? extends CapaConfigStore>) Class.forName(capaConfigStoreClassPath);
Constructor<? extends CapaConfigStore> constructor = aClass.getConstructor(CapaObjectSerializer.class);
Object newInstance = constructor.newInstance(this.objectSerializer);
return (CapaConfigStore) newInstance;
} catch (ClassNotFoundException | NoSuchMethodException | InvocationTargetException | InstantiationException | IllegalAccessException e) {
throw new IllegalArgumentException("No CapaConfigStore Client supported.");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package group.rxcloud.capa.component.configstore;

import java.util.Map;

public class ConfigurationItem<T> {

/**
* Required. The key of configuration item
*/
private String key;
/**
* The content of configuration item
* Empty if the configuration is not set, including the case that the configuration is changed from value-set to value-not-set.
*/
private T content;
/**
* The group of configuration item.
*/
private String group;
/**
* The label of configuration item.
*/
private String label;
/**
* The tag list of configuration item.
*/
private Map<String, String> tags;
/**
* The metadata which will be passed to configuration store component.
*/
private Map<String, String> metadata;

public String getKey() {
return key;
}

public void setKey(String key) {
this.key = key;
}

public T getContent() {
return content;
}

public void setContent(T content) {
this.content = content;
}

public String getGroup() {
return group;
}

public void setGroup(String group) {
this.group = group;
}

public String getLabel() {
return label;
}

public void setLabel(String label) {
this.label = label;
}

public Map<String, String> getTags() {
return tags;
}

public void setTags(Map<String, String> tags) {
this.tags = tags;
}

public Map<String, String> getMetadata() {
return metadata;
}

public void setMetadata(Map<String, String> metadata) {
this.metadata = metadata;
}
}
Loading

0 comments on commit 535c88f

Please sign in to comment.