一、创建Personproto.proto

创建Personproto.proto文件

syntax = "proto2";

package com.example.protobuf;

option optimize_for = SPEED;
option java_package = "com.example.sixthexample";
option java_outer_classname = "MyDataInfo"; message Person{
required string name = 1;
optional int32 age = 2;
optional string address = 3; }

  

2、重新生成

D:\workspace\study\basic\netty_demo>protoc --java_out=src/main/java  src/protobuf/Person.proto

二、创建Netty服务端代码

1、TestServer 类

public class TestServer {

    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 TestServerInitializer()); ChannelFuture channelFuture = serverBootstrap.bind(8899).sync();
channelFuture.channel().closeFuture().sync();
}finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}

  

2、TestServerHandle类

public class TestServerHandle extends SimpleChannelInboundHandler<MyDataInfo.Person> {

    @Override
protected void channelRead0(ChannelHandlerContext ctx, MyDataInfo.Person msg) throws Exception {
System.out.println("---- 服务端接收到消息 ----");
System.out.println(msg.getName());
System.out.println(msg.getAge());
System.out.println(msg.getAddress());
}
}

  

3、TestServerInitializer 类

public class TestServerInitializer extends ChannelInitializer<SocketChannel>{

    protected void initChannel(SocketChannel socketChannel) throws Exception {
ChannelPipeline pipeline = socketChannel.pipeline();
pipeline.addLast(new ProtobufVarint32FrameDecoder());
pipeline.addLast(new ProtobufDecoder(MyDataInfo.Person.getDefaultInstance()));
pipeline.addLast(new ProtobufVarint32LengthFieldPrepender());
pipeline.addLast(new ProtobufEncoder()); pipeline.addLast(new TestServerHandle());
}
}

  

三、创建客户端代码

1、TestClient 类

public class TestClient {

    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 TestClientInitializer()); ChannelFuture channelFuture = bootstrap.connect("localhost",8899).sync();
channelFuture.channel().closeFuture().sync(); }finally {
eventLoopGroup.shutdownGracefully();
}
}
}

  

2、TestClientHandle 类

public class TestClientHandle extends SimpleChannelInboundHandler<MyDataInfo.Person> {

    // 对于客户端来说,输入来自控制台
@Override
protected void channelRead0(ChannelHandlerContext ctx, MyDataInfo.Person msg) throws Exception { } @Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
//客户端启动后,将消息发送给服务端
MyDataInfo.Person person = MyDataInfo.Person.newBuilder()
.setName("张三").setAge(30).setAddress("上海").build();
ctx.channel().writeAndFlush(person);
}
}

  

3、TestClientInitializer 类

public class TestClientInitializer extends ChannelInitializer<SocketChannel> {

    protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new ProtobufVarint32FrameDecoder());
pipeline.addLast(new ProtobufDecoder(MyDataInfo.Person.getDefaultInstance()));
pipeline.addLast(new ProtobufVarint32LengthFieldPrepender());
pipeline.addLast(new ProtobufEncoder());
pipeline.addLast(new TestClientHandle());
}
}

  

四、测试

1、启动服务端

2、启动客户端

3、服务端输出

Netty集成Protobuf的更多相关文章

  1. Netty学习——Netty和Protobuf的整合(二)

    Netty学习——Netty和Protobuf的整合(二) 这程序是有瑕疵的,解码器那里不通用,耦合性太强,有两个很明显的问题,但是要怎么解决呢?如:再加一个内部类型 Person2,之前的代码就不能 ...

  2. netty 对 protobuf 协议的解码与包装探究(2)

    netty 默认支持protobuf 的封装与解码,如果通信双方都使用netty则没有什么障碍,但如果客户端是其它语言(C#)则需要自己仿写与netty一致的方式(解码+封装),提前是必须很了解net ...

  3. netty 集成 wss 安全链接

    netty集成ssl完整参考指南(含完整源码) 虽然我们在内部rpc通信中使用的是基于认证和报文头加密的方式实现安全性,但是有些时候仍然需要使用SSL加密,可能是因为对接的三方系统需要,也可能是由于o ...

  4. Netty学习——Netty和Protobuf的整合(一)

    Netty学习——Netty和Protobuf的整合 Protobuf作为序列化的工具,将序列化后的数据,通过Netty来进行在网络上的传输 1.将proto文件里的java包的位置修改一下,然后再执 ...

  5. netty和protobuf的使用

    一.什么是protobuf Protobuf是google的开源项目,全称是Google Protocol Buffers,它是一个与语言无关.平台无关.可扩展的结构化数据序列化机制,类似XML,但它 ...

  6. netty集成ssl完整参考指南(含完整源码)

    虽然我们在内部rpc通信中使用的是基于认证和报文头加密的方式实现安全性,但是有些时候仍然需要使用SSL加密,可能是因为对接的三方系统需要,也可能是由于open的考虑.中午特地测了下netty下集成ss ...

  7. cocos2dx 3.x 集成protobuf

    vs2013+cocos2dx 3.13.1 这篇博文是集成Lua版本的protobuf,集成C++版本的过程也可参考. 主要参考博文地址<cocos2dx 3.x C++搭建protobuf环 ...

  8. 【转】cocos2dx 3.x 集成protobuf

    http://www.cnblogs.com/chevin/p/6001872.html vs2013+cocos2dx 3.13.1 这篇博文是集成Lua版本的protobuf,集成C++版本的过程 ...

  9. iOS 集成Protobuf,转换proto文件

    原文地址:http://blog.csdn.net/hyq4412/article/details/54891038 附加Homebrew安装地址:https://brew.sh/index_zh-c ...

随机推荐

  1. linux绝大部分命令集合(自己需要的时候方便查找)

    原网页:https://www.linuxidc.com/Linux/2018-04/151726.htm 系统信息显示命令arch 显示机器的处理器架构(1) uname -m 显示机器的处理器架构 ...

  2. ubuntu18上解决Qt无法输入中文

    系统:Ubuntu 18.04.3 64bit 软件: QtCreator 4.8 安装Ubuntu18.04 时已经安装fcitx框架的搜狗输入法,在此不再安装fcitx及搜狗输入法 只需要安装fc ...

  3. 完美快速解决百度分享不支持HTTPS的问题

    百度分享不支持HTTPS这件事由来已久,我之前向百度分享提交过这个问题,无果!但近期themebetter主题用户咨询的比较多,我们就总结了解决方案. 第一步:下载百度分享必备文件 点此下载stati ...

  4. Jenkins+robotframework单机版简约教程

    迫于某人极渴望学自动化测试,因此写下此简约教程.妈蛋我是个JAVA后端开发啊... 此教程为基于window系统的Jenkins单机版,测试代码无版本控制的精要压缩版本教程,勿喷 前提:通过Jenki ...

  5. Django项目中使用plupload插件实现上传图片功能

    首先下载plupload插件放在static静态文件下面,官方地址:https://www.plupload.com/ 项目根目录下创建media文件夹用来存放上传的图片,配置settings文件,添 ...

  6. 最全 webpak4.0 打包性能优化清单

    最全 webpak4.0 打包性能优化清单 webpack4.0如何进行打包优化? 无非是从两个角度进行优化,其一:优化打包速度,其二:优化打包体积,送你一份打包性能优化清单 1.使用loader的时 ...

  7. Dubbo源码分析:设计总结

    设计原则 1.   多用组合,少用继承 2.   针对接口编程,不针对实现编程 3.   依赖抽象,不要依赖具体实现类. 设计模式 1.   策略设计模式:Dubbo扩展Spring的xml标签解析 ...

  8. docker拷贝宿主与容器中的文件

    从容器里面拷文件到宿主机 语法:docker cp 容器名:要拷贝的文件在容器里面的路径 要拷贝到宿主机的相应路径 例子:容器名为ubuntu,要从容器里面拷贝的文件路为:/usr/local/tom ...

  9. selenium模块及类组织关系

    问题:webdriver子模块中为什么可以直接使用类Chrome.ChromeOptions.Firefox.FirefoxProfile... 在webdriver的__init__.py文件中已经 ...

  10. JavaScript基础03——函数的作用域及变量提升

    1.作用域 作用域,变量在函数内部作用的范围/区域.有函数的地方就有作用域.   2.局部作用域和全局作用域 function fn(){ var a = 1; } console.log(a); / ...