在公司开发长连接的场景是复杂的,需要开发人员足够的了解netty各种类的使用,现在离职刚好有时间,本人打算把之前用过的netty经验分享出来,想要一起学习的同学,关注走一波,关注不迷路。本文先介绍如何优雅地在springboot中启动netty服务端。
一、pom导包
<!-- netty -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.66.Final</version>
</dependency>
二、服务端
package com.haocun.netty.netty.server;
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.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.net.InetSocketAddress;
/**
* @Classname NettyServer
* @Description TODO
* @Date 2023/8/26 11:03
* @Created by Administrator
*/
@Component
public class NettyServer {
@PostConstruct//此注解表示随着spring容器的加载而加载
public void start() throws Exception {
EventLoopGroup boss = new NioEventLoopGroup();
EventLoopGroup work = new NioEventLoopGroup();
ServerBootstrap b = new ServerBootstrap();
b.group(boss,work)
.channel(NioServerSocketChannel.class)
.localAddress(new InetSocketAddress(8099))
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new ServerHandler());
}
});
ChannelFuture channelFuture = b.bind().sync();
if (channelFuture.isSuccess()){
System.out.println("netty启动成功");
}
}
}
@Component与@PostConstruct 这两个注解很重要,@Component表示告诉spring容器,这个类型需要你扫码一下,我想变成你容器里面的一个bean;@PostConstruct表示被加上的方法随着容器的加载而执行。
三、添加一个netty的Handel事件处理器
/**
* @Classname ServerHandler
* @Description TODO
* @Date 2023/8/26 11:05
* @Created by Administrator
*/
public class ServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg){
ByteBuf in = (ByteBuf) msg;
System.out.println("Server received: " + in.toString(CharsetUtil.UTF_8));
ctx.write(in);
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx){
ctx.writeAndFlush(ChannelFutureListener.CLOSE);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause){
cause.printStackTrace();
ctx.close();
}
}
至此结束,你可以加一个client端来测试一下
public class Client {
public static void main(String[] args) throws Exception {
NioEventLoopGroup eventExecutors = new NioEventLoopGroup();
try {
//创建bootstrap对象,配置参数
Bootstrap bootstrap = new Bootstrap();
//设置线程组
bootstrap.group(eventExecutors)
//设置客户端的通道实现类型
.channel(NioSocketChannel.class)
//使用匿名内部类初始化通道
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
//添加客户端通道的处理器
ch.pipeline().addLast(new ClientHandler());
}
});
//连接服务端
ChannelFuture channelFuture = bootstrap.connect("127.0.0.1", 8099).sync();
channelFuture.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture channelFuture) throws Exception {
System.out.println(channelFuture.isSuccess());
}
});
//对通道关闭进行监听
channelFuture.channel().closeFuture().sync();
} finally {
//关闭线程组
eventExecutors.shutdownGracefully();
}
}
}
/**
* @Classname ClientHandler
* @Description TODO
* @Date 2023/8/25 23:03
* @Created by Administrator
*/
public class ClientHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
//发送消息到服务端
ChannelFuture future =ctx.writeAndFlush(Unpooled.copiedBuffer("歪比巴卜~茉莉~Are you good~马来西亚~", CharsetUtil.UTF_8));
future.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture channelFuture) throws Exception {
System.out.println("fasong"+channelFuture.isSuccess());
}
});
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
//接收服务端发送过来的消息
ByteBuf byteBuf = (ByteBuf) msg;
System.out.println("收到服务端" + ctx.channel().remoteAddress() + "的消息:" + byteBuf.toString(CharsetUtil.UTF_8));
}
}
当然,我实际项目中用的是另一种方法,但是觉得不够优雅,也可以贴一下代码
还有几种方式可以启动,也可以去其他地方找一些,原理是大同小异。