Netty模拟dubbo RPC调用看着视频敲的
1.服务接口和实现类
package dubborpc.publicinterface; //一个接口,服务提供方和 服务消费方都需要 public interface DemoService { String hello(String str); }
package dubborpc.provider; import dubborpc.publicinterface.DemoService; public class DemoServiceImpl implements DemoService { private static int count = 0; //当有消费方调用该方法时, 就返回一个结果 @Override public String hello(String mes) { System.out.println("收到客户端消息= " + mes); //根据mes 返回不同的结果 if(mes != null) { return "服务器收到消息 [" + mes + "] 第" + (++count) + " 次"; } else { return "服务器收到消息"; } } }
2.Netty服务器
package dubborpc.netty; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelPipeline; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.handler.codec.string.StringDecoder; import io.netty.handler.codec.string.StringEncoder; public class NettyServer { public static void startServer0(String hostName, int port) { startServer(hostName,port); } //编写一个方法,完成对NettyServer的初始化和启动 private static void startServer(String hostname, int port) { EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap serverBootstrap = new ServerBootstrap(); serverBootstrap.group(bossGroup,workerGroup) .channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast(new StringDecoder()); pipeline.addLast(new StringEncoder()); pipeline.addLast(new NettyServerHandler()); //业务处理器 } } ); ChannelFuture channelFuture = serverBootstrap.bind(hostname, port).sync(); System.out.println("服务提供方开始提供服务~~"); channelFuture.channel().closeFuture().sync(); }catch (Exception e) { e.printStackTrace(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } } }
package dubborpc.netty; import dubborpc.customer.ClientBootstrap; import dubborpc.provider.DemoServiceImpl; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; //服务器handler public class NettyServerHandler extends ChannelInboundHandlerAdapter { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { //获取客户端发送的消息,并调用服务 System.out.println("msg=" + msg); //客户端在调用服务器的api 时,我们需要定义一个协议 //比如我们要求 每次发消息是都必须以某个字符串开头 "$" if(msg.toString().startsWith(ClientBootstrap.providerName)) { String result = new DemoServiceImpl().hello(msg.toString().substring(msg.toString().lastIndexOf("$") + 1)); ctx.writeAndFlush(result); } } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { ctx.close(); } }
3.客户端
package dubborpc.netty; import io.netty.bootstrap.Bootstrap; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelOption; import io.netty.channel.ChannelPipeline; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.handler.codec.string.StringDecoder; import io.netty.handler.codec.string.StringEncoder; import java.lang.reflect.Proxy; import java.util.concurrent.Executor; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class NettyClient { //创建线程池 private static ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); private static NettyClientHandler client; private int count = 0; //编写方法使用代理模式,获取一个代理对象 public Object getBean(final Class<?> serivceClass, final String providerName) { return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class<?>[]{serivceClass}, (proxy, method, args) -> { System.out.println("(proxy, method, args) 进入...." + (++count) + " 次"); //{} 部分的代码,客户端每调用一次 hello, 就会进入到该代码 if (client == null) { initClient(); } //设置要发给服务器端的信息 //providerName 协议头 args[0] 就是客户端调用api hello(???), 参数 client.setPara(providerName + args[0]); // return executor.submit(client).get(); }); } //初始化客户端 private static void initClient() { client = new NettyClientHandler(); //创建EventLoopGroup NioEventLoopGroup group = new NioEventLoopGroup(); Bootstrap bootstrap = new Bootstrap(); bootstrap.group(group) .channel(NioSocketChannel.class) .option(ChannelOption.TCP_NODELAY, true) .handler( new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast(new StringDecoder()); pipeline.addLast(new StringEncoder()); pipeline.addLast(client); } } ); try { bootstrap.connect("127.0.0.1", 7000).sync(); } catch (Exception e) { e.printStackTrace(); } } }
package dubborpc.netty; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import java.util.concurrent.Callable; public class NettyClientHandler extends ChannelInboundHandlerAdapter implements Callable { private ChannelHandlerContext context;//上下文 private String result; //返回的结果 private String para; //客户端调用方法时,传入的参数 //与服务器的连接创建后,就会被调用, // 第一个被调用(1) @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { System.out.println(" channelActive 被调用 "); context = ctx; //因为我们在其它方法会使用到 ctx } //收到服务器的数据后,调用方法 (4) // @Override public synchronized void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { System.out.println(" channelRead 被调用 "); result = msg.toString(); notify(); //唤醒等待的线程 } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { ctx.close(); } //被代理对象调用, 发送数据给服务器,-> wait -> 等待被唤醒(channelRead) -> 返回结果 (3)-》5 @Override public synchronized Object call() throws Exception { System.out.println(" call1 被调用 "); context.writeAndFlush(para); //进行wait wait(); //等待channelRead 方法获取到服务器的结果后,唤醒 System.out.println(" call2 被调用 "); return result; //服务方返回的结果 } //(2) void setPara(String para) { System.out.println(" setPara "); this.para = para; } }
4.服务启动线程和客户启动线程
package dubborpc.provider; import dubborpc.netty.NettyServer; //ServerBootstrap 会启动一个服务提供者,就是 NettyServer public class ServerBootstrap { public static void main(String[] args) { NettyServer.startServer0("127.0.0.1", 7000); } }
package dubborpc.customer; import dubborpc.netty.NettyClient; import dubborpc.publicinterface.DemoService; public class ClientBootstrap { //这里定义协议头 public static final String providerName = "$"; public static void main(String[] args) throws Exception{ //创建一个消费者 NettyClient customer = new NettyClient(); //创建代理对象 DemoService service = (DemoService) customer.getBean(DemoService.class, providerName); for (;; ) { Thread.sleep(2 * 1000); //通过代理对象调用服务提供者的方法(服务) String res = service.hello(" 你好 dubbo~"); System.out.println("调用的结果 res= " + res); } } }

浙公网安备 33010602011771号