欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot整合netty框架的方式小結

 更新時間:2022年06月12日 08:45:07   作者:菩提樹下的韋小寶  
Netty 是一個基于NIO的客戶、服務器端編程框架,使用Netty 可以確保你快速和簡單的開發(fā)出一個網(wǎng)絡應用,這篇文章主要介紹了springboot整合netty框架的方式小結,需要的朋友可以參考下

netty作為一個高性能的io框架,是非好用的一個技術框架,

Netty 是一個基于NIO的客戶、服務器端編程框架,使用Netty 可以確保你快速和簡單的開發(fā)出一個網(wǎng)絡應用,例如實現(xiàn)了某種協(xié)議的客戶、服務端應用。Netty相當于簡化和流線化了網(wǎng)絡應用的編程開發(fā)過程,例如:基于TCP和UDP的socket服務開發(fā)。
“快速”和“簡單”并不用產(chǎn)生維護性或性能上的問題。Netty 是一個吸收了多種協(xié)議(包括FTP、SMTP、HTTP等各種二進制文本協(xié)議)的實現(xiàn)經(jīng)驗,并經(jīng)過相當精心設計的項目。最終,Netty 成功的找到了一種方式,在保證易于開發(fā)的同時還保證了其應用的性能,穩(wěn)定性和伸縮性
那么如何和springboot這個比較流行的框架進行整合呢?
首先整個項目引入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是我從官網(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("啟動記載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("啟動成功");
        }
    }
}

點擊啟動:

看日志:

說明已經(jīng)啟動

那么這個注解為什么這么神奇呢:

大概的意思,大家看下,意思就是這個方法會隨著類的加載而加載,初始化加載的意思:

@Documented
@Retention (RUNTIME)
@Target(METHOD)
public @interface PostConstruct {
}

方式二:利用監(jiān)聽器啟動:

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) {
        
    }
}

啟動類:

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 {
    /**
     * 注冊監(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();
        }
    }
}

啟動類:

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 {
   /**
     * 注冊監(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);
    }
}

看啟動日志:

方式四: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;
/*
@SpringBootApplication
public class NettyApplication {
   */
/**
     * 注冊監(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();
    }
}

看日志:

代表這四種都可以啟動成功,下章接再分析后面三種為什么可以啟動成功

到此這篇關于springboot整合netty的方式小結的文章就介紹到這了,更多相關springboot整合netty內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • SpringBoot 使用 FTP 操作文件的過程(刪除、上傳、下載文件)

    SpringBoot 使用 FTP 操作文件的過程(刪除、上傳、下載文件)

    這篇文章主要介紹了SpringBoot 使用 FTP 操作文件,主要包括配置ftp服務器,上傳、刪除、下載文件操作,本文結合示例代碼給大家介紹的非常詳細,需要的朋友可以參考下
    2022-12-12
  • java 遍歷request中的所有表單數(shù)據(jù)的實例代碼

    java 遍歷request中的所有表單數(shù)據(jù)的實例代碼

    下面小編就為大家?guī)硪黄猨ava 遍歷request中的所有表單數(shù)據(jù)的實例代碼。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-09-09
  • java 字符串截取的實例詳解

    java 字符串截取的實例詳解

    這篇文章主要介紹了java 字符串截取的實例詳解的相關資料,這里提供了實例代碼幫助大家實現(xiàn)這樣的功能,需要的朋友可以參考下
    2017-08-08
  • Java 實戰(zhàn)項目錘煉之醫(yī)院門診收費管理系統(tǒng)的實現(xiàn)流程

    Java 實戰(zhàn)項目錘煉之醫(yī)院門診收費管理系統(tǒng)的實現(xiàn)流程

    讀萬卷書不如行萬里路,只學書上的理論是遠遠不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+html+jdbc+mysql實現(xiàn)一個醫(yī)院門診收費管理系統(tǒng),大家可以在過程中查缺補漏,提升水平
    2021-11-11
  • Java超詳細大文件分片上傳代碼

    Java超詳細大文件分片上傳代碼

    文件上傳是一個很常見的功能。在項目開發(fā)過程中,我們通常都會使用一些成熟的上傳組件來實現(xiàn)對應的功能,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧<BR>
    2022-06-06
  • SpringBoot使用JDBC獲取相關的數(shù)據(jù)方法

    SpringBoot使用JDBC獲取相關的數(shù)據(jù)方法

    這篇文章主要介紹了SpringBoot使用JDBC獲取相關的數(shù)據(jù)方法,JDBC與數(shù)據(jù)庫建立連接、發(fā)送 操作數(shù)據(jù)庫的語句并處理結果,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-03-03
  • Java中的異常測試框架JUnit使用上手指南

    Java中的異常測試框架JUnit使用上手指南

    這篇文章主要介紹了Java的異常測試框架JUnit使用上手指南,JUnit是Java代碼進行單元測試中的常用工具,需要的朋友可以參考下
    2016-03-03
  • 深度解析Java中的國際化底層類ResourceBundle

    深度解析Java中的國際化底層類ResourceBundle

    做項目應該都會實現(xiàn)國際化,那么大家知道Java底層是如何實現(xiàn)國際化的嗎?這篇文章就來和大家深度解析一下Java中的國際化底層類ResourceBundle,希望對大家有所幫助
    2023-03-03
  • Java利用布隆過濾器實現(xiàn)快速檢查元素是否存在

    Java利用布隆過濾器實現(xiàn)快速檢查元素是否存在

    布隆過濾器是一個很長的二進制向量和一系列隨機映射函數(shù)。布隆過濾器可以用于檢索一個元素是否在一個集合中。本文就來詳細說說實現(xiàn)的方法,需要的可以參考一下
    2022-10-10
  • 快速排序和分治排序介紹

    快速排序和分治排序介紹

    這篇文章主要介紹了快速排序和分治排序,需要的朋友可以參考下
    2015-04-04

最新評論