单片机学习小组
登录
直播中
张鑫
7年用户
1002经验值
私信
关注
怎样使用springboot整合netty来开发一套高性能的通信系统呢
开启该帖子的消息推送
通信系统
怎样使用springboot整合netty来开发一套高性能的
通信
系统呢?为什么要用这两个框架来实现通信服务呢?如何去实现呢?
回帖
(1)
刘梅
2022-2-22 14:42:25
这篇来讲述一个springboot整合netty框架与单片机TCP/UDP通信开发小案例,开源架构正在开发当中,到时候开发者就可以像dubbo一样使用netty来进行通信,从而把关注点放到业务层面。
随着移动5G的快速发展,物联网应用将会出现个爆发是增长,在这里使用springboot整合netty来开发一套高性能的通信系统,成为一种快速高效的实现方案。
为什么要用这两个框架来实现通信服务呢?其中的优势在我另一篇文章中会有体现,在这里我们只讨论实现的步骤。
实现步骤
准备的原材料工具
我构建是用gradle来构建的,所以需要gradle、Google guava
单片机、通讯板子(具备网络连接模块)
构建springboot工程
使用idea构建springboot工程,构建完成后的工程目录如下:
添加netty组件
工程初始化
工程构建完成后,就到了整个springboot和netty的核心部分了
为什么要对工程进行初始化,这个就要从Netty本身的原理说起,初学的同学建议先了解一下netty的基本原理,然后再接着读下面的内容。
springboot是为了简化Spring应用的初始搭建以及后续开发过程的一个框架,在这次整合中我依然整合的是web架构。
boot工程的主程序如下图:
很显然,如上图的代码是无法启动netty来提供TCP服务的,所以我们需要在启动spring工程后来初始化netty,用以提供完整的TCP链接服务。
初始化netty的代码如下图:
实现一个实时监控连接数的功能
如下图效果:
客户端的代码
客户端使用的编码解码器是netty的String类型
public class NettyClient implements Runnable {
@Override
public void run() {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group);
b.channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true);
b.handler(new ChannelInitializer
() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
pipeline.addLast("frameEncoder", new LengthFieldPrepender(4));
pipeline.addLast("decoder", new StringDecoder(CharsetUtil.UTF_8));
pipeline.addLast("encoder", new StringEncoder(CharsetUtil.UTF_8));
pipeline.addLast("handler", new ClientHandler());
}
});
ChannelFuture f = b.connect("127.0.0.1", 8000).sync();
f.channel().writeAndFlush("Netty Hello Service!" + Thread.currentThread().getName() + ":--->:" + Thread.currentThread().getId());
f.channel().closeFuture().sync();
InetSocketAddress socketAddress = (InetSocketAddress) f.channel().localAddress();
System.out.print(String.format("Client port:{%s}", socketAddress.getPort()));
} catch (Exception e) {
System.out.print("netty服务异常了:{}");
e.printStackTrace();
} finally {
group.shutdownGracefully();
System.out.print("netty服务端断开了链接:" + Thread.currentThread().getId());
}
}
}
如果要请求链接,直接实例化该类即可,连接到服务端
也可以用其他形式的请求链接方式,比如单片机使用WIFI模块来链接
8.
总结
在使用netty之前一定要掌握netty的基本原理,做到知其然、知其所以然,才能够在自己的业务系统中灵活得使用netty,才能够做到和自己现有知识体系下的框架无缝结合。
任何框架必须是建立在能够解决实际的业务需求基础上的,不然就是空中楼阁,所以我们要做到架构落地,切勿不切实际的空谈。
这篇来讲述一个springboot整合netty框架与单片机TCP/UDP通信开发小案例,开源架构正在开发当中,到时候开发者就可以像dubbo一样使用netty来进行通信,从而把关注点放到业务层面。
随着移动5G的快速发展,物联网应用将会出现个爆发是增长,在这里使用springboot整合netty来开发一套高性能的通信系统,成为一种快速高效的实现方案。
为什么要用这两个框架来实现通信服务呢?其中的优势在我另一篇文章中会有体现,在这里我们只讨论实现的步骤。
实现步骤
准备的原材料工具
我构建是用gradle来构建的,所以需要gradle、Google guava
单片机、通讯板子(具备网络连接模块)
构建springboot工程
使用idea构建springboot工程,构建完成后的工程目录如下:
添加netty组件
工程初始化
工程构建完成后,就到了整个springboot和netty的核心部分了
为什么要对工程进行初始化,这个就要从Netty本身的原理说起,初学的同学建议先了解一下netty的基本原理,然后再接着读下面的内容。
springboot是为了简化Spring应用的初始搭建以及后续开发过程的一个框架,在这次整合中我依然整合的是web架构。
boot工程的主程序如下图:
很显然,如上图的代码是无法启动netty来提供TCP服务的,所以我们需要在启动spring工程后来初始化netty,用以提供完整的TCP链接服务。
初始化netty的代码如下图:
实现一个实时监控连接数的功能
如下图效果:
客户端的代码
客户端使用的编码解码器是netty的String类型
public class NettyClient implements Runnable {
@Override
public void run() {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group);
b.channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true);
b.handler(new ChannelInitializer
() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
pipeline.addLast("frameEncoder", new LengthFieldPrepender(4));
pipeline.addLast("decoder", new StringDecoder(CharsetUtil.UTF_8));
pipeline.addLast("encoder", new StringEncoder(CharsetUtil.UTF_8));
pipeline.addLast("handler", new ClientHandler());
}
});
ChannelFuture f = b.connect("127.0.0.1", 8000).sync();
f.channel().writeAndFlush("Netty Hello Service!" + Thread.currentThread().getName() + ":--->:" + Thread.currentThread().getId());
f.channel().closeFuture().sync();
InetSocketAddress socketAddress = (InetSocketAddress) f.channel().localAddress();
System.out.print(String.format("Client port:{%s}", socketAddress.getPort()));
} catch (Exception e) {
System.out.print("netty服务异常了:{}");
e.printStackTrace();
} finally {
group.shutdownGracefully();
System.out.print("netty服务端断开了链接:" + Thread.currentThread().getId());
}
}
}
如果要请求链接,直接实例化该类即可,连接到服务端
也可以用其他形式的请求链接方式,比如单片机使用WIFI模块来链接
8.
总结
在使用netty之前一定要掌握netty的基本原理,做到知其然、知其所以然,才能够在自己的业务系统中灵活得使用netty,才能够做到和自己现有知识体系下的框架无缝结合。
任何框架必须是建立在能够解决实际的业务需求基础上的,不然就是空中楼阁,所以我们要做到架构落地,切勿不切实际的空谈。
举报
更多回帖
rotate(-90deg);
回复
相关问答
通信系统
怎样
去使用
springboot
呢
2021-10-25
797
请问
怎样
去设计
高性能
的罪犯抓捕
系统
?
2021-04-28
1443
如何打造打造自己的
通信
框架?
2020-11-05
1136
如何利用FPGA
开发
高性能
网络安全处理平台?
2019-08-12
3357
加
一
个硬件并用labview程序
来
控制
一套
硬件
系统
,请问有对VISA串口比较精通的大神可以帮助吗?
2018-05-29
4770
如何设计
一套
基于ARM和DSP的嵌入式数控机床控制
系统
?
2021-04-25
1373
一套
DAQ设备大概需要多少钱
2013-10-22
5529
请问如何设计
一
个灵活、
高性能
的嵌入式
系统
?
2021-04-22
627
做
一套
温湿度记录
系统
2014-02-27
2311
如何设计超
高性能
微波天线馈源
系统
?
2019-08-12
2850
发帖
登录/注册
20万+
工程师都在用,
免费
PCB检查工具
无需安装、支持浏览器和手机在线查看、实时共享
查看
点击登录
登录更多精彩功能!
英国威廉希尔公司网站
william hill官网 版块
小组
免费开发板试用
ebook
直播
搜索
登录
×
20
完善资料,
赚取积分