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出品的一种轻量 & 高效的结构化数据存 ...
随机推荐
- Spring JDBC(二)SimpleJdbcInsert
上一篇写了关于jdbcTemplate的一些基本使用,这一篇来聊聊SimpleJdbcInsert SimpleJdbcInsert是springjdbc提供的一个简化插入操作的类,下面来看一下常用的 ...
- webpack 图片的打包
1. 在img文件夹下随便找一个小一点的图片放进去. 2.修改entry.js require('../css/index.css'); var demo1 = require('../js/demo ...
- JS对象属性命名规则
JS标识符的命名规则,即变量的命名规则: 标识符只能由字母.数字.下划线和'$'组成 数字不可以作为标识符的首字符 对象属性的命名规则 通过[]操作符为对象添加属性时,属性名称可以是任何字符串(包括只 ...
- 阿里云轻量应用服务器Lamp部署php工程踩过的坑
第一次写博客,也不知道写什么,但是想坚持写博客来提升自己,不喜勿喷. 切回正题,使用阿里云的轻量应用服务器Lamp其实非常方便,价格也很便宜,一键购买需要的环境都帮你搭配好了,剩下的就是自己修改一下数 ...
- CSS3学习笔记--media query 响应式布局
语法:@media screen and (min-width: 320px) and (max-width : 479px) media属性后面跟着的是一个 screen 的媒体类型(上面说过的十种 ...
- FSharp 调用 Oracle.ManagedDataAccess.dll
FSharp 调用 Oracle.ManagedDataAccess.dll 1.Oracle.ManagedDataAccess.dll 的下载地址.好像如今必需要注冊才干下载. 即使是 64 位系 ...
- linux进程标识符具体解释1
每一个进程都有一个实际用户标识符和一个实际组标识符,它们永远是启动该进程之用户的用户标识符和组标识符. 进程的有效用户标识符和有效组标识符或许更重要些,它们被用来确定一个用户是否能訪问某个确定的文件. ...
- HTTP请求中怎样选择Get和Post方式
在应用中最经常使用的Http请求无非是get和post,get请求能够获取静态页面,也能够把參数放在URL字串后面,传递给servlet.post与get的不同之处在于post的參数不是放在URL字串 ...
- 在Visual Studio 中开发Office Add-in
作者:陈希章 发表于2017年7月13日 "Talk is cheap, show me the code",我们就用代码来说话吧.这一篇将给大家介绍如何开始Office Add- ...
- Microsoft Graph 桌面应用程序
作者:陈希章 发表于 2017年3月22日 桌面应用程序,在我这篇文章的语境中,我是特指在Windows桌面上面直接运行的.NET应用程序,包括Console Application,WPF Appl ...