Netty入门实例-Http服务(1)

  • @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);

}

}

}

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

@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();

最后

我还为大家准备了一套体系化的架构师学习资料包以及BAT面试资料,供大家参考及学习

已经将知识体系整理好(源码,笔记,PPT,学习视频)

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

料包以及BAT面试资料,供大家参考及学习

已经将知识体系整理好(源码,笔记,PPT,学习视频)

[外链图片转存中…(img-kfFVryV9-1714430395201)]

[外链图片转存中…(img-XASXQWoQ-1714430395201)]

[外链图片转存中…(img-n5jSiYTm-1714430395202)]

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

  • 15
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值