}
if (byteBuf.readableBytes() < Constant.headLength) {
return;
}
}
byte method = byteBuf.readByte();
//设置方法
protocol.setMethod(MethodEnum.getByRequestCode(method));
int bagLength = byteBuf.readInt();
if (byteBuf.readableBytes() < bagLength - 7) {
// 还原读指针
byteBuf.readerIndex(beginReader);
return;
}
byteBuf.skipBytes(32);
//读取数据
int dataLength = byteBuf.readInt();
byte[] dataByte = new byte[dataLength];
byteBuf.readBytes(dataByte);
String dataString = new String(dataByte, StandardCharsets.UTF_8);
JSONObject data = JSONObject.parseObject(dataString);
if(method == MethodEnum.QUERY_FEE.getRequestCode() || method == MethodEnum.NOTIFY_PAY.getRequestCode()){
JSONArray jsonArray = data.getJSONArray(“data”);
JSONObject json = jsonArray.getJSONObject(0);
String flowNo = json.getString(“inserialno”);
protocol.setFlowNo(flowNo);
}
//设置数据
protocol.setData(data);
list.add(protocol);
}
}
private boolean messageStart(ByteBuf byteBuffer){
if(byteBuffer.readByte() == 0x24){
byteBuffer.markReaderIndex();
return byteBuffer.readByte() == 0x24;
}
return false;
}
}
2.编码器
编码器没什么可说的,按位写入协议数据即可
public class ProtocolEncoder extends MessageToByteEncoder {
ParkingConfig parkingConfig;
public ProtocolEncoder(ParkingConfig config){
this.parkingConfig = config;
}
@Override
protected void encode(ChannelHandlerContext channelHandlerContext, Protocol protocol, ByteBuf out) throws Exception {
String data = protocol.getData().toJSONString();
int textLength = data.getBytes().length;
//包头标识
out.writeBytes(Constant.beginBytes);
//协议指令码
if(protocol.isUp()) {
out.writeByte(protocol.getMethod().getRequestCode());
}else{
out.writeByte(protocol.getMethod().getResponseCode());
}
//包长
out.writeInt(textLength + Constant.headLength);
//授权码
out.writeCharSequence(parkingConfig.getTcpKey(), StandardCharsets.UTF_8);
//文本长度
out.writeInt(textLength);
//文本内容
out.writeCharSequence(data, StandardCharsets.UTF_8);
//校验码
byte[] req = new byte[out.readableBytes()];
out.writeBytes(CrcUtils.setParamCRC(req));
//包尾标识
out.writeByte(0X0D);
out.writeByte(0X0D);
}
}
3.TCP-HTTP
最大的问题是如何将请求和响应对应上,并且将其包装为HTTP
协议,我们知道HTTP
协议是基于请求-响应模型实现的,要在TCP
长连接中实现这一点,就必须要把响应数据从信道中拿出来返回给请求的线程,并且要能和请求对应上。前面说了,三方系统并未提供请求ID,但基于已有的协议数据我们可以手动生成请求ID。首先第三方协议的上行下行协议码是一一对应的,简单来说,查费上行协议码是0x00
,那下行协议码就是0x01
,这个对应关系不会改变,我们定义一个协议枚举,在这个枚举中添加上行,下行协议码,并提供根据协议码获取协议的方法即可。同时停车业务中少不了车牌这个元素,我们在设定同一个车牌只能在一个车厂停车的边界后,可以用协议码 + 车牌来标识请求并且将响应数据匹配到请求数据中。代码如下
public class RequestBlockUtils {
private static ConcurrentHashMap<String, Protocol> responseMap = new ConcurrentHashMap<>();
private static ConcurrentHashMap<String, String> requestMap = new ConcurrentHashMap<>();
private static Lock lock = new ReentrantLock();
public static int queueRemain(){
return requestMap.size();
}
public static void putRequest(Protocol requestProtocol){
String requestUid = getRequestIdByRequest(requestProtocol);
String responseUid = getResponseIdByRequest(requestProtocol);
requestMap.put(requestUid, responseUid);
}
public static void putResponse(Protocol responseProtocol){
String requestId = getRequestIdByResponse(responseProtocol);
lock.lock();
if(requestMap.containsKey(requestId)) {
String responseId = getResponseIdByResponse(responseProtocol);
responseMap.put(responseId, responseProtocol);
}
lock.unlock();
}
public static Protocol pullResponseWithTimeOut(Protocol requestProtocol, int timeOut){
String requestUid = getRequestIdByRequest(requestProtocol);
if(!requestMap.containsKey(requestUid)){
return null;
}
String responseUid = requestMap.get(requestUid);
long startTime = new Date().getTime();
while(true){
if(new Date().getTime() - startTime >= timeOut * 1000 || responseMap.containsKey(responseUid)){
break;
}
}
lock.lock();
if(responseMap.containsKey(responseUid)){
return responseMap.remove(responseUid);
}
requestMap.remove(requestUid);
lock.unlock();
throw new RuntimeException(“获取响应结果超时param:” + requestProtocol.getData().toJSONString());
}
private static String getRequestIdByRequest(Protocol requestProtocol){
return requestProtocol.getFlowNo() + “ M e t h o d Method Method” + requestProtocol.getMethod().getRequestCode();
}
private static String getResponseIdByRequest(Protocol requestProtocol){
return requestProtocol.getFlowNo() + “ M e t h o d Method Method” + requestProtocol.getMethod().getResponseCode();
}
private static String getRequestIdByResponse(Protocol responseProtocol){
return responseProtocol.getFlowNo() + “ M e t h o d Method Method” + responseProtocol.getMethod().getRequestCode();
}
private static String getResponseIdByResponse(Protocol responseProtocol){
return responseProtocol.getFlowNo() + “ M e t h o d Method Method” + responseProtocol.getMethod().getResponseCode();
}
}
Protocol.flowNo
这里放入的是车牌,我们用两个Map
来保存请求,响应关系的映射。在发起请求时,首先根据协议的上行下行协议码+车牌生成请求-响应ID映射并且放入Map同时调用pullResponseWithTimeOut
阻塞一段时间尝试获取相应,当信道中有响应数据时会根据响应数据生成相应ID并将结果放入map
中pullResponseWithTimeOut
方法如果未超时就会返回数据,如果已超时,直接抛出异常,注意这里需要加锁,否则可能会出现超时的响应数据未清除的问题
4.加个重试功能
第三方的这种项目通常是部署在机房的,网络波动是很正常的事情,不可能每次都再去重启一次服务,所以我们这里做一个简单短线自动重连功能
@Component
public class NettyClient {
private static final Logger logger = LoggerFactory.getLogger(NettyClient.class);
@Autowired
private ParkingConfig parkingConfig;
@Autowired
private WjlService wjlService;
@Autowired
private Store store;
private Channel channel;
Bootstrap b;
private boolean start = false;
public void start() throws Exception {
if(start){
throw new RuntimeException(“cannot start client that has started”);
}
start = true;
logger.info(“开始初始化客户端”);
NioEventLoopGroup group = new NioEventLoopGroup();
b = new Bootstrap();
b.group(group) // 注册线程池
.channel(NioSocketChannel.class)
.remoteAddress(new InetSocketAddress(parkingConfig.getTcpIp(), parkingConfig.getTcpPort()))
.handler(new LoggingHandler(LogLevel.INFO))
.option(ChannelOption.SO_KEEPALIVE, true)
.handler(new ChannelInitializer() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new IdleStateHandler(120, 0, 0, TimeUnit.SECONDS));
pipeline.addLast(“decoder”, new ProtocolDecoder());
pipeline.addLast(“encoder”, new ProtocolEncoder(parkingConfig));
pipeline.addLast(new MethodHandler(parkingConfig, wjlService, store));
pipeline.addLast(new ExceptionHandler());
pipeline.addLast(new HeartHandler());
}
总结
大厂面试问深度,小厂面试问广度,如果有同学想进大厂深造一定要有一个方向精通的惊艳到面试官,还要平时遇到问题后思考一下问题的本质,找方法解决是一个方面,看到问题本质是另一个方面。还有大家一定要有目标,我在很久之前就想着以后一定要去大厂,然后默默努力,每天看一些大佬们的文章,总是觉得只有再学深入一点才有机会,所以才有恒心一直学下去。