溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何在Java項目中調用Redis集群

發(fā)布時間:2021-03-10 15:22:43 來源:億速云 閱讀:540 作者:Leah 欄目:編程語言

這篇文章給大家介紹如何在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

關于如何在Java項目中調用Redis集群就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI