Netty框架的理解和简单使用

Netty是什么

Netty是一个高性能的异步的,基于事件驱动的NIO框架,它是JBOSS提供的一个开源框架,用以快速开发高性能,高可靠性的网络服务器和客户端程序。

netty的架构

Netty官网

https://netty.io/index.html 这里可以找到jar包或者maven依赖

类似框架

Apache 的 Mina

java和netty

 Java使用netty,建议jdk版本为1.5以后的,这是netty官网摘录的

 开始-------------

我使用maven构建的项目,所以就使用maven依赖,也可以普通项目导入jar。

<dependency>
    <groupId>io.netty</groupId>
    <artifactId>netty</artifactId> <!-- Use 'netty-all' for 4.0 or above -->
    <version>X.Y.Z.Q</version>
    <scope>compile</scope>
  </dependency>
在maven项目的pom.xml文件中添加即可

 创建一个 DiscardServer 类

代码如下

package com.demo.netty;

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.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;

/**
* @ Author : yjp
* @ Date : 2019/5/7 20:56
* @ Version : 1.0
* @ Desciption :
*/
public class DiscardServer {

private int port;

public DiscardServer(int port) {
this.port = port;
}

public void run(){

ServerBootstrap bootstrap = new ServerBootstrap();

EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup(8);

//设置管道
bootstrap.channel(NioServerSocketChannel.class);

//处理pipeline
bootstrap.group(bossGroup, workerGroup);

bootstrap.childHandler(new ChannelInitializer<NioSocketChannel>() {

@Override
protected void initChannel(NioSocketChannel ch) throws Exception {
ch.pipeline().addLast("http-decoder", new HttpRequestDecoder());//设置http报文解码
ch.pipeline().addLast("http-response-encoder", new HttpResponseEncoder());//设置http报文编码
ch.pipeline().addLast("http-servlet", new DiscardServerHandler());
}

});

try {
ChannelFuture sync = bootstrap.bind(port).sync();
System.out.println("服务启动----------------------");
sync.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}


}


public static void main(String[] args) {//启动测试

new DiscardServer(8080).run();
}
}

业务处理类 DiscardServerHandler
package com.demo.netty;

import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;

/**
* @ Author : yjp
* @ Date : 2019/5/7 20:51
* @ Version : 1.0
* @ Desciption :
*/
public class DiscardServerHandler extends SimpleChannelInboundHandler {//也可以在main函数类里面,可以写成内部类的形式。

@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, Object o) throws Exception {
//设置响应
DefaultFullHttpResponse defaultFullHttpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
//设置响应头
defaultFullHttpResponse.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html;charset=UTF-8");
//设置响应内容
defaultFullHttpResponse.content().writeBytes("第一次测试netty框架".getBytes());
//把内容加入管道
channelHandlerContext.writeAndFlush(defaultFullHttpResponse).addListener(ChannelFutureListener.CLOSE);

}
}

运行main函数

 

 因为模拟的是http请求,所以打开浏览器输入localhost:8080

成功。

猜你喜欢

转载自www.cnblogs.com/yjp372928571/p/10828459.html
今日推荐