第一个 Netty 应用

写一个 Hello 服务器我这里主要是基于spring boot 2.6.9 依赖

spring-boot-starter-webflux 讲netty 

Netty 实现的 Hello服务器都需要下面这些:

    一个服务器 handler :这个组件实现了服务器的业务逻辑,决定了连接创建后和接收到信
息后该如何处理
Bootstrapping : 这个是配置服务器的启动代码。最少需要设置服务器绑定的端口,用来
监听连接请求。
    通过 ChannelHandler 来实现服务器的逻辑
Hello Server 将会将接受到的数据的拷贝发送给客户端。因此,我们需要实现
ChannelInboundHandler 接口,用来定义处理入站事件的方法。由于我们的应用很简单,只
需要继承 ChannelInboundHandlerAdapter 就行了。这个类 提供了默认
ChannelInboundHandler 的实现,所以只需要覆盖下面的方法:
channelRead() - 每个信息入站都会调用
channelReadComplete() - 通知处理器最后的 channelread() 是当前批处理中的最后一条
消息时调用
exceptionCaught()- 读操作时捕获到异常时调用
HelloServerHandler 代码如下:
package com.kais.nettylesson.lesson1;


import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.util.CharsetUtil;
import org.junit.jupiter.api.Test;

import java.net.InetSocketAddress;

//开启共享
@Sharable
public class HelloServerHandler extends ChannelInboundHandlerAdapter {

    /**
     * 调用{@link ChannelHandlerContextfireChannelRead(Object)}转发到{@link ChannelInboundHandler}中的下一个{@link ChannelInboundHandler}<p>
     * 子类可以重写此方法以更改行为@param ctx@param msg
     *
     * @param ctx
     * @param msg
     */
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        ByteBuf in = (ByteBuf) msg;
        System.out.println("收到的服务器: " + in.toString(CharsetUtil.UTF_8)); //2 ctx.write(in);
    }

    /**
     * 调用{@link ChannelHandlerContextfireChannelReadComplete()}转发到{@link ChannelInboundHandler}中的下一个{@link ChannelInboundHandler}<p>
     * 子类可以重写此方法以更改行为。
     *
     * @param ctx
     */
    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ctx.writeAndFlush(Unpooled.EMPTY_BUFFER)
                .addListener(ChannelFutureListener.CLOSE);
    }

    /**
     * 调用{@link ChannelHandlerContextfireExceptionCaught(Throwable)}转发到{@link ChannelPipeline}中的下一个{@link ChannelHandler}<p>
     * 子类可以重写此方法以更改行为@参数ctx@参数原因
     *
     * @param ctx
     * @param cause
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }


    @Test
    public void helloServerStart() throws Exception {
        int port = 8001;
        NioEventLoopGroup group = new NioEventLoopGroup();

        ServerBootstrap b = new ServerBootstrap();
        b.group(group)
                .channel(NioServerSocketChannel.class).localAddress(new InetSocketAddress(port)).childHandler(new ChannelInitializer<SocketChannel>() {
            @Override
            public void initChannel(SocketChannel ch) throws Exception {
                ch.pipeline().addLast(new HelloServerHandler());
            }
        });
        ChannelFuture f = b.bind().sync();
        System.out.println(HelloServerHandler.class.getName() + " 开始并收听  " + f.channel().localAddress());
        f.channel().closeFuture().sync();
        group.shutdownGracefully().sync();
    }
}

访问http://127.0.0.1:8001/?123321213=123123

返回如下:

收到的服务器: GET /?123321213=123123 HTTP/1.1
User-Agent: PostmanRuntime/7.29.0
Accept: */*
Postman-Token: f788edb6-c66a-46f9-b42e-cca131eca028
Host: 127.0.0.1:8001
Accept-Encoding: gzip, deflate, br
Connection: keep-alive

就成功了

Gitee地址:https://gitee.com/MrxR/nettyLesson/blob/master/src/main/java/com/kais/nettylesson/lesson1/HelloServerHandler.java

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值