netty框架和springboot框架对比,spring集成netty
目录
方式一:注解@PostConstruct方式二:利用监听器启动:方式三:利用应用程序监听器上下文监听器方式四:commiandLinerunner启动网状的作为一个高性能的超正析象管框架,是非好用的一个技术框架,
妮蒂是一个基于尼奥的客户、服务器端编程框架,使用妮蒂可以确保你快速和简单的开发出一个网络应用,例如实现了某种协议的客户、服务端应用妮蒂相当于简化和流线化了网络应用的编程开发过程,例如:基于三氯苯酚和用户数据报协议(用户数据报协议)的窝服务开发。"快速"和"简单"并不用产生维护性或性能上的问题妮蒂是一个吸收了多种协议(包括FTP、SMTP、HTTP等各种二进制文本协议)的实现经验,并经过相当精心设计的项目。最终内蒂成功的找到了一种方式,在保证易于开发的同时还保证了其应用的性能,稳定性和伸缩性那么如何和跳羚这个比较流行的框架进行整合呢?首先整个项目引入砰的一声
?可扩展标记语言版本=1.0 编码=UTF八号?项目xmlns= http://maven。阿帕奇。org/POM/4。0 .0 xmlns : xsi= http://www。w3。org/2001/XML schema-instance xsi :架构位置= http://maven。阿帕奇。org/POM/4。0 .0 https://maven.apache.org/xsd/maven-4.0.0.xsd模型版本4 .0 .0/模型版本父groupIdorg.springframework.boot/groupId人工弹簧-启动-母公司/version2.2.1.RELEASE/version人工弹簧相对路径/。-从存储库中查找父级-/父级groupIdcom.cxy/groupId手工Id netty/手工id版本0。0 .1-快照/版本名称netty/名称描述Spring Boot的演示项目/描述属性Java。版本1.8/Java。版本/属性依赖项groupIdorg.springframework.boot/groupId artifact id spring-boot-starter-web/artifact id/依赖项群体白痴。netty/groupId artifactId netty-all/artifactId version4.1.25.Final/version/依赖项依赖项groupIdorg.springframework.boot/groupId artifactId弹簧-启动-起动机-测试/artifactId范围测试/范围排除排除groupIdorg.junit.vintage/groupId
<artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>handler类是不会改变的
package com.cxy.netty.controller;import io.netty.buffer.ByteBuf;import io.netty.channel.ChannelFutureListener;import io.netty.channel.ChannelHandlerContext;import io.netty.channel.ChannelInboundHandlerAdapter;import io.netty.util.CharsetUtil;public class EchoServerHandler 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); } public void channelReadComplete(ChannelHandlerContext ctx){ ctx.writeAndFlush(ChannelFutureListener.CLOSE); public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause){ cause.printStackTrace(); ctx.close();}
这个handler是我从官网上copy下来的
方式一:注解@PostConstruct
package com.cxy.netty.controller;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.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.boot.autoconfigure.web.ServerProperties;import org.springframework.stereotype.Component;import javax.annotation.PostConstruct;import javax.annotation.PreDestroy;import java.net.InetSocketAddress;@Componentpublic class NettyServer { /*private int port =8080; public int getPort() { return port; } public void setPort(int port) { this.port = port; } public NettyServer(int port) { this.port = port; }*/ @PostConstruct public void start() throws Exception { System.out.println("启动记载netty"); EventLoopGroup boss = new NioEventLoopGroup(); EventLoopGroup work = new NioEventLoopGroup(); ServerBootstrap b = new ServerBootstrap(); b.group(boss,work) .channel(NioServerSocketChannel.class) .localAddress(new InetSocketAddress(8082)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new EchoServerHandler()); } }); System.out.println("启动加载netty2"); ChannelFuture channelFuturef = b.bind().sync(); if (channelFuturef.isSuccess()){ System.out.println("启动成功"); } }}
点击启动:
看日志:
说明已经启动
那么这个注解为什么这么神奇呢:
大概的意思,大家看下,意思就是这个方法会随着类的加载而加载,初始化加载的意思:
@Documented@Retention (RUNTIME)@Target(METHOD)public @interface PostConstruct {}
方式二:利用监听器启动:
package com.cxy.netty.controller;import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;/** * 系统初始化监听器 * @author Administrator * */public class InitListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent sce) { NettyServer nettyServer = new NettyServer(8081); try { nettyServer.start(); } catch (Exception e) { e.printStackTrace(); } } @Override public void contextDestroyed(ServletContextEvent sce) { }}
启动类:
package com.cxy.netty;import com.cxy.netty.controller.InitListener;import com.cxy.netty.controller.NettyServer;import org.springframework.boot.CommandLineRunner;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;import org.springframework.context.annotation.Bean;@SpringBootApplicationpublic class NettyApplication { /** * 注册监听器 * @return */ @SuppressWarnings({ "rawtypes", "unchecked" }) @Bean public ServletListenerRegistrationBean servletListenerRegistrationBean() { ServletListenerRegistrationBean servletListenerRegistrationBean = new ServletListenerRegistrationBean(); servletListenerRegistrationBean.setListener(new InitListener()); return servletListenerRegistrationBean; } public static void main(String[] args) { SpringApplication.run(NettyApplication.class, args); }}
看日志:
方式三 :利用ApplicationListener 上下文监听器
package com.cxy.netty.controller;import com.cxy.netty.controller.NettyServer;import org.springframework.context.ApplicationListener;import org.springframework.context.event.ContextRefreshedEvent;import org.springframework.stereotype.Component;@Componentpublic class NettyBooter implements ApplicationListener<ContextRefreshedEvent> { @Override public void onApplicationEvent(ContextRefreshedEvent event) { NettyServer nettyServer = new NettyServer(8081); try { nettyServer.start(); } catch (Exception e) { e.printStackTrace(); } }}
启动类:
package com.cxy.netty;import com.cxy.netty.controller.NettyServer;import org.springframework.boot.CommandLineRunner;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;import org.springframework.context.annotation.Bean;@SpringBootApplicationpublic class NettyApplication { /** * 注册监听器 * @return */ /* @SuppressWarnings({ "rawtypes", "unchecked" }) @Bean public ServletListenerRegistrationBean servletListenerRegistrationBean() { ServletListenerRegistrationBean servletListenerRegistrationBean = new ServletListenerRegistrationBean(); servletListenerRegistrationBean.setListener(new InitListener()); return servletListenerRegistrationBean; }*/ public static void main(String[] args) { SpringApplication.run(NettyApplication.class, args); }}
看启动日志:
方式四:commiandLinerunner启动
package com.cxy.netty;import com.cxy.netty.controller.NettyServer;import org.springframework.boot.CommandLineRunner;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;import org.springframework.context.annotation.Bean;/*@SpringBootApplicationpublic class NettyApplication { *//** * 注册监听器 * @return *//* *//* @SuppressWarnings({ "rawtypes", "unchecked" }) @Bean public ServletListenerRegistrationBean servletListenerRegistrationBean() { ServletListenerRegistrationBean servletListenerRegistrationBean = new ServletListenerRegistrationBean(); servletListenerRegistrationBean.setListener(new InitListener()); return servletListenerRegistrationBean; }*//* public static void main(String[] args) { SpringApplication.run(NettyApplication.class, args); }}*/@SpringBootApplicationpublic class NettyApplication implements CommandLineRunner { public static void main(String[] args) { SpringApplication.run(NettyApplication.class, args); } @Override public void run(String... args) throws Exception { NettyServer echoServer = new NettyServer(8083); echoServer.start(); }}
看日志:
代表这四种都可以启动成功,下章接再分析后面三种为什么可以启动成功
到此这篇关于springboot整合netty的方式小结的文章就介绍到这了,更多相关springboot整合netty内容请搜索盛行IT以前的文章或继续浏览下面的相关文章希望大家以后多多支持盛行IT!
郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。