Skip to content

Commit df7d2d4

Browse files
committed
Add support springboot starter
1 parent 09718d7 commit df7d2d4

12 files changed

Lines changed: 535 additions & 1 deletion

File tree

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ redisJSON.set(key, SetArgs.Builder.create(".", GsonUtils.toJson(m)));
154154
Map<String, Object> actual = redisJSON.get(key, Map.class, new GetArgs().path(".").indent("\t").newLine("\n").space(" "));
155155
redisJSONClient.shutdown();
156156
```
157+
158+
SpringBoot Starter
159+
160+
see [spring-boot-starter](./spring-boot-starter)
161+
157162
## License
158163

159164
[Apache License 2.0](/LICENSE)

pom.xml

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<module>redisjson</module>
1919
<module>redisgraph</module>
2020
<module>commons</module>
21+
<module>spring-boot-starter</module>
2122
</modules>
2223

2324
<name>Redis-Modules-Java</name>
@@ -53,7 +54,7 @@
5354
<properties>
5455
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
5556
<redisson.version>3.27.1</redisson.version>
56-
<junit.version>5.6.2</junit.version>
57+
<junit.version>5.6.3</junit.version>
5758
<java.version>1.8</java.version>
5859
<maven-source-plugin.version>3.0.1</maven-source-plugin.version>
5960
<maven-shade-plugin.version>3.2.1</maven-shade-plugin.version>
@@ -67,6 +68,7 @@
6768
<maven-checkstyle-plugin.version>3.1.0</maven-checkstyle-plugin.version>
6869
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
6970
<maven-jar-plugin.version>2.4</maven-jar-plugin.version>
71+
<spring-boot.version>2.7.6</spring-boot.version>
7072
</properties>
7173

7274
<dependencyManagement>
@@ -118,6 +120,27 @@
118120
<artifactId>redisgraph</artifactId>
119121
<version>${project.version}</version>
120122
</dependency>
123+
124+
<dependency>
125+
<groupId>io.github.dengliming.redismodule</groupId>
126+
<artifactId>spring-boot-starter</artifactId>
127+
<version>${project.version}</version>
128+
</dependency>
129+
130+
<dependency>
131+
<groupId>io.github.dengliming.redismodule</groupId>
132+
<artifactId>all</artifactId>
133+
<version>${project.version}</version>
134+
</dependency>
135+
136+
<dependency>
137+
<groupId>org.springframework.boot</groupId>
138+
<artifactId>spring-boot-dependencies</artifactId>
139+
<version>${spring-boot.version}</version>
140+
<type>pom</type>
141+
<scope>import</scope>
142+
</dependency>
143+
121144
</dependencies>
122145
</dependencyManagement>
123146

spring-boot-starter/README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# SpringBoot Starter
2+
3+
4+
## Usage
5+
6+
Add dependency
7+
```xml
8+
<dependencies>
9+
<dependency>
10+
<groupId>io.github.dengliming.redismodule</groupId>
11+
<artifactId>spring-boot-starter</artifactId>
12+
<version>2.0.4-SNAPSHOT</version>
13+
</dependency>
14+
</dependencies>
15+
```
16+
17+
RedisJSON
18+
```yaml
19+
redis-module:
20+
enabled: true
21+
redisjson:
22+
enabled: true
23+
config: |
24+
singleServerConfig:
25+
idleConnectionTimeout: 10000
26+
connectTimeout: 10000
27+
timeout: 3000
28+
retryAttempts: 3
29+
retryInterval: 1500
30+
password: null
31+
subscriptionsPerConnection: 5
32+
clientName: null
33+
address: "redis://127.0.0.1:6379"
34+
subscriptionConnectionMinimumIdleSize: 1
35+
subscriptionConnectionPoolSize: 50
36+
connectionMinimumIdleSize: 24
37+
connectionPoolSize: 64
38+
database: 0
39+
dnsMonitoringInterval: 5000
40+
threads: 16
41+
nettyThreads: 32
42+
codec: !<org.redisson.codec.Kryo5Codec> {}
43+
transportMode: "NIO"
44+
```
45+
46+
Use in Spring
47+
```java
48+
@Autowired(required = false)
49+
private RedisJSONClient redisJSONClient;
50+
51+
public void test() {
52+
RedisJSON redisJSON = redisJSONClient.getRedisJSON();
53+
String key = "foo";
54+
redisJSON.set(key, SetArgs.Builder.create(".", "\"bar\""));
55+
}
56+
```

spring-boot-starter/pom.xml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>io.github.dengliming.redismodule</groupId>
6+
<artifactId>redis-modules-java</artifactId>
7+
<version>2.0.4-SNAPSHOT</version>
8+
</parent>
9+
10+
<artifactId>spring-boot-starter</artifactId>
11+
<name>SpringBootStarter</name>
12+
13+
<dependencyManagement>
14+
<dependencies>
15+
<dependency>
16+
<groupId>org.springframework.boot</groupId>
17+
<artifactId>spring-boot-dependencies</artifactId>
18+
<version>${spring-boot.version}</version>
19+
<type>pom</type>
20+
<scope>import</scope>
21+
</dependency>
22+
</dependencies>
23+
</dependencyManagement>
24+
25+
<dependencies>
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-autoconfigure</artifactId>
29+
<scope>provided</scope>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-configuration-processor</artifactId>
35+
<optional>true</optional>
36+
</dependency>
37+
38+
<dependency>
39+
<groupId>org.springframework.boot</groupId>
40+
<artifactId>spring-boot-starter-test</artifactId>
41+
<exclusions>
42+
<exclusion>
43+
<groupId>org.junit.vintage</groupId>
44+
<artifactId>junit-vintage-engine</artifactId>
45+
</exclusion>
46+
</exclusions>
47+
<scope>test</scope>
48+
</dependency>
49+
50+
<dependency>
51+
<groupId>io.github.dengliming.redismodule</groupId>
52+
<artifactId>commons</artifactId>
53+
</dependency>
54+
55+
<dependency>
56+
<groupId>io.github.dengliming.redismodule</groupId>
57+
<artifactId>all</artifactId>
58+
</dependency>
59+
</dependencies>
60+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/*
2+
* Copyright 2024 dengliming.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.github.dengliming.redismodule.springboot.starter.autoconfigure;
18+
19+
import io.github.dengliming.redismodule.redisai.client.RedisAIClient;
20+
import io.github.dengliming.redismodule.redisbloom.client.RedisBloomClient;
21+
import io.github.dengliming.redismodule.redisearch.client.RediSearchClient;
22+
import io.github.dengliming.redismodule.redisgears.client.RedisGearsClient;
23+
import io.github.dengliming.redismodule.redisgraph.client.RedisGraphClient;
24+
import io.github.dengliming.redismodule.redisjson.client.RedisJSONClient;
25+
import io.github.dengliming.redismodule.redistimeseries.client.RedisTimeSeriesClient;
26+
import io.github.dengliming.redismodule.springboot.starter.env.RedisModuleProperties;
27+
import org.redisson.Redisson;
28+
import org.redisson.config.Config;
29+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
30+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
31+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
32+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
33+
import org.springframework.context.annotation.Bean;
34+
import org.springframework.context.annotation.Configuration;
35+
36+
import java.io.IOException;
37+
38+
@ConditionalOnProperty(
39+
prefix = RedisModuleProperties.PREFIX,
40+
name = "enabled",
41+
havingValue = "true",
42+
matchIfMissing = true
43+
)
44+
@ConditionalOnClass(Redisson.class)
45+
@EnableConfigurationProperties(RedisModuleProperties.class)
46+
@Configuration
47+
public class RedisModuleAutoConfiguration {
48+
private final RedisModuleProperties redisModuleProperties;
49+
50+
public RedisModuleAutoConfiguration(RedisModuleProperties redisModuleProperties) {
51+
this.redisModuleProperties = redisModuleProperties;
52+
}
53+
54+
@ConditionalOnProperty(
55+
prefix = RedisModuleProperties.PREFIX + ".redisearch",
56+
name = "enabled",
57+
havingValue = "true"
58+
)
59+
@Bean(destroyMethod = "shutdown")
60+
@ConditionalOnMissingBean
61+
public RediSearchClient rediSearchClient() {
62+
return new RediSearchClient(parseRedisModuleConfig(redisModuleProperties.getRedisearch().getConfig()));
63+
}
64+
65+
@ConditionalOnProperty(
66+
prefix = RedisModuleProperties.PREFIX + ".redisai",
67+
name = "enabled",
68+
havingValue = "true"
69+
)
70+
@Bean(destroyMethod = "shutdown")
71+
@ConditionalOnMissingBean
72+
public RedisAIClient redisAIClient() {
73+
return new RedisAIClient(parseRedisModuleConfig(redisModuleProperties.getRedisai().getConfig()));
74+
}
75+
76+
@ConditionalOnProperty(
77+
prefix = RedisModuleProperties.PREFIX + ".redisbloom",
78+
name = "enabled",
79+
havingValue = "true"
80+
)
81+
@Bean(destroyMethod = "shutdown")
82+
@ConditionalOnMissingBean
83+
public RedisBloomClient redisBloomClient() {
84+
return new RedisBloomClient(parseRedisModuleConfig(redisModuleProperties.getRedisbloom().getConfig()));
85+
}
86+
87+
@ConditionalOnProperty(
88+
prefix = RedisModuleProperties.PREFIX + ".redisgears",
89+
name = "enabled",
90+
havingValue = "true"
91+
)
92+
@Bean(destroyMethod = "shutdown")
93+
@ConditionalOnMissingBean
94+
public RedisGearsClient redisGearsClient() {
95+
return new RedisGearsClient(parseRedisModuleConfig(redisModuleProperties.getRedisgears().getConfig()));
96+
}
97+
98+
@ConditionalOnProperty(
99+
prefix = RedisModuleProperties.PREFIX + ".redisgraph",
100+
name = "enabled",
101+
havingValue = "true"
102+
)
103+
@Bean(destroyMethod = "shutdown")
104+
@ConditionalOnMissingBean
105+
public RedisGraphClient redisGraphClient() {
106+
return new RedisGraphClient(parseRedisModuleConfig(redisModuleProperties.getRedisgraph().getConfig()));
107+
}
108+
109+
@ConditionalOnProperty(
110+
prefix = RedisModuleProperties.PREFIX + ".redisjson",
111+
name = "enabled",
112+
havingValue = "true"
113+
)
114+
@Bean(destroyMethod = "shutdown")
115+
@ConditionalOnMissingBean
116+
public RedisJSONClient redisJSONClient() {
117+
return new RedisJSONClient(parseRedisModuleConfig(redisModuleProperties.getRedisjson().getConfig()));
118+
}
119+
120+
@ConditionalOnProperty(
121+
prefix = RedisModuleProperties.PREFIX + ".redistimeseries",
122+
name = "enabled",
123+
havingValue = "true"
124+
)
125+
@Bean(destroyMethod = "shutdown")
126+
@ConditionalOnMissingBean
127+
public RedisTimeSeriesClient redisTimeSeriesClient() {
128+
return new RedisTimeSeriesClient(parseRedisModuleConfig(redisModuleProperties.getRedistimeseries().getConfig()));
129+
}
130+
131+
private Config parseRedisModuleConfig(String configStr) {
132+
Config config;
133+
try {
134+
config = Config.fromYAML(configStr);
135+
} catch (IOException e) {
136+
try {
137+
config = Config.fromJSON(configStr);
138+
} catch (IOException ex) {
139+
ex.addSuppressed(e);
140+
throw new IllegalArgumentException("Can't parse config", ex);
141+
}
142+
}
143+
return config;
144+
}
145+
}

0 commit comments

Comments
 (0)