|
@@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSON;
|
|
|
import com.zhili.zkstationserver.configuration.ServerConfig;
|
|
|
import com.zhili.zkstationserver.dto.ChargerState;
|
|
|
import com.zhili.zkstationserver.dto.ExchangeEvent;
|
|
|
+import com.zhili.zkstationserver.dto.StationConnInfo;
|
|
|
import com.zhili.zkstationserver.dto.StationMessage;
|
|
|
import com.zhili.zkstationserver.dto.StationMessage.CommandType;
|
|
|
import com.zhili.zkstationserver.util.ApplicationContextUtils;
|
|
@@ -13,11 +14,13 @@ import io.netty.channel.ChannelHandlerContext;
|
|
|
import io.netty.channel.ChannelInboundHandlerAdapter;
|
|
|
import io.netty.util.AttributeKey;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.beanutils.BeanUtils;
|
|
|
import org.apache.commons.lang.ArrayUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.kafka.core.KafkaTemplate;
|
|
|
|
|
|
+import java.util.Map;
|
|
|
import java.util.Optional;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
@@ -28,15 +31,22 @@ import java.util.concurrent.TimeUnit;
|
|
|
*/
|
|
|
@Slf4j
|
|
|
public class StationMessageHandler extends ChannelInboundHandlerAdapter {
|
|
|
- KafkaTemplate kafkaTemplate = ApplicationContextUtils.getApplicationContext().getBean(KafkaTemplate.class);
|
|
|
+ KafkaTemplate kafkaTemplate = (KafkaTemplate)ApplicationContextUtils.getApplicationContext().getBean("kafkaTemplate");
|
|
|
ServerConfig serverConfig = ApplicationContextUtils.getApplicationContext().getBean(ServerConfig.class);
|
|
|
- RedisTemplate redisTemplate = ApplicationContextUtils.getApplicationContext().getBean(RedisTemplate.class);
|
|
|
+ RedisTemplate redisTemplate = (RedisTemplate) ApplicationContextUtils.getApplicationContext().getBean("redisTemplate");
|
|
|
+
|
|
|
|
|
|
@Override
|
|
|
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
|
|
- super.channelActive(ctx);
|
|
|
log.info("channel connected: " + ctx.channel().remoteAddress());
|
|
|
StationCenter.add(ctx.channel());
|
|
|
+ super.channelActive(ctx);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
|
|
+ log.info("channel disconnected: " + ctx.channel().remoteAddress());
|
|
|
+ super.channelInactive(ctx);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -51,6 +61,8 @@ public class StationMessageHandler extends ChannelInboundHandlerAdapter {
|
|
|
String stationCode;
|
|
|
StationMessage stationMessageReply;
|
|
|
byte[] replyBytes;
|
|
|
+ String topic;
|
|
|
+ StationConnInfo stationConnInfo;
|
|
|
switch (command) {
|
|
|
case CommandType.SIGN_IN:
|
|
|
//签入命令,将连接打上站的标记, 并计入redis, 代表此站在这台服务器上{"stationConn:st001": "serverId/channelId"}, 12s超时
|
|
@@ -65,7 +77,10 @@ public class StationMessageHandler extends ChannelInboundHandlerAdapter {
|
|
|
replyBytes[8] = (byte) 0;
|
|
|
stationMessageReply.setData(replyBytes);
|
|
|
ctx.channel().writeAndFlush(stationMessageReply);
|
|
|
- redisTemplate.opsForValue().set("stationConn:" + stationCode, serverConfig.getInstanceId()+"/"+StationCenter.getChannelIdStr(ctx.channel()), 12, TimeUnit.SECONDS);
|
|
|
+ stationConnInfo = new StationConnInfo();
|
|
|
+ stationConnInfo.setServerInstanceId(serverConfig.getInstanceId());
|
|
|
+ stationConnInfo.setConnIdStr(StationCenter.getChannelIdStr(ctx.channel()));
|
|
|
+ redisTemplate.opsForValue().set("stationConn:" + stationCode, stationConnInfo, 12, TimeUnit.SECONDS);
|
|
|
break;
|
|
|
case CommandType.HEART_BEAT:
|
|
|
//心跳命令,计入redis,代表此站在这台服务器上{"stationConn:st001": "serverId/channelId"},12s超时
|
|
@@ -73,7 +88,13 @@ public class StationMessageHandler extends ChannelInboundHandlerAdapter {
|
|
|
if (StringUtils.isEmpty(stationCode)) {
|
|
|
break;
|
|
|
}
|
|
|
- redisTemplate.opsForValue().set("stationConn:" + stationCode, serverConfig.getInstanceId()+"/"+StationCenter.getChannelIdStr(ctx.channel()), 12, TimeUnit.SECONDS);
|
|
|
+ stationConnInfo = new StationConnInfo();
|
|
|
+ stationConnInfo.setServerInstanceId(serverConfig.getInstanceId());
|
|
|
+ stationConnInfo.setConnIdStr(StationCenter.getChannelIdStr(ctx.channel()));
|
|
|
+ redisTemplate.opsForValue().set("stationConn:" + stationCode, stationConnInfo, 12, TimeUnit.SECONDS);
|
|
|
+ StationConnInfo stationConnInfo1 = new StationConnInfo();
|
|
|
+ BeanUtils.populate(stationConnInfo1, (Map)redisTemplate.opsForValue().get("stationConn:" + stationCode));
|
|
|
+ log.info("get from redis: {}",stationConnInfo1);
|
|
|
break;
|
|
|
case CommandType.EXCHANGE_EVENT:
|
|
|
stationCode = (String) Optional.ofNullable(ctx.channel().attr(AttributeKey.valueOf("station")).get()).orElse("");
|
|
@@ -84,7 +105,9 @@ public class StationMessageHandler extends ChannelInboundHandlerAdapter {
|
|
|
//为了削峰,将对象构建后,放到kafka中,由业务中心处理。
|
|
|
ExchangeEvent exchangeEvent = new ExchangeEvent(stationCode, data);
|
|
|
log.info("exchangeEvent:" + exchangeEvent);
|
|
|
- kafkaTemplate.send(serverConfig.getServiceName(), JSON.toJSONString(exchangeEvent));
|
|
|
+ topic = serverConfig.getServiceCenterName()+"_ExchangeEvent";
|
|
|
+ log.info("send to {}",topic);
|
|
|
+ kafkaTemplate.send(topic, JSON.toJSONString(exchangeEvent));
|
|
|
break;
|
|
|
case CommandType.CHARGER_STATE:
|
|
|
stationCode = (String) Optional.ofNullable(ctx.channel().attr(AttributeKey.valueOf("station")).get()).orElse("");
|
|
@@ -94,10 +117,16 @@ public class StationMessageHandler extends ChannelInboundHandlerAdapter {
|
|
|
}
|
|
|
ChargerState chargerState = new ChargerState(stationCode, data);
|
|
|
log.info("chargerState:" + chargerState);
|
|
|
- kafkaTemplate.send(serverConfig.getServiceName(), JSON.toJSONString(chargerState));
|
|
|
break;
|
|
|
default:
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
|
|
+ super.exceptionCaught(ctx, cause);
|
|
|
+ cause.printStackTrace();
|
|
|
+ log.error(cause.getMessage());
|
|
|
+ }
|
|
|
}
|