ZuulAHTravel/pom.xml | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
ZuulAHTravel/src/main/java/com/sinata/zuul/config/RedisConfig.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
ZuulAHTravel/src/main/java/com/sinata/zuul/config/RedisTemplateConfig.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
ZuulAHTravel/src/main/java/com/sinata/zuul/controller/RedisController.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
ZuulAHTravel/src/main/java/com/sinata/zuul/util/RedisUtil.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
ZuulAHTravel/src/main/java/com/sinata/zuul/util/applets/NettyWebSocketController.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
ZuulAHTravel/src/main/java/com/sinata/zuul/util/echo/NettyServerController.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
ZuulAHTravel/src/main/resources/application.yml | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
ZuulAHTravel/src/main/resources/redis.properties | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 |
ZuulAHTravel/pom.xml
@@ -37,12 +37,16 @@ <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> <!-- <!– jedis –>--> <!-- <dependency>--> <!-- <groupId>redis.clients</groupId>--> <!-- <artifactId>jedis</artifactId>--> <!-- <version>2.9.0</version>--> <!-- </dependency>--> <!--<dependency>--> <!--<groupId>org.springframework.boot</groupId>--> ZuulAHTravel/src/main/java/com/sinata/zuul/config/RedisConfig.java
File was deleted ZuulAHTravel/src/main/java/com/sinata/zuul/config/RedisTemplateConfig.java
New file @@ -0,0 +1,33 @@ package com.sinata.zuul.config; import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.StringRedisSerializer; /** * @author zhibing.pu * @Date 2024/12/18 10:22 */ @Configuration public class RedisTemplateConfig { @Bean @Primary public RedisTemplate<String,Object> redisCacheTemplate(LettuceConnectionFactory redisConnectionFactory){ RedisTemplate<String,Object> template = new RedisTemplate<>(); template.setConnectionFactory(redisConnectionFactory); FastJsonRedisSerializer fastJsonRedisSerializer = new FastJsonRedisSerializer(Object.class); // key 的序列化方式为 string 序列化方式 template.setKeySerializer(new StringRedisSerializer()); template.setHashKeySerializer(new StringRedisSerializer()); template.setValueSerializer(fastJsonRedisSerializer); template.setHashValueSerializer(fastJsonRedisSerializer); template.setDefaultSerializer(fastJsonRedisSerializer); template.afterPropertiesSet(); return template; } } ZuulAHTravel/src/main/java/com/sinata/zuul/controller/RedisController.java
@@ -33,19 +33,7 @@ } /** * 批量获取 * @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)); } /** @@ -89,8 +77,9 @@ */ @ResponseBody @PostMapping("/addListRight") public void addListRight(String key, String value){ public String addListRight(String key, String value){ redisUtil.addListRight(key, value); return JSON.toJSONString(ResultUtil.success()); } @@ -101,8 +90,9 @@ */ @ResponseBody @PostMapping("/addListLeft") public void addListLeft(String key, String value){ public String addListLeft(String key, String value){ redisUtil.addListLeft(key, value); return JSON.toJSONString(ResultUtil.success()); } ZuulAHTravel/src/main/java/com/sinata/zuul/util/RedisUtil.java
@@ -1,14 +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 java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.concurrent.TimeUnit; /** @@ -18,9 +15,8 @@ public class RedisUtil { @Autowired private JedisPool jedisPool; private RedisTemplate redisTemplate; private Jedis resource; /** @@ -30,10 +26,7 @@ */ public void setStrValue(String key, String value){ if(StringUtil.isNotEmpty(key)){ if(null == resource || !resource.isConnected()){ resource = jedisPool.getResource(); } String set = resource.set(key, value); redisTemplate.opsForValue().set(key, value); } } @@ -46,10 +39,7 @@ */ public void setStrValue(String key, String value, int time){ if(StringUtil.isNotEmpty(key)){ if(null == resource || !resource.isConnected()){ resource = jedisPool.getResource(); } String setex = resource.setex(key, time, value); redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS); } } @@ -61,48 +51,14 @@ */ public String getValue(String key){ if(StringUtil.isNotEmpty(key)){ if(null == resource || !resource.isConnected()){ resource = jedisPool.getResource(); } String data = resource.get(key); return data; Object o = redisTemplate.opsForValue().get(key); return null != o ? o.toString() : null; } return null; } /** * 批量获取 * @param kes * @return */ public List<Object> getValues(List<String> kes){ if(null != kes){ if(null == resource || !resource.isConnected()){ resource = jedisPool.getResource(); } Pipeline pipelined = resource.pipelined(); for(String key : kes){ pipelined.get(key); } List<Object> list = pipelined.syncAndReturnAll(); 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; } /** * 添加数据到set集群 @@ -110,10 +66,7 @@ * @param value */ public void addListRight(String key, String value){ if(null == resource || !resource.isConnected()){ resource = jedisPool.getResource(); } resource.rpush(key, value); redisTemplate.opsForList().rightPush(key, value); } /** @@ -122,10 +75,7 @@ * @param value */ public void addListLeft(String key, String value){ if(null == resource || !resource.isConnected()){ resource = jedisPool.getResource(); } resource.lpush(key, value); redisTemplate.opsForList().leftPush(key, value); } @@ -135,11 +85,8 @@ * @return */ public String getListFirstValue(String key){ if(null == resource || !resource.isConnected()){ resource = jedisPool.getResource(); } String lpop = resource.lpop(key); return lpop; Object o = redisTemplate.opsForList().leftPop(key); return null != o ? o.toString() : null; } @@ -149,12 +96,7 @@ * @param key */ public void remove(String key){ if(StringUtil.isNotEmpty(key)){ if(null == resource || !resource.isConnected()){ resource = jedisPool.getResource(); } Long del = resource.del(key); } redisTemplate.delete(key); } ZuulAHTravel/src/main/java/com/sinata/zuul/util/applets/NettyWebSocketController.java
@@ -82,30 +82,30 @@ String userId1 = jsonCon.getString("userId"); if (StringUtil.isNotEmpty(userId1)) { //确保账号在单个设备上登录 if (StringUtil.isNotEmpty(token)) { NettyChannelMap.update_(token.substring(0, 23), ctx);//存储单点登录的通道 String token_ = redisUtil.getValue("USER_Applets_" + userId1);//获取缓存中最新的数据 if (StringUtil.isNotEmpty(token_) && !token.equals(token_)) {//不在同一设备上登录,向其他设备发送数据 JSONObject msg_ = new JSONObject(); msg_.put("code", 200); msg_.put("msg", "SUCCESS"); msg_.put("method", "OFFLINE"); msg_.put("data", new Object()); this.sendMsgToClient(ctx, msg_.toJSONString()); TimerTask timerTask = new TimerTask() { @Override public void run() { NettyChannelMap.remove_(ctx); } }; Timer timer = new Timer(); timer.schedule(timerTask, 3000); timer.cancel(); } if (StringUtil.isEmpty(token_)) {//确保登录的时候存储token失败的情况 redisUtil.setStrValue("USER_Applets_" + userId1, token); } } // if (StringUtil.isNotEmpty(token)) { // NettyChannelMap.update_(token.substring(0, 23), ctx);//存储单点登录的通道 // String token_ = redisUtil.getValue("USER_Applets_" + userId1);//获取缓存中最新的数据 // if (StringUtil.isNotEmpty(token_) && !token.equals(token_)) {//不在同一设备上登录,向其他设备发送数据 // JSONObject msg_ = new JSONObject(); // msg_.put("code", 200); // msg_.put("msg", "SUCCESS"); // msg_.put("method", "OFFLINE"); // msg_.put("data", new Object()); // this.sendMsgToClient(ctx, msg_.toJSONString()); // TimerTask timerTask = new TimerTask() { // @Override // public void run() { // NettyChannelMap.remove_(ctx); // } // }; // Timer timer = new Timer(); // timer.schedule(timerTask, 3000); // timer.cancel(); // } // if (StringUtil.isEmpty(token_)) {//确保登录的时候存储token失败的情况 // redisUtil.setStrValue("USER_Applets_" + userId1, token); // } // } //存储业务使用的通道 if (null != ctx && ctx.channel().isActive()) { ZuulAHTravel/src/main/java/com/sinata/zuul/util/echo/NettyServerController.java
@@ -102,38 +102,38 @@ //判断用户或者司机长连接 if(type==1){ //确保账号在单个设备上登录 if(StringUtil.isNotEmpty(token)){ String token_ = redisUtil.getValue("USER_APP_"+ userId1);//获取缓存中最新的数据 if(StringUtil.isNotEmpty(token_) && !token.equals(token_)){//不在同一设备上登录,向其他设备发送数据 ChannelHandlerContext data_ = NettyChannelMap.getData_(token_.substring(0, 23)); JSONObject msg_ = new JSONObject(); msg_.put("code", 200); msg_.put("msg", "SUCCESS"); msg_.put("method", "OFFLINE"); msg_.put("data", new Object()); this.sendMsgToClient(data_, msg_.toJSONString());//给当前通道发送消息 TimerTask timerTask = new TimerTask() { @Override public void run() { NettyChannelMap.remove_(data_); NettyChannelMap.remove(data_); } }; Timer timer = new Timer(); timer.schedule(timerTask, 3000); timer.cancel(); } NettyChannelMap.update_(token.substring(0, 23), ctx); NettyChannelMap.update("USER" + userId1, ctx); String s = NettyMsg.setMsg(Method.ok, new HashMap<String, Object>()); ctx.writeAndFlush(Unpooled.copiedBuffer((s).getBytes())); if(StringUtil.isEmpty(token_)){//确保登录的时候存储token失败的情况 redisUtil.setStrValue("USER_APP_" + userId1, token); } } // if(StringUtil.isNotEmpty(token)){ // String token_ = redisUtil.getValue("USER_APP_"+ userId1);//获取缓存中最新的数据 // if(StringUtil.isNotEmpty(token_) && !token.equals(token_)){//不在同一设备上登录,向其他设备发送数据 // ChannelHandlerContext data_ = NettyChannelMap.getData_(token_.substring(0, 23)); // // JSONObject msg_ = new JSONObject(); // msg_.put("code", 200); // msg_.put("msg", "SUCCESS"); // msg_.put("method", "OFFLINE"); // msg_.put("data", new Object()); // this.sendMsgToClient(data_, msg_.toJSONString());//给当前通道发送消息 // TimerTask timerTask = new TimerTask() { // @Override // public void run() { // NettyChannelMap.remove_(data_); // NettyChannelMap.remove(data_); // } // }; // Timer timer = new Timer(); // timer.schedule(timerTask, 3000); // timer.cancel(); // } // // NettyChannelMap.update_(token.substring(0, 23), ctx); // NettyChannelMap.update("USER" + userId1, ctx); // String s = NettyMsg.setMsg(Method.ok, new HashMap<String, Object>()); // ctx.writeAndFlush(Unpooled.copiedBuffer((s).getBytes())); // // if(StringUtil.isEmpty(token_)){//确保登录的时候存储token失败的情况 // redisUtil.setStrValue("USER_APP_" + userId1, token); // } // } }else{ //添加司机在线 @@ -156,37 +156,37 @@ if(StringUtil.isNotEmpty(device) && device.equals("carDevice")){ redisUtil.setStrValue("DEVICE_" + userId1, String.valueOf(System.currentTimeMillis())); String token_ = redisUtil.getValue("DRIVER_" + userId1);//缓存中拿最新数据 if(StringUtil.isNotEmpty(token_) && !token_.equals(token)){ ChannelHandlerContext data_ = NettyChannelMap.getData_(token_.substring(0, 23)); //如果是车载端登录,则将其它端都强迫下线 JSONObject msg_ = new JSONObject(); msg_.put("code", 200); msg_.put("msg", "SUCCESS"); msg_.put("method", "OFFLINE"); msg_.put("data", new Object()); this.sendMsgToClient(data_, msg_.toJSONString());//给当前通道发送消息 TimerTask timerTask = new TimerTask() { @Override public void run() { NettyChannelMap.remove_(data_); NettyChannelMap.remove(data_); } }; Timer timer = new Timer(); timer.schedule(timerTask, 3000); timer.cancel(); } NettyChannelMap.update("DRIVER" + userId1, ctx); NettyChannelMap.update_(token.substring(0, 23), ctx); String s = NettyMsg.setMsg(Method.ok, new HashMap<String, Object>()); ctx.writeAndFlush(Unpooled.copiedBuffer((s).getBytes())); if(StringUtil.isEmpty(token_)){//确保登录的时候存储token失败的情况 redisUtil.setStrValue("DRIVER_" + userId1, token); } // String token_ = redisUtil.getValue("DRIVER_" + userId1);//缓存中拿最新数据 // if(StringUtil.isNotEmpty(token_) && !token_.equals(token)){ // ChannelHandlerContext data_ = NettyChannelMap.getData_(token_.substring(0, 23)); // // //如果是车载端登录,则将其它端都强迫下线 // JSONObject msg_ = new JSONObject(); // msg_.put("code", 200); // msg_.put("msg", "SUCCESS"); // msg_.put("method", "OFFLINE"); // msg_.put("data", new Object()); // this.sendMsgToClient(data_, msg_.toJSONString());//给当前通道发送消息 // TimerTask timerTask = new TimerTask() { // @Override // public void run() { // NettyChannelMap.remove_(data_); // NettyChannelMap.remove(data_); // } // }; // Timer timer = new Timer(); // timer.schedule(timerTask, 3000); // timer.cancel(); // } // // NettyChannelMap.update("DRIVER" + userId1, ctx); // NettyChannelMap.update_(token.substring(0, 23), ctx); // String s = NettyMsg.setMsg(Method.ok, new HashMap<String, Object>()); // ctx.writeAndFlush(Unpooled.copiedBuffer((s).getBytes())); // // if(StringUtil.isEmpty(token_)){//确保登录的时候存储token失败的情况 // redisUtil.setStrValue("DRIVER_" + userId1, token); // } } @@ -226,37 +226,37 @@ // } //确保账号在单个设备上登录 String value = redisUtil.getValue("DEVICE_" + userId1); if(StringUtil.isNotEmpty(token) && StringUtil.isEmpty(value)){//APP端登录的操作 String token_ = redisUtil.getValue("DRIVER_" + userId1);//缓存中拿最新数据 if(StringUtil.isNotEmpty(token_) && !token.equals(token_)){//不在同一设备上登录,向当前设备发送数据 JSONObject msg_ = new JSONObject(); msg_.put("code", 200); msg_.put("msg", "SUCCESS"); msg_.put("method", "OFFLINE"); msg_.put("data", new Object()); this.sendMsgToClient(ctx, msg_.toJSONString());//给当前通道发送消息 TimerTask timerTask = new TimerTask() { @Override public void run() { NettyChannelMap.remove_(ctx); NettyChannelMap.remove(ctx); } }; Timer timer = new Timer(); timer.schedule(timerTask, 3000); timer.cancel(); }else{ // System.err.println("开始存储司机通道" + userId1); NettyChannelMap.update("DRIVER" + userId1, ctx); NettyChannelMap.update_(token.substring(0, 23), ctx); String s = NettyMsg.setMsg(Method.ok, new HashMap<String, Object>()); ctx.writeAndFlush(Unpooled.copiedBuffer((s).getBytes())); } if(StringUtil.isEmpty(token_)){//确保登录的时候存储token失败的情况 redisUtil.setStrValue("DRIVER_" + userId1, token); } } // String value = redisUtil.getValue("DEVICE_" + userId1); // if(StringUtil.isNotEmpty(token) && StringUtil.isEmpty(value)){//APP端登录的操作 // String token_ = redisUtil.getValue("DRIVER_" + userId1);//缓存中拿最新数据 // if(StringUtil.isNotEmpty(token_) && !token.equals(token_)){//不在同一设备上登录,向当前设备发送数据 // JSONObject msg_ = new JSONObject(); // msg_.put("code", 200); // msg_.put("msg", "SUCCESS"); // msg_.put("method", "OFFLINE"); // msg_.put("data", new Object()); // this.sendMsgToClient(ctx, msg_.toJSONString());//给当前通道发送消息 // TimerTask timerTask = new TimerTask() { // @Override // public void run() { // NettyChannelMap.remove_(ctx); // NettyChannelMap.remove(ctx); // } // }; // Timer timer = new Timer(); // timer.schedule(timerTask, 3000); // timer.cancel(); // }else{ //// System.err.println("开始存储司机通道" + userId1); // NettyChannelMap.update("DRIVER" + userId1, ctx); // NettyChannelMap.update_(token.substring(0, 23), ctx); // String s = NettyMsg.setMsg(Method.ok, new HashMap<String, Object>()); // ctx.writeAndFlush(Unpooled.copiedBuffer((s).getBytes())); // } // if(StringUtil.isEmpty(token_)){//确保登录的时候存储token失败的情况 // redisUtil.setStrValue("DRIVER_" + userId1, token); // } // } //存储通讯通道 ZuulAHTravel/src/main/resources/application.yml
@@ -3,6 +3,11 @@ spring: application: name: zuul-gateway #服务名称 redis: host: 127.0.0.1 database: 0 port: 6379 password: 123456 eureka: client: @@ -35,4 +40,5 @@ # 配置ribbon超时时间 ribbon: ReadTimeout: 10000 ConnectTimeout: 10000 ConnectTimeout: 10000 ZuulAHTravel/src/main/resources/redis.properties
File was deleted