Redis --- Jedis 使用简单示例

Redis --- Jedis 使用简单示例

Jedis连接独立Redis

JedisPoolConfig config = new JedisPoolConfig();

       config.setMaxTotal(100);

       config.setMaxIdle(20);

       config.setMaxWaitMillis(1000);

       config.setTimeBetweenEvictionRunsMillis(60000);

      

       JedisPool jedisPool = new JedisPool(config,

              "192.168.89.131", 6379);

      

 

       Jedis jedis = jedisPool.getResource();

      

       Iterator iter = jedis.keys("*").iterator();

       while(iter.hasNext()){

           System.out.println(iter.next());

       }

       jedis.close();

       jedisPool.close();

 

 

Jedis 连接Sentinel

Sentinels的创建见《Redis ---Sentinel 》

Set sentinels = new HashSet();

       sentinels.add("192.168.89.131:26379");

       sentinels.add("192.168.89.131:27379");

       sentinels.add("192.168.89.131:28379");

 

JedisSentinelPool jedisPool = new JedisSentinelPool("mymaster", sentinels, config);

Jedis jedis = jedisPool.getResource();

              Iterator iter = jedis.keys("*").iterator();

       while(iter.hasNext()){

           System.out.println(iter.next());

       }

       jedis.close();

       jedisPool.close();

 

Jedis 连接Redis Cluster

创建实验用redis集群

简单实用Redis脚本create-cluster创建实验集群:。

运行脚本之前先安装:

Yum install ruby

Yum install rubygems

cd /usr/lib/ruby/site_ruby/1.8/rubygems

gem install redis

 

运行脚本创建redis集群

[root@localhost create-cluster]# ./create-cluster start

Starting 30001

Starting 30002

Starting 30003

Starting 30004

Starting 30005

Starting 30006

[root@localhost create-cluster]# ./create-cluster create

>>> Creating cluster

Connecting to node 127.0.0.1:30001: OK

Connecting to node 127.0.0.1:30002: OK

Connecting to node 127.0.0.1:30003: OK

Connecting to node 127.0.0.1:30004: OK

Connecting to node 127.0.0.1:30005: OK

Connecting to node 127.0.0.1:30006: OK

>>> Performing hash slots allocation on 6 nodes...

Using 3 masters:

127.0.0.1:30001

127.0.0.1:30002

127.0.0.1:30003

Adding replica 127.0.0.1:30004 to 127.0.0.1:30001

Adding replica 127.0.0.1:30005 to 127.0.0.1:30002

Adding replica 127.0.0.1:30006 to 127.0.0.1:30003

M: e25cdacf0aac95464e0e5216d3b00424376f53a5 127.0.0.1:30001

   slots:0-5460 (5461 slots) master

M: 1b8e226332478e361d446bdd321ecad95bfd7173 127.0.0.1:30002

   slots:5461-10922 (5462 slots) master

M: 97be80ba39e51b51b8a502a8ce15485a85306867 127.0.0.1:30003

   slots:10923-16383 (5461 slots) master

S: 9692ea7687aa72c44c4ce9149fe802705d54ad8a 127.0.0.1:30004

   replicates e25cdacf0aac95464e0e5216d3b00424376f53a5

S: a3aeb572fada8b4f4cc1d87fec7c76f59e223c01 127.0.0.1:30005

   replicates 1b8e226332478e361d446bdd321ecad95bfd7173

S: 802fb8cc906b3b29c4babc9f91aed6a845743301 127.0.0.1:30006

   replicates 97be80ba39e51b51b8a502a8ce15485a85306867

Can I set the above configuration? (type 'yes' to accept): yes

>>> Nodes configuration updated

>>> Assign a different config epoch to each node

>>> Sending CLUSTER MEET messages to join the cluster

Waiting for the cluster to join.

>>> Performing Cluster Check (using node 127.0.0.1:30001)

M: e25cdacf0aac95464e0e5216d3b00424376f53a5 127.0.0.1:30001

   slots:0-5460 (5461 slots) master

M: 1b8e226332478e361d446bdd321ecad95bfd7173 127.0.0.1:30002

   slots:5461-10922 (5462 slots) master

M: 97be80ba39e51b51b8a502a8ce15485a85306867 127.0.0.1:30003

   slots:10923-16383 (5461 slots) master

M: 9692ea7687aa72c44c4ce9149fe802705d54ad8a 127.0.0.1:30004

   slots: (0 slots) master

   replicates e25cdacf0aac95464e0e5216d3b00424376f53a5

M: a3aeb572fada8b4f4cc1d87fec7c76f59e223c01 127.0.0.1:30005

   slots: (0 slots) master

   replicates 1b8e226332478e361d446bdd321ecad95bfd7173

M: 802fb8cc906b3b29c4babc9f91aed6a845743301 127.0.0.1:30006

   slots: (0 slots) master

   replicates 97be80ba39e51b51b8a502a8ce15485a85306867

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

[root@localhost create-cluster]#

 

 

示例代码

Set nodes = new HashSet();

       nodes.add(new HostAndPort("192.168.89.131",30001));

       JedisCluster jedisCluster = new JedisCluster(nodes);

       jedisCluster.set("testkey", "testvalue");

      

       System.out.println(jedisCluster.get("testkey"));

这里注意到只需要添加一个主机节点到JedisCluster构造函数的节点集合中,其它会由jedisCluster自动发现和更新。

 

如果添加多个的话还会发生异常??

Exception in thread "main" redis.clients.jedis.exceptions.JedisClusterMaxRedirectionsException: Too many Cluster redirections?

    at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:34)

    at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:68)

    at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:85)

    at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:68)

    at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:85)

    at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:68)

    at redis.clients.jedis.JedisClusterCommand.run(JedisClusterCommand.java:29)

    at redis.clients.jedis.JedisCluster.get(JedisCluster.java:96)

    at com.lh.example.jedis.JedisTest.main(JedisTest.java:49)

 

你可能感兴趣的:(java,redis)