From ef3606f6f0d3ab7705eba6d6d0b48b779e8e9c51 Mon Sep 17 00:00:00 2001
From: Pu Zhibing <393733352@qq.com>
Date: 星期三, 23 四月 2025 11:54:40 +0800
Subject: [PATCH] 修改redis连接方式

---
 /dev/null                                                                   |   23 -------
 ZuulIGOTravel/pom.xml                                                       |    6 -
 ZuulIGOTravel/src/main/resources/application.yml                            |    5 +
 ZuulIGOTravel/src/main/java/com/sinata/zuul/controller/RedisController.java |   15 -----
 ZuulIGOTravel/src/main/java/com/sinata/zuul/util/RedisUtil.java             |   86 +++++++---------------------
 5 files changed, 28 insertions(+), 107 deletions(-)

diff --git a/ZuulIGOTravel/pom.xml b/ZuulIGOTravel/pom.xml
index 5e3844d..7dbf99d 100644
--- a/ZuulIGOTravel/pom.xml
+++ b/ZuulIGOTravel/pom.xml
@@ -37,11 +37,9 @@
             <artifactId>swagger-spring-boot-starter</artifactId>
             <version>1.7.0.RELEASE</version>
         </dependency>
-        <!-- jedis -->
         <dependency>
-            <groupId>redis.clients</groupId>
-            <artifactId>jedis</artifactId>
-            <version>2.9.0</version>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-data-redis</artifactId>
         </dependency>
 
 <!--        <dependency>-->
diff --git a/ZuulIGOTravel/src/main/java/com/sinata/zuul/config/RedisConfig.java b/ZuulIGOTravel/src/main/java/com/sinata/zuul/config/RedisConfig.java
deleted file mode 100644
index cffaeb7..0000000
--- a/ZuulIGOTravel/src/main/java/com/sinata/zuul/config/RedisConfig.java
+++ /dev/null
@@ -1,46 +0,0 @@
-package com.sinata.zuul.config;
-
-import org.springframework.beans.factory.annotation.Value;
-import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
-import org.springframework.context.annotation.PropertySource;
-import redis.clients.jedis.JedisPool;
-import redis.clients.jedis.JedisPoolConfig;
-
-@Configuration
-@PropertySource("classpath:redis.properties")
-public class RedisConfig {
-    @Value("${spring.redis.host}")
-    private String host;
-
-    @Value("${spring.redis.port}")
-    private int port;
-
-    @Value("${spring.redis.timeout}")
-    private int timeout;
-
-    @Value("${spring.redis.jedis.pool.max-idle}")
-    private int maxIdle;
-
-    @Value("${spring.redis.jedis.pool.max-wait}")
-    private long maxWaitMillis;
-
-    @Value("${spring.redis.password}")
-    private String password;
-
-    @Value("${spring.redis.block-when-exhausted}")
-    private boolean  blockWhenExhausted;
-
-    @Bean
-    public JedisPool redisPoolFactory()  throws Exception{
-        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
-        jedisPoolConfig.setMaxIdle(maxIdle);
-        jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
-        // 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true
-        jedisPoolConfig.setBlockWhenExhausted(blockWhenExhausted);
-        // 是否启用pool的jmx管理功能, 默认true
-        jedisPoolConfig.setJmxEnabled(true);
-        JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password);
-        return jedisPool;
-    }
-}
diff --git a/ZuulIGOTravel/src/main/java/com/sinata/zuul/controller/RedisController.java b/ZuulIGOTravel/src/main/java/com/sinata/zuul/controller/RedisController.java
index 4866ef5..8732568 100644
--- a/ZuulIGOTravel/src/main/java/com/sinata/zuul/controller/RedisController.java
+++ b/ZuulIGOTravel/src/main/java/com/sinata/zuul/controller/RedisController.java
@@ -34,21 +34,6 @@
 
 
     /**
-     * 批量获取
-     * @param keys
-     * @return
-     */
-    @ResponseBody
-    @PostMapping("/getValues")
-    public String getValues(String keys){
-        String[] split = keys.split(",");
-        List<String> list = Arrays.asList(split);
-        List<Object> values = redisUtil.getValues(list);
-        return JSON.toJSONString(ResultUtil.success(values));
-    }
-
-
-    /**
      * 存值
      * @param key
      * @param value
diff --git a/ZuulIGOTravel/src/main/java/com/sinata/zuul/util/RedisUtil.java b/ZuulIGOTravel/src/main/java/com/sinata/zuul/util/RedisUtil.java
index 2bcbd87..40a81e1 100644
--- a/ZuulIGOTravel/src/main/java/com/sinata/zuul/util/RedisUtil.java
+++ b/ZuulIGOTravel/src/main/java/com/sinata/zuul/util/RedisUtil.java
@@ -1,15 +1,9 @@
 package com.sinata.zuul.util;
 
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Component;
-import redis.clients.jedis.Jedis;
-import redis.clients.jedis.JedisPool;
-import redis.clients.jedis.Pipeline;
-import redis.clients.jedis.Response;
-
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.List;
+import java.util.concurrent.TimeUnit;
 
 
 /**
@@ -17,25 +11,24 @@
  */
 @Component
 public class RedisUtil {
-
+    
     @Autowired
-    private JedisPool jedisPool;
-
-
+    private RedisTemplate redisTemplate;
+    
+    
+    
     /**
      * 向redis中存储字符串没有过期时间
      * @param key
      * @param value
      */
     public void setStrValue(String key, String value){
-        if(StringUtil.isNotEmpty(key)){
-            Jedis resource = jedisPool.getResource();
-            String set = resource.set(key, value);
-            resource.close();
+        if(StringUtil.isNotEmpty(key) && StringUtil.isNotEmpty(value)){
+            redisTemplate.opsForValue().set(key, value);
         }
     }
-
-
+    
+    
     /**
      * 以分钟为单位设置存储值(设置过期时间)
      * @param key
@@ -43,14 +36,12 @@
      * @param time 秒
      */
     public void setStrValue(String key, String value, int time){
-        if(StringUtil.isNotEmpty(key)){
-            Jedis resource = jedisPool.getResource();
-            String setex = resource.setex(key, time, value);
-            resource.close();
+        if(StringUtil.isNotEmpty(key) && StringUtil.isNotEmpty(value)){
+            redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
         }
     }
-
-
+    
+    
     /**
      * 从redis中获取值
      * @param key
@@ -58,57 +49,22 @@
      */
     public String getValue(String key){
         if(StringUtil.isNotEmpty(key)){
-            Jedis resource = jedisPool.getResource();
-            String data = resource.get(key);
-
-            resource.close();
+            String data = (String) redisTemplate.opsForValue().get(key);
             return data;
         }
         return null;
     }
-
-
-    /**
-     * 批量获取
-     * @param kes
-     * @return
-     */
-    public List<Object> getValues(List<String> kes){
-        if(null != kes){
-            Jedis resource = jedisPool.getResource();
-            Pipeline pipelined = resource.pipelined();
-            for(String key : kes){
-                pipelined.get(key);
-            }
-            List<Object> list = pipelined.syncAndReturnAll();
-            resource.close();
-            pipelined.clear();
-            try {
-                pipelined.close();
-            } catch (IOException e) {
-                e.printStackTrace();
-            }
-            List<Object> data = new ArrayList<>();
-            for(Object o : list){
-                if(null != o){
-                    data.add(o);
-                }
-            }
-            return data;
-        }
-        return null;
-    }
-
-
+    
+    
+    
+    
     /**
      * 删除key
      * @param key
      */
     public void remove(String key){
         if(StringUtil.isNotEmpty(key)){
-            Jedis resource = jedisPool.getResource();
-            Long del = resource.del(key);
-            resource.close();
+            redisTemplate.delete(key);
         }
     }
 }
diff --git a/ZuulIGOTravel/src/main/resources/application.yml b/ZuulIGOTravel/src/main/resources/application.yml
index f767123..8a398eb 100644
--- a/ZuulIGOTravel/src/main/resources/application.yml
+++ b/ZuulIGOTravel/src/main/resources/application.yml
@@ -9,6 +9,11 @@
     multipart:
       max-file-size: 100MB
       max-request-size: 100MB
+  redis:
+    database: 0
+    host: 127.0.0.1
+    port: 16379
+    password: mPMHThYzlT8DWgl8HLqwPEyPOiHDPPB5
 
 eureka:
   client:
diff --git a/ZuulIGOTravel/src/main/resources/redis.properties b/ZuulIGOTravel/src/main/resources/redis.properties
deleted file mode 100644
index 72627aa..0000000
--- a/ZuulIGOTravel/src/main/resources/redis.properties
+++ /dev/null
@@ -1,23 +0,0 @@
-#redis���ÿ�ʼ
-# Redis���ݿ�������Ĭ��Ϊ0��
-spring.redis.database=0
-# Redis��������ַ
-spring.redis.host=127.0.0.1
-# Redis���������Ӷ˿�
-spring.redis.port=16379
-#spring.redis.port=6379
-# Redis�������������루Ĭ��Ϊ�գ�
-spring.redis.password=mPMHThYzlT8DWgl8HLqwPEyPOiHDPPB5
-#spring.redis.password=123456
-# ���ӳ������������ʹ�ø�ֵ��ʾû�����ƣ�
-spring.redis.jedis.pool.max-active=1024
-# ���ӳ���������ȴ�ʱ�䣨ʹ�ø�ֵ��ʾû�����ƣ�
-spring.redis.jedis.pool.max-wait=10000
-# ���ӳ��е�����������
-spring.redis.jedis.pool.max-idle=200
-# ���ӳ��е���С��������
-spring.redis.jedis.pool.min-idle=0
-# ���ӳ�ʱʱ�䣨���룩
-spring.redis.timeout=10000
-#redis���ý���
-spring.redis.block-when-exhausted=true
\ No newline at end of file

--
Gitblit v1.7.1