我的netty之旅(2)(ProtoBuf)

学习过程中,需要使用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();
	}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值