学习过程中,需要使用proto,这个是谷歌的一个软件,可以起到语言中立,平台中立,替代xml使用,可以跨语言传输数据,直接到github上下载即可
我是github网址
然后如果想下载软件,就是
我是软件下载网址
下载完软件以后解压一下,再配置一下环境变量就可以用了,path里配置完环境以后cmd输入proto --version就知道生没生效了,然后是在项目里添加file文件.proto结尾,下边的 字段的 1,2,3代表的是顺序,不是默认值,是tag,区分用的
syntax = "proto2";
package com.protobuf;
option optimize_for = SPEED;
option java_package = "com.netty.demo6";
option java_outer_classname = "DataInfo";
message Student{
required string name = 1;
optional int32 age = 2;
optional string address = 3;
}
具体的每一个代表什么请自行百度,然后输入
>protoc --java_out=src/main/java src/main/java/com/protobuf/Student.proto
java_out代标生成的.java文件的路径,后边的是源文件路径,需要注意的是java_outer_classname和这个.proto文件名不能类似,不然会报错,然后在项目里引一下jar包
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.11.0</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java-util</artifactId>
<version>3.11.0</version>
</dependency>
package com.protobuf;
import com.google.protobuf.InvalidProtocolBufferException;
public class Test {
public static void main(String[] args) throws InvalidProtocolBufferException {
DataInfo.Student student = DataInfo.Student.newBuilder().setName("喜马拉雅").setAddress("北京天安门").setAge(20).build();
System.out.println(student);
byte[] byteArray = student.toByteArray();
DataInfo.Student studentnew=DataInfo.Student.parseFrom(byteArray);
System.out.println(studentnew);
System.out.println(studentnew.getAge());
System.out.println(studentnew.getAddress());
System.out.println(studentnew.getName());
}
}
如果想知道更详细的资料,最好还是查一下官网文档比较全面
我就粘一下initializer的代码,具体的请看我的码云
package com.netty.demo6protostring;
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;
public class MyClientInitlizer extends ChannelInitializer<SocketChannel>{
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new ProtobufVarint32FrameDecoder());
pipeline.addLast(new ProtobufDecoder(DataInfo.Student.getDefaultInstance()));
pipeline.addLast(new ProtobufVarint32LengthFieldPrepender());
pipeline.addLast(new ProtobufEncoder());
pipeline.addLast(new MyClientHandler());
}
}
package com.netty.demo6protostring;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
public class MyClientHandler extends SimpleChannelInboundHandler<DataInfo.Student> {
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
com.netty.demo6protostring.DataInfo.Student build = DataInfo.Student.newBuilder().setAddress("云顶山").setAge(5)
.setName("孙悟空").build();
ctx.channel().writeAndFlush(build);
}
@Override
protected void channelRead0(ChannelHandlerContext ctx, com.netty.demo6protostring.DataInfo.Student msg) throws Exception {
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.close();
}
}
本文介绍Google的ProtoBuf,一种高效的数据交换格式。讲解如何在项目中配置与使用ProtoBuf,包括下载安装、环境变量设置、语法规范及依赖管理。通过实例演示数据序列化与反序列化过程。

被折叠的 条评论
为什么被折叠?



