Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lettuce): 新增solon-lettuce插件 #183

Merged
merged 2 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions solon-parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<luffy.version>1.6.4</luffy.version>
<redisx.version>1.4.8</redisx.version>
<redisson.version>3.21.0</redisson.version>
<lettuce.version>6.2.5.RELEASE</lettuce.version>

<fastjson.version>1.2.83</fastjson.version>
<fastjson2.version>2.0.34</fastjson2.version>
Expand Down Expand Up @@ -1066,6 +1067,12 @@
<version>${solon.version}</version>
</dependency>

<dependency>
<groupId>org.noear</groupId>
<artifactId>lettuce-solon-plugin</artifactId>
<version>${solon.version}</version>
</dependency>

<!-- for solon cloud -->
<dependency>
<groupId>org.noear</groupId>
Expand Down
48 changes: 48 additions & 0 deletions solon-projects/solon-plugin-data/lettuce-solon-plugin/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.noear</groupId>
<artifactId>solon-parent</artifactId>
<version>2.3.8-SNAPSHOT</version>
<relativePath>../../../solon-parent/pom.xml</relativePath>
</parent>

<artifactId>lettuce-solon-plugin</artifactId>
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>org.noear</groupId>
<artifactId>solon.data</artifactId>
</dependency>

<dependency>
<groupId>org.noear</groupId>
<artifactId>solon.config.yaml</artifactId>
</dependency>


<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
<version>${lettuce.version}</version>
</dependency>

<dependency>
<groupId>org.noear</groupId>
<artifactId>solon-lib</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>test</scope>
</dependency>

</dependencies>
</project>
124 changes: 124 additions & 0 deletions solon-projects/solon-plugin-data/lettuce-solon-plugin/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# lettuce-solon-plugin

> lettuce本身易于适配,方便使用,本插件只是做了一层薄薄的适配,方便与在某些情况下更加便利的使用。


## 依赖引入

```xml
<dependency>
<groupId>org.noear</groupId>
<artifactId>lettuce-solon-plugin</artifactId>
<version>${solon.version}</version>
</dependency>
```

## 配置文件

```yaml
### 任意选一种
### 模式一
lettuce.rd1:
# Redis模式 (standalone, cluster, sentinel)
redis-mode: standalone
redis-uri: redis://localhost:6379/0

#### 模式二
lettuce.rd2:
# Redis模式 (standalone, cluster, sentinel)
redis-mode: standalone
config:
host: localhost
port: 6379
# socket: xxxx
# client-name: myClientName
# database: 0
# sentinel-masterId: 'mymaster'
# username: 'myusername'
# password: 'mypassword'
# ssl: false
# verify-mode: FULL
# startTls: false
# timeout: 10000
# sentinels:
# - host: localhost
# port: 16379
# password: 'mypassword'
# - host: localhost
# port: 26379
# password: 'mypassword'
```
## java

### Config

```java
package io.lettuce.solon;

import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisURI;
import io.lettuce.core.cluster.ClusterClientOptions;
import io.lettuce.core.cluster.RedisClusterClient;
import org.noear.solon.annotation.Bean;
import org.noear.solon.annotation.Configuration;
import org.noear.solon.annotation.Inject;

@Configuration
public class Config {

@Bean(typed = true, name = "redisClient1")
public RedisClient redisClient1(@Inject("${lettuce.rd1}") LettuceSupplier lettuceSupplier) {
// 默认参数生成
return (RedisClient) lettuceSupplier.get();
}

@Bean(name = "redisClient2")
public RedisClient redisClient2(@Inject("${lettuce.rd2}") LettuceSupplier lettuceSupplier) {
// 获取 配置文件解析的RedisURI
RedisURI redisURI = lettuceSupplier.getRedisURI();
// 手动创建
RedisClient redisClient = RedisClient.create(redisURI);
// 手动设置参数
redisClient.setOptions(ClusterClientOptions.builder().validateClusterNodeMembership(false).build());
return redisClient;
}

@Bean(name = "redisClusterClient1")
public RedisClusterClient redisClusterClient1(@Inject("${lettuce.rd3}") LettuceSupplier lettuceSupplier) {
// 默认参数生成
return (RedisClusterClient) lettuceSupplier.get();
}

@Bean(name = "redisClusterClient2")
public RedisClusterClient redisClusterClient2(@Inject("${lettuce.rd3}") LettuceSupplier lettuceSupplier) {
// 获取 配置文件解析的RedisURI
RedisURI redisURI = lettuceSupplier.getRedisURI();
// 手动创建
RedisClusterClient redisClusterClient = RedisClusterClient.create(redisURI);
// 手动设置参数
redisClusterClient.setOptions(ClusterClientOptions.builder().validateClusterNodeMembership(false).build());
return redisClusterClient;
}

}
```

### 使用

```java
@Component
public class DemoService {

@Inject
RedisClient redisClient;

/**
* 仅仅做测试使用,以实际情况为准
*/
public void demoSet() {
redisClient.connect().sync().set("test", "test");
System.out.println(redisClient.connect().sync().get("test"));
redisClient.connect().sync().del("test");
}
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package io.lettuce.solon;

import io.lettuce.core.RedisCredentialsProvider;
import io.lettuce.core.SslVerifyMode;

import java.util.List;

import static io.lettuce.core.RedisURI.DEFAULT_REDIS_PORT;


/**
* Lettuce配置
*
* @author Sorghum
* @since 2.3.8
*/
public class LettuceConfig{

private String host;

private String socket;

private String sentinelMasterId;

private int port = DEFAULT_REDIS_PORT;

private int database;

private String clientName;

private String username;

private String password;

private RedisCredentialsProvider credentialsProvider;

private boolean ssl = false;

private SslVerifyMode verifyMode = SslVerifyMode.FULL;

private boolean startTls = false;

private Long timeout = 60L;

private List<LettuceSentinel> sentinels;

public String getHost() {
return host;
}

public void setHost(String host) {
this.host = host;
}

public String getSocket() {
return socket;
}

public void setSocket(String socket) {
this.socket = socket;
}

public String getSentinelMasterId() {
return sentinelMasterId;
}

public void setSentinelMasterId(String sentinelMasterId) {
this.sentinelMasterId = sentinelMasterId;
}

public int getPort() {
return port;
}

public void setPort(int port) {
this.port = port;
}

public int getDatabase() {
return database;
}

public void setDatabase(int database) {
this.database = database;
}

public String getClientName() {
return clientName;
}

public void setClientName(String clientName) {
this.clientName = clientName;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public RedisCredentialsProvider getCredentialsProvider() {
return credentialsProvider;
}

public void setCredentialsProvider(RedisCredentialsProvider credentialsProvider) {
this.credentialsProvider = credentialsProvider;
}


public boolean isSsl() {
return ssl;
}

public void setSsl(boolean ssl) {
this.ssl = ssl;
}

public SslVerifyMode getVerifyMode() {
return verifyMode;
}

public void setVerifyMode(SslVerifyMode verifyMode) {
this.verifyMode = verifyMode;
}

public boolean isStartTls() {
return startTls;
}

public void setStartTls(boolean startTls) {
this.startTls = startTls;
}

public Long getTimeout() {
return timeout;
}

public void setTimeout(Long timeout) {
this.timeout = timeout;
}

public List<LettuceSentinel> getSentinels() {
return sentinels;
}

public void setSentinels(List<LettuceSentinel> sentinels) {
this.sentinels = sentinels;
}

}
Loading