Skip to content

Commit 535c88f

Browse files
committed
feat: configuration module
1 parent fb6dcd4 commit 535c88f

File tree

27 files changed

+953
-68
lines changed

27 files changed

+953
-68
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ For a Maven project, add the following to your pom.xml file:
7474
<dependency>
7575
<groupId>group.rxcloud</groupId>
7676
<artifactId>capa-sdk</artifactId>
77-
<version>1.0.1.RELEASE</version>
77+
<version>1.0.2.SNAPSHOT</version>
7878
</dependency>
7979
...
8080
</dependencies>
@@ -93,7 +93,7 @@ Sample implementation library:
9393
<dependency>
9494
<groupId>group.rxcloud</groupId>
9595
<artifactId>capa-sdk-spi-demo</artifactId>
96-
<version>1.0.1.RELEASE</version>
96+
<version>1.0.2.SNAPSHOT</version>
9797
</dependency>
9898
...
9999
</dependencies>

examples/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<parent>
88
<artifactId>capa-parent</artifactId>
99
<groupId>group.rxcloud</groupId>
10-
<version>1.0.1.RELEASE</version>
10+
<version>1.0.2.SNAPSHOT</version>
1111
</parent>
1212

1313
<artifactId>capa-examples</artifactId>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,49 @@
11
package group.rxcloud.capa.examples.configuration;
22

3+
import group.rxcloud.capa.component.configstore.StoreConfig;
4+
import group.rxcloud.capa.configuration.CapaConfigurationClient;
5+
import group.rxcloud.capa.configuration.CapaConfigurationClientBuilder;
6+
import group.rxcloud.cloudruntimes.domain.core.configuration.ConfigurationItem;
7+
import group.rxcloud.cloudruntimes.domain.core.configuration.ConfigurationRequestItem;
8+
import group.rxcloud.cloudruntimes.domain.core.configuration.SubConfigurationResp;
9+
import group.rxcloud.cloudruntimes.utils.TypeRef;
10+
import reactor.core.publisher.Flux;
11+
import reactor.core.publisher.Mono;
12+
13+
import java.util.Collections;
14+
import java.util.List;
15+
316
public class DemoConfigurationClient {
17+
18+
public static void main(String[] args) throws InterruptedException {
19+
StoreConfig storeConfig = new StoreConfig();
20+
storeConfig.setStoreName("config");
21+
22+
CapaConfigurationClient capaConfigurationClient = new CapaConfigurationClientBuilder(storeConfig).build();
23+
24+
ConfigurationRequestItem configurationRequestItem = new ConfigurationRequestItem();
25+
configurationRequestItem.setAppId("test");
26+
configurationRequestItem.setStoreName("config");
27+
configurationRequestItem.setKeys(Collections.singletonList("test.json"));
28+
29+
// get
30+
Mono<List<ConfigurationItem<String>>> configuration =
31+
capaConfigurationClient.getConfiguration(configurationRequestItem, TypeRef.STRING);
32+
33+
List<ConfigurationItem<String>> block = configuration.block();
34+
35+
for (ConfigurationItem<String> item : block) {
36+
System.out.println(item);
37+
}
38+
39+
// subscribe
40+
Flux<SubConfigurationResp<String>> subConfigurationRespFlux =
41+
capaConfigurationClient.subscribeConfiguration(configurationRequestItem, TypeRef.STRING);
42+
43+
subConfigurationRespFlux.subscribe(resp -> {
44+
System.out.println(resp);
45+
});
46+
47+
Thread.sleep(1000 * 10);
48+
}
449
}

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<groupId>group.rxcloud</groupId>
88
<artifactId>capa-parent</artifactId>
99
<packaging>pom</packaging>
10-
<version>1.0.1.RELEASE</version>
10+
<version>1.0.2.SNAPSHOT</version>
1111
<name>capa-sdk-parent</name>
1212
<description>SDK for Capa.</description>
1313
<url>https://github.com/reactivegroup</url>

sdk-component/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<parent>
88
<groupId>group.rxcloud</groupId>
99
<artifactId>capa-parent</artifactId>
10-
<version>1.0.1.RELEASE</version>
10+
<version>1.0.2.SNAPSHOT</version>
1111
</parent>
1212

1313
<artifactId>capa-sdk-component</artifactId>

sdk-component/src/main/java/group/rxcloud/capa/component/configstore/CapaConfigStore.java

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@
22

33

44
import group.rxcloud.capa.infrastructure.serializer.CapaObjectSerializer;
5+
import group.rxcloud.cloudruntimes.utils.TypeRef;
6+
import reactor.core.publisher.Flux;
7+
import reactor.core.publisher.Mono;
58

9+
import java.util.List;
10+
11+
/**
12+
* The Abstract ConfigStore Client. Extend this and provide your specific impl.
13+
*/
614
public abstract class CapaConfigStore implements AutoCloseable {
715

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

1826
/**
19-
* Instantiates a new Capa configuration.
27+
* Init init the configuration store.
28+
*/
29+
private String storeName;
30+
31+
/**
32+
* Instantiates a new Capa ConfigStore.
2033
*
2134
* @param objectSerializer Serializer for transient request/response objects.
2235
*/
2336
public CapaConfigStore(CapaObjectSerializer objectSerializer) {
2437
this.objectSerializer = objectSerializer;
2538
}
39+
40+
/**
41+
* Init init the configuration store.
42+
*/
43+
public void init(StoreConfig storeConfig) {
44+
this.storeName = storeConfig.getStoreName();
45+
this.doInit(storeConfig);
46+
}
47+
48+
/**
49+
* Init init the configuration store.
50+
*/
51+
protected abstract void doInit(StoreConfig storeConfig);
52+
53+
/**
54+
* Gets store name.
55+
*/
56+
public String getStoreName() {
57+
return this.storeName;
58+
}
59+
60+
/**
61+
* GetSpecificKeysValue get specific key value.
62+
*/
63+
public abstract <T> Mono<List<ConfigurationItem<T>>> get(GetRequest getRequest, TypeRef<T> type);
64+
65+
/**
66+
* Subscribe subscribe the configurations updates.
67+
*/
68+
public abstract <T> Flux<SubscribeResp<T>> subscribe(SubscribeReq subscribeReq, TypeRef<T> type);
69+
70+
/**
71+
* StopSubscribe stop subs
72+
*/
73+
public abstract String stopSubscribe();
74+
75+
/**
76+
* GetDefaultGroup returns default group.This method will be invoked if a request doesn't specify the group field
77+
*/
78+
public abstract String getDefaultGroup();
79+
80+
/**
81+
* GetDefaultLabel returns default label
82+
*/
83+
public abstract String getDefaultLabel();
2684
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package group.rxcloud.capa.component.configstore;
2+
3+
4+
import group.rxcloud.capa.infrastructure.config.CapaProperties;
5+
import group.rxcloud.capa.infrastructure.serializer.CapaObjectSerializer;
6+
import group.rxcloud.capa.infrastructure.serializer.DefaultObjectSerializer;
7+
8+
import java.lang.reflect.Constructor;
9+
import java.lang.reflect.InvocationTargetException;
10+
import java.util.Properties;
11+
12+
/**
13+
* A builder for the {@link CapaConfigStore} implementor.
14+
*/
15+
public class CapaConfigStoreBuilder {
16+
17+
/**
18+
* Serializer used for request and response objects in CapaClient.
19+
*/
20+
private CapaObjectSerializer objectSerializer;
21+
22+
private final StoreConfig storeConfig;
23+
24+
/**
25+
* Creates a constructor for CapaConfigStore.
26+
* <p>
27+
* {@link DefaultObjectSerializer} is used for object and state serializers by default but is not recommended
28+
* for production scenarios.
29+
*/
30+
public CapaConfigStoreBuilder(StoreConfig storeConfig) {
31+
this.objectSerializer = new DefaultObjectSerializer();
32+
this.storeConfig = storeConfig;
33+
}
34+
35+
/**
36+
* Sets the serializer for objects to be sent and received from Capa.
37+
* See {@link DefaultObjectSerializer} as possible serializer for non-production scenarios.
38+
*
39+
* @param objectSerializer Serializer for objects to be sent and received from Capa.
40+
* @return This instance.
41+
*/
42+
public CapaConfigStoreBuilder withObjectSerializer(CapaObjectSerializer objectSerializer) {
43+
if (objectSerializer == null) {
44+
throw new IllegalArgumentException("Object serializer is required");
45+
}
46+
if (objectSerializer.getContentType() == null
47+
|| objectSerializer.getContentType().isEmpty()) {
48+
throw new IllegalArgumentException("Content Type should not be null or empty");
49+
}
50+
this.objectSerializer = objectSerializer;
51+
return this;
52+
}
53+
54+
/**
55+
* Build an instance of the client based on the provided setup.
56+
*
57+
* @return an instance of {@link CapaConfigStore}
58+
* @throws IllegalStateException if any required field is missing
59+
*/
60+
public CapaConfigStore build() {
61+
CapaConfigStore capaConfigStore = buildCapaConfigStore();
62+
capaConfigStore.init(this.storeConfig);
63+
return capaConfigStore;
64+
}
65+
66+
/**
67+
* Creates an instance of the {@link CapaConfigStore} implementor.
68+
*
69+
* @return Instance of {@link CapaConfigStore} implementor
70+
*/
71+
private CapaConfigStore buildCapaConfigStore() {
72+
// load spi capa config store impl
73+
try {
74+
Properties properties = CapaProperties.COMPONENT_PROPERTIES_SUPPLIER.get();
75+
String capaConfigStoreClassPath = properties.getProperty(CapaConfigStore.class.getName());
76+
Class<? extends CapaConfigStore> aClass = (Class<? extends CapaConfigStore>) Class.forName(capaConfigStoreClassPath);
77+
Constructor<? extends CapaConfigStore> constructor = aClass.getConstructor(CapaObjectSerializer.class);
78+
Object newInstance = constructor.newInstance(this.objectSerializer);
79+
return (CapaConfigStore) newInstance;
80+
} catch (ClassNotFoundException | NoSuchMethodException | InvocationTargetException | InstantiationException | IllegalAccessException e) {
81+
throw new IllegalArgumentException("No CapaConfigStore Client supported.");
82+
}
83+
}
84+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package group.rxcloud.capa.component.configstore;
2+
3+
import java.util.Map;
4+
5+
public class ConfigurationItem<T> {
6+
7+
/**
8+
* Required. The key of configuration item
9+
*/
10+
private String key;
11+
/**
12+
* The content of configuration item
13+
* Empty if the configuration is not set, including the case that the configuration is changed from value-set to value-not-set.
14+
*/
15+
private T content;
16+
/**
17+
* The group of configuration item.
18+
*/
19+
private String group;
20+
/**
21+
* The label of configuration item.
22+
*/
23+
private String label;
24+
/**
25+
* The tag list of configuration item.
26+
*/
27+
private Map<String, String> tags;
28+
/**
29+
* The metadata which will be passed to configuration store component.
30+
*/
31+
private Map<String, String> metadata;
32+
33+
public String getKey() {
34+
return key;
35+
}
36+
37+
public void setKey(String key) {
38+
this.key = key;
39+
}
40+
41+
public T getContent() {
42+
return content;
43+
}
44+
45+
public void setContent(T content) {
46+
this.content = content;
47+
}
48+
49+
public String getGroup() {
50+
return group;
51+
}
52+
53+
public void setGroup(String group) {
54+
this.group = group;
55+
}
56+
57+
public String getLabel() {
58+
return label;
59+
}
60+
61+
public void setLabel(String label) {
62+
this.label = label;
63+
}
64+
65+
public Map<String, String> getTags() {
66+
return tags;
67+
}
68+
69+
public void setTags(Map<String, String> tags) {
70+
this.tags = tags;
71+
}
72+
73+
public Map<String, String> getMetadata() {
74+
return metadata;
75+
}
76+
77+
public void setMetadata(Map<String, String> metadata) {
78+
this.metadata = metadata;
79+
}
80+
}

0 commit comments

Comments
 (0)