netty4+protobuf

 

http://hbxflihua.iteye.com/blog/2231878

 

本实例使用netty4+protobuf-2.5.0,在win7下执行,并且假设已经安装jdk和maven。

1、下载Protobuf的Windows版,网址如下:https://developers.google.com/protocol-buffers/docs/downloads?hl=zh-cn,本示例基于protoc-2.6.1-win32.zip

    2、下载Protobuf Java语言所需的jar包,网址如下:http://repo2.maven.org/maven2/com/google/protobuf/protobuf-java/2.6.1/,本示例基于protobuf-java-2.6.1.jar

下载并解压protoc-2.5.0-win32.zip和protobuf-2.5.0.zip

到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();
}
}
}

 

 

 http://hbxflihua.iteye.com/blog/2231878

 

猜你喜欢

转载自liyonghui160com.iteye.com/blog/2313900