redis java客户端连接

young 445 2021-10-18

https://redis.io/clients#java

星标为推荐客户端

Jedis

https://github.com/xetorthio/jedis

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>3.3.0</version>
</dependency>
Jedis jedis = new Jedis("10.0.16.6",6379);
jedis.set("yang","YangHuiSheng");
System.out.println(jedis.get("yang"));
jedis.close();

多线程中直接使用会有线程安全问题,需引入连接池

JedisPoolConfig poolConfig = new JedisPoolConfig();
        // Redis服务器
//        JedisShardInfo shardInfo1 = new JedisShardInfo("10.0.16.6", 6379);
        JedisShardInfo shardInfo2 = new JedisShardInfo("10.0.16.6", 6379);

        // 连接池
        List<JedisShardInfo> infoList = Arrays.asList(shardInfo2);
        ShardedJedisPool jedisPool = new ShardedJedisPool(poolConfig, infoList);

        ShardedJedis jedis = null;
        try{
            jedis = jedisPool.getResource();
            for(int i=0; i<100; i++){
                jedis.set("k"+i, ""+i);
            }
            for(int i=0; i<100; i++){
                Client client = jedis.getShard("k"+i).getClient();
                System.out.println("取到值:"+jedis.get("k"+i)+","+"当前key位于:" + client.getHost() + ":" + client.getPort());
            }
        }finally{
            if(jedis!=null) {
                jedis.close();
            }
        }

lettuce

https://lettuce.io/

可以解决线程安全问题

使用netty实现,支持同步,异步,响应式的模式

SpringBoot2.X版本中默认使用lettuce,2.X版本之前默认使用Jedis

同步执行

// 创建客户端
RedisClient client = RedisClient.create("redis://10.0.16.6:6379");
// 线程安全的长连接,连接丢失会自动重连
StatefulRedisConnection<String, String> connect = client.connect();
// 获取同步执行命令,默认超时时间60秒
RedisCommands<String, String> sync = connect.sync();
sync.set("lettuce:sync","lettuce:sync-111");
String value = sync.get("lettuce:sync");
System.out.println(value);
// 关闭连接
connect.close();
// 关闭客户端
client.shutdown();

异步执行

// 创建客户端
RedisClient client = RedisClient.create("redis://10.0.16.6:6379");
// 线程安全的长连接,连接丢失会自动重连
StatefulRedisConnection<String, String> connect = client.connect();
// 获取异步执行api
RedisAsyncCommands<String, String> async = connect.async();
async.set("lettuce:async","lettuce:async-111");
// 获取RedisFuture
RedisFuture<String> future = async.get("lettuce:async");
try {
    String value = future.get();
    System.out.println(value);
} catch (InterruptedException | ExecutionException e) {
    e.printStackTrace();
}

Redisson

http://redisson.org/

https://github.com/redisson/redisson

https://github.com/redisson/redisson/wiki/%E7%9B%AE%E5%BD%95

基于redis提供分布式和可扩展的java的数据结构

分布式锁

private static RedissonClient redissonClient;

static {
    Config config = new Config();
    config.useSingleServer().setAddress("redis://10.0.16.6:6379");
    redissonClient = Redisson.create(config);
}
public static void main(String[] args) throws InterruptedException {
    RLock lock = redissonClient.getLock("xxxxxxxxxxxxxxxx");
    // 最多等待100秒,10秒后自动解锁
    if (lock.tryLock(100,10, TimeUnit.SECONDS)) {
        System.out.println("获取锁成功");
    }
    if (lock.isLocked()) {
        lock.unlock();;
    }
    redissonClient.shutdown();
}