springboot整合netty框架的方式小結(jié)
netty作為一個(gè)高性能的io框架,是非好用的一個(gè)技術(shù)框架,
Netty 是一個(gè)基于NIO的客戶、服務(wù)器端編程框架,使用Netty 可以確保你快速和簡(jiǎn)單的開發(fā)出一個(gè)網(wǎng)絡(luò)應(yīng)用,例如實(shí)現(xiàn)了某種協(xié)議的客戶、服務(wù)端應(yīng)用。Netty相當(dāng)于簡(jiǎn)化和流線化了網(wǎng)絡(luò)應(yīng)用的編程開發(fā)過程,例如:基于TCP和UDP的socket服務(wù)開發(fā)。
“快速”和“簡(jiǎn)單”并不用產(chǎn)生維護(hù)性或性能上的問題。Netty 是一個(gè)吸收了多種協(xié)議(包括FTP、SMTP、HTTP等各種二進(jìn)制文本協(xié)議)的實(shí)現(xiàn)經(jīng)驗(yàn),并經(jīng)過相當(dāng)精心設(shè)計(jì)的項(xiàng)目。最終,Netty 成功的找到了一種方式,在保證易于開發(fā)的同時(shí)還保證了其應(yīng)用的性能,穩(wěn)定性和伸縮性
那么如何和springboot這個(gè)比較流行的框架進(jìn)行整合呢?
首先整個(gè)項(xiàng)目引入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類是不會(huì)改變的
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();
}這個(gè)handler是我從官網(wǎng)上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;
@Component
public 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("啟動(dòng)記載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("啟動(dòng)加載netty2");
ChannelFuture channelFuturef = b.bind().sync();
if (channelFuturef.isSuccess()){
System.out.println("啟動(dòng)成功");
}
}
}點(diǎn)擊啟動(dòng):
看日志:

說明已經(jīng)啟動(dòng)
那么這個(gè)注解為什么這么神奇呢:

大概的意思,大家看下,意思就是這個(gè)方法會(huì)隨著類的加載而加載,初始化加載的意思:
@Documented
@Retention (RUNTIME)
@Target(METHOD)
public @interface PostConstruct {
}方式二:利用監(jiān)聽器啟動(dòng):
package com.cxy.netty.controller;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
/**
* 系統(tǒng)初始化監(jiān)聽器
* @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) {
}
}啟動(dòng)類:
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;
@SpringBootApplication
public class NettyApplication {
/**
* 注冊(cè)監(jiān)聽器
* @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 上下文監(jiān)聽器
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;
@Component
public class NettyBooter implements ApplicationListener<ContextRefreshedEvent> {
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
NettyServer nettyServer = new NettyServer(8081);
try {
nettyServer.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}啟動(dòng)類:
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;
@SpringBootApplication
public class NettyApplication {
/**
* 注冊(cè)監(jiān)聽器
* @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);
}
}看啟動(dòng)日志:

方式四:commiandLinerunner啟動(dòng)
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;
/*
@SpringBootApplication
public class NettyApplication {
*/
/**
* 注冊(cè)監(jiān)聽器
* @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);
}
}
*/
@SpringBootApplication
public 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();
}
}看日志:

代表這四種都可以啟動(dòng)成功,下章接再分析后面三種為什么可以啟動(dòng)成功
到此這篇關(guān)于springboot整合netty的方式小結(jié)的文章就介紹到這了,更多相關(guān)springboot整合netty內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot如何集成Netty
- SpringBoot集成netty實(shí)現(xiàn)websocket通信功能
- SpringBoot整合Netty+Websocket實(shí)現(xiàn)消息推送的示例代碼
- SpringBoot 整合 Netty 多端口監(jiān)聽的操作方法
- SpringBoot整合Netty的流程步驟
- springboot之springboot與netty整合方案
- springboot整合netty框架實(shí)現(xiàn)站內(nèi)信
- Springboot整合Netty自定義協(xié)議實(shí)現(xiàn)示例詳解
- Springboot+netty實(shí)現(xiàn)Web聊天室
- SpringBoot整合Netty服務(wù)端的實(shí)現(xiàn)示例
相關(guān)文章
通過jxl.jar 讀取、導(dǎo)出excel的實(shí)例代碼
通過jxl.jar 讀取、導(dǎo)出excel的實(shí)例代碼,需要的朋友可以參考一下2013-03-03
java實(shí)現(xiàn)的導(dǎo)出Excel工具類實(shí)例
這篇文章主要介紹了java實(shí)現(xiàn)的導(dǎo)出Excel工具類,結(jié)合具體實(shí)例形式分析了java導(dǎo)出Excel導(dǎo)出并生成Excel表格相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2017-10-10
Java基于Dijkstra算法實(shí)現(xiàn)校園導(dǎo)游程序
這篇文章主要為大家詳細(xì)介紹了Java基于Dijkstra算法實(shí)現(xiàn)校園導(dǎo)游程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
解決Error occurred during initialization o
這篇文章主要介紹了解決Error occurred during initialization of VM Java虛擬機(jī)初始化失敗問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-03-03
IDEA運(yùn)行SSM項(xiàng)目的超詳細(xì)圖解教程
SSM項(xiàng)目部署其實(shí)很簡(jiǎn)單,下面這篇文章主要給大家介紹了關(guān)于IDEA運(yùn)行SSM項(xiàng)目的超詳細(xì)圖解教程,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-10-10

