Netty学习——Netty和Protobuf的整合(二)


这程序是有瑕疵的,解码器那里不通用,耦合性太强,有两个很明显的问题,但是要怎么解决呢?
如:再加一个内部类型 Person2,之前的代码就不能用了。

问题1:客户端和服务器端 分别 这里解码器都不能写死吧

问题2:客户端和服务器端Handler里面的泛型,也都不能写死吧

Stack Overflow , 善用搜索引擎解决此问题

在Stack Overflow上面搜的结果
https://stackoverflow.com/questions/38363160/netty-protobuf-websocket-how-to-convert-binarywebsocketframe-to-protobuf-type
Google:netty如何集成Protobuf的解码器,点开了前几篇帖子看了看
https://www.cnblogs.com/Binhua-Liu/p/5577622.html
里面提供了两种解决方案,挺详细的

方法一:自定义协议,官网提供的有基于Netty的自定义协议的方法
方法二: 通过消息的定义方式去解决问题

方法二详解如下:
1.在外层,就定义一个消息。
2.通过枚举,来决定你要传递的类型是什么

syntax ="proto2";

package com.dawa.protobuf;

option optimize_for = SPEED;
option java_package ="com.dawa.netty.sixthexample";
option java_outer_classname = "MyDataInfo"; message MyMessage{
enum DataType{
PersonType = ;
DogType = ;
CatType =;
} required DataType data_type = ; oneof dataBody{
Person person =;
Dog dog = ;
Cat cat = ;
}
} message Person{
optional string name = ;
optional int32 age = ;
optional string address = ;
} message Dog{
optional string name = ;
optional int32 age = ;
} message Cat{
optional string name = ;
optional string city = ;
}

oneof 关键词
1.共享内存
2.只存在一个

使用protoc 重新生成一下proto文件

然后修改 客户端和服务端的初始化器的类,从以前单一的指定修改成 枚举类型的指定

修改之后的服务器端  Handler和Initializer代码

package com.dawa.netty.sixthexample;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler; /**
* @Title: TestServerHandler
* @Author: 大娃
* @Date: 2019/12/3 10:09
* @Description: handler本身是个泛型,这里的泛型就是取 要处理的类型
*/
public class TestServerHandler extends SimpleChannelInboundHandler<MyDataInfo.MyMessage> { @Override
protected void channelRead0(ChannelHandlerContext ctx, MyDataInfo.MyMessage msg) throws Exception {
MyDataInfo.MyMessage.DataType dataType = msg.getDataType();
if (dataType == MyDataInfo.MyMessage.DataType.PersonType) {
MyDataInfo.Person person = msg.getPerson();
System.out.println(person.getName());
System.out.println(person.getAge());
System.out.println(person.getAddress());
} else if (dataType == MyDataInfo.MyMessage.DataType.DogType) {
MyDataInfo.Dog dog = msg.getDog();
System.out.println(dog.getAge());
System.out.println(dog.getName());
} else {
MyDataInfo.Cat cat = msg.getCat();
System.out.println(cat.getName());
System.out.println(cat.getCity());
}
}
}
package com.dawa.netty.sixthexample;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.protobuf.ProtobufDecoder;
import io.netty.handler.codec.protobuf.ProtobufEncoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender; /**
* @Title: TestServerInitializer
* @Author: 大娃
* @Date: 2019/12/3 10:05
* @Description:
*/
public class TestServerInitializer extends ChannelInitializer<SocketChannel> { @Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
//这里和之前的不一样了,但也就是处理器的不一样
//编解码处理器,protobuf提供的专门的编解码器.4个处理器
pipeline.addLast(new ProtobufVarint32FrameDecoder());
//Decoder是重点,解码器,将字节码转换成想要的数据类型
//参数 messageLite,外层的要转换的类的实例
pipeline.addLast(new ProtobufDecoder(MyDataInfo.MyMessage.getDefaultInstance()));
pipeline.addLast(new ProtobufVarint32LengthFieldPrepender());
pipeline.addLast(new ProtobufEncoder());
pipeline.addLast(new TestServerHandler());
}
}

修改之后的客户端  Handler和Initializer的代码

package com.dawa.netty.sixthexample;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.protobuf.ProtobufDecoder;
import io.netty.handler.codec.protobuf.ProtobufEncoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender; /**
* @Title: TestClientInitializer
* @Author: 大娃
* @Date: 2019/12/3 10:43
* @Description:
*/
public class TestClientInitializer extends ChannelInitializer<SocketChannel> {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
//这里和之前的不一样了,但也就是处理器的不一样
//编解码处理器,protobuf提供的专门的编解码器.4个处理器
pipeline.addLast(new ProtobufVarint32FrameDecoder());
//Decoder是重点,解码器,将字节码转换成想要的数据类型
//参数 messageLite,外层的要转换的类的实例
pipeline.addLast(new ProtobufDecoder(MyDataInfo.MyMessage.getDefaultInstance()));
pipeline.addLast(new ProtobufVarint32LengthFieldPrepender());
pipeline.addLast(new ProtobufEncoder());
//自己的处理器
pipeline.addLast(new TestClientHandler());
}
}
package com.dawa.netty.sixthexample;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler; import java.util.Random; /**
* @Title: TestClientHandler
* @Author: 大娃
* @Date: 2019/12/3 10:44
* @Description:
*/
public class TestClientHandler extends SimpleChannelInboundHandler<MyDataInfo.MyMessage> { @Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
//处于活动状态
int randomInt = new Random().nextInt(3);
MyDataInfo.MyMessage myMessage = null; if (0 == randomInt) {
myMessage = MyDataInfo.MyMessage.newBuilder().
setDataType(MyDataInfo.MyMessage.DataType.PersonType).
setPerson(MyDataInfo.Person.newBuilder().
setName("大娃").
setAge(22).
setAddress("北京").build()).
build();
} else if (1 == randomInt) {
myMessage = MyDataInfo.MyMessage.newBuilder().
setDataType(MyDataInfo.MyMessage.DataType.DogType).
setDog(MyDataInfo.Dog.newBuilder().
setName("一只狗").
setAge(2).build()).
build();
} else {
myMessage = MyDataInfo.MyMessage.newBuilder().
setDataType(MyDataInfo.MyMessage.DataType.CatType).
setCat(MyDataInfo.Cat.newBuilder().
setName("一只猫").setCity("上海").build()).
build();
}
ctx.channel().writeAndFlush(myMessage);
} @Override
protected void channelRead0(ChannelHandlerContext ctx, MyDataInfo.MyMessage msg) throws Exception { }
}

先启动服务器端,再启动客户端,成功获取到客户端发送的消息到服务器端,效果图如下

点了好多下才随机出来的大娃,哈哈

Netty学习——Netty和Protobuf的整合(二)的更多相关文章

  1. Netty学习——Netty和Protobuf的整合(一)

    Netty学习——Netty和Protobuf的整合 Protobuf作为序列化的工具,将序列化后的数据,通过Netty来进行在网络上的传输 1.将proto文件里的java包的位置修改一下,然后再执 ...

  2. netty学习--netty源码中的部分util方法

    io.netty.buffer.AbstractByteBuf#calculateNewCapacity  申请内存空间 private int calculateNewCapacity(int mi ...

  3. Netty学习篇③--整合springboot

    经过前面的netty学习,大概了解了netty各个组件的概念和作用,开始自己瞎鼓捣netty和我们常用的项目的整合(很简单的整合) 项目准备 工具:IDEA2017 jar包导入:maven 项目框架 ...

  4. Netty 学习(二):服务端与客户端通信

    Netty 学习(二):服务端与客户端通信 作者: Grey 原文地址: 博客园:Netty 学习(二):服务端与客户端通信 CSDN:Netty 学习(二):服务端与客户端通信 说明 Netty 中 ...

  5. Netty学习笔记(二) 实现服务端和客户端

    在Netty学习笔记(一) 实现DISCARD服务中,我们使用Netty和Python实现了简单的丢弃DISCARD服务,这篇,我们使用Netty实现服务端和客户端交互的需求. 前置工作 开发环境 J ...

  6. Netty学习(二)使用及执行流程

    Netty简单使用 1.本文先介绍一下 server 的 demo 2.(重点是这个)根据代码跟踪一下 Netty 的一些执行流程 和 事件传递的 pipeline. 首先到官网看一下Netty Se ...

  7. Netty学习二:Java IO与序列化

    1 Java IO 1.1 Java IO 1.1.1 IO IO,即输入(Input)输出(Output)的简写,是描述计算机软硬件对二进制数据的传输.读写等操作的统称. 按照软硬件可分为: 磁盘I ...

  8. MINA、Netty、Twisted一起学(十二):HTTPS

    由于HTTPS协议是由HTTP协议加上SSL/TLS协议组合而成,在阅读本文前可以先阅读一下HTTP服务器和SSL/TLS两篇博文,本文中的代码也是由这两篇博文中的代码组合而成. HTTPS介绍 上一 ...

  9. Netty学习第一节Netty的总体概况

    一.Netty简介 什么是Netty? 1.高性能事件驱动,异步非阻塞的IO加载开源框架. 它是由JBoss提供,用于建立TCP等底层链接.基于Netty可以建立高性能的HTTP服务器,快速开发高性能 ...

随机推荐

  1. CSPS模拟 84

    整场考试就一个字虚 真的啥也不会 T1 80很好打 可是100这鬼畜的数据范围...二分答案? 没做过蚯蚓跪..果然多刷题有好处.. 于是死在80分处 T2 56很好打 可是100这鬼畜....... ...

  2. kettle6.1如何连接mongodb

    . Kettle的结构图如下: 2.介绍各个组件详细情况 表输入:通常是你的sql语句,这个会Kettle基础知识的都会不介绍了 JSON Output如下: MogoDB output如下: 下面这 ...

  3. Spring Boot 2.x监控数据可视化(Actuator + Prometheus + Grafana手把手)

    TIPS 本文基于Spring Boot 2.1.4,理论支持Spring Boot 2.x所有版本 众所周知,Spring Boot有个子项目Spring Boot Actuator,它为应用提供了 ...

  4. 常见Java数据结构&优缺点

      数组   优点:查询快,如果知道索引可以快速地存取   缺点:删除慢,大小固定     有序数组   优点:比无序数组查找快   缺点:删除和插入慢,大小固定   栈   优点:提供后进先出的存取 ...

  5. 在linux用ueditor遇到的问题

    在使用ueditor时,遇到很多问题.最大问题是就是服务器返回出错 最让人头疼的是preview没有任何信息.之前也找过很多的资料,很多资料都没有提到修改controller.php文件中error_ ...

  6. 瞎折腾实录:构建 Armel 版本的 .NET Core 教程和资料资源

    目录 首先我要说明,我失败了~ 我把我的进度和经验放出来,希望能够帮助别人完成编译工作~ 背景:最近接手一个华为某型号的嵌入式设备,需要在上面搭建 .NET Core 环境. 设备是 Armel 架构 ...

  7. Eclipse下载安装并运行第一个Hello world(详细)

    Eclipse下载安装并运行第一个Hello world(详细) 1.下载安装和配置JDK JDK详细的安装教程参考:https://www.cnblogs.com/mxxbc/p/11845150. ...

  8. 一个excel(20M)就能干趴你的poi,你信吗?

    自从上一篇:一个普通类就能干趴你的springboot,你信吗?后,很巧的是这次又发现一个问题,所以有了这篇文章,还是想沿用上篇的”流水帐“的方式查找问题和解决问题.这篇文章主要是因为使用POI导入一 ...

  9. (Codeforce)Correct Solution?

    One cold winter evening Alice and her older brother Bob was sitting at home near the fireplace and g ...

  10. tp5验证码的使用

    <div><img id="verify_img" src="{:captcha_src()}" alt="验证码" on ...