标签:protocal java nio netty 二进制
各项目之间通常使用二进制进行通讯,占用带宽小、处理速度快~
感谢netty作者Trustin Lee,让netty天生支持protocol buffer。
本实例使用netty4+protobuf-2.5.0,在win7下执行,并且假设已经安装jdk和maven。
1、下载并解压protoc-2.5.0-win32.zip和protobuf-2.5.0.zip
2、到protobuf-2.5.0.zip安装目录protobuf-2.5.0\java下,执行maven命令:mvn package jar:jar,将生成target\protobuf-java-2.5.0.jar
3、定义proto文件test.proto:
package domain;
option java_package = "com.server.domain";
message TestPro {
required string test = 1;
}
4、将第2部的jar包拷贝到java文件存放目录下,然后运行下面命令,生成java文件:
protoc-2.5.0-win32.zip安装目录\protoc.exe --java_out=java文件存放目录 proto定义文件目录\test.proto
5、将生成的protobuf-java-2.5.0.jar和netty4的jar包放到项目的classpath中,并将第4部生成的java文件放到项目的相应路径下。
6、编写Server端代码
1)、编写handler类:
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
public class ServerHandler extends SimpleChannelInboundHandler<Test.TestPro> {
@Override
public void channelRead0(ChannelHandlerContext ctx, Test.TestPro msg) throws Exception {
System.out.println("server:" + "channelRead:" + msg.getTest());
Test.TestPro.Builder builder = Test.TestPro.newBuilder();
builder.setTest("Received your message !");
ctx.writeAndFlush(builder.build());
}
}
2)、注册服务,绑定端口:
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.protobuf.ProtobufDecoder;
import io.netty.handler.codec.protobuf.ProtobufEncoder;
public class Server {
public static void main(String[] args) {
EventLoopGroup bossEventLoopGroup = new NioEventLoopGroup();
EventLoopGroup workerEventLoopGroup = new NioEventLoopGroup();
try {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossEventLoopGroup, workerEventLoopGroup);
serverBootstrap.channel(NioServerSocketChannel.class);
serverBootstrap.childHandler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
ch.pipeline().addLast("encoder", new ProtobufEncoder());
ch.pipeline().addLast("decoder", new ProtobufDecoder(Test.TestPro.getDefaultInstance()));
ch.pipeline().addLast("handler", new ServerHandler());
};
});
serverBootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);
ChannelFuture channelFuture = serverBootstrap.bind(8888).sync();
channelFuture.channel().closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
} finally {
bossEventLoopGroup.shutdownGracefully();
workerEventLoopGroup.shutdownGracefully();
}
}
}
7、编写Client端代码
1)、定义Client端的handler类:
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
public class ClientHandler extends SimpleChannelInboundHandler<Test.TestPro> {
@Override
protected void channelRead0(ChannelHandlerContext ctx, Test.TestPro msg) throws Exception {
System.out.println("client:" + "channelRead:" + msg.getTest());
}
}
2)、建立Client端到Server端的连接
import java.io.BufferedReader;
import java.io.InputStreamReader;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.protobuf.ProtobufDecoder;
import io.netty.handler.codec.protobuf.ProtobufEncoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
public class Client {
public static void main(String[] args) {
EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(eventLoopGroup);
bootstrap.channel(NioSocketChannel.class);
bootstrap.handler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
ch.pipeline().addLast("encoder", new ProtobufEncoder());
ch.pipeline().addLast("decoder", new ProtobufDecoder(Test.TestPro.getDefaultInstance()));
ch.pipeline().addLast("handler", new ClientHandler());
};
});
Channel ch = bootstrap.connect("127.0.0.1", 8888).sync().channel();
// 控制台输入
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
for (;;) {
String line = in.readLine();
if (line == null || "".equals(line)) {
continue;
}
Test.TestPro.Builder builder = Test.TestPro.newBuilder();
builder.setTest(line);
ch.writeAndFlush(builder.build());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
eventLoopGroup.shutdownGracefully();
}
}
}
入门教程,欢迎拍砖。
推荐其他参考教程:protocol与netty结合的资料http://blog.csdn.net/goldenfish1919/article/details/6719276
标签:protocal java nio netty 二进制
原文地址:http://blog.csdn.net/wodeyuer125/article/details/38817035