搭建简单的Netty开发环境
来源:互联网 发布:php小例子 编辑:程序博客网 时间:2024/05/20 15:59
今天准备学学声明显赫的Netty框架,自然要先学会怎么搭建一个简单的Netty开发环境啦。话不多说,下面进入正文。
编辑器方面自然是推荐IntelJ idea了,idea的强大不必多说,百度一下你就知道。哈哈
下面就是准备Netty的jar包了,如果你会maven的话自然是使用maven最为方便了。只需要在pom文件中导入以下几行
<!-- https://mvnrepository.com/artifact/io.netty/netty-all --> <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.16.Final</version> </dependency>
当然啦,不会maven的也不用愁,可以在官网直接下载jar包,点击跳转。并在编辑器中将下载的jar包引入你的lib中,就可以愉快的开始Netty开发了
下面贴一个简单的netty案例
一、 服务端代码
1. EchoServerHandler.java
import io.netty.buffer.ByteBuf;import io.netty.buffer.Unpooled;import io.netty.channel.ChannelFutureListener;import io.netty.channel.ChannelHandler.Sharable;import io.netty.channel.ChannelHandlerContext;import io.netty.channel.ChannelInboundHandlerAdapter;import io.netty.util.CharsetUtil;@Sharablepublic class EchoServerHandler extends ChannelInboundHandlerAdapter{ @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { //将客户端传入的消息转换为Netty的ByteBuf类型 ByteBuf in = (ByteBuf) msg; // 在控制台打印传入的消息 System.out.println( "Server received: " + in.toString(CharsetUtil.UTF_8) ); //将接收到的消息写给发送者,而不冲刷出站消息 ctx.write(in); } @Override public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { // 将未处决消息冲刷到远程节点, 并且关闭该Channel ctx.writeAndFlush(Unpooled.EMPTY_BUFFER) .addListener(ChannelFutureListener.CLOSE); } /** * 异常处理 * @param ctx * @param cause * @throws Exception */ @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { //打印异常栈跟踪 cause.printStackTrace(); // 关闭该Channel ctx.close(); }}2. EchoServer.java
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;import java.net.InetSocketAddress;public class EchoServer { private final static int port = 8080; public static void main(String[] args) { start(); } private static void start() { final EchoServerHandler serverHandler = new EchoServerHandler(); // 创建EventLoopGroup EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); // 创建EventLoopGroup ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup) //指定所使用的NIO传输Channel .channel(NioServerSocketChannel.class) //使用指定的端口设置套接字地址 .localAddress(new InetSocketAddress(port)) // 添加一个EchoServerHandler到Channle的ChannelPipeline .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel socketChannel) throws Exception { //EchoServerHandler被标注为@shareable,所以我们可以总是使用同样的案例 socketChannel.pipeline().addLast(serverHandler); } }); try { // 异步地绑定服务器;调用sync方法阻塞等待直到绑定完成 ChannelFuture f = b.bind().sync(); // 获取Channel的CloseFuture,并且阻塞当前线程直到它完成 f.channel().closeFuture().sync(); } catch (InterruptedException e) { e.printStackTrace(); } finally { // 优雅的关闭EventLoopGroup,释放所有的资源 bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }}
二、 客户端代码
1. EchoClientHandler.java
import io.netty.buffer.ByteBuf;import io.netty.buffer.Unpooled;import io.netty.channel.ChannelHandler.Sharable;import io.netty.channel.ChannelHandlerContext;import io.netty.channel.SimpleChannelInboundHandler;import io.netty.util.CharsetUtil;@Sharablepublic class EchoClientHandler extends SimpleChannelInboundHandler<ByteBuf> { @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { //当被通知Channel是活跃的时候,发送一条消息 ctx.writeAndFlush(Unpooled.copiedBuffer("Netty rocks!", CharsetUtil.UTF_8)); } @Override protected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf) throws Exception { System.out.println( "Client received: " + byteBuf.toString(CharsetUtil.UTF_8) ); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { cause.printStackTrace(); ctx.close(); }}
import io.netty.bootstrap.Bootstrap;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;import java.net.InetSocketAddress;public class EchoClient { private final static String HOST = "localhost"; private final static int PORT = 8080; public static void start() { EventLoopGroup group = new NioEventLoopGroup(); Bootstrap bootstrap = new Bootstrap(); bootstrap.group(group) .channel(NioSocketChannel.class) .remoteAddress(new InetSocketAddress(HOST, PORT)) .handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel socketChannel) throws Exception { socketChannel.pipeline().addLast(new EchoClientHandler()); } }); try { ChannelFuture f = bootstrap.connect().sync(); f.channel().closeFuture().sync(); } catch (InterruptedException e) { e.printStackTrace(); }finally { group.shutdownGracefully(); } } public static void main(String[] args) { start(); }}先后运行EchoServer.java和EchoClient.java.
如果控制台分别打印了
Server received: Netty rocks!和
Client received: Netty rocks!那么恭喜你,你已经可以开始netty的开发了。
阅读全文
0 0
- 搭建简单的Netty开发环境
- 最简单的PHP开发环境搭建
- 简单的搭建安卓开发环境,ionic开发环境
- Netty学习:搭建一个简单的Netty服务
- Netty学习:搭建一个简单的Netty服务
- netty开发环境配置
- 最简单的android开发环境的搭建
- netty源码编译环境搭建
- 一种Windows下搭建QT开发环境的简单方法
- 一步步超简单的Android开发环境搭建
- Android APP开发环境的搭建简单总结
- PhoneGap开发的android项目环境搭建简单流程
- 安卓开发环境最简单的搭建方法
- PhoneGap开发的android项目环境搭建简单流程
- Mac OS搭建简单的Java开发环境
- Meteor开发环境的搭建及简单实用
- 开发环境的搭建
- 开发环境的搭建
- 笔试题小结
- 基于稀疏编码的信号去噪
- Linux下Rsync+sersync实现数据实时同步
- HDU
- 发布--订阅
- 搭建简单的Netty开发环境
- android studio 使用JNI调用C程序
- 项目分栏
- easyui
- 质因数分解
- PHP
- java8的一些应用
- SharePoint REST API
- Python库--numpy学习笔记总结