Rumah >Java >javaTutorial >Apakah cara untuk springboot menyepadukan rangka kerja netty?
Netty, sebagai rangka kerja io berprestasi tinggi, adalah rangka kerja teknikal yang sangat mudah digunakan
Netty ialah rangka kerja pengaturcaraan pelanggan dan pelayan berdasarkan NIO dapat memastikan anda boleh dengan cepat dan mudah membangunkan aplikasi rangkaian, seperti aplikasi klien atau pelayan yang melaksanakan protokol tertentu. Netty adalah setara dengan memudahkan dan memperkemas proses pengaturcaraan dan pembangunan aplikasi rangkaian, seperti: pembangunan perkhidmatan soket berdasarkan TCP dan UDP.
"Pantas" dan "mudah" tidak menimbulkan masalah penyelenggaraan atau prestasi. Netty ialah projek yang menyerap pengalaman pelaksanaan berbilang protokol (termasuk pelbagai protokol teks binari seperti FTP, SMTP, HTTP, dll.) dan direka bentuk dengan agak teliti. Pada akhirnya, Netty berjaya menemui cara untuk memastikan pembangunan yang mudah di samping memastikan prestasi, kestabilan dan kebolehskalaan aplikasinya
Pertama sekali, tidak mungkin untuk memperkenalkan kelas pengendali 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>
ke dalam keseluruhan projek Perubahan
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(); }
Pengendali ini disalin daripada laman web rasmi
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("启动成功"); } } }
Klik untuk bermula:
Lihat log:
Menunjukkan bahawa ia telah dimulakan
Jadi mengapa anotasi ini sangat ajaib:
Tentang maksud , mari kita lihat, ini bermakna kaedah ini akan dimuatkan apabila kelas dimuatkan Maksud pemuatan awal ialah:
@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) { } }
Mulakan kelas:
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 { /** * 注册监听器 * @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); } }
Lihat log:
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(); } } }
Mulakan kelas:
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 { /** * 注册监听器 * @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); } }
Lihat Log permulaan:
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 { */ /** * 注册监听器 * @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(); } }
Lihat log:
Atas ialah kandungan terperinci Apakah cara untuk springboot menyepadukan rangka kerja netty?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!