Netty实战-如何在web项目中对接第三方tcp协议(1)

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并将结果放入mappullResponseWithTimeOut方法如果未超时就会返回数据,如果已超时,直接抛出异常,注意这里需要加锁,否则可能会出现超时的响应数据未清除的问题

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));

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。

因此收集整理了一份《2024年Web前端开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
[外链图片转存中…(img-BKgFmcRZ-1714941096215)]

[外链图片转存中…(img-RGz11F5e-1714941096215)]

[外链图片转存中…(img-w4btR72x-1714941096216)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

  • 23
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值