SpringBoot 集成Netty实现UDP Server
注:ApplicationRunner 接口是在容器启动成功后的最后一步回调(类似开机自启动)。
UDPServer
package com.vmware.vCenterEvent.netty;import io.netty.bootstrap.Bootstrap; import io.netty.channel.*; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioDatagramChannel; import lombok.extern.log4j.Log4j2; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.stereotype.Component;@Log4j2 @Component public class UdpServer implements ApplicationRunner {private final Bootstrap bootstrap;private final NioEventLoopGroup group;private Channel channel;private void Start() throws InterruptedException {try {channel = bootstrap.bind("0.0.0.0", 8888).sync().channel();System.out.println("UdpServer start success");channel.closeFuture().await();} finally {group.shutdownGracefully();}}private static final class NettyUdpServerHolder {static final NettyUdpServer INSTANCE = new NettyUdpServer();}public static NettyUdpServer getInstance() {return NettyUdpServerHolder.INSTANCE;}private NettyUdpServer() {group = new NioEventLoopGroup();bootstrap = new Bootstrap();bootstrap.group(group).channel(NioDatagramChannel.class).option(ChannelOption.SO_BROADCAST, true).option(ChannelOption.SO_RCVBUF, 1024 * 1024 * 100).handler(new ChannelInitializer<Channel>() {@Overrideprotected void initChannel(Channel channel) throws Exception {ChannelPipeline pipeline = channel.pipeline();pipeline.addLast(new NettyUdpServerHandler());}});}@Overridepublic void run(ApplicationArguments args) throws Exception {NettyUdpServer.getInstance().Start();} }
UDPServerHandler
import com.vmware.vCenterEvent.domain.Syslog; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; import io.netty.channel.socket.DatagramPacket; import io.netty.util.CharsetUtil; import lombok.extern.log4j.Log4j2; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.jms.core.JmsMessagingTemplate; import org.springframework.stereotype.Component;import javax.annotation.PostConstruct;@Log4j2 @Component public class NettyUdpServerHandler extends SimpleChannelInboundHandler<DatagramPacket> {public static NettyUdpServerHandler nettyUdpServerHandler;@Autowiredprivate JmsMessagingTemplate jmsMessagingTemplate;public NettyUdpServerHandler(){}@PostConstructpublic void init(){nettyUdpServerHandler = this;nettyUdpServerHandler.jmsMessagingTemplate = this.jmsMessagingTemplate;}@Overrideprotected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) {// log.info("开始接收数据");String msgString = msg.content().toString(CharsetUtil.UTF_8); // 将接收到数据放入ActiveMQ队列中nettyUdpServerHandler.jmsMessagingTemplate.convertAndSend("mq", msgString);} }
转载于:https://www.cnblogs.com/vincenshen/p/10759422.html
SpringBoot 集成Netty实现UDP Server相关推荐
- springboot集成netty使用udp协议实现消息接收与转发
一.转发服务 1.创建NettyServer,使用线程池实现异步处理 *** udp服务*/ public class NettyServer {private static final Logger ...
- Springboot集成netty实例
一.简介 netty是jboss提供的一个java开源框架,netty提供异步的.事件驱动的网络应用程序框架和工具,用以快速开发高性能.高可用性的网络服务器和客户端程序.也就是说netty是一个基于n ...
- java websocket netty_用SpringBoot集成Netty开发一个基于WebSocket的聊天室
前言 基于SpringBoot,借助Netty控制长链接,使用WebSocket协议做一个实时的聊天室. 项目效果 项目统一登录路径:http://localhost:8080/chat/netty ...
- springboot集成netty实现代理服务器
说明 使用netty实现代理服务功能,思路是:客户端发送请求,由netty服务端通过端口监听到请求,然后在内部再开启一个netty客户端作为代理去访问真实的服务器,最后由真实的服务器将响应返回给代理, ...
- SpringBoot集成Netty
一.pom依赖 <dependency><groupId>io.netty</groupId><artifactId>netty-all</art ...
- springboot集成netty框架(物联网tcp连接,只服务端)
Maven <dependency><groupId>io.netty</groupId><artifactId>netty-all</artif ...
- Springboot+Netty搭建UDP客户端
使用Netty+SpringBoot方式可以快速地开发一套基于UDP协议的服务端程序,同样的也可以开发客户端,一般使用UDP都是使用原生的方式,发送消息后就不管不问,也就是不需要确定消息是否收到,这里 ...
- Springboot 集成Springcloud gateway的入门
最近做项目使用到了springcloude gateway作为网关,因此在此记录下springcloud gateway的入门操作,后续再将源码解读写出来,先立个flag. 回归正题,Springcl ...
- springboot集成elasticsearch6.8.23设置密码xpack连接,及遇到的None of the configured nodes are available
遇到一个 None of the configured nodes are available 的坑 一.背景: 因现网扫描出来几个漏洞,目前版本使用的springboot2.1.17.RELEASE ...
- SpringBoot集成WebSocket实现及时通讯聊天功能!!!
1:在SpringBoot的pom.xml文件里添加依赖: <!-- websocket --> <dependency><groupId>org.springfr ...
最新文章
- 二分法:二分查找(递归+非递归)实现
- linux系统下redhat7之虚拟机控制
- 大容量类 Redis 存储的场景补充-pika
- jsp+servlet 导出Excel表格
- idea使用ant将wsdl文件_文件曝光:奔驰GLC将使用E级同款1.5T发动机
- 在.Net 模板页中使用CSS样式
- 久谦咨询python笔试题目_【久谦咨询面试|面试题】-看准网
- [jQuery]回到顶部
- 2018年全国多校算法寒假训练营练习比赛(第一场)F. 大吉大利,今晚吃鸡——跑毒篇(模拟)
- 指尖初体验之手指的舞蹈
- 基于PaddleHub实现简易人像抠图
- 我卖掉北京 500 万的房子,在老家生活的这两年…
- 参考文献编号[9]之后出现空格解决方法
- 为什么我卸载了微博?
- 整车行业 SAP APO 开发备忘(刘欣)
- vulnhub刷题记录(The Planets: Earth)
- 花指令 用c语言,最新一批花指令
- 京东校招java面试题_2018京东校招Java笔试题
- ubuntu内存不足1G解决办法
- 2020大学生网络安全知识大赛总决赛模拟卷错题集(10)
热门文章
- 华为鸿蒙深度研究(100页)
- 再见!收费的 XShell,我改用国产良心工具!
- 数数FastJson那些年犯下的'血案'...
- 阿里巴巴中台技术架构实践与思考
- 仅限今天,《Linux就该这么学》半价优惠仅需39.5元。
- JavaScript技巧总结和本地存储(一)
- Golang学习 - sort 包
- 【转】Android将Activity打成jar包供第三方调用(解决资源文件不能打包的问题)...
- Java IO学习笔记(五):内存操作流
- Ancient Knight(打造Windows Mobile平台最专业的游戏修改器)