本文章基于Redis 6.0.9版本,Lettuce 6.0.1.RELEASE版本
目录
[对于Maven用户]
将这些行添加到文件pom.xml中:
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
<version>6.0.1.RELEASE</version>
</dependency>
[对于Ivy用户]
将这些行添加到文件ivy.xml中:
<ivy-module>
<dependencies>
<dependency org="io.lettuce" name="lettuce-core" rev="6.0.1.RELEASE"/>
</dependencies>
</ivy-module>
[对于Gradle用户]
将这些行添加到文件build.gradle中:
dependencies {
compile 'io.lettuce:lettuce-core:6.0.1.RELEASE
}
[下载release]
https://github.com/lettuce-io/lettuce-core/releases
导入所需的类:
import io.lettuce.core.*;
现在,编写你的代码:
RedisClient redisClient = RedisClient.create("redis://password@localhost:6379/0");
StatefulRedisConnection<String, String> connection = redisClient.connect();
RedisCommands<String, String> syncCommands = connection.sync();
syncCommands.set("key", "Hello, Redis!");
connection.close();
redisClient.shutdown();
完成
package io.lettuce.examples;
import io.lettuce.core.RedisClient;
import io.lettuce.core.api.StatefulRedisConnection;
public class ConnectToRedis {
public static void main(String[] args) {
// Syntax: redis://[password@]host[:port][/databaseNumber]
// Syntax: redis://[username:password@]host[:port][/databaseNumber]
RedisClient redisClient = RedisClient.create("redis://password@localhost:6379/0");
StatefulRedisConnection<String, String> connection = redisClient.connect();
System.out.println("Connected to Redis");
connection.close();
redisClient.shutdown();
}
}
package io.lettuce.examples;
import io.lettuce.core.RedisClient;
import io.lettuce.core.api.StatefulRedisConnection;
public class ConnectToRedisSSL {
public static void main(String[] args) {
// Syntax: rediss://[password@]host[:port][/databaseNumber]
// Adopt the port to the stunnel port in front of your Redis instance
RedisClient redisClient = RedisClient.create("rediss://password@localhost:6443/0");
StatefulRedisConnection<String, String> connection = redisClient.connect();
System.out.println("Connected to Redis using SSL");
connection.close();
redisClient.shutdown();
}
}
package io.lettuce.examples;
import io.lettuce.core.RedisClient;
import io.lettuce.core.api.StatefulRedisConnection;
public class ConnectToRedisUsingRedisSentinel {
public static void main(String[] args) {
// Syntax: redis-sentinel://[password@]host[:port][,host2[:port2]][/databaseNumber]#sentinelMasterId
RedisClient redisClient = RedisClient.create("redis-sentinel://localhost:26379,localhost:26380/0#mymaster");
StatefulRedisConnection<String, String> connection = redisClient.connect();
System.out.println("Connected to Redis using Redis Sentinel");
connection.close();
redisClient.shutdown();
}
}
package io.lettuce.examples;
import io.lettuce.core.cluster.RedisClusterClient;
import io.lettuce.core.cluster.api.StatefulRedisClusterConnection;
public class ConnectToRedisCluster {
public static void main(String[] args) {
// Syntax: redis://[password@]host[:port]
// Syntax: redis://[username:password@]host[:port]
RedisClusterClient redisClient = RedisClusterClient.create("redis://password@localhost:7379");
StatefulRedisClusterConnection<String, String> connection = redisClient.connect();
System.out.println("Connected to Redis");
connection.close();
redisClient.shutdown();
}
}
package io.lettuce.examples;
import io.lettuce.core.cluster.RedisClusterClient;
import io.lettuce.core.cluster.api.StatefulRedisClusterConnection;
public class ConnectToRedisCluster {
public static void main(String[] args) {
// Syntax: redis://[password@]host[:port]
// Syntax: redis://[username:password@]host[:port]
RedisClusterClient redisClient = RedisClusterClient.create("redis://password@localhost:7379");
StatefulRedisClusterConnection<String, String> connection = redisClient.connect();
System.out.println("Connected to Redis");
connection.close();
redisClient.shutdown();
}
}
package io.lettuce.examples;
import java.util.Arrays;
import java.util.List;
import io.lettuce.core.ReadFrom;
import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisURI;
import io.lettuce.core.codec.StringCodec;
import io.lettuce.core.masterreplica.MasterReplica;
import io.lettuce.core.masterreplica.StatefulRedisMasterReplicaConnection;
public class ConnectToMasterSlaveUsingElastiCacheCluster {
public static void main(String[] args) {
// Syntax: redis://[password@]host[:port][/databaseNumber]
RedisClient redisClient = RedisClient.create();
List<RedisURI> nodes = Arrays.asList(RedisURI.create("redis://host1"), RedisURI.create("redis://host2"),
RedisURI.create("redis://host3"));
StatefulRedisMasterReplicaConnection<String, String> connection = MasterReplica.connect(redisClient, StringCodec.UTF8,
nodes);
connection.setReadFrom(ReadFrom.UPSTREAM_PREFERRED);
System.out.println("Connected to Redis");
connection.close();
redisClient.shutdown();
}
}
package io.lettuce.examples;
import io.lettuce.core.RedisURI;
import io.lettuce.core.cluster.RedisClusterClient;
import io.lettuce.core.cluster.api.StatefulRedisClusterConnection;
public class ConnectToRedisClusterSSL {
public static void main(String[] args) {
// Syntax: rediss://[password@]host[:port]
RedisURI redisURI = RedisURI.create("rediss://password@localhost:7379");
redisURI.setVerifyPeer(false); // depending on your setup, you might want to disable peer verification
RedisClusterClient redisClient = RedisClusterClient.create(redisURI);
StatefulRedisClusterConnection<String, String> connection = redisClient.connect();
System.out.println("Connected to Redis");
connection.close();
redisClient.shutdown();
}
}
@Configuration
class AppConfig {
@Bean
public LettuceConnectionFactory redisConnectionFactory() {
return new LettuceConnectionFactory(new RedisStandaloneConfiguration("server", 6379));
}
}