java netty html,Netty框架的理解和简单使用

Netty是什么

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

netty的架构

20190508003723503320.png

Netty官网

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

类似框架

Apache 的 Mina

java和netty

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

20190508003723734765.png

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

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

io.netty

netty

3.10.6.Final

compile

io.netty

netty-transport

4.1.34.Final

io.netty

netty-codec-http

4.1.7.Final

在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() {

@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函数

20190508003723829492.png

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

20190508003723915429.png

成功。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值