Pu Zhibing
2025-04-18 f17933a034b526eaec27c7bfe6759b1579bc1216
修改bug
6个文件已修改
1个文件已添加
359 ■■■■ 已修改文件
DriverZYTravel/guns-admin/pom.xml 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
DriverZYTravel/guns-admin/src/main/java/com/stylefeng/guns/config/RedisConfig.java 46 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
DriverZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/service/impl/OrderServiceImpl.java 8 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
DriverZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/PushUtil.java 12 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
DriverZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/RedisUtil.java 268 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
DriverZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/TaskUtil.java 16 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
DriverZYTravel/guns-admin/src/main/resources/redis.properties 7 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
DriverZYTravel/guns-admin/pom.xml
@@ -9,7 +9,7 @@
        <relativePath>../pom.xml</relativePath>
    </parent>
    <artifactId>guns-admin</artifactId>
    <artifactId>driver</artifactId>
    <name>guns-admin</name>
    <description>guns 的spring boot版本</description>
DriverZYTravel/guns-admin/src/main/java/com/stylefeng/guns/config/RedisConfig.java
New file
@@ -0,0 +1,46 @@
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;
    }
}
DriverZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/service/impl/OrderServiceImpl.java
@@ -31,6 +31,8 @@
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
/**
@@ -720,11 +722,11 @@
     */
    @Override
    public void reservationOrder() throws Exception {
        //快车
        List<OrderTaxi> list = orderTaxiService.queryMaturity();//获取所有快到期的预约单(出行时间在10分钟内)
        List<OrderPrivateCar> orderPrivateCars = orderPrivateCarService.selectList(new EntityWrapper<OrderPrivateCar>().eq("orderType", 2).eq("state", 2)
                .eq("isDelete", 1).eq("isFrozen", 1).last(" and DATE_SUB(travelTime, INTERVAL 30 MINUTE) <= now()"));
        List<Driver> drivers = new ArrayList<>();
        Set<Integer> ids = new HashSet<>();
        for(OrderTaxi orderTaxi : list){
        for(OrderPrivateCar orderTaxi : orderPrivateCars){
            Driver driver = driverService.selectById(orderTaxi.getDriverId());
            if(driver.getState() == 2 && !ids.contains(driver.getId())){
                driver.setState(3);//修改司机为服务中
DriverZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/PushUtil.java
@@ -100,7 +100,7 @@
        params.add("id", String.valueOf(uid));
        params.add("type", String.valueOf(type));
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(params, headers);
        String s = internalRestTemplate.postForObject("http://zuul-gateway/netty/sendMsgToClient",requestEntity , String.class);
        String s = internalRestTemplate.postForObject("http://message-push/netty/sendMsgToClient",requestEntity , String.class);
        JSONObject jsonObject1 = JSON.parseObject(s, JSONObject.class);
        if(jsonObject1.getIntValue("code") != 200){
            log.error(jsonObject1.getString("msg"));
@@ -134,7 +134,7 @@
        params.add("id", String.valueOf(uid));
        params.add("type", type.toString());
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(params, headers);
        String s = internalRestTemplate.postForObject("http://zuul-gateway/netty/sendMsgToClient",requestEntity , String.class);
        String s = internalRestTemplate.postForObject("http://message-push/netty/sendMsgToClient",requestEntity , String.class);
        JSONObject jsonObject1 = JSON.parseObject(s, JSONObject.class);
        if(jsonObject1.getIntValue("code") != 200){
            log.error(jsonObject1.getString("msg"));
@@ -363,7 +363,7 @@
        params.add("id", jsonObject.getString("id"));
        params.add("type", jsonObject.getString("type"));
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(params, headers);
        String s = internalRestTemplate.postForObject("http://zuul-gateway/netty/sendMsgToClient",requestEntity , String.class);
        String s = internalRestTemplate.postForObject("http://message-push/netty/sendMsgToClient",requestEntity , String.class);
        JSONObject jsonObject1 = JSON.parseObject(s, JSONObject.class);
        if(jsonObject1.getIntValue("code") != 200){
            log.error(jsonObject1.getString("msg"));
@@ -394,7 +394,7 @@
        params.add("id", id.toString());
        params.add("type", type.toString());
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(params, headers);
        String s = internalRestTemplate.postForObject("http://zuul-gateway/netty/sendMsgToClient",requestEntity , String.class);
        String s = internalRestTemplate.postForObject("http://message-push/netty/sendMsgToClient",requestEntity , String.class);
        JSONObject jsonObject1 = JSON.parseObject(s, JSONObject.class);
        if(jsonObject1.getIntValue("code") != 200){
            System.err.println(jsonObject1.getString("msg"));
@@ -433,7 +433,7 @@
        params.add("id", String.valueOf(uid));
        params.add("type", String.valueOf(type));
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(params, headers);
        String s = internalRestTemplate.postForObject("http://zuul-gateway/netty/sendMsgToClient",requestEntity , String.class);
        String s = internalRestTemplate.postForObject("http://message-push/netty/sendMsgToClient",requestEntity , String.class);
        JSONObject jsonObject1 = JSON.parseObject(s, JSONObject.class);
        if(jsonObject1.getIntValue("code") != 200){
            log.error(jsonObject1.getString("msg"));
@@ -470,7 +470,7 @@
        params.add("id", String.valueOf(uid));
        params.add("type", String.valueOf(type));
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(params, headers);
        String s = internalRestTemplate.postForObject("http://zuul-gateway/netty/sendMsgToClient",requestEntity , String.class);
        String s = internalRestTemplate.postForObject("http://message-push/netty/sendMsgToClient",requestEntity , String.class);
        JSONObject jsonObject1 = JSON.parseObject(s, JSONObject.class);
        if(jsonObject1.getIntValue("code") != 200){
            log.error(jsonObject1.getString("msg"));
DriverZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/RedisUtil.java
@@ -10,7 +10,14 @@
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.*;
/**
@@ -18,37 +25,27 @@
 */
@Component
public class RedisUtil {
    @Autowired
    private RestTemplate internalRestTemplate;
    private JedisPool jedisPool;
    private Timer timer;
    /**
     * 向redis中存储字符串没有过期时间
     * @param key
     * @param value
     */
    public void setStrValue(String key, String value){
        if(ToolUtil.isNotEmpty(key)){
            //发送验证码短信
            HttpHeaders headers = new HttpHeaders();
            // 以表单的方式提交
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            //将请求头部和参数合成一个请求
            MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
            params.add("key", key);
            params.add("value", value);
            HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(params, headers);
            String s = internalRestTemplate.postForObject("http://zuul-gateway/redis/setValue_", requestEntity, String.class);
            JSONObject jsonObject = JSON.parseObject(s, JSONObject.class);
            if(jsonObject.getIntValue("code") != 200){
                System.err.println("调用redis出错了");
            }
        if(ToolUtil.isNotEmpty(key) && ToolUtil.isNotEmpty(value)){
            Jedis resource = jedisPool.getResource();
            String set = resource.set(key, value);
            closeJedis(resource);
        }
    }
    /**
     * 以分钟为单位设置存储值(设置过期时间)
     * @param key
@@ -56,26 +53,14 @@
     * @param time 秒
     */
    public void setStrValue(String key, String value, int time){
        if(ToolUtil.isNotEmpty(key)){
            //发送验证码短信
            HttpHeaders headers = new HttpHeaders();
            // 以表单的方式提交
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            //将请求头部和参数合成一个请求
            MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
            params.add("key", key);
            params.add("value", value);
            params.add("time", String.valueOf(time));
            HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, headers);
            String s = internalRestTemplate.postForObject("http://zuul-gateway/redis/setValue", requestEntity, String.class);
            JSONObject jsonObject = JSON.parseObject(s, JSONObject.class);
            if(jsonObject.getIntValue("code") != 200){
                System.err.println("调用redis出错了");
            }
        if(ToolUtil.isNotEmpty(key) && ToolUtil.isNotEmpty(value)){
            Jedis resource = jedisPool.getResource();
            String setex = resource.setex(key, time, value);
            closeJedis(resource);
        }
    }
    /**
     * 从redis中获取值
     * @param key
@@ -83,45 +68,186 @@
     */
    public String getValue(String key){
        if(ToolUtil.isNotEmpty(key)){
            HttpHeaders headers = new HttpHeaders();
            // 以表单的方式提交
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            //将请求头部和参数合成一个请求
            MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
            params.add("key", key);
            HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(params, headers);
            String s = internalRestTemplate.postForObject("http://zuul-gateway/redis/getValue",requestEntity , String.class);
            JSONObject jsonObject = JSON.parseObject(s, JSONObject.class);
            if(jsonObject.getIntValue("code") != 200){
                System.err.println("调用redis出错了");
            }
            return jsonObject.getString("data");
            Jedis resource = jedisPool.getResource();
            String data = resource.get(key);
            closeJedis(resource);
            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;
    }
    /**
     * 删除key
     * @param key
     */
    public String remove(String key){
    public void remove(String key){
        if(ToolUtil.isNotEmpty(key)){
            HttpHeaders headers = new HttpHeaders();
            // 以表单的方式提交
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            //将请求头部和参数合成一个请求
            MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
            params.add("key", key);
            HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(params, headers);
            String s = internalRestTemplate.postForObject("http://zuul-gateway/redis/remove",requestEntity , String.class);
            JSONObject jsonObject = JSON.parseObject(s, JSONObject.class);
            if(jsonObject.getIntValue("code") != 200){
                System.err.println("调用redis出错了");
            }
            return jsonObject.getString("data");
            Jedis resource = jedisPool.getResource();
            Long del = resource.del(key);
            closeJedis(resource);
        }
        return null;
    }
    /**
     * 向集合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();
        }
    }
    /**
     * redis加锁
     * @param key
     * @param value
     * @param time
     * @return
     */
    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 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";
            Jedis resource = jedisPool.getResource();
            timer.cancel();//取消定时任务
            Long del = resource.del(key);
            resource.close();
            return del != 0 ? true : false;
        }
        return false;
    }
    /**
     * 删除锁
     * @return
     */
    public boolean unlock(){
        return unlock("redis");
    }
}
DriverZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/TaskUtil.java
@@ -38,8 +38,6 @@
        try {
            //修改满足活动条件的数据(在线时长,订单量)
            driverService.taskMinute();
            //处于预约单
            orderService.reservationOrder();
//            处理车载端断电后的自动下班
//            driverService.taskOffWork();
//            处理结束订单后30分钟解绑小号功能
@@ -48,6 +46,20 @@
            e.printStackTrace();
        }
    }
    /**
     * 每隔一分钟去处理的定时任务
     */
    @Scheduled(fixedRate = 1000 * 60)
    public void taskMinute2(){
        try {
            //处于预约单
            orderService.reservationOrder();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
DriverZYTravel/guns-admin/src/main/resources/redis.properties
@@ -4,10 +4,11 @@
# Redis·þÎñÆ÷µØÖ·
spring.redis.host=127.0.0.1
# Redis·þÎñÆ÷Á¬½Ó¶Ë¿Ú
spring.redis.port=6379
spring.redis.port=16379
#spring.redis.port=6379
# Redis·þÎñÆ÷Á¬½ÓÃÜÂ루ĬÈÏΪ¿Õ£©
spring.redis.password=123456
#spring.redis.password=
spring.redis.password=mPMHThYzlT8DWgl8HLqwPEyPOiHDPPB5
#spring.redis.password=123456
# Á¬½Ó³Ø×î´óÁ¬½ÓÊý£¨Ê¹ÓøºÖµ±íʾûÓÐÏÞÖÆ£©
spring.redis.jedis.pool.max-active=1024
# Á¬½Ó³Ø×î´ó×èÈûµÈ´ýʱ¼ä£¨Ê¹ÓøºÖµ±íʾûÓÐÏÞÖÆ£©