tianwu.sun il y a 8 mois
Parent
commit
90d9059685

+ 108 - 31
src/main/java/com/bootdo/common/redis/shiro/RedisManager.java

@@ -5,12 +5,13 @@ package com.bootdo.common.redis.shiro;
  * @version V1.0
  */
 
+import java.nio.charset.StandardCharsets;
+import java.util.HashSet;
+import java.util.List;
 import java.util.Set;
 
 import org.springframework.beans.factory.annotation.Value;
-import redis.clients.jedis.Jedis;
-import redis.clients.jedis.JedisPool;
-import redis.clients.jedis.JedisPoolConfig;
+import redis.clients.jedis.*;
 
 /**
  *
@@ -33,7 +34,8 @@ public class RedisManager {
     @Value("${spring.redis.password}")
     private String password = "";
 
-    private static JedisPool jedisPool = null;
+    private static Jedis jedis = null;
+    private static JedisCluster jedisCluster = null;
 
     public RedisManager() {
 
@@ -43,18 +45,43 @@ public class RedisManager {
      * 初始化方法
      */
     public void init() {
-        if (jedisPool == null) {
-            if (password != null && !"".equals(password)) {
-                jedisPool = new JedisPool(new JedisPoolConfig(), host, port, timeout, password);
-            } else if (timeout != 0) {
-                jedisPool = new JedisPool(new JedisPoolConfig(), host, port, timeout);
-            } else {
-                jedisPool = new JedisPool(new JedisPoolConfig(), host, port);
+        if (host.contains(",")) {
+            if(jedisCluster == null){
+                JedisPoolConfig config = new JedisPoolConfig();
+                config.setEvictionPolicyClassName("org.apache.commons.pool2.impl.DefaultEvictionPolicy");
+                config.setLifo(true);
+                config.setMaxIdle(30);
+                config.setMaxTotal(30);
+                config.setMaxWaitMillis(-1);
+                config.setMinEvictableIdleTimeMillis(3600000);
+                config.setNumTestsPerEvictionRun(3);
+                config.setSoftMinEvictableIdleTimeMillis(3600000);
+                String[] addrs = host.split(",");
+                Set<HostAndPort> nodes = new HashSet<>();
+                for (int i = 0; i < addrs.length; i++) {
+                    nodes.add(new HostAndPort(addrs[i].split(":")[0], Integer.parseInt(addrs[i].split(":")[1])));
+                }
+
+                jedisCluster = new JedisCluster(nodes, 10000, 10000, 100, password, config);//xxxx是Redis的密码
+
             }
 
+        } else {
+            if (jedis == null) {
+                if (password != null && !"".equals(password)) {
+                    jedis = new JedisPool(new JedisPoolConfig(), host, port, timeout, password).getResource();
+                } else if (timeout != 0) {
+                    jedis = new JedisPool(new JedisPoolConfig(), host, port, timeout).getResource();
+                } else {
+                    jedis = new JedisPool(new JedisPoolConfig(), host, port).getResource();
+                }
+
+            }
         }
+
     }
 
+
     /**
      * get value from redis
      *
@@ -63,9 +90,14 @@ public class RedisManager {
      */
     public byte[] get(byte[] key) {
         byte[] value = null;
-        Jedis jedis = jedisPool.getResource();
+
         try {
-            value = jedis.get(key);
+            if(jedisCluster != null){
+                value = jedisCluster.get(key);
+            } else {
+                value = jedis.get(key);
+            }
+
         } finally {
             if (jedis != null) {
                 jedis.close();
@@ -82,12 +114,20 @@ public class RedisManager {
      * @return
      */
     public byte[] set(byte[] key, byte[] value) {
-        Jedis jedis = jedisPool.getResource();
+
         try {
-            jedis.set(key, value);
-            if (this.expire != 0) {
-                jedis.expire(key, this.expire);
+            if(jedisCluster != null){
+                jedisCluster.set(key, value);
+                if (this.expire != 0) {
+                    jedisCluster.expire(key, this.expire);
+                }
+            } else {
+                jedis.set(key, value);
+                if (this.expire != 0) {
+                    jedis.expire(key, this.expire);
+                }
             }
+
         } finally {
             if (jedis != null) {
                 jedis.close();
@@ -105,12 +145,20 @@ public class RedisManager {
      * @return
      */
     public byte[] set(byte[] key, byte[] value, int expire) {
-        Jedis jedis = jedisPool.getResource();
+
         try {
-            jedis.set(key, value);
-            if (expire != 0) {
-                jedis.expire(key, expire);
+            if(jedisCluster != null){
+                jedisCluster.set(key, value);
+                if (expire != 0) {
+                    jedisCluster.expire(key, expire);
+                }
+            } else {
+                jedis.set(key, value);
+                if (expire != 0) {
+                    jedis.expire(key, expire);
+                }
             }
+
         } finally {
             if (jedis != null) {
                 jedis.close();
@@ -125,9 +173,13 @@ public class RedisManager {
      * @param key
      */
     public void del(byte[] key) {
-        Jedis jedis = jedisPool.getResource();
         try {
-            jedis.del(key);
+            if(jedisCluster != null){
+                jedisCluster.del(key);
+            } else {
+                jedis.del(key);
+            }
+
         } finally {
             if (jedis != null) {
                 jedis.close();
@@ -139,9 +191,14 @@ public class RedisManager {
      * flush
      */
     public void flushDB() {
-        Jedis jedis = jedisPool.getResource();
+
         try {
-            jedis.flushDB();
+            if(jedisCluster != null){
+                jedisCluster.flushDB();
+            } else {
+                jedis.flushDB();
+            }
+
         } finally {
             if (jedis != null) {
                 jedis.close();
@@ -154,9 +211,13 @@ public class RedisManager {
      */
     public Long dbSize() {
         Long dbSize = 0L;
-        Jedis jedis = jedisPool.getResource();
         try {
-            dbSize = jedis.dbSize();
+            if(jedisCluster != null){
+                dbSize = jedis.dbSize();
+            } else {
+                dbSize = jedis.dbSize();
+            }
+
         } finally {
             if (jedis != null) {
                 jedis.close();
@@ -168,14 +229,30 @@ public class RedisManager {
     /**
      * keys
      *
-     * @param regex
      * @return
      */
     public Set<byte[]> keys(String pattern) {
-        Set<byte[]> keys = null;
-        Jedis jedis = jedisPool.getResource();
+        Set<byte[]> keys = new HashSet<>();
         try {
-            keys = jedis.keys(pattern.getBytes());
+            if(jedisCluster != null){
+                Set<String> keysStr = new HashSet<>();
+                String cursor = "0";
+                ScanParams scanParams = new ScanParams().match(pattern).count(100);
+                do {
+                    ScanResult<String> scanResult = jedisCluster.scan(cursor, scanParams);
+                    List<String> result = scanResult.getResult();
+                    keysStr.addAll(result);
+                    cursor = scanResult.getStringCursor();
+                } while (!"0".equals(cursor));
+
+                for (String str : keysStr) {
+                    byte[] byteArray = str.getBytes(StandardCharsets.UTF_8); // 将字符串转换为UTF-8编码的字节数组
+                    keys.add(byteArray); // 将字节数组添加到新的集合中
+                }
+            } else {
+                keys = jedis.keys(pattern.getBytes());
+            }
+
         } finally {
             if (jedis != null) {
                 jedis.close();

+ 2 - 2
src/main/resources/application-prod.yml

@@ -54,8 +54,8 @@ spring:
       login-username: admin
       login-password: admin
   redis:
-    host: 172.18.105.10
-    port: 7001
+    host: 172.18.105.10:7001,172.18.105.10:7002,172.18.105.10:7003,172.18.105.11:7004,172.18.105.11:7005,172.18.105.11:7006
+    port: 6379
     password: YXSA@2024
     # 连接超时时间(毫秒)
     timeout: 10000