本文的示例代码参考NettyBasic
目录
开始
spring init -b 1.5.6.RELEASE -dweb --build gradle NettyBasic
vim src/main/java/com/example/NettyBasic/NettyServer.java
package com.example.NettyBasic;
public class NettyServer {
public static void start() {
System.out.println("server start");
}
}
vim src/main/java/com/example/NettyBasic/DemoApplication.java
package com.example.NettyBasic;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
NettyServer.start();
}
}
- 测试
./gradlew bootrun
server start
Netty
vim build.gradle
# 省略了未修改代码
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
testCompile('org.springframework.boot:spring-boot-starter-test')
compile('io.netty:netty-all:4.1.25.Final')
}
vim src/main/java/com/example/NettyBasic/NettyServer.java
package com.example.NettyBasic;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
public class NettyServer {
public static void start() {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childOption(ChannelOption.SO_KEEPALIVE, true)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline channelPipeline = ch.pipeline();
channelPipeline.addLast(new LoggerHandler());
}
});
System.out.println("server start");
ChannelFuture channelFuture = serverBootstrap.bind(10001).sync();
channelFuture.channel().closeFuture().sync();
System.out.println("server closed");
} catch (Exception e) {
e.printStackTrace();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
vim src/main/java/com/example/NettyBasic/LoggerHandler.java
package com.example.NettyBasic;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
public class LoggerHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println("read: " + msg.toString().trim());
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
System.out.println("read complete");
}
}
- 测试
# server
./gradlew bootrun
# client
telnet 127.0.0.1 10001
# server
server start
read: PooledUnsafeDirectByteBuf(ridx: 0, widx: 7, cap: 1024)
read complete
read: PooledUnsafeDirectByteBuf(ridx: 0, widx: 7, cap: 1024)
read complete
# client
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
hello
world
Pipeline
vim src/main/java/com/example/NettyBasic/NettyServer.java
public class NettyServer {
public static void start() {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childOption(ChannelOption.SO_KEEPALIVE, true)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline channelPipeline = ch.pipeline();
channelPipeline.addLast(new StringDecoder());
channelPipeline.addLast(new LoggerHandler());
}
});
System.out.println("server start");
ChannelFuture channelFuture = serverBootstrap.bind(10001).sync();
channelFuture.channel().closeFuture().sync();
System.out.println("server closed");
} catch (Exception e) {
e.printStackTrace();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
- 测试
# server
./gradlew bootrun
# client 1
telnet 127.0.0.1 10001
# client 2
telnet 127.0.0.1 10001
# server
server start
read: hello
read complete
read: world
read complete
read: hello
read complete
read: world
read complete
# client 1
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
hello
world
# client 2
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
hello
world
网友评论