netty框架
在網(wǎng)絡(luò)編程領(lǐng)域,Netty是Java的一個(gè)優(yōu)秀的框架,他將java的復(fù)雜和難以使用的關(guān)于OIO和NIO的一些框架
進(jìn)行了封裝留荔,使其隱藏在易用的api后面±骄耄總之聚蝶,如果你想編寫(xiě)高性能的網(wǎng)絡(luò)服務(wù),那么使用Netty就沒(méi)錯(cuò)了藻治。
目前各個(gè)大公司比如google,facebook等公司都在使用Netty框架碘勉,很多項(xiàng)目比如dubbo和Elasticsearch等就使用了Netty.
netty和springboot的整合
之前一直使用的都是springboot框架,習(xí)慣了spring的容器桩卵,于是就想著把netty也放到容器中以便于方便使用验靡。
這次先用個(gè)demo先實(shí)現(xiàn)下,以后有更好的方式的時(shí)候再來(lái)更新雏节。
首先是 pom.xml 文件
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>liqiangz</groupId>
<artifactId>netty-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId> <!-- Use 'netty-all' for 4.0 or above -->
<version>4.1.32.Final</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
<version>1.3.8.RELEASE</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>aliyunmaven</id>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
然后是 啟動(dòng)main方法
在這里實(shí)現(xiàn)了CommandLineRunner接口胜嗓,并重寫(xiě)了run方法,以便在springboot啟動(dòng)后就立即啟動(dòng)netty服務(wù)钩乍。
package io.liqiangz.server;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* The type Netty application.
* @author liqiangz
*/
@SpringBootApplication
public class NettyApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(NettyApplication.class, args);
}
@Override
public void run(String... strings) throws Exception {
EchoServer echoServer = new EchoServer(8080);
echoServer.start();
}
}
接下來(lái)就是netty服務(wù)啟動(dòng)引導(dǎo)類,很簡(jiǎn)單的一個(gè)echo服務(wù)
package io.liqiangz.server;
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 java.net.InetSocketAddress;
/**
* @author :liqiangz.
* @date :Created in 21:03 2018/12/15
*/
public class EchoServer {
private final int port;
private Logger log = LoggerFactory.getLogger(this.getClass());
public EchoServer(int port) {
this.port = port;
}
public void start() throws Exception {
final EchoServerHandler serverHandler = new EchoServerHandler();
log.info("啟動(dòng)服務(wù)");
EventLoopGroup group = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(group)
.channel(NioServerSocketChannel.class)
.localAddress(new InetSocketAddress(port))
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(serverHandler);
}
});
ChannelFuture f = b.bind().sync();
f.channel().closeFuture().sync();
}finally {
group.shutdownGracefully().sync();
}
}
}
serverHandler類
package io.liqiangz.server;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandler.*;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;
/**
* @author :liqiang.
* @date :Created in 21:04 2018/12/15
*/
@Sharable
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);
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx){
ctx.writeAndFlush(ChannelFutureListener.CLOSE);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause){
cause.printStackTrace();
ctx.close();
}
}