由于dubbo默认使用的是netty3进行通信的,这里简单的列出一个netty3通信的例子。
一 server端
1 Server
package com.hulk.netty.server; import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.*;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import org.jboss.netty.handler.codec.string.StringDecoder;
import org.jboss.netty.handler.codec.string.StringEncoder; import java.net.InetSocketAddress;
import java.util.concurrent.Executors; public class Server {
public void start(){
ChannelFactory factory = new NioServerSocketChannelFactory(
Executors.newCachedThreadPool(),//boss线程池
Executors.newCachedThreadPool(),//worker线程池
8//worker线程数
);
ServerBootstrap bootstrap = new ServerBootstrap(factory);
/**
* 对于每一个连接channel, server都会调用PipelineFactory为该连接创建一个ChannelPipline
*/
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline pipeline = Channels.pipeline();
pipeline.addLast("decoder", new StringDecoder());
pipeline.addLast("encoder", new StringEncoder());
pipeline.addLast("handler", new ServerLogicHandler());
return pipeline;
}
}); Channel channel = bootstrap.bind(new InetSocketAddress("127.0.0.1", 8080));
System.out.println("server start success!");
} public static void main(String[] args) throws InterruptedException {
Server server = new Server();
server.start();
Thread.sleep(Integer.MAX_VALUE);
}
}
步骤:
- 首先创建了NioServerSocketChannelFactory:创建boss线程池,创建worker线程池以及worker线程数。(boss线程数默认为1个)
- 创建ServerBootstrap server端启动辅助类
- 为ServerBootstrap设置ChannelPipelineFactory工厂,并为ChannelPipelineFactory将来创建出的ChannelPipeline设置编码器/解码器/事件处理器
- 使用ServerBootstrap绑定监听地址和端口
2 事件处理器ServerLogicHandler
package com.hulk.netty.server; import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler; public class ServerLogicHandler extends SimpleChannelHandler {
@Override
public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
System.out.println("连接成功, channel: " + e.getChannel().toString());
} @Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
String msg = (String) e.getMessage();
System.out.println("接收到了client的消息, msg: " + msg); Channel channel = e.getChannel();
String str = "hi, client"; channel.write(str);//写消息发给client端
System.out.println("服务端发送数据: " + str + "完成");
} @Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
e.getCause().printStackTrace();
e.getChannel().close();
}
}
说明:
- 监听与客户端连接成功事件
- 监听接收到来自客户端的消息,之后写回给客户端消息
- 捕捉异常事件
二 client端
1 Client
package com.hulk.netty.client; import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
import org.jboss.netty.handler.codec.string.StringDecoder;
import org.jboss.netty.handler.codec.string.StringEncoder; import java.net.InetSocketAddress;
import java.util.concurrent.Executors; public class Client {
public static void main(String[] args) {
ChannelFactory factory = new NioClientSocketChannelFactory(
Executors.newCachedThreadPool(),
Executors.newCachedThreadPool(),
8
);
ClientBootstrap bootstrap = new ClientBootstrap(factory);
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline pipeline = Channels.pipeline();
pipeline.addLast("decoder", new StringDecoder());
pipeline.addLast("encoder", new StringEncoder());
pipeline.addLast("handler", new ClientLogicHandler());
return pipeline;
}
}); bootstrap.connect(new InetSocketAddress("127.0.0.1", 8080));
System.out.println("client start success!");
}
}
步骤:(与Server几乎相同)
- 首先创建了NioClientSocketChannelFactory:创建boss线程池,创建worker线程池以及worker线程数。(boss线程数默认为1个)
- 创建ClientBootstrap client端启动辅助类
- 为ClientBootstrap设置ChannelPipelineFactory工厂,并为ChannelPipelineFactory将来创建出的ChannelPipeline设置编码器/解码器/事件处理器
- 使用ClientBootstrap连接Server端监听的地址和端口
2 ClientLogicHandler
package com.hulk.netty.client; import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler;
import org.jboss.netty.channel.WriteCompletionEvent; public class ClientLogicHandler extends SimpleChannelHandler {
@Override
public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
System.out.println("客户端连接成功!");
String str = "hi server!";
e.getChannel().write(str);//异步
} @Override
public void writeComplete(ChannelHandlerContext ctx, WriteCompletionEvent e) throws Exception {
System.out.println("客户端写消息完成");
} @Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
String msg = (String) e.getMessage();
System.out.println("客户端接收到消息, msg: " + msg);
} @Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
e.getCause().printStackTrace();
e.getChannel().close();
}
}
说明:
- 监听与服务端连接成功事件,连接成功后,写消息给服务端
- 监听向服务端写消息完成的事件
- 监听接收到来自服务端的消息
- 捕捉异常事件
这就是一个简单的netty3通信的例子,关于netty,后续会读源码。