Skip to content

Commit fce4de2

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

File tree

5 files changed

+244
-1
lines changed

5 files changed

+244
-1
lines changed

springboot-netty-sample/pom.xml

+12-1
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,21 @@
55
<parent>
66
<groupId>org.springframework.boot</groupId>
77
<artifactId>spring-boot-starter-parent</artifactId>
8-
<version>2.6.2</version>
8+
<version>2.4.1</version>
99
<relativePath/> <!-- lookup parent from repository -->
1010
</parent>
1111
<groupId>com.example</groupId>
1212
<artifactId>springboot-netty-sample</artifactId>
1313
<version>0.0.1-SNAPSHOT</version>
1414
<name>springboot-netty-sample</name>
1515
<description>Demo project for Spring Boot</description>
16+
1617
<properties>
1718
<java.version>1.8</java.version>
19+
<maven.version>1.8</maven.version>
1820
</properties>
21+
22+
1923
<dependencies>
2024
<dependency>
2125
<groupId>org.springframework.boot</groupId>
@@ -27,6 +31,13 @@
2731
<artifactId>spring-boot-starter-test</artifactId>
2832
<scope>test</scope>
2933
</dependency>
34+
35+
<!-- 引入Netty依赖 -->
36+
<dependency>
37+
<groupId>io.netty</groupId>
38+
<artifactId>netty-all</artifactId>
39+
<version>4.1.39.Final</version>
40+
</dependency>
3041
</dependencies>
3142

3243
<build>
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+
}
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+
}
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.ipman.netty.springboot.sample.echo;
2+
3+
import io.netty.channel.ChannelHandler.Sharable;
4+
import io.netty.channel.ChannelHandlerContext;
5+
import io.netty.channel.ChannelInboundHandlerAdapter;
6+
7+
/**
8+
* Created by ipipman on 2022/1/19.
9+
*
10+
* @version V1.0
11+
* @Package com.ipman.netty.springboot.sample.http
12+
* @Description: (用一句话描述该文件做什么)
13+
* @date 2022/1/19 9:11 下午
14+
*/
15+
@Sharable
16+
public class EchoServerHandler extends ChannelInboundHandlerAdapter {
17+
18+
19+
/**
20+
* 读取
21+
*
22+
* @param ctx
23+
* @param msg
24+
* @throws Exception
25+
*/
26+
@Override
27+
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
28+
ctx.write(msg);
29+
}
30+
31+
/**
32+
* 读取完毕时
33+
*
34+
* @param ctx
35+
* @throws Exception
36+
*/
37+
@Override
38+
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
39+
ctx.flush();
40+
}
41+
42+
/**
43+
* 抓住异常
44+
*
45+
* @param ctx
46+
* @param cause
47+
* @throws Exception
48+
*/
49+
@Override
50+
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
51+
cause.printStackTrace();
52+
ctx.close();
53+
}
54+
}

0 commit comments

Comments
 (0)