<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
<protobuf.version>3.6.1protobuf.version>
properties>
<dependencies>
<dependency>
<groupId>com.google.protobufgroupId>
<artifactId>protobuf-java-utilartifactId>
<version>${protobuf.version}version>
dependency>
dependencies>
<build>
<defaultGoal>packagedefaultGoal>
<extensions>
<extension>
<groupId>kr.motd.mavengroupId>
<artifactId>os-maven-pluginartifactId>
<version>1.5.0.Finalversion>
extension>
extensions>
<plugins>
<plugin>
<groupId>org.xolstice.maven.pluginsgroupId>
<artifactId>protobuf-maven-pluginartifactId>
<version>0.5.1version>
<extensions>trueextensions>
<configuration>
<protoSourceRoot>${project.basedir}/src/main/java/com/jhj/netty/protoprotoSourceRoot>
<protocArtifact>com.google.protobuf:protoc:3.2.0:exe:${os.detected.classifier}protocArtifact>
configuration>
<executions>
<execution>
<goals>
<goal>compilegoal>
goals>
execution>
executions>
plugin>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<version>3.1version>
<configuration>
<source>${java.version}source>
<target>${java.version}target>
configuration>
plugin>
plugins>
build>
Protobuf
syntax = "proto3";
option java_outer_classname = "StudentPOJO"; //生成的外部类名,同时也是文件名
message Student{
int32 id=1; //Student 类中有 一个属性 名字为id 类型为 int32(proto 类型) 1表示属性序号,不是值
string name=2;
}
ch.pipeline().addLast("encoder",new ProtobufEncoder());//在pieline中加入ProtoBufEncoder
//发送一个student 对象
StudentPOJO.Student jhj =StudentPOJO.Student.newBuilder().setId(101).setName("jhj").build();
ctx.writeAndFlush(jhj);
ch.pipeline().addLast("decoder",new ProtobufDecoder(StudentPOJO.Student.getDefaultInstance()));//加入ProtoBufDecoder
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
//读取从客户端发送的StudentPojo.student
StudentPOJO.Student student = (StudentPOJO.Student) msg;
System.out.println("客户端发送消息是:" + student.getId()+student.getName());
}
syntax = "proto3";
option optimize_for = SPEED; //加快解析
option java_package = "com.jhj.netty.protobuf2"; //指定生成到哪个包下
option java_outer_classname = "MyDataInfo"; //生成的外部类名,同时也是文件名
//protobuf 可以使用message 管理其他的message
message MyMessage{
//定义一个枚举类型
enum DataType{
StudentType=0;//在prop3 要求enum的编号从0开始
WorkerType=1;
}
//用data_type 来标识传的是哪一个枚举类型
DataType data_type = 1;
//标识每次枚举类型最多只能出现其中的一个,节省空间
oneof dataBody{
Student student = 2;
Worker worker = 3;
}
}
message Student{
int32 id=1; //Student 类中有 一个属性 名字为id 类型为 int32(proto 类型) 1表示属性序号,不是值
string name=2;
}
message Worker{
string name=1;
int32 age=2;
}
ch.pipeline().addLast("encoder",new ProtobufEncoder());//在pieline中加入ProtoBufEncoder
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
//随机发送Student 或者 Worker对象
int random = new Random().nextInt(3);
MyDataInfo.MyMessage myMessage=null;
if (0==random){
myMessage=MyDataInfo.MyMessage.newBuilder().setDataType(MyDataInfo.MyMessage.DataType.StudentType).setStudent(MyDataInfo.Student.newBuilder().setId(5).setName("jhj student").build()).build();
}else{
myMessage=MyDataInfo.MyMessage.newBuilder().setDataType(MyDataInfo.MyMessage.DataType.WorkerType).setWorker(MyDataInfo.Worker.newBuilder().setAge(5).setName("jhj").build()).build();
}
ctx.writeAndFlush(myMessage);
}
ch.pipeline().addLast("decoder",new ProtobufDecoder(MyDataInfo.MyMessage.getDefaultInstance()));//加入ProtoBufDecoder
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
//读取从客户端发送的StudentPojo.student
MyDataInfo.MyMessage myMessage = (MyDataInfo.MyMessage) msg;
MyDataInfo.MyMessage.DataType dataType = myMessage.getDataType();
if (dataType== MyDataInfo.MyMessage.DataType.StudentType){
System.out.println("客户端发送消息是:" + myMessage.getStudent().getId()+myMessage.getStudent().getName());
}else if (dataType== MyDataInfo.MyMessage.DataType.WorkerType) {
System.out.println("客户端发送消息是:" + myMessage.getWorker().getAge()+myMessage.getWorker().getName());
}else {
System.out.println("参数不对");
}
}
如有问题,欢迎指正!