iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >springboot整合netty框架的方式小结
  • 906
分享到

springboot整合netty框架的方式小结

2024-04-02 19:04:59 906人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

目录方式一:注解@PostConstruct方式二:利用监听器启动:方式三 :利用ApplicationListener 上下文监听器方式四:commiandLinerunner启动

Netty作为一个高性能的io框架,是非好用的一个技术框架,

Netty 是一个基于NIO的客户、服务器编程框架,使用Netty 可以确保你快速和简单的开发出一个网络应用,例如实现了某种协议的客户、服务端应用。Netty相当于简化和流线化了网络应用的编程开发过程,例如:基于tcp和UDP的Socket服务开发。
“快速”和“简单”并不用产生维护性或性能上的问题。Netty 是一个吸收了多种协议(包括FTP、SMTP、Http等各种二进制文本协议)的实现经验,并经过相当精心设计的项目。最终,Netty 成功的找到了一种方式,在保证易于开发的同时还保证了其应用的性能,稳定性和伸缩性
那么如何和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是我从官网上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 {
    
    @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;

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 {
    
    @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;
@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 {
   
   
    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 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内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: springboot整合netty框架的方式小结

本文链接: https://www.lsjlt.com/news/151180.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • springboot整合netty框架的方式小结
    目录方式一:注解@PostConstruct方式二:利用监听器启动:方式三 :利用ApplicationListener 上下文监听器方式四:commiandLinerunner启动...
    99+
    2024-04-02
  • springboot整合netty框架的方式有哪些
    本篇内容主要讲解“springboot整合netty框架的方式有哪些”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“springboot整合netty框架的方式有哪些”吧!netty作为一个高性能...
    99+
    2023-07-02
  • springboot整合netty框架实现站内信
    目录代码用到的组件介绍websocket连接过程代码用到的组件介绍 ChannelInitializer 见名知意,就是channel 初始化器,当每个客户端创建连接时这里面的代码都...
    99+
    2022-12-23
    springboot站内信 springboot netty站内消息通知
  • Springboot安全框架整合SpringSecurity实现方式
    1.工业级安全框架介绍 Spring Security基于Spring开发,项目中如果使用Spring作为基础,配合Spring Security做权限更加方便,而Shiro需要和S...
    99+
    2024-04-02
  • SpringBoot怎么整合JPA框架
    这篇文章主要介绍了SpringBoot怎么整合JPA框架的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇SpringBoot怎么整合JPA框架文章都会有所收获,下面我们一起来看看吧。一. Spring Boot数...
    99+
    2023-07-04
  • SpringBoot框架如何整合SwaggerUI
    这篇文章主要介绍了SpringBoot框架如何整合SwaggerUI的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇SpringBoot框架如何整合SwaggerUI文章都会有所收获,下面我们一起来看看吧。整合s...
    99+
    2023-06-29
  • springboot整合netty的正确姿势
    近期做一些物联网方面项目,使用到了tcp协议,之前公司做过socket短连接,网上找了一个简单的demo,很早便学习到nio方面知识,学习了《netty从入门到精通》这本书,同时也根据网上视频做了几个demo,但学习不太深入,刚好物联网项目...
    99+
    2023-10-08
    spring boot java 后端
  • Java springboot整合Shiro框架的方法是什么
    本篇内容主要讲解“Java springboot整合Shiro框架的方法是什么”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Java springboot整合Shiro框架的...
    99+
    2023-06-26
  • SpringBoot整合MQTT小结汇总
    目录前言:一、什么是mqtt二、主要思想发布/订阅模式三、MQTT重要概念3.1MQTTClient3.2MQTTBroker3.3MQTTConnection3.4MQTT主要参数...
    99+
    2024-04-02
  • SpringBoot框架整合SwaggerUI的示例代码
    整合swagger进行模块测试 注意事项:为方便SpringBoot更好的整合Swagger,需要专门放置在一个模块中(maven子工程) 创建公共模块,整合swagger,为了所有...
    99+
    2024-04-02
  • springboot整合flowable框架入门步骤
    最近工作中有用到工作流的开发,引入了flowable工作流框架,在此记录一下springboot整合flowable工作流框架的过程,以便后续再次使用到时可以做一些参考使用,如果项目...
    99+
    2024-04-02
  • SpringBoot框架整合Mybatis简单攻略
    目录步骤 1 添加mybatis-starter依赖步骤 2 如何配置mybatis到SpringBoot项目步骤 3 测试查询步骤 4 mybatis注解方式步骤 5 用注解方式做...
    99+
    2024-04-02
  • SpringBoot如何整合Dozer映射框架
    今天小编给大家分享一下SpringBoot如何整合Dozer映射框架的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。1. Do...
    99+
    2023-07-02
  • Springboot整合Mybatis传值的常用方式总结
    方式一:直接传 接口 public interface UserMapper { public List<User> getUserById(int id);...
    99+
    2024-04-02
  • springboot框架中如何整合mybatis框架思路详解
    目录springboot框架中如何整合mybatis框架 一、在pom.xml 文件引入对应依赖二、写配置springboot框架中如何整合mybatis框架 思路: 1....
    99+
    2022-12-20
    springboot整合mybatis框架 springboot整合mybatis
  • 最全面的SpringBoot教程(五)——整合框架
    前言 本文为 最全面的SpringBoot教程(五)——整合框架 相关知识,下边将对SpringBoot整合Junit,SpringBoot整合Mybatis,SpringBoot整合Redis等进...
    99+
    2023-09-08
    spring boot java mybatis
  • SpringBoot整合Activiti工作流框架的使用
    目录Activiti 介绍SpringBoot 整合使用 starter不使用 starter使用 ActivitiActiviti 介绍 Activiti是一个开源的工作流引擎,它...
    99+
    2024-04-02
  • springboot整合quartz定时任务框架的完整步骤
    目录Spring整合Quartzpom文件对应的properties 文件配置类自定义任务类:ScheduledTask获取spring中bean的工具类:SpringContext...
    99+
    2024-04-02
  • springboot整合quartz定时任务框架的方法是什么
    今天小编给大家分享一下springboot整合quartz定时任务框架的方法是什么的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下...
    99+
    2023-06-26
  • springboot分布式整合dubbo的方式
     Dubbo是Alibaba开源的分布式服务框架,它最大的特点是按照分层的方式来架构,使用这种方式可以使各个层之间解耦合(或者最大限度地松耦合)。从服务模型的角度来看,Dubbo采用...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作