@Java中使用Jedis操作Redis之一 使用连接池+分布式

依赖的jar包:jedis

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.0</version>
</dependency>

使用单连接

此方式仅建议用于开发环境做调试用。

public class TestJedis {
    
    public static void main(String[] args) {
        // 创建连接,需要制定ip和端口号
        String host = "127.0.0.1";
        int port = 6379;
        Jedis client = new Jedis(host, port);

        // 执行set指令,jedis中一个命令对应一个方法~
        String result = client.set("key-string", "Hello, Redis!");
        System.out.println( String.format("set指令执行结果:%s", result) );

        // 执行get指令
        String value = client.get("key-string");
        System.out.println( String.format("get指令执行结果:%s", value) );
client.close();//注意关闭连接 } }

运行上述代码,控制台输出:

set指令执行结果:OK
get指令执行结果:Hello, Redis!

使用连接池

此方式适用于仅使用单个Redis实例的场景。创建连接是一个比较耗费资源的操作,如果每次使用redis都要创建一个连接是不合适的!!所以使用连接池!!

public class TestJedis {

    public static void main(String[] args) {
        // 生成连接池配置信息
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxIdle(10);
        config.setMaxActive(10);
        config.setMaxWait(20);
        
        // 在应用初始化的时候生成连接池
        JedisPool pool = new JedisPool(config, "127.0.0.1", 6379);

        // 在业务操作时,从连接池获取连接
        Jedis client = pool.getResource();
        try {
            // 执行指令
            String result = client.set("key-string", "Hello, Redis!");
            System.out.println(String.format("set指令执行结果:%s", result));
            String value = client.get("key-string");
            System.out.println(String.format("get指令执行结果:%s", value));
        } catch (Exception e) {
            // TODO: handle exception
        } finally {
            // 业务操作完成,将连接返回给连接池
            if (null != client) {
                pool.returnResource(client);
            }
        } // end of try block

        // 应用关闭时,释放连接池资源
        pool.destroy();
    }

}

运行上述代码,控制台输出:

set指令执行结果:OK
get指令执行结果:Hello, Redis!

在规模较大的系统中,往往会有多个Redis实例做负载均衡。并且还实现主从备份,当主实例发生故障时,切换至从实例提供服务。
类似于Memcached的客户端,Jedis也提供了客户端分布式操作的方式,采用一致性哈希算法。

public class TestJedis {

    public static void main(String[] args) {
        // 生成多机连接信息列表
        List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
        shards.add( new JedisShardInfo("127.0.0.1", 6379) );
        shards.add( new JedisShardInfo("192.168.56.102", 6379) );

        // 生成连接池配置信息
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxIdle(10);
        config.setMaxTotal(30);
        config.setMaxWaitMillis(3*1000);

        // 在应用初始化的时候生成连接池
        ShardedJedisPool pool = new ShardedJedisPool(config, shards);

        // 在业务操作时,从连接池获取连接
        ShardedJedis client = pool.getResource();
        try {
            // 执行指令
            String result = client.set("key-string", "Hello, Redis!");
            System.out.println( String.format("set指令执行结果:%s", result) );
            String value = client.get("key-string");
            System.out.println( String.format("get指令执行结果:%s", value) );
        } catch (Exception e) {
            // TODO: handle exception
        } finally {
            // 业务操作完成,将连接返回给连接池
            if (null != client) {
                pool.returnResource(client);
            }
        } // end of try block

        // 应用关闭时,释放连接池资源
        pool.destroy();
    }

}