Skip to content

Commit

Permalink
Add support springboot starter
Browse files Browse the repository at this point in the history
  • Loading branch information
dengliming committed May 3, 2024
1 parent 09718d7 commit df7d2d4
Show file tree
Hide file tree
Showing 12 changed files with 535 additions and 1 deletion.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ redisJSON.set(key, SetArgs.Builder.create(".", GsonUtils.toJson(m)));
Map<String, Object> actual = redisJSON.get(key, Map.class, new GetArgs().path(".").indent("\t").newLine("\n").space(" "));
redisJSONClient.shutdown();
```

SpringBoot Starter

see [spring-boot-starter](./spring-boot-starter)

## License

[Apache License 2.0](/LICENSE)
25 changes: 24 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<module>redisjson</module>
<module>redisgraph</module>
<module>commons</module>
<module>spring-boot-starter</module>
</modules>

<name>Redis-Modules-Java</name>
Expand Down Expand Up @@ -53,7 +54,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<redisson.version>3.27.1</redisson.version>
<junit.version>5.6.2</junit.version>
<junit.version>5.6.3</junit.version>
<java.version>1.8</java.version>
<maven-source-plugin.version>3.0.1</maven-source-plugin.version>
<maven-shade-plugin.version>3.2.1</maven-shade-plugin.version>
Expand All @@ -67,6 +68,7 @@
<maven-checkstyle-plugin.version>3.1.0</maven-checkstyle-plugin.version>
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
<maven-jar-plugin.version>2.4</maven-jar-plugin.version>
<spring-boot.version>2.7.6</spring-boot.version>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -118,6 +120,27 @@
<artifactId>redisgraph</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>io.github.dengliming.redismodule</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>io.github.dengliming.redismodule</groupId>
<artifactId>all</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>

</dependencies>
</dependencyManagement>

Expand Down
56 changes: 56 additions & 0 deletions spring-boot-starter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# SpringBoot Starter


## Usage

Add dependency
```xml
<dependencies>
<dependency>
<groupId>io.github.dengliming.redismodule</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.0.4-SNAPSHOT</version>
</dependency>
</dependencies>
```

RedisJSON
```yaml
redis-module:
enabled: true
redisjson:
enabled: true
config: |
singleServerConfig:
idleConnectionTimeout: 10000
connectTimeout: 10000
timeout: 3000
retryAttempts: 3
retryInterval: 1500
password: null
subscriptionsPerConnection: 5
clientName: null
address: "redis://127.0.0.1:6379"
subscriptionConnectionMinimumIdleSize: 1
subscriptionConnectionPoolSize: 50
connectionMinimumIdleSize: 24
connectionPoolSize: 64
database: 0
dnsMonitoringInterval: 5000
threads: 16
nettyThreads: 32
codec: !<org.redisson.codec.Kryo5Codec> {}
transportMode: "NIO"
```
Use in Spring
```java
@Autowired(required = false)
private RedisJSONClient redisJSONClient;

public void test() {
RedisJSON redisJSON = redisJSONClient.getRedisJSON();
String key = "foo";
redisJSON.set(key, SetArgs.Builder.create(".", "\"bar\""));
}
```
60 changes: 60 additions & 0 deletions spring-boot-starter/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.github.dengliming.redismodule</groupId>
<artifactId>redis-modules-java</artifactId>
<version>2.0.4-SNAPSHOT</version>
</parent>

<artifactId>spring-boot-starter</artifactId>
<name>SpringBootStarter</name>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.github.dengliming.redismodule</groupId>
<artifactId>commons</artifactId>
</dependency>

<dependency>
<groupId>io.github.dengliming.redismodule</groupId>
<artifactId>all</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/*
* Copyright 2024 dengliming.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.github.dengliming.redismodule.springboot.starter.autoconfigure;

import io.github.dengliming.redismodule.redisai.client.RedisAIClient;
import io.github.dengliming.redismodule.redisbloom.client.RedisBloomClient;
import io.github.dengliming.redismodule.redisearch.client.RediSearchClient;
import io.github.dengliming.redismodule.redisgears.client.RedisGearsClient;
import io.github.dengliming.redismodule.redisgraph.client.RedisGraphClient;
import io.github.dengliming.redismodule.redisjson.client.RedisJSONClient;
import io.github.dengliming.redismodule.redistimeseries.client.RedisTimeSeriesClient;
import io.github.dengliming.redismodule.springboot.starter.env.RedisModuleProperties;
import org.redisson.Redisson;
import org.redisson.config.Config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

@ConditionalOnProperty(
prefix = RedisModuleProperties.PREFIX,
name = "enabled",
havingValue = "true",
matchIfMissing = true
)
@ConditionalOnClass(Redisson.class)
@EnableConfigurationProperties(RedisModuleProperties.class)
@Configuration
public class RedisModuleAutoConfiguration {
private final RedisModuleProperties redisModuleProperties;

public RedisModuleAutoConfiguration(RedisModuleProperties redisModuleProperties) {
this.redisModuleProperties = redisModuleProperties;
}

@ConditionalOnProperty(
prefix = RedisModuleProperties.PREFIX + ".redisearch",
name = "enabled",
havingValue = "true"
)
@Bean(destroyMethod = "shutdown")
@ConditionalOnMissingBean
public RediSearchClient rediSearchClient() {
return new RediSearchClient(parseRedisModuleConfig(redisModuleProperties.getRedisearch().getConfig()));
}

@ConditionalOnProperty(
prefix = RedisModuleProperties.PREFIX + ".redisai",
name = "enabled",
havingValue = "true"
)
@Bean(destroyMethod = "shutdown")
@ConditionalOnMissingBean
public RedisAIClient redisAIClient() {
return new RedisAIClient(parseRedisModuleConfig(redisModuleProperties.getRedisai().getConfig()));
}

@ConditionalOnProperty(
prefix = RedisModuleProperties.PREFIX + ".redisbloom",
name = "enabled",
havingValue = "true"
)
@Bean(destroyMethod = "shutdown")
@ConditionalOnMissingBean
public RedisBloomClient redisBloomClient() {
return new RedisBloomClient(parseRedisModuleConfig(redisModuleProperties.getRedisbloom().getConfig()));
}

@ConditionalOnProperty(
prefix = RedisModuleProperties.PREFIX + ".redisgears",
name = "enabled",
havingValue = "true"
)
@Bean(destroyMethod = "shutdown")
@ConditionalOnMissingBean
public RedisGearsClient redisGearsClient() {
return new RedisGearsClient(parseRedisModuleConfig(redisModuleProperties.getRedisgears().getConfig()));
}

@ConditionalOnProperty(
prefix = RedisModuleProperties.PREFIX + ".redisgraph",
name = "enabled",
havingValue = "true"
)
@Bean(destroyMethod = "shutdown")
@ConditionalOnMissingBean
public RedisGraphClient redisGraphClient() {
return new RedisGraphClient(parseRedisModuleConfig(redisModuleProperties.getRedisgraph().getConfig()));
}

@ConditionalOnProperty(
prefix = RedisModuleProperties.PREFIX + ".redisjson",
name = "enabled",
havingValue = "true"
)
@Bean(destroyMethod = "shutdown")
@ConditionalOnMissingBean
public RedisJSONClient redisJSONClient() {
return new RedisJSONClient(parseRedisModuleConfig(redisModuleProperties.getRedisjson().getConfig()));
}

@ConditionalOnProperty(
prefix = RedisModuleProperties.PREFIX + ".redistimeseries",
name = "enabled",
havingValue = "true"
)
@Bean(destroyMethod = "shutdown")
@ConditionalOnMissingBean
public RedisTimeSeriesClient redisTimeSeriesClient() {
return new RedisTimeSeriesClient(parseRedisModuleConfig(redisModuleProperties.getRedistimeseries().getConfig()));
}

private Config parseRedisModuleConfig(String configStr) {
Config config;
try {
config = Config.fromYAML(configStr);
} catch (IOException e) {
try {
config = Config.fromJSON(configStr);
} catch (IOException ex) {
ex.addSuppressed(e);
throw new IllegalArgumentException("Can't parse config", ex);
}
}
return config;
}
}
Loading

0 comments on commit df7d2d4

Please sign in to comment.