SpringBoot整合Netty
来源:互联网 发布:网络最热门表情包 编辑:程序博客网 时间:2024/06/15 11:48
SpringBoot整合Netty
SpringBoot中使用Netty与spring中使用Netty没有差别,在Spring中使用Netty可以考虑Netty的启动时机,可以在Bean加载的时候启动,可以写一个自执行的函数启动,这里采用监听Spring容器的启动事件来启动Netty。
业务需求:
- Netty端口可以配置
- Netty监听事件进行启动
- 端口配置采用SpringBoot风格
下面是Netty的启动代码,只是一个样例
public void start() { bootstrap.group(bossLoopGroup, workerLoopGroup).channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel socketChannel) throws Exception { ChannelPipeline pipeline = socketChannel.pipeline(); if (getSslContext() != null) { pipeline.addLast(getSslContext().newHandler(socketChannel.alloc())); } } }); try { ChannelFuture sync = bootstrap.bind(getPort()).sync(); this.channel = sync.channel(); logger.info("Baymax RPC server start on port(s) : {}", getPort()); } catch (InterruptedException e) { e.printStackTrace(); } }
这里端口是可以配置的
Spring容器中内置事件有
- ContextStoppedEvent
- ContextRefreshEvent
- ContextStartedEvent
- ContextClosedEvent
事件的关系如下
这里采取监听 ContextRefreshEvent
事件
事件监听代码如下
public class BaymaxRpcDeployedNotifier implements SmartApplicationListener { private final static int BACHELOR = 110; private ApplicationContext applicationContext; private RpcServer rpcServer; public BaymaxRpcDeployedNotifier(ApplicationContext applicationContext, RpcServer rpcServer) { this.applicationContext = applicationContext; this.rpcServer = rpcServer; } @Override public boolean supportsEventType(Class<? extends ApplicationEvent> aClass) { return aClass == ContextRefreshedEvent.class; } @Override public boolean supportsSourceType(Class<?> aClass) { return ApplicationContext.class.isAssignableFrom(aClass); } @Override public void onApplicationEvent(ApplicationEvent applicationEvent) { rpcServer.start(); } @Override public int getOrder() { return BACHELOR; }}
SmartApplicationListener
可以做事件监听流,以后在介绍
现在Netty可以根据Spring的启动进行启动,还差一个工作,将netty的端口配置添加到SpringBoot的配置中,SpringBoot中添加自定义配置代码如下
@ConfigurationProperties(prefix = "baymax.rpc.server")public class BaymaxNettyServerProperties { private int port = 10220; private List<ChannelHandler> channelHandlerList = new ArrayList<>(); public int getPort() { return port; } public void setPort(int port) { this.port = port; } public List<ChannelHandler> getChannelHandlerList() { return channelHandlerList; } public void setChannelHandlerList(List<ChannelHandler> channelHandlerList) { this.channelHandlerList = channelHandlerList; }}
这里需要添加一个文件名字叫做spring.factories
,内容如下
org.springframework.boot.autoconfigure.EnableAutoConfiguration=cn.com.immortals.baymax.boot.BaymaxNettyServerProperties
最近缺点资源分,手头宽裕的支援点
到这里基本就结束了代码在SpringBootNetty直接下载就好,在父pom中把parent改成springboot,下面的依赖添加版本就好,否则不好使的。
阅读全文
0 0
- SpringBoot整合Netty
- netty整合springmvc
- SpringBoot学习:SpringBoot整合mybatis
- SpringBoot整合Mybatis(分层整合)
- springboot整合freemarker
- SpringBoot和Mybatis整合
- springboot整合jsp
- springboot 整合mybatis
- springboot整合swagger
- springboot+mybatis+dubbo整合
- springboot mybaits mysql 整合
- springboot整合jedisCluster
- springboot整合dubbox
- SpringBoot整合MyBatis
- SpringBoot整合Mybatis
- springBoot整合mybatis
- springboot与mybatis整合
- SpringBoot整合hibernate
- React-classnames库
- 表格存储在QCon2017的分享
- MSP430平台下实现Si4432的收发数据
- Netty 时间轮算法实现HashedWheelTimer
- Linux搭建邮件服务器
- SpringBoot整合Netty
- 怎样把U盘里的文件,复制到LINUX下?
- 浅谈物联网的关键技术和难点
- Gradle for Android(二)——build.gradle基本配置
- a标签实现下载图片功能
- List存放map值
- mybatis中的#和$的区别
- IBM SoftLayer数据中心内部的服务器机架
- Linux Shell编程case语句-yellowcong