没有合适的资源?快使用搜索试试~ 我知道了~
首页springboot整合netty过程详解
资源详情
资源评论
资源推荐

springboot整合整合netty过程详解过程详解
主要介绍了springboot整合netty过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定
的参考学习价值,需要的朋友可以参考下
这篇文章主要介绍了springboot整合netty过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参
考学习价值,需要的朋友可以参考下
前言前言
上一篇讲了netty的一个入门的demo;项目上我也把数据处理做好了,就要开始存数据库了;我用的mybatis框架,如果单独
使用还是觉得比较麻烦,所以就用了springboot+mybatis+netty;本篇主要讲netty与springboot的整合,以及我在这个过程中
遇到的问题,又是怎么去解决的;
正文正文
我在做springboot与netty整合的时候在谷歌,百度找了无数文章,都没有一篇是自己想要的,也达不到自己所想的目的;
代码代码
1. 新建一个springboot项目,在pom文件中添加netty依赖:
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>5.0.0.Alpha1</version>
</dependency>
2.新建netty服务
其实可以复制上一篇文章的netty的三个服务类,做一些稍微的修改就行了;这里为了方便演示,且修都是改好了的,就直接贴
出来了;
DiscardServer类:
@Component
public class DiscardServer {
@Resource
private ChildChannelHandler childChannelHandler;
public void run(int port) throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
System.out.println("准备运行端口:" + port);
try {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 128)
.childHandler(childChannelHandler);
//绑定端口,同步等待成功
ChannelFuture f = bootstrap.bind(port).sync();
//等待服务监听端口关闭
f.channel().closeFuture().sync();
} finally {
//退出,释放线程资源
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
}
ChildChannelHandler类
@Component
public class ChildChannelHandler extends ChannelInitializer<SocketChannel> {
@Resource
private DiscardServerHandler discardServerHandler;
public void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline().addLast(discardServerHandler);
}
}
3.DiscardServerHandler类
特别注意DiscardServerHandler类上需要加@Sharable注解,如果不加的话会报错;















安全验证
文档复制为VIP权益,开通VIP直接复制

评论0