Java調用Redis集群


前文

       需要使用以下jar包

               

       Maven項目引用以下配置: 

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
    <version>2.6.2</version>
</dependency>

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

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.26</version>
</dependency>

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>1.7.26</version>
    <scope>test</scope>
</dependency>

  

代碼

 

package Main;

import java.io.IOException;
import java.util.LinkedHashSet;
import java.util.Set;

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

@SuppressWarnings("all")
public class RedisMain {
     public static void main(String[] args) {
         JedisCluster cluster =null;
         try {
             
                Set<HostAndPort> nodes = new LinkedHashSet<HostAndPort>();
                //一般選用slaveof從IP+端口進行增刪改查,不用master
                nodes.add(new HostAndPort("外網IP", 7003));
                nodes.add(new HostAndPort("外網", 7004));
                nodes.add(new HostAndPort("外網IP", 7004));
                // Jedis連接池配置
                JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
                // 最大空閑連接數, 默認8個
                jedisPoolConfig.setMaxIdle(100);
                // 最大連接數, 默認8個
                jedisPoolConfig.setMaxTotal(500);
                //最小空閑連接數, 默認0
                jedisPoolConfig.setMinIdle(0);
                // 獲取連接時的最大等待毫秒數(如果設置為阻塞時BlockWhenExhausted),如果超時就拋異常, 小於零:阻塞不確定的時間,  默認-1
                jedisPoolConfig.setMaxWaitMillis(2000); // 設置2秒
                //對拿到的connection進行validateObject校驗
                jedisPoolConfig.setTestOnBorrow(true);
                //未設置auth Password
                JedisCluster jedis = new JedisCluster(nodes, jedisPoolConfig);
                //設置auth Password
                //JedisCluster jedis = new JedisCluster(nodes,5000,3000,10,{auth_password}, new JedisPoolConfig());
                System.out.println(jedis.get("mykey"));
             
         }catch(Exception e) {
             e.printStackTrace();
         }finally {
             if(null !=cluster)
                 cluster.close();
         }
     }
}

 

 

 

 

可能出現的異常

    1、DENIED Redis is running in protected mode because protected mode is enabled...

          解決方法:redis.conf默認禁止外網訪問,修改”protected-mode yes”為“protected-mode no”

      2、No more cluster attempts left.

         解決方法:redis設置集群時,服務器沒有配置開啟集群總線端口(redis端口+10000),如果redis-cli端口有7000-7005,則集群總線端口為17000-17005,服務器7000-70005、17000-17005端口都要打開

      3、No reachable node in cluster

          解決方法:查看redis.conf 的 "bind xxxxxxx" 是否限制了IP訪問,注銷bind則可以任意IP訪問服務器Redis


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM