Netty对Protocol Buffer的支持(七)
Netty对Protocol Buffer的支持(七)
一.简介
在上一篇博文中笔者已经介绍了google的Protocol Buffer的使用,那么本文笔者就开始介绍netty对Protocol Buffer的支持。
二.编码的实现
2.1 服务端启动代码
public class ServerTest {
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 ServerChannelInitilizer());
ChannelFuture channelFuture = serverBootstrap.bind(8989).sync();
channelFuture.channel().closeFuture().sync();
}finally{
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
2.2 服务端管道初始化代码
public class ServerChannelInitilizer extends ChannelInitializer<SocketChannel>{
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
/**
* 采用Base 128 Varints进行编码,在消息头上加上32个整数,来标注数据的长度。
*/
pipeline.addLast("protobufVarint32FrameDecoder", new ProtobufVarint32FrameDecoder());
pipeline.addLast("protobufDecoder", new ProtobufDecoder(PersonsBook.AddressBook.getDefaultInstance()));
/**
* 对采用Base 128 Varints进行编码的数据解码
*/
pipeline.addLast("protobufVarint32LengthFieldPrepender", new ProtobufVarint32LengthFieldPrepender());
pipeline.addLast("protobufEncoder", new ProtobufEncoder());
pipeline.addLast("serverHandler", new ServerHandler());
}
}
2.3 服务端Handler处理
public class ServerHandler extends SimpleChannelInboundHandler<AddressBook>{
@Override
protected void channelRead0(ChannelHandlerContext ctx, AddressBook msg) throws Exception {
List<Person> list = msg.getPersonList();
list.forEach(p -> System.out.println(p.getName() + ";;" + p.getId() + ";;" + p.getEmail()));
}
}
2.4 客户端启动程序
public class ClientTest {
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 ClientChannelInitializer());
ChannelFuture channelFuture = bootstrap.connect("localhost", 8989).sync();
channelFuture.channel().closeFuture().sync();
}finally{
eventLoopGroup.shutdownGracefully();
}
}
}
2.5客户端通道初始化
public class ClientChannelInitializer extends ChannelInitializer<SocketChannel>{
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("protobufVarint32FrameDecoder", new ProtobufVarint32FrameDecoder());
pipeline.addLast("protobufDecoder", new ProtobufDecoder(PersonsBook.AddressBook.getDefaultInstance()));
pipeline.addLast("protobufVarint32LengthFieldPrepender", new ProtobufVarint32LengthFieldPrepender());
pipeline.addLast("protobufEncoder", new ProtobufEncoder());
pipeline.addLast("clientHandler", new ClientHandler());
}
}
2.6 客户端Handler处理代码
public class ClientHandler extends SimpleChannelInboundHandler<AddressBook>{
@Override
protected void channelRead0(ChannelHandlerContext ctx, AddressBook msg) throws Exception {
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
AddressBook ab = AddressBook.newBuilder()
.addPerson(Person.newBuilder().setEmail("123@qq.com").setId(23).setName("zhangsan"))
.addPerson(Person.newBuilder().setEmail("789@163.com").setId(45).setName("lisi"))
.build();
ctx.writeAndFlush(ab);
}
}
三.程序运行
先运行服务端启动代码,在运行客户端启动代码。
Netty对Protocol Buffer的支持(七)的更多相关文章
- Netty对Protocol Buffer多协议的支持(八)
Netty对Protocol Buffer多协议的支持(八) 一.背景 在上篇博文中笔者已经用代码演示了如何在netty中使用Protocol Buffer,然而细心的用户可能会发现一个明显的不足之处 ...
- Protocol Buffer技术
转载自http://www.cnblogs.com/stephen-liu74/archive/2013/01/02/2841485.html 该系列Blog的内容主体主要源自于Protocol Bu ...
- Protocol Buffer技术详解(数据编码)
Protocol Buffer技术详解(数据编码) 之前已经发了三篇有关Protocol Buffer的技术博客,其中第一篇介绍了Protocol Buffer的语言规范,而后两篇则分别基于C++和J ...
- Protocol Buffer技术详解(语言规范)
Protocol Buffer技术详解(语言规范) 该系列Blog的内容主体主要源自于Protocol Buffer的官方文档,而代码示例则抽取于当前正在开发的一个公司内部项目的Demo.这样做的目的 ...
- Protocol Buffer基本介绍
转自:http://www.cnblogs.com/stephen-liu74/archive/2013/01/02/2841485.html 该系列Blog的内容主体主要源自于Protocol Bu ...
- Protocol Buffer详解
1.Protocol Buffer 概念 Google Protocol Buffer( 简称 Protobuf) 是 Google 公司内部的混合语言数据标准,目前已经正在使用的有超过 48,162 ...
- Google Protocol Buffer
Google Protocol Buffer(protobuf)是一种高效且格式可扩展的编码结构化数据的方法.和JSON不同,protobuf支持混合二进制数据,它还有先进的和可扩展的模式支持.pro ...
- [翻译]Protocol Buffer 基础: C++
目录 Protocol Buffer Basics: C++ 为什么使用 Protocol Buffers 在哪可以找到示例代码 定义你的协议格式 编译你的 Protocol Buffers Prot ...
- 快来看看Google出品的Protocol Buffer,别只会用Json和XML了
前言 习惯用 Json.XML 数据存储格式的你们,相信大多都没听过Protocol Buffer Protocol Buffer 其实 是 Google出品的一种轻量 & 高效的结构化数据存 ...
随机推荐
- 深入理解Mysql数据库主从延迟
1什么会增加主从延迟? 1 网络不好 2 从库硬件差 3 索引没做好,从库执行慢 4 从库锁等待,多见于myisam 5 主库写频繁,从库单线程执行慢 6 使用row复制,或mix使用行复制 2如何优 ...
- Git基本使用命令(windows)
1. 记住一个名词repository版本库 =======================基本操作======================== git init 在需要的地方建立一个版本库(也 ...
- 查看SQL Server数据读写分离,并设置读写分离
1. 查看读写分离脚本,直接执行以下脚本: select name,is_read_committed_snapshot_on from sys.databases 执行结果列表中,name表示数据库 ...
- Python 开发与接口测试学习笔记
这是我跟着虫师学习中积累下来的学习笔记,写得比较简单,适合想学习Python开发与接口测试的初学者学习. 一.开发投票系统 1.参考官网文档,创建投票系统. https://docs.djangopr ...
- NNSZ OIers' Blog Archive
HWL:ssttkkl ,已经搬家到这个地址 NINGLONG:NINGLONG 愤鸟先飞: FNXF Syhien:13355936 ,已经搬家到这个地址 IDE:ThetaS TFW:TFX‘s ...
- Python笔记·第六章——集合 (set) 的增删改查及 copy()方法
简介: 集合是无序的,不重复的数据集合,它里面的元素是可哈希的(不可变类型),但是集合本身是不可哈希(所以集合做不了字典的键)的.以下是集合最重要的两点: 1.去重,把一个列表变成集合,就自动去重了. ...
- 运行期以索引获取tuple元素-C++14(原创)
在编译期很容易根据索引来获取对应位置的元素,因为 tuple 的帮助函数 std::get<N>(tp) 就能获取 tuple 中第 N 个元素.然而我们却不能直接在运行期通过变量来获取 ...
- db2 调整连接数的优化
The Version 9.5 default for the max_coordagents and max_connections parameters will be AUTOMATIC, wi ...
- oracle 10g下范围分区扫描的几种方式
oracle 10g下有几种扫描方式,注意最后一种扫描方式,当对分区的列进行计算时,会不走分区.这跟对索引列进行计算会导致无法用索引一样. --扫描单个分区 PARTITION RANGE SING ...
- 【leetcode】123. Best Time to Buy and Sell Stock III
@requires_authorization @author johnsondu @create_time 2015.7.22 19:04 @url [Best Time to Buy and Se ...