由于dubbo默认使用的是netty3进行通信的,这里简单的列出一个netty3通信的例子。
一 server端
1 Server
1 package com.hulk.netty.server; 2 ?3 import org.jboss.netty.bootstrap.ServerBootstrap; 4 import org.jboss.netty.channel.*; 5 import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; 6 import org.jboss.netty.handler.codec.string.StringDecoder; 7 import org.jboss.netty.handler.codec.string.StringEncoder; 8 ?9 import java.net.InetSocketAddress;10 import java.util.concurrent.Executors;11 12 public class Server {13 ????public void start(){14 ????????ChannelFactory factory = new NioServerSocketChannelFactory(15 ????????????????Executors.newCachedThreadPool(),//boss线程池16 ????????????????Executors.newCachedThreadPool(),//worker线程池17 ????????????????8//worker线程数18 ????????);19 ????????ServerBootstrap bootstrap = new ServerBootstrap(factory);20 ????????/**21 ?????????* 对于每一个连接channel, server都会调用PipelineFactory为该连接创建一个ChannelPipline22 ?????????*/23 ????????bootstrap.setPipelineFactory(new ChannelPipelineFactory() {24 ????????????public ChannelPipeline getPipeline() throws Exception {25 ????????????????ChannelPipeline pipeline = Channels.pipeline();26 ????????????????pipeline.addLast("decoder", new StringDecoder());27 ????????????????pipeline.addLast("encoder", new StringEncoder());28 ????????????????pipeline.addLast("handler", new ServerLogicHandler());29 ????????????????return pipeline;30 ????????????}31 ????????});32 33 ????????Channel channel = bootstrap.bind(new InetSocketAddress("127.0.0.1", 8080));34 ????????System.out.println("server start success!");35 ????}36 37 ????public static void main(String[] args) throws InterruptedException {38 ????????Server server = new Server();39 ????????server.start();40 ????????Thread.sleep(Integer.MAX_VALUE);41 ????}42 }
步骤:
- 首先创建了NioServerSocketChannelFactory:创建boss线程池,创建worker线程池以及worker线程数。(boss线程数默认为1个)
- 创建ServerBootstrap server端启动辅助类
- 为ServerBootstrap设置ChannelPipelineFactory工厂,并为ChannelPipelineFactory将来创建出的ChannelPipeline设置编码器/解码器/事件处理器
- 使用ServerBootstrap绑定监听地址和端口
2 事件处理器ServerLogicHandler
1 package com.hulk.netty.server; 2 ?3 import org.jboss.netty.channel.Channel; 4 import org.jboss.netty.channel.ChannelHandlerContext; 5 import org.jboss.netty.channel.ChannelStateEvent; 6 import org.jboss.netty.channel.ExceptionEvent; 7 import org.jboss.netty.channel.MessageEvent; 8 import org.jboss.netty.channel.SimpleChannelHandler; 9 10 public class ServerLogicHandler extends SimpleChannelHandler {11 ????@Override12 ????public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {13 ????????System.out.println("连接成功, channel: " + e.getChannel().toString());14 ????}15 16 ????@Override17 ????public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {18 ????????String msg = (String) e.getMessage();19 ????????System.out.println("接收到了client的消息, msg: " + msg);20 21 ????????Channel channel = e.getChannel();22 ????????String str = "hi, client";23 24 ????????channel.write(str);//写消息发给client端25 ????????System.out.println("服务端发送数据: " + str + "完成");26 ????}27 28 ????@Override29 ????public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {30 ????????e.getCause().printStackTrace();31 ????????e.getChannel().close();32 ????}33 }
说明:
- 监听与客户端连接成功事件
- 监听接收到来自客户端的消息,之后写回给客户端消息
- 捕捉异常事件
二 client端
1 Client
1 package com.hulk.netty.client; 2 ?3 import org.jboss.netty.bootstrap.ClientBootstrap; 4 import org.jboss.netty.channel.ChannelFactory; 5 import org.jboss.netty.channel.ChannelPipeline; 6 import org.jboss.netty.channel.ChannelPipelineFactory; 7 import org.jboss.netty.channel.Channels; 8 import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory; 9 import org.jboss.netty.handler.codec.string.StringDecoder;10 import org.jboss.netty.handler.codec.string.StringEncoder;11 12 import java.net.InetSocketAddress;13 import java.util.concurrent.Executors;14 15 public class Client {16 ????public static void main(String[] args) {17 ????????ChannelFactory factory = new NioClientSocketChannelFactory(18 ????????????????Executors.newCachedThreadPool(),19 ????????????????Executors.newCachedThreadPool(),20 ????????????????821 ????????);22 ????????ClientBootstrap bootstrap = new ClientBootstrap(factory);23 ????????bootstrap.setPipelineFactory(new ChannelPipelineFactory() {24 ????????????public ChannelPipeline getPipeline() throws Exception {25 ????????????????ChannelPipeline pipeline = Channels.pipeline();26 ????????????????pipeline.addLast("decoder", new StringDecoder());27 ????????????????pipeline.addLast("encoder", new StringEncoder());28 ????????????????pipeline.addLast("handler", new ClientLogicHandler());29 ????????????????return pipeline;30 ????????????}31 ????????});32 33 ????????bootstrap.connect(new InetSocketAddress("127.0.0.1", 8080));34 ????????System.out.println("client start success!");35 ????}36 }
步骤:(与Server几乎相同)
- 首先创建了NioClientSocketChannelFactory:创建boss线程池,创建worker线程池以及worker线程数。(boss线程数默认为1个)
- 创建ClientBootstrap client端启动辅助类
- 为ClientBootstrap设置ChannelPipelineFactory工厂,并为ChannelPipelineFactory将来创建出的ChannelPipeline设置编码器/解码器/事件处理器
- 使用ClientBootstrap连接Server端监听的地址和端口
2 ClientLogicHandler
1 package com.hulk.netty.client; 2 ?3 import org.jboss.netty.channel.ChannelHandlerContext; 4 import org.jboss.netty.channel.ChannelStateEvent; 5 import org.jboss.netty.channel.ExceptionEvent; 6 import org.jboss.netty.channel.MessageEvent; 7 import org.jboss.netty.channel.SimpleChannelHandler; 8 import org.jboss.netty.channel.WriteCompletionEvent; 9 10 public class ClientLogicHandler extends SimpleChannelHandler {11 ????@Override12 ????public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {13 ????????System.out.println("客户端连接成功!");14 ????????String str = "hi server!";15 ????????e.getChannel().write(str);//异步16 ????}17 18 ????@Override19 ????public void writeComplete(ChannelHandlerContext ctx, WriteCompletionEvent e) throws Exception {20 ????????System.out.println("客户端写消息完成");21 ????}22 23 ????@Override24 ????public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {25 ????????String msg = (String) e.getMessage();26 ????????System.out.println("客户端接收到消息, msg: " + msg);27 ????}28 29 ????@Override30 ????public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {31 ????????e.getCause().printStackTrace();32 ????????e.getChannel().close();33 ????}34 }
说明:
- 监听与服务端连接成功事件,连接成功后,写消息给服务端
- 监听向服务端写消息完成的事件
- 监听接收到来自服务端的消息
- 捕捉异常事件
这就是一个简单的netty3通信的例子,关于netty,后续会读源码。
7.3 ?netty3基本使用
原文地址:http://www.cnblogs.com/java-zhao/p/7625557.html