Java连接Redis

Java连接Redis

  • 1. Jedis
  • 2. lettuce
  • 3. More About Lettuce

1. Jedis

1. 新建一个maven项目
(略…)
2. 导入Jedis的依赖

可以去maven仓库选择自己需要的版本.

https://mvnrepository.com/artifact/redis.clients/jedis
<dependency>
    <groupId>redis.clientsgroupId>
    <artifactId>jedisartifactId>
    <version>3.2.0version>
dependency>

Java连接Redis_第1张图片
导入依赖后,发现,Jedis3.2依赖

commons-pool2-2.6.2.jar
slf4j-api-1.7.25.jar

所以,如果没有使用maven项目的话,那么手动将这两个jar引入也可以.
3. 修改redis配置文件

修改bind为指定的ip或所有
关闭保护模式

1. bind 0.0.0.0
2. protected-mode no

4. 重启(启动)redis-server

shutdown
exit
redis-server /path/to/conf-file/redis*.conf

5. 配置java端连接

package edu.shuu.redis;

import redis.clients.jedis.Jedis;

public class RedisDemo {
    public static void main(String[] args) {
    	// new Jedis(IP, PORT)
        Jedis jedis = new Jedis("master-server", 6379);
        System.out.println(jedis.ping());
    }
}

连接ok!

Java连接Redis_第2张图片
6. 简单演示

目前还没有任何数据
在这里插入图片描述
注意,这里有个坑,如果要使用 multi和exec的话,不能使用Jedis对象进行写操作,而是要使用Transaction对象.

package edu.shuu.redis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Transaction;

public class RedisDemo {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("master-server", 6379);
        Transaction transaction = jedis.multi();
        transaction.set("k1", "v1");
        transaction.hset("hash", "f1", "v1");
        transaction.lpush("list", "v1", "v2", "v3", "v4", "v5");
        transaction.sadd("set", "v1", "v2", "v3");
        transaction.zadd("zset", 60, "v1");
        transaction.exec();
    }
}

发现已经成功写入到redis了,具体API就不演示了.

在这里插入图片描述

2. lettuce

1. 导入依赖

<dependency>
    <groupId>io.lettucegroupId>
    <artifactId>lettuce-coreartifactId>
    <version>5.2.2.RELEASEversion>
dependency>

可以发现这次的依赖比较多,而且很多是netty的包.
Java连接Redis_第3张图片
2. 简单演示

package edu.shuu.redis;

import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisURI;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.sync.RedisCommands;

import java.time.Duration;
import java.time.temporal.ChronoUnit;

public class RedisDemo {
    public static void main(String[] args) {
        RedisURI uri = RedisURI.builder()
                .withHost("master-server")
                .withPort(6379)
                .withTimeout(Duration.of(10, ChronoUnit.SECONDS))
                .build();
        RedisClient redisClient = RedisClient.create(uri);
        StatefulRedisConnection<String, String> connect = redisClient.connect();
        RedisCommands<String, String> sync = connect.sync();
        sync.multi();
        sync.set("k1", "v1");
        sync.hset("hash", "f1", "v1");
        sync.lpush("list", "v1", "v2", "v3", "v4", "v5");
        sync.sadd("set", "v1", "v2", "v3");
        sync.zadd("zset", 60, "v1");
        sync.exec();
        connect.close();
        redisClient.shutdown();
    }

}

Java连接Redis_第4张图片

3. More About Lettuce

更多有关Lettuce的信息,推荐大家去看下面的这篇文章.

https://cloud.tencent.com/developer/article/1526886

你可能感兴趣的:(redis)