Netty之入门程序编写指南

一、环境搭建

      netty的环境需要JDK1.6版本以上,以及对应的对应netty包,这里我使用的是maven管理项目,所以只需要引入对应的依赖就行了。

<!-- https://mvnrepository.com/artifact/io.netty/netty-all -->
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>5.0.0.Alpha1</version>
        </dependency>

二、netty通信步骤

  1. 创建两个的NIO线程组,一个专门用于网络事件处理(接受客户端的连接),另一个则进行网络通信读写。
  2. 创建一个ServerBootStrap对象,配置netty的一系列对象,例如传出数据的缓存大小等等。
  3. 创建一个实际处理数据的类Channellnitializer,进行初始化的准备工作,比如设置接受传出参数的字符集、格式,已经实际处理数据的接口
  4. 绑定端口,执行同步阻塞方法等待服务端就编写完后了,此简单的四个步骤,我们的服务器端就编写完成了,几十行的代码就可以把他完成的健壮、性能稳定。

三、代码实现

  server.java

package com.bee.springboot.netty.helloworld;


import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * @Description: 服务器端
 * @author: maoqichuan
 * @date 2018/8/23  11:47
 */
public class Server {
	public static void main(String args[]) throws InterruptedException {
		//1 第一个线程组 是用于接收Client端连接的
		EventLoopGroup bossGroup = new NioEventLoopGroup();
		//2 第二个线程组 是用于实际的业务处理操作的
		EventLoopGroup workerGroup = new NioEventLoopGroup();

		//3 创建一个辅助类Bootstrap,就是对我们的Server进行一系列的配置
		ServerBootstrap b = new ServerBootstrap();
		//把俩个工作线程组加入进来
		b.group(bossGroup, workerGroup)
				//我要指定使用NioServerSocketChannel这种类型的通道
				.channel(NioServerSocketChannel.class)
				//一定要使用 childHandler 去绑定具体的 事件处理器
				.childHandler(new ChannelInitializer<SocketChannel>() {
					@Override
					protected void initChannel(SocketChannel sc) throws Exception {
						sc.pipeline().addLast(new ServerHandler());
					}
				});

		//绑定指定的端口 进行监听
		ChannelFuture f = b.bind(8765).sync();

		//Thread.sleep(1000000);
		f.channel().closeFuture().sync();

		bossGroup.shutdownGracefully();
		workerGroup.shutdownGracefully();
	}
}

ServerHandler.java

package com.bee.springboot.netty.helloworld;


import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

/**
 * @Description: 服务端事件处理器
 * @author: maoqichuan
 * @date 2018/8/23  9:46
 */
public class ServerHandler extends ChannelHandlerAdapter{
	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		//do something msg
		ByteBuf buf = (ByteBuf)msg;
		byte[] data = new byte[buf.readableBytes()];
		buf.readBytes(data);
		String request = new String(data, "utf-8");
		System.out.println("Server: " + request);
		//写给客户端
		String response = "我是反馈的信息";
		ctx.writeAndFlush(Unpooled.copiedBuffer("888".getBytes()));
		//.addListener(ChannelFutureListener.CLOSE);
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		cause.printStackTrace();
		ctx.close();
	}
}

Client.java

package com.bee.springboot.netty.helloworld;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;


/**
 * @Description: 客户端
 * @author: maoqichuan
 * @date 2018/8/23  13:52
 */
public class Client {

	public static void main(String args[]) throws InterruptedException {
		EventLoopGroup workgroup = new NioEventLoopGroup();
		Bootstrap b = new Bootstrap();
		b.group(workgroup);
		b.channel(NioSocketChannel.class);
		b.handler(new ChannelInitializer<SocketChannel>() {
			@Override
			protected void initChannel(SocketChannel sc) throws Exception {
				sc.pipeline().addLast(new ClientHandler());
			}
		});

		ChannelFuture cf1 = b.connect("127.0.0.1", 8765).sync();

		//buf
		cf1.channel().writeAndFlush(Unpooled.copiedBuffer("777".getBytes()));

		cf1.channel().closeFuture().sync();
		workgroup.shutdownGracefully();
	}
}

ClientHandler.java

package com.bee.springboot.netty.helloworld;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.ReferenceCountUtil;

/**
 * @Description: 客户端事件处理
 * @author: maoqichuan
 * @date 2018/8/23  13:57
 */
public class ClientHandler extends ChannelHandlerAdapter {
	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		try {
			//do something msg
			ByteBuf buf = (ByteBuf)msg;
			byte[] data = new byte[buf.readableBytes()];
			buf.readBytes(data);
			String request = new String(data, "utf-8");
			System.out.println("Client: " + request);
		} finally {
			ReferenceCountUtil.release(msg);
		}
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		cause.printStackTrace();
		ctx.close();
	}
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值