您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

springboot整合netty框架的方式有哪些

2024/2/17 18:17:41发布20次查看
netty作为一个高性能的io框架,是非好用的一个技术框架,
netty 是一个基于nio的客户、服务器端编程框架,使用netty 可以确保你快速和简单的开发出一个网络应用,例如实现了某种协议的客户、服务端应用。netty相当于简化和流线化了网络应用的编程开发过程,例如:基于tcp和udp的socket服务开发。
“快速”和“简单”并不用产生维护性或性能上的问题。netty 是一个吸收了多种协议(包括ftp、smtp、http等各种二进制文本协议)的实现经验,并经过相当精心设计的项目。最终,netty 成功的找到了一种方式,在保证易于开发的同时还保证了其应用的性能,稳定性和伸缩性
首先整个项目引入pom
<?xml version="1.0" encoding="utf-8"?><project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>2.2.1.release</version> <relativepath/> <!-- lookup parent from repository --> </parent> <groupid>com.cxy</groupid> <artifactid>netty</artifactid> <version>0.0.1-snapshot</version> <name>netty</name> <description>demo project for spring boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>io.netty</groupid> <artifactid>netty-all</artifactid> <version>4.1.25.final</version> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> <exclusions> <exclusion> <groupid>org.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下来的
方式一:注解@postconstructpackage 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框架的方式有哪些的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product