1. 程式人生 > >java操作redis叢集(基本操作)

java操作redis叢集(基本操作)

package com.company;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;

public class Main {
    public static void main(String[] args) throws FileNotFoundException {
        Set
<HostAndPort> nodes = new HashSet<HostAndPort>(); nodes.add(new HostAndPort("192.168.3.231", 7001)); nodes.add(new HostAndPort("192.168.3.231", 7002)); nodes.add(new HostAndPort("192.168.3.232", 7001)); nodes.add(new HostAndPort("192.168.3.232", 7002)); nodes.add
(new HostAndPort("192.168.3.233", 7001)); nodes.add(new HostAndPort("192.168.3.233", 7002)); JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); jedisPoolConfig.setMaxTotal(100); jedisPoolConfig.setMaxIdle(20); jedisPoolConfig.setMaxWaitMillis(-1); jedisPoolConfig.setTestOnBorrow
(true); JedisCluster jedisCluster = new JedisCluster(nodes, 6000, 100, jedisPoolConfig); String k = "jfpc"; String v = jedisCluster.get(k); System.err.println(v); jedisCluster.set("jfpc", "20"); try { jedisCluster.close(); } catch (IOException e) { e.printStackTrace(); } } }