Netty入门实例-Http服务
【摘要】
本文我们继续来实现Netty的第二个入门案例,一个Http服务。
Http服务
1.需求
Netty 服务器在 6668 端口监听浏览器发出请求 "http://localhost:6668/ ...
本文我们继续来实现Netty的第二个入门案例,一个Http服务。
Http服务
1.需求
- Netty 服务器在 6668 端口监听
- 浏览器发出请求 "http://localhost:6668/ "
- 服务器可以回复消息给客户端 "Hello! 我是服务器 5 " , 并对特定请求资源进行过滤.
2.创建服务端handler
在handler中我们对浏览器提交的Http请求做出处理
package com.dpb.netty.http;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.*;
import io.netty.util.CharsetUtil;
import java.net.URI;
/**
* @program: netty4demo
* @description:
* @author: 波波烤鸭
* @create: 2019-12-24 16:49
*/
public class TestHttpServerHandler extends SimpleChannelInboundHandler<HttpObject> {
/**
* 读取客户端发送的数据
* @param context
* @param httpObject
* @throws Exception
*/
@Override
protected void channelRead0(ChannelHandlerContext context, HttpObject httpObject) throws Exception {
if(httpObject instanceof HttpRequest){
// 判断是否是 Http请求
System.out.println(context.pipeline().hashCode());
System.out.println(httpObject.hashCode());
System.out.println(context.channel().remoteAddress());
HttpRequest request = (HttpRequest) httpObject;
URI uri = new URI(request.uri());
if("/favicon.ico".equals(uri.getPath())){
System.out.println("请求了 favicon.ico 。。");
return ;
}
ByteBuf byteBuf = Unpooled.copiedBuffer("hello,我是服务端...", CharsetUtil.UTF_8);
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,HttpResponseStatus.OK,byteBuf);
response.headers().set(HttpHeaderNames.CONTENT_TYPE,"text/plain;charset=utf-8");
response.headers().set(HttpHeaderNames.CONTENT_LENGTH,byteBuf.readableBytes());
context.writeAndFlush(response);
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
3.创建服务端
创建服务端程序,创建服务。
package com.dpb.netty.http;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.ServerSocketChannel;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.HttpServerCodec;
/**
* @program: netty4demo
* @description:
* @author: 波波烤鸭
* @create: 2019-12-24 16:45
*/
public class TestHttpServer {
public static void main(String[] args) {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workGroup = new NioEventLoopGroup();
try{
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(bossGroup,workGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel sc) throws Exception {
ChannelPipeline pipeline = sc.pipeline();
// 添加对应的服务端编解码器
pipeline.addLast("MyHttpServerCodec",new HttpServerCodec());
// 添加对应的处理器
pipeline.addLast("MyTestHttpServerHandler",new TestHttpServerHandler());
}
});
System.out.println("服务器启动了...");
ChannelFuture future = bootstrap.bind(8666).sync();
future.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture channelFuture) throws Exception {
if(future.isSuccess()){
System.out.println("请求处理成功了....");
}
}
});
future.channel().closeFuture().sync();
}catch (Exception e){
}finally {
bossGroup.shutdownGracefully();
workGroup.shutdownGracefully();
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
4. 效果测试
启动服务器,返回在浏览器地址栏中输入 http://localhost:8666/index.html
处理成功~
文章来源: dpb-bobokaoya-sm.blog.csdn.net,作者:波波烤鸭,版权归原作者所有,如需转载,请联系作者。
原文链接:dpb-bobokaoya-sm.blog.csdn.net/article/details/103746361
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)