码迷,mamicode.com
首页 > 编程语言 > 详细

Redis的JAVA连接

时间:2019-03-09 20:27:33      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:port   成功   res   info   ssh   client   color   xid   date   

ShardedJedis用法

package com.zhi.demo;

import java.util.Arrays;
import java.util.List;

import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

/**
 * 多个redis服务器获取连接
 *
 * @date 2019年03月07日19:33:05
 *
 */
public class SharedRedis {
    private static ShardedJedisPool shardedJedisPool;

    static {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxIdle(5);
        config.setTestOnBorrow(false);

        JedisShardInfo info1 = new JedisShardInfo("192.168.153.137", 6379);
        JedisShardInfo info2 = new JedisShardInfo("192.168.153.137", 6379);

        List<JedisShardInfo> infoList = Arrays.asList(info1, info2);
        shardedJedisPool = new ShardedJedisPool(config, infoList);
    }

    public static void main(String[] args) {
        try (ShardedJedis jedis = shardedJedisPool.getResource()) {
            System.out.println("获取reidis连接成功");
            if ("OK".equals(jedis.set("reids", "reids"))) {
                System.out.println("向redis设置值成功");
            } else {
                System.out.println("向redis设置值失败");
            }
        }
// 关闭资源池,否则线程会一直等待 shardedJedisPool.close(); System.out.println(
"关闭redis资源池"); } }

 

Redis的JAVA连接

标签:port   成功   res   info   ssh   client   color   xid   date   

原文地址:https://www.cnblogs.com/zhi-leaf/p/10502704.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!