add netty tcp client to server logic in spring boot sample · KLines/JavaSpringBootSamples@fce4de2 · GitHub
Skip to content

Commit fce4de2

Browse files
committed
add netty tcp client to server logic in spring boot sample
1 parent f530336 commit fce4de2

5 files changed

Lines changed: 244 additions & 1 deletion

File tree

‎springboot-netty-sample/pom.xml‎

Lines changed: 12 additions & 1 deletion
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.ipman.netty.springboot.sample.echo;
2+
3+
import io.netty.bootstrap.Bootstrap;
4+
import io.netty.channel.*;
5+
import io.netty.channel.nio.NioEventLoopGroup;
6+
import io.netty.channel.socket.SocketChannel;
7+
import io.netty.channel.socket.nio.NioSocketChannel;
8+
import io.netty.handler.logging.LogLevel;
9+
import io.netty.handler.logging.LoggingHandler;
10+
11+
/**
12+
* Created by ipipman on 2022/1/19.
13+
*
14+
* @version V1.0
15+
* @Package com.ipman.netty.springboot.sample.echo
16+
* @Description: (用一句话描述该文件做什么)
17+
* @date 2022/1/19 9:26 下午
18+
*/
19+
public class EchoClient {
20+
21+
public static void main(String[] args) {
22+
EventLoopGroup group = new NioEventLoopGroup();
23+
EchoClientHandler clientHandler = new EchoClientHandler();
24+
try {
25+
Bootstrap b = new Bootstrap();
26+
b.group(group)
27+
.channel(NioSocketChannel.class)
28+
.option(ChannelOption.TCP_NODELAY, true)
29+
.handler(new ChannelInitializer<SocketChannel>() {
30+
@Override
31+
public void initChannel(SocketChannel ch) throws Exception {
32+
ChannelPipeline p = ch.pipeline();
33+
p.addLast(new LoggingHandler(LogLevel.INFO));
34+
p.addLast(clientHandler);
35+
}
36+
});
37+
// 连接server端
38+
ChannelFuture cf = b.connect("127.0.0.1", 8090).sync();
39+
// 等待连接关闭
40+
cf.channel().closeFuture().sync();
41+
} catch (Exception e) {
42+
e.printStackTrace();
43+
} finally {
44+
group.shutdownGracefully();
45+
}
46+
47+
}
48+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.ipman.netty.springboot.sample.echo;
2+
3+
import io.netty.buffer.ByteBuf;
4+
import io.netty.buffer.Unpooled;
5+
import io.netty.channel.ChannelHandler.Sharable;
6+
import io.netty.channel.ChannelHandlerContext;
7+
import io.netty.channel.ChannelInboundHandlerAdapter;
8+
9+
import java.nio.charset.StandardCharsets;
10+
import java.util.concurrent.TimeUnit;
11+
12+
/**
13+
* Created by ipipman on 2022/1/19.
14+
*
15+
* @version V1.0
16+
* @Package com.ipman.netty.springboot.sample.echo
17+
* @Description: (用一句话描述该文件做什么)
18+
* @date 2022/1/19 9:26 下午
19+
*/
20+
@Sharable
21+
public class EchoClientHandler extends ChannelInboundHandlerAdapter {
22+
23+
private final ByteBuf firstMsg;
24+
25+
public EchoClientHandler() {
26+
firstMsg = Unpooled.wrappedBuffer("Hello Codeing".getBytes(StandardCharsets.UTF_8));
27+
}
28+
29+
/**
30+
* 通道活跃时
31+
*
32+
* @param ctx
33+
* @throws Exception
34+
*/
35+
@Override
36+
public void channelActive(ChannelHandlerContext ctx) throws Exception {
37+
// 第一次传输
38+
ctx.writeAndFlush(firstMsg);
39+
}
40+
41+
/**
42+
* 读取数据时
43+
*
44+
* @param ctx
45+
* @param msg
46+
* @throws Exception
47+
*/
48+
@Override
49+
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
50+
ctx.write(msg);
51+
}
52+
53+
/**
54+
* 读取完毕时
55+
*
56+
* @param ctx
57+
* @throws Exception
58+
*/
59+
@Override
60+
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
61+
// 延迟3秒
62+
TimeUnit.SECONDS.sleep(3);
63+
ctx.flush();
64+
}
65+
66+
/**
67+
* 捕获到异常时
68+
*
69+
* @param ctx
70+
* @param cause
71+
* @throws Exception
72+
*/
73+
@Override
74+
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
75+
cause.printStackTrace();
76+
ctx.close();
77+
}
78+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.ipman.netty.springboot.sample.echo;
2+
3+
import io.netty.bootstrap.ServerBootstrap;
4+
import io.netty.channel.ChannelFuture;
5+
import io.netty.channel.ChannelInitializer;
6+
import io.netty.channel.ChannelPipeline;
7+
import io.netty.channel.EventLoopGroup;
8+
import io.netty.channel.nio.NioEventLoopGroup;
9+
import io.netty.channel.socket.SocketChannel;
10+
import io.netty.channel.socket.nio.NioServerSocketChannel;
11+
import io.netty.handler.logging.LogLevel;
12+
import io.netty.handler.logging.LoggingHandler;
13+
14+
/**
15+
* Created by ipipman on 2022/1/19.
16+
*
17+
* @version V1.0
18+
* @Package com.ipman.netty.springboot.sample.http
19+
* @Description: (用一句话描述该文件做什么)
20+
* @date 2022/1/19 9:09 下午
21+
*/
22+
public class EchoServer {
23+
24+
public static void main(String[] args) {
25+
// 创建Server端
26+
EventLoopGroup workGroup = new NioEventLoopGroup();
27+
final EchoServerHandler serverHandler = new EchoServerHandler();
28+
try {
29+
ServerBootstrap b = new ServerBootstrap();
30+
b.group(workGroup)
31+
.channel(NioServerSocketChannel.class)
32+
.handler(new LoggingHandler(LogLevel.INFO))
33+
.childHandler(new ChannelInitializer<SocketChannel>() {
34+
@Override
35+
public void initChannel(SocketChannel ch) throws Exception {
36+
ChannelPipeline p = ch.pipeline();
37+
p.addLast(new LoggingHandler(LogLevel.INFO));
38+
p.addLast(serverHandler);
39+
}
40+
});
41+
// 绑定端口
42+
ChannelFuture f = b.bind(8090).sync();
43+
// 等待连接关闭
44+
f.channel().closeFuture().sync();
45+
} catch (InterruptedException e) {
46+
e.printStackTrace();
47+
} finally {
48+
// 关闭所有线程
49+
workGroup.shutdownGracefully();
50+
}
51+
}
52+
}
Lines changed: 54 additions & 0 deletions

0 commit comments

Comments
 (0)