From efd9ac5b88dfdb3c4d2e4bcc5e7a5258aa55542c Mon Sep 17 00:00:00 2001 From: Pu Zhibing <393733352@qq.com> Date: 星期一, 21 四月 2025 20:29:15 +0800 Subject: [PATCH] 更换缓存连接方式 --- ZuulZYTravel/src/main/java/com/sinata/zuul/util/RedisUtil.java | 151 ++++++---- UserZYTravel/guns-admin/src/main/resources/application.yml | 5 MessagePushTravel/src/main/resources/application.yml | 6 UserZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/service/impl/DriverServiceImpl.java | 2 ZuulZYTravel/src/main/resources/application.yml | 5 /dev/null | 23 - ZuulZYTravel/src/main/java/com/sinata/zuul/controller/RedisController.java | 14 - MessagePushTravel/pom.xml | 6 MessagePushTravel/src/main/java/com/sinata/push/util/RedisUtil.java | 216 ++++---------- UserZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/RedisUtil.java | 145 --------- DriverZYTravel/guns-admin/src/main/resources/application.yml | 6 MessagePushTravel/src/main/java/com/sinata/push/controller/RedisController.java | 94 ------ DriverZYTravel/guns-admin/pom.xml | 5 ZuulZYTravel/pom.xml | 6 DriverZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/RedisUtil.java | 139 --------- UserZYTravel/guns-admin/pom.xml | 7 16 files changed, 207 insertions(+), 623 deletions(-) diff --git a/DriverZYTravel/guns-admin/pom.xml b/DriverZYTravel/guns-admin/pom.xml index 26b84fb..4c9615e 100644 --- a/DriverZYTravel/guns-admin/pom.xml +++ b/DriverZYTravel/guns-admin/pom.xml @@ -140,9 +140,8 @@ <artifactId>jjwt</artifactId> </dependency> <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/DriverZYTravel/guns-admin/src/main/java/com/stylefeng/guns/config/RedisConfig.java b/DriverZYTravel/guns-admin/src/main/java/com/stylefeng/guns/config/RedisConfig.java deleted file mode 100644 index d459612..0000000 --- a/DriverZYTravel/guns-admin/src/main/java/com/stylefeng/guns/config/RedisConfig.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.stylefeng.guns.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/DriverZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/RedisUtil.java b/DriverZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/RedisUtil.java index 82190c1..f9a24a1 100644 --- a/DriverZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/RedisUtil.java +++ b/DriverZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/RedisUtil.java @@ -1,23 +1,12 @@ package com.stylefeng.guns.modular.system.util; -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONObject; import com.stylefeng.guns.core.util.ToolUtil; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.HttpEntity; -import org.springframework.http.HttpHeaders; -import org.springframework.http.MediaType; +import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; -import org.springframework.util.LinkedMultiValueMap; -import org.springframework.util.MultiValueMap; import org.springframework.util.StringUtils; -import org.springframework.web.client.RestTemplate; -import redis.clients.jedis.Jedis; -import redis.clients.jedis.JedisPool; -import redis.clients.jedis.Pipeline; - -import java.io.IOException; import java.util.*; +import java.util.concurrent.TimeUnit; /** @@ -27,9 +16,8 @@ public class RedisUtil { @Autowired - private JedisPool jedisPool; + private RedisTemplate redisTemplate; - private Timer timer; /** @@ -39,9 +27,7 @@ */ public void setStrValue(String key, String value){ if(ToolUtil.isNotEmpty(key) && ToolUtil.isNotEmpty(value)){ - Jedis resource = jedisPool.getResource(); - String set = resource.set(key, value); - closeJedis(resource); + redisTemplate.opsForValue().set(key, value); } } @@ -54,9 +40,7 @@ */ public void setStrValue(String key, String value, int time){ if(ToolUtil.isNotEmpty(key) && ToolUtil.isNotEmpty(value)){ - Jedis resource = jedisPool.getResource(); - String setex = resource.setex(key, time, value); - closeJedis(resource); + redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS); } } @@ -68,46 +52,13 @@ */ public String getValue(String key){ if(ToolUtil.isNotEmpty(key)){ - Jedis resource = jedisPool.getResource(); - String data = resource.get(key); - closeJedis(resource); + 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(); - - closeJedis(resource); - 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; - } /** @@ -116,66 +67,14 @@ */ public void remove(String key){ if(ToolUtil.isNotEmpty(key)){ - Jedis resource = jedisPool.getResource(); - Long del = resource.del(key); - closeJedis(resource); + redisTemplate.delete(key); } } - /** - * 向集合key添加数据 - * @param key - * @param members - */ - public void addSetValue(String key, String...members){ - if(ToolUtil.isNotEmpty(key) && ToolUtil.isNotEmpty(members)){ - Jedis resource = jedisPool.getResource(); - Long sadd = resource.sadd(key, members); - resource.close(); - } - } - /** - * 返回Set集合数据 - * @param key - * @return - */ - public Set<String> getSetAllValue(String key){ - Set<String> smembers = new HashSet<>(); - if(ToolUtil.isNotEmpty(key)){ - Jedis resource = jedisPool.getResource(); - smembers = resource.smembers(key); - resource.close(); - } - return smembers; - } - - /** - * 删除Set集合中的值 - * @param key - * @param members - */ - public void delSetValue(String key, String...members){ - if(ToolUtil.isNotEmpty(key) && ToolUtil.isNotEmpty(members)){ - Jedis resource = jedisPool.getResource(); - Long sadd = resource.srem(key, members); - resource.close(); - } - } - - - /** - * 删除资源 - * @param jedis - */ - public void closeJedis(Jedis jedis){ - if(null != jedis){ - jedis.close(); - } - } /** @@ -188,23 +87,7 @@ public boolean lock(String key, String value, int time){ if(!StringUtils.isEmpty(key)){ key += "_lock"; - Jedis resource = jedisPool.getResource(); - String set = resource.set(key, value, "nx", "ex", time); - if("OK".equals(set)){ - String finalKey = key; - timer = new Timer(); - timer.schedule(new TimerTask() { - @Override - public void run() { - System.err.println("定时任务启动"); - Jedis resource = jedisPool.getResource(); - resource.setex(finalKey, time, value); - resource.close(); - } - }, 1000, 500); - } - resource.close(); - return "OK".equals(set) ? true : false; + return redisTemplate.opsForValue().setIfAbsent(key, value); } return false; } @@ -234,11 +117,7 @@ public boolean unlock(String key){ if(!StringUtils.isEmpty(key)){ key += "_lock"; - Jedis resource = jedisPool.getResource(); - timer.cancel();//取消定时任务 - Long del = resource.del(key); - resource.close(); - return del != 0 ? true : false; + return redisTemplate.delete(key); } return false; } diff --git a/DriverZYTravel/guns-admin/src/main/resources/application.yml b/DriverZYTravel/guns-admin/src/main/resources/application.yml index 8db4c0a..01738fd 100644 --- a/DriverZYTravel/guns-admin/src/main/resources/application.yml +++ b/DriverZYTravel/guns-admin/src/main/resources/application.yml @@ -27,6 +27,12 @@ multipart: max-request-size: 100MB max-file-size: 100MB + redis: + database: 0 + host: 127.0.0.1 + port: 16379 + password: mPMHThYzlT8DWgl8HLqwPEyPOiHDPPB5 + mybatis-plus: typeAliasesPackage: com.stylefeng.guns.modular diff --git a/DriverZYTravel/guns-admin/src/main/resources/redis.properties b/DriverZYTravel/guns-admin/src/main/resources/redis.properties deleted file mode 100644 index 72627aa..0000000 --- a/DriverZYTravel/guns-admin/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 diff --git a/MessagePushTravel/pom.xml b/MessagePushTravel/pom.xml index 3f92f27..9a92890 100644 --- a/MessagePushTravel/pom.xml +++ b/MessagePushTravel/pom.xml @@ -26,11 +26,9 @@ <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </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/MessagePushTravel/src/main/java/com/sinata/push/config/RedisConfig.java b/MessagePushTravel/src/main/java/com/sinata/push/config/RedisConfig.java deleted file mode 100644 index 83807c6..0000000 --- a/MessagePushTravel/src/main/java/com/sinata/push/config/RedisConfig.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.sinata.push.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/MessagePushTravel/src/main/java/com/sinata/push/controller/RedisController.java b/MessagePushTravel/src/main/java/com/sinata/push/controller/RedisController.java index 782296a..f7a3509 100644 --- a/MessagePushTravel/src/main/java/com/sinata/push/controller/RedisController.java +++ b/MessagePushTravel/src/main/java/com/sinata/push/controller/RedisController.java @@ -33,19 +33,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)); - } /** @@ -82,85 +69,4 @@ } - /** - * 添加数据到list集合 - * @param key - * @param s - * @return - */ - @ResponseBody - @PostMapping("/addValueList") - public String addValueList(String key, String value){ - redisUtil.addValueList(key, value); - return JSON.toJSONString(ResultUtil.success()); - } - - - /** - * 集合中获取数据 - * @param key - * @return - */ - @ResponseBody - @PostMapping("/getValueList") - public String getValueList(String key){ - String valueList = redisUtil.getValueList(key); - return JSON.toJSONString(ResultUtil.success(valueList)); - } - - - /** - * 删除集合数据 - * @param key - * @param count - * @param value - * @return - */ - @ResponseBody - @PostMapping("/delValueList") - public String delValueList(String key, long count, String value){ - redisUtil.delValueList(key, count, value); - return JSON.toJSONString(ResultUtil.success()); - } - - - /** - * 获取list集合数量 - * @param key - * @return - */ - @ResponseBody - @PostMapping("/getListCount") - public String getListCount(String key){ - Long length = redisUtil.getListCount(key); - return JSON.toJSONString(ResultUtil.success(length)); - } - - - /** - * 获取list集合数据 - * @param key - * @param start - * @param end - * @return - */ - @ResponseBody - @PostMapping("/getList") - public String getList(String key, Long start, Long end){ - List<String> list = redisUtil.getList(key, start, end); - return JSON.toJSONString(ResultUtil.success(list)); - } - - - /** - * 获取list集合所有数据 - * @param key - * @return - */ - @ResponseBody - @PostMapping("/getAllList") - public String getAllList(String key){ - List<String> list = redisUtil.getAllList(key); - return JSON.toJSONString(ResultUtil.success(list)); - } } diff --git a/MessagePushTravel/src/main/java/com/sinata/push/util/RedisUtil.java b/MessagePushTravel/src/main/java/com/sinata/push/util/RedisUtil.java index 61d39a8..17a8e74 100644 --- a/MessagePushTravel/src/main/java/com/sinata/push/util/RedisUtil.java +++ b/MessagePushTravel/src/main/java/com/sinata/push/util/RedisUtil.java @@ -1,14 +1,15 @@ package com.sinata.push.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 org.springframework.util.StringUtils; import java.io.IOException; import java.util.ArrayList; import java.util.List; +import java.util.UUID; +import java.util.concurrent.TimeUnit; /** @@ -16,25 +17,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(!StringUtils.isEmpty(key) && !StringUtils.isEmpty(value)){ + redisTemplate.opsForValue().set(key, value); } } - - + + /** * 以分钟为单位设置存储值(设置过期时间) * @param key @@ -42,174 +42,94 @@ * @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(!StringUtils.isEmpty(key) && !StringUtils.isEmpty(value)){ + redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS); } } - - + + /** * 从redis中获取值 * @param key * @return */ public String getValue(String key){ - if(StringUtil.isNotEmpty(key)){ - Jedis resource = jedisPool.getResource(); - String data = resource.get(key); - resource.close(); + if(!StringUtils.isEmpty(key)){ + 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; - } - - - /** - * 从右侧获取数据 - * @param key - * @return - */ - public String getValueList(String key){ - if(StringUtil.isNotEmpty(key)){ - Jedis resource = jedisPool.getResource(); - String s = resource.lpop(key); - resource.close(); - return s; - } - return null; - } - - - - - - - /** - * 从左侧添加数据到list中 - * @param key - * @param s - */ - public void addValueList(String key, String s){ - if(StringUtil.isNotEmpty(key)){ - Jedis resource = jedisPool.getResource(); - resource.rpush(key, s); - resource.close(); - } - } - - - - - + + + + /** * 删除key * @param key */ public void remove(String key){ - if(StringUtil.isNotEmpty(key)){ - Jedis resource = jedisPool.getResource(); - Long del = resource.del(key); - resource.close(); + if(!StringUtils.isEmpty(key)){ + redisTemplate.delete(key); } } - - + + + + + + + /** - * 删除list集合数据 + * redis加锁 * @param key - * @param count * @param value + * @param time + * @return */ - public void delValueList(String key, long count, String value){ - if(StringUtil.isNotEmpty(key)){ - Jedis resource = jedisPool.getResource(); - Long lrem = resource.lrem(key, count, value); - resource.close(); + public boolean lock(String key, String value, int time){ + if(!StringUtils.isEmpty(key)){ + key += "_lock"; + return redisTemplate.opsForValue().setIfAbsent(key, value); } + return false; } - - + /** - * 获取list集合数量 + * 获取redis锁 + * @param time + * @return + */ + public boolean lock(int time){ + String uuid = UUID.randomUUID().toString(); + return lock("redis", uuid, time); + } + + + public boolean lock(String key, int time){ + String uuid = UUID.randomUUID().toString(); + return lock(key, uuid, time); + } + + + /** + * redis释放锁 * @param key * @return */ - public Long getListCount(String key){ - if(StringUtil.isNotEmpty(key)){ - Jedis resource = jedisPool.getResource(); - Long length = resource.llen(key); - resource.close(); - return length; + public boolean unlock(String key){ + if(!StringUtils.isEmpty(key)){ + key += "_lock"; + return redisTemplate.delete(key); } - return 0L; + return false; } - - + /** - * 获取集合内容 - * @param key - * @param start - * @param end + * 删除锁 * @return */ - public List<String> getList(String key, long start, long end){ - if(StringUtil.isNotEmpty(key)){ - Jedis resource = jedisPool.getResource(); - List<String> lrange = resource.lrange(key, start, end); - resource.close(); - return lrange; - } - return new ArrayList<>(); - } - - - /** - * 获取list所有数据 - * @param key - * @return - */ - public List<String> getAllList(String key){ - if(StringUtil.isNotEmpty(key)){ - Jedis resource = jedisPool.getResource(); - Long llen = resource.llen(key); - List<String> lrange = resource.lrange(key, 0, llen - 1); - resource.close(); - return lrange; - } - return new ArrayList<>(); + public boolean unlock(){ + return unlock("redis"); } } diff --git a/MessagePushTravel/src/main/resources/application.yml b/MessagePushTravel/src/main/resources/application.yml index 377b1a9..3c78ad5 100644 --- a/MessagePushTravel/src/main/resources/application.yml +++ b/MessagePushTravel/src/main/resources/application.yml @@ -7,6 +7,12 @@ multipart: max-request-size: 100MB max-file-size: 100MB + + redis: + database: 0 + host: 127.0.0.1 + port: 16379 + password: mPMHThYzlT8DWgl8HLqwPEyPOiHDPPB5 eureka: client: diff --git a/MessagePushTravel/src/main/resources/redis.properties b/MessagePushTravel/src/main/resources/redis.properties deleted file mode 100644 index 72627aa..0000000 --- a/MessagePushTravel/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 diff --git a/UserZYTravel/guns-admin/pom.xml b/UserZYTravel/guns-admin/pom.xml index a075460..3db4d24 100644 --- a/UserZYTravel/guns-admin/pom.xml +++ b/UserZYTravel/guns-admin/pom.xml @@ -146,11 +146,10 @@ <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt</artifactId> </dependency> - + <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/UserZYTravel/guns-admin/src/main/java/com/stylefeng/guns/config/RedisConfig.java b/UserZYTravel/guns-admin/src/main/java/com/stylefeng/guns/config/RedisConfig.java deleted file mode 100644 index d459612..0000000 --- a/UserZYTravel/guns-admin/src/main/java/com/stylefeng/guns/config/RedisConfig.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.stylefeng.guns.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/UserZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/service/impl/DriverServiceImpl.java b/UserZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/service/impl/DriverServiceImpl.java index 57ce1a4..123d332 100644 --- a/UserZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/service/impl/DriverServiceImpl.java +++ b/UserZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/service/impl/DriverServiceImpl.java @@ -61,6 +61,8 @@ if(d < (distance * 1000)){ list.add(driver); } + }else{ + System.err.println(driver.getName() + "-----------------没有上传经纬度----------------"); } } return list; diff --git a/UserZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/RedisUtil.java b/UserZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/RedisUtil.java index 82190c1..2e15fec 100644 --- a/UserZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/RedisUtil.java +++ b/UserZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/RedisUtil.java @@ -1,23 +1,12 @@ package com.stylefeng.guns.modular.system.util; -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONObject; import com.stylefeng.guns.core.util.ToolUtil; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.HttpEntity; -import org.springframework.http.HttpHeaders; -import org.springframework.http.MediaType; +import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; -import org.springframework.util.LinkedMultiValueMap; -import org.springframework.util.MultiValueMap; import org.springframework.util.StringUtils; -import org.springframework.web.client.RestTemplate; -import redis.clients.jedis.Jedis; -import redis.clients.jedis.JedisPool; -import redis.clients.jedis.Pipeline; - -import java.io.IOException; import java.util.*; +import java.util.concurrent.TimeUnit; /** @@ -27,9 +16,8 @@ public class RedisUtil { @Autowired - private JedisPool jedisPool; + private RedisTemplate redisTemplate; - private Timer timer; /** @@ -39,9 +27,7 @@ */ public void setStrValue(String key, String value){ if(ToolUtil.isNotEmpty(key) && ToolUtil.isNotEmpty(value)){ - Jedis resource = jedisPool.getResource(); - String set = resource.set(key, value); - closeJedis(resource); + redisTemplate.opsForValue().set(key, value); } } @@ -54,9 +40,7 @@ */ public void setStrValue(String key, String value, int time){ if(ToolUtil.isNotEmpty(key) && ToolUtil.isNotEmpty(value)){ - Jedis resource = jedisPool.getResource(); - String setex = resource.setex(key, time, value); - closeJedis(resource); + redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS); } } @@ -68,46 +52,15 @@ */ public String getValue(String key){ if(ToolUtil.isNotEmpty(key)){ - Jedis resource = jedisPool.getResource(); - String data = resource.get(key); - closeJedis(resource); - return data; + System.err.println("查询缓存:" + key); + Object o = redisTemplate.opsForValue().get(key); + System.err.println(o); + return (String) o; } 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(); - - closeJedis(resource); - 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; - } + /** @@ -116,66 +69,14 @@ */ public void remove(String key){ if(ToolUtil.isNotEmpty(key)){ - Jedis resource = jedisPool.getResource(); - Long del = resource.del(key); - closeJedis(resource); + redisTemplate.delete(key); } } - /** - * 向集合key添加数据 - * @param key - * @param members - */ - public void addSetValue(String key, String...members){ - if(ToolUtil.isNotEmpty(key) && ToolUtil.isNotEmpty(members)){ - Jedis resource = jedisPool.getResource(); - Long sadd = resource.sadd(key, members); - resource.close(); - } - } - /** - * 返回Set集合数据 - * @param key - * @return - */ - public Set<String> getSetAllValue(String key){ - Set<String> smembers = new HashSet<>(); - if(ToolUtil.isNotEmpty(key)){ - Jedis resource = jedisPool.getResource(); - smembers = resource.smembers(key); - resource.close(); - } - return smembers; - } - - /** - * 删除Set集合中的值 - * @param key - * @param members - */ - public void delSetValue(String key, String...members){ - if(ToolUtil.isNotEmpty(key) && ToolUtil.isNotEmpty(members)){ - Jedis resource = jedisPool.getResource(); - Long sadd = resource.srem(key, members); - resource.close(); - } - } - - - /** - * 删除资源 - * @param jedis - */ - public void closeJedis(Jedis jedis){ - if(null != jedis){ - jedis.close(); - } - } /** @@ -188,23 +89,7 @@ public boolean lock(String key, String value, int time){ if(!StringUtils.isEmpty(key)){ key += "_lock"; - Jedis resource = jedisPool.getResource(); - String set = resource.set(key, value, "nx", "ex", time); - if("OK".equals(set)){ - String finalKey = key; - timer = new Timer(); - timer.schedule(new TimerTask() { - @Override - public void run() { - System.err.println("定时任务启动"); - Jedis resource = jedisPool.getResource(); - resource.setex(finalKey, time, value); - resource.close(); - } - }, 1000, 500); - } - resource.close(); - return "OK".equals(set) ? true : false; + return redisTemplate.opsForValue().setIfAbsent(key, value); } return false; } @@ -234,11 +119,7 @@ public boolean unlock(String key){ if(!StringUtils.isEmpty(key)){ key += "_lock"; - Jedis resource = jedisPool.getResource(); - timer.cancel();//取消定时任务 - Long del = resource.del(key); - resource.close(); - return del != 0 ? true : false; + return redisTemplate.delete(key); } return false; } diff --git a/UserZYTravel/guns-admin/src/main/resources/application.yml b/UserZYTravel/guns-admin/src/main/resources/application.yml index a6d8277..02a6398 100644 --- a/UserZYTravel/guns-admin/src/main/resources/application.yml +++ b/UserZYTravel/guns-admin/src/main/resources/application.yml @@ -27,6 +27,11 @@ multipart: max-request-size: 100MB max-file-size: 100MB + redis: + database: 0 + host: 127.0.0.1 + port: 16379 + password: mPMHThYzlT8DWgl8HLqwPEyPOiHDPPB5 mybatis-plus: typeAliasesPackage: com.stylefeng.guns.modular diff --git a/UserZYTravel/guns-admin/src/main/resources/redis.properties b/UserZYTravel/guns-admin/src/main/resources/redis.properties deleted file mode 100644 index 72627aa..0000000 --- a/UserZYTravel/guns-admin/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 diff --git a/ZuulZYTravel/pom.xml b/ZuulZYTravel/pom.xml index a70e075..6911e4e 100644 --- a/ZuulZYTravel/pom.xml +++ b/ZuulZYTravel/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/ZuulZYTravel/src/main/java/com/sinata/zuul/config/RedisConfig.java b/ZuulZYTravel/src/main/java/com/sinata/zuul/config/RedisConfig.java deleted file mode 100644 index cffaeb7..0000000 --- a/ZuulZYTravel/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/ZuulZYTravel/src/main/java/com/sinata/zuul/controller/RedisController.java b/ZuulZYTravel/src/main/java/com/sinata/zuul/controller/RedisController.java index 4866ef5..73ae614 100644 --- a/ZuulZYTravel/src/main/java/com/sinata/zuul/controller/RedisController.java +++ b/ZuulZYTravel/src/main/java/com/sinata/zuul/controller/RedisController.java @@ -33,20 +33,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)); - } - /** * 存值 diff --git a/ZuulZYTravel/src/main/java/com/sinata/zuul/util/RedisUtil.java b/ZuulZYTravel/src/main/java/com/sinata/zuul/util/RedisUtil.java index 2bcbd87..de4c41d 100644 --- a/ZuulZYTravel/src/main/java/com/sinata/zuul/util/RedisUtil.java +++ b/ZuulZYTravel/src/main/java/com/sinata/zuul/util/RedisUtil.java @@ -1,15 +1,11 @@ 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 org.springframework.util.StringUtils; +import java.util.UUID; +import java.util.concurrent.TimeUnit; /** @@ -17,25 +13,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(!StringUtils.isEmpty(key) && !StringUtils.isEmpty(value)){ + redisTemplate.opsForValue().set(key, value); } } - - + + /** * 以分钟为单位设置存储值(设置过期时间) * @param key @@ -43,72 +38,94 @@ * @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(!StringUtils.isEmpty(key) && !StringUtils.isEmpty(value)){ + redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS); } } - - + + /** * 从redis中获取值 * @param key * @return */ public String getValue(String key){ - if(StringUtil.isNotEmpty(key)){ - Jedis resource = jedisPool.getResource(); - String data = resource.get(key); - - resource.close(); + if(!StringUtils.isEmpty(key)){ + 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(); + if(!StringUtils.isEmpty(key)){ + redisTemplate.delete(key); } } + + + + + + + + /** + * redis加锁 + * @param key + * @param value + * @param time + * @return + */ + public boolean lock(String key, String value, int time){ + if(!StringUtils.isEmpty(key)){ + key += "_lock"; + return redisTemplate.opsForValue().setIfAbsent(key, value); + } + return false; + } + + /** + * 获取redis锁 + * @param time + * @return + */ + public boolean lock(int time){ + String uuid = UUID.randomUUID().toString(); + return lock("redis", uuid, time); + } + + + public boolean lock(String key, int time){ + String uuid = UUID.randomUUID().toString(); + return lock(key, uuid, time); + } + + + /** + * redis释放锁 + * @param key + * @return + */ + public boolean unlock(String key){ + if(!StringUtils.isEmpty(key)){ + key += "_lock"; + return redisTemplate.delete(key); + } + return false; + } + + /** + * 删除锁 + * @return + */ + public boolean unlock(){ + return unlock("redis"); + } } diff --git a/ZuulZYTravel/src/main/resources/application.yml b/ZuulZYTravel/src/main/resources/application.yml index fb375e1..f166adf 100644 --- a/ZuulZYTravel/src/main/resources/application.yml +++ b/ZuulZYTravel/src/main/resources/application.yml @@ -3,6 +3,11 @@ spring: application: name: zuul-gateway #服务名称 + redis: + database: 0 + host: 127.0.0.1 + port: 16379 + password: mPMHThYzlT8DWgl8HLqwPEyPOiHDPPB5 eureka: client: diff --git a/ZuulZYTravel/src/main/resources/redis.properties b/ZuulZYTravel/src/main/resources/redis.properties deleted file mode 100644 index 72627aa..0000000 --- a/ZuulZYTravel/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