上一篇只有Person的message,如果多了一个message,如Dog,这样就会有问题。

解决方法: 定义多协议

一、定义proto文件

syntax = "proto2";

package com.example.protobuf;

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

  

然后用命令生成

D:\workspace\study\basic\netty_demo>protoc --java_out=src/main/java  src/protobuf/Person2.proto

二、客户端代码

1、TestClient 类,和上一篇一样

public class TestClient {

    public static void main(String[] args) throws  Exception{
EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(eventLoopGroup).channel(NioSocketChannel.class)
.handler(new TestClientInitializer()); ChannelFuture channelFuture = bootstrap.connect("localhost",8899).sync();
channelFuture.channel().closeFuture().sync(); }finally {
eventLoopGroup.shutdownGracefully();
}
}
}

  

2、TestClientHandle 类

public class TestClientHandle extends SimpleChannelInboundHandler<MyDataInfo.Person> {

    // 对于客户端来说,输入来自控制台
@Override
protected void channelRead0(ChannelHandlerContext ctx, MyDataInfo.Person msg) throws Exception { } @Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
//客户端启动后,将消息发送给服务端
int randomInt = new Random().nextInt(3);
MyDataInfo.MyMessage myMessage = null;
if(0 == randomInt){
MyDataInfo.Person person = MyDataInfo.Person.newBuilder()
.setName("张三").setAge(30).setAddress("上海").build(); myMessage = MyDataInfo.MyMessage.newBuilder()
.setDataType(MyDataInfo.MyMessage.DataType.PersonType)
.setPerson(person).build(); }else if(1 == randomInt){
MyDataInfo.Dog dog = MyDataInfo.Dog.newBuilder()
.setName("一只狗").setAge(10).build(); myMessage = MyDataInfo.MyMessage.newBuilder()
.setDataType(MyDataInfo.MyMessage.DataType.DogType)
.setDog(dog).build();
}else{
MyDataInfo.Cat dog = MyDataInfo.Cat.newBuilder()
.setName("一只猫").setCity("杭州").build(); myMessage = MyDataInfo.MyMessage.newBuilder()
.setDataType(MyDataInfo.MyMessage.DataType.CatType)
.setCat(dog).build();
} ctx.channel().writeAndFlush(myMessage); }
}

  

3、TestClientInitializer 改变的地方,如下图红色部分

三、服务端

1、TestServer  和上一篇一样

public class TestServer {

    public static void main(String[] args) throws  Exception{
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try{ ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup,workerGroup).channel(NioServerSocketChannel.class)
.handler(new LoggingHandler(LogLevel.INFO)) //增加日志处理器
.childHandler(new TestServerInitializer()); ChannelFuture channelFuture = serverBootstrap.bind(8899).sync();
channelFuture.channel().closeFuture().sync();
}finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}

  

2、TestServerHandle 类

public class TestServerHandle extends SimpleChannelInboundHandler<MyDataInfo.MyMessage> {

    @Override
protected void channelRead0(ChannelHandlerContext ctx, MyDataInfo.MyMessage msg) throws Exception {
System.out.println("---- 服务端接收到消息 ----");
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.getName());
System.out.println(dog.getAge()); }else {
MyDataInfo.Cat cat = msg.getCat();
System.out.println(cat.getName());
System.out.println(cat.getCity()); } }
}

  

3、TestServerInitializer   改变的地方,如下图红色部分

四、测试

1、启动服务端

2、启动多个客户端

3、服务端输出

Protobuf多协议的更多相关文章

  1. netty中使用protobuf实现多协议的消息

    在我们使用 netty 的过程中,有时候为了高效的传输数据,经常使用 protobuf 进行数据的传输,netty默认情况下为我们实现的 protobuf 的编解码,但是默认的只能实现单个对象的编解码 ...

  2. Netty对Protocol Buffer多协议的支持(八)

    Netty对Protocol Buffer多协议的支持(八) 一.背景 在上篇博文中笔者已经用代码演示了如何在netty中使用Protocol Buffer,然而细心的用户可能会发现一个明显的不足之处 ...

  3. python通过protobuf实现rpc

    由于项目组现在用的rpc是基于google protobuf rpc协议实现的,所以花了点时间了解下protobuf rpc.rpc对于做分布式系统的人来说肯定不陌生,对于rpc不了解的童鞋可以自行g ...

  4. Protobuf使用规范分享

    一.Protobuf 的优点 Protobuf 有如 XML,不过它更小.更快.也更简单.它以高效的二进制方式存储,比 XML 小 3 到 10 倍,快 20 到 100 倍.你可以定义自己的数据结构 ...

  5. java netty socket库和自定义C#socket库利用protobuf进行通信完整实例

    之前的文章讲述了socket通信的一些基本知识,已经本人自定义的C#版本的socket.和java netty 库的二次封装,但是没有真正的发表测试用例. 本文只是为了讲解利用protobuf 进行C ...

  6. 在Wcf中应用ProtoBuf替代默认的序列化器

    Google的ProtoBuf序列化器性能的牛逼已经有目共睹了,可以把它应用到Socket通讯,队列,Wcf中,身为dotnet程序员一边期待着不久后Grpc对dotnet core的支持更期待着Wc ...

  7. protobuf的编译安装

    github地址:https://github.com/google/protobuf支持多种语言,有多个语言的版本,本文采用的是在centos7下编译源码进行安装. github上有详细的安装说明: ...

  8. 编译protobuf的jar文件

    1.准备工作 需要到github上下载相应的文件,地址https://github.com/google/protobuf/releases protobuf有很多不同语言的版本,因为我们需要的是ja ...

  9. protobuf学习(2)-相关学习资料

    protobuf官方git地址 protobuf官方英文文档   (你懂的需要FQ) protobuf中文翻译文档 protobuf概述          (官方翻译 推荐阅读) protobuf入门 ...

随机推荐

  1. 从零搭建consul

    从零搭建consul 原文链接:https://blog.csdn.net/weixin_42107541/article/details/87640807#2linux_25 从零搭建consul1 ...

  2. Gtest:参数化

    转自:玩转Google开源C++单元测试框架Google Test系列(gtest)之四 - 参数化 一.前言 在设计测试案例时,经常需要考虑给被测函数传入不同的值的情况.我们之前的做法通常是写一个通 ...

  3. linux /bin/bash^M: bad interpreter的解决办法

    linux下执行shell脚本时报错:-bash: ./a.sh: /bin/bash^M: bad interpreter: No such file or directory. 原因是window ...

  4. httpd的压力测试工具-ab工具使用案例

    httpd的压力测试工具-ab工具使用案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.   一.httpd自带的工具程序 事实上,在我们安装完Apache之后,它默认就会给我们安 ...

  5. Kali下的内网劫持(二)

    前一小节说到在Kali下可以进行捕获客户端的图片,那么同样的,客户端访问的网页也是可以捕获的: . 以上可以看出监听完毕,那么在被监听端访问阿里云的一个网站: 在监听端用urlsnart这个工具进行捕 ...

  6. not syncing: Attempted to kill init

    这个是selinux造成的原因. 解决方法:  键系统启动的时候,按下‘e’键进入grub编辑界面,编辑grub菜单,选择“kernel /vmlinuz-2.6.23.1-42.fc8 ro roo ...

  7. Codeforces F. Vus the Cossack and Numbers(贪心)

    题目描述: D. Vus the Cossack and Numbers Vus the Cossack has nn real numbers aiai. It is known that the ...

  8. unicode转换为中文

    unicode转换为中文 \u5f53\u5730\u65f6\u95f42019\u5e747\u670813\u65e5\uff0c\u82f1\u56fd\u8d1d\u5fb7\u798f\u ...

  9. sort()函数中的key

    d = { , , } #for k in d.items(): # print(k) content = list(d.items()) print(content) content.sort(ke ...

  10. OLED液晶屏幕(4)串口读取文字并分割,液晶屏幕显示

    ESP8266-07 0.93存 液晶屏 128*64  驱动芯片 ssd1306 接线 VCC-5v GND-GND SCL-D1(SCL) SDA-D2(SDA) 安装两个库 #include & ...