Skip to content

Commit 1908309

Browse files
committed
feat: add demo-netty module with Netty client and server implementations, update parent POM
1 parent 480f8c3 commit 1908309

6 files changed

Lines changed: 230 additions & 0 deletions

File tree

demo-kafka/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
<dependency>
6363
<groupId>org.testcontainers</groupId>
6464
<artifactId>kafka</artifactId>
65+
<version>${testcontainers.version}</version>
6566
<scope>test</scope>
6667
</dependency>
6768
</dependencies>

demo-netty/.gitignore

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
!**/src/main/**/target/
4+
!**/src/test/**/target/
5+
6+
### IntelliJ IDEA ###
7+
.idea/modules.xml
8+
.idea/jarRepositories.xml
9+
.idea/compiler.xml
10+
.idea/libraries/
11+
*.iws
12+
*.iml
13+
*.ipr
14+
15+
### Eclipse ###
16+
.apt_generated
17+
.classpath
18+
.factorypath
19+
.project
20+
.settings
21+
.springBeans
22+
.sts4-cache
23+
24+
### NetBeans ###
25+
/nbproject/private/
26+
/nbbuild/
27+
/dist/
28+
/nbdist/
29+
/.nb-gradle/
30+
build/
31+
!**/src/main/**/build/
32+
!**/src/test/**/build/
33+
34+
### VS Code ###
35+
.vscode/
36+
37+
### Mac OS ###
38+
.DS_Store

demo-netty/pom.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<groupId>com.helltractor.demo</groupId>
9+
<artifactId>parent</artifactId>
10+
<version>${revision}</version>
11+
<relativePath>../pom.xml</relativePath>
12+
</parent>
13+
14+
<artifactId>demo-netty</artifactId>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>io.netty</groupId>
19+
<artifactId>netty-all</artifactId>
20+
<version>${netty.version}</version>
21+
</dependency>
22+
</dependencies>
23+
24+
</project>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.helltractor.demo.netty;
2+
3+
import io.netty.bootstrap.Bootstrap;
4+
import io.netty.channel.ChannelFuture;
5+
import io.netty.channel.ChannelHandlerContext;
6+
import io.netty.channel.ChannelInitializer;
7+
import io.netty.channel.EventLoop;
8+
import io.netty.channel.SimpleChannelInboundHandler;
9+
import io.netty.channel.nio.NioEventLoopGroup;
10+
import io.netty.channel.socket.SocketChannel;
11+
import io.netty.channel.socket.nio.NioSocketChannel;
12+
import io.netty.handler.codec.LineBasedFrameDecoder;
13+
import io.netty.handler.codec.string.StringDecoder;
14+
import io.netty.handler.codec.string.StringEncoder;
15+
16+
import java.util.concurrent.TimeUnit;
17+
18+
public class NettyCustom {
19+
20+
public static void main(String[] args) {
21+
Bootstrap bootstrap = new Bootstrap();
22+
bootstrap.group((new NioEventLoopGroup()))
23+
.channel(NioSocketChannel.class)
24+
.handler(new ChannelInitializer<SocketChannel>() {
25+
@Override
26+
protected void initChannel(SocketChannel socketChannel) throws Exception {
27+
socketChannel.pipeline().addLast(new LineBasedFrameDecoder(1024))
28+
.addLast(new StringDecoder())
29+
.addLast(new StringEncoder())
30+
.addLast(new SimpleChannelInboundHandler<String>() {
31+
@Override
32+
protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
33+
System.out.println(msg);
34+
}
35+
});
36+
}
37+
});
38+
39+
ChannelFuture connect = bootstrap.connect("localhost", 8080);
40+
connect.addListener(future -> {
41+
if (future.isSuccess()) {
42+
System.out.println("Connected to server successfully");
43+
EventLoop eventLoop = connect.channel().eventLoop();
44+
eventLoop.scheduleAtFixedRate(() -> {
45+
String msg = "cnm" + System.currentTimeMillis() + "\n";
46+
connect.channel().writeAndFlush(msg);
47+
}, 0, 1, TimeUnit.SECONDS);
48+
} else {
49+
System.err.println("Failed to connect to server: " + future.cause());
50+
}
51+
});
52+
}
53+
54+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package com.helltractor.demo.netty;
2+
3+
import io.netty.bootstrap.ServerBootstrap;
4+
import io.netty.channel.Channel;
5+
import io.netty.channel.ChannelHandlerContext;
6+
import io.netty.channel.ChannelInitializer;
7+
import io.netty.channel.SimpleChannelInboundHandler;
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.codec.LineBasedFrameDecoder;
12+
import io.netty.handler.codec.string.StringDecoder;
13+
import io.netty.handler.codec.string.StringEncoder;
14+
15+
import java.util.ArrayList;
16+
import java.util.List;
17+
import java.util.Map;
18+
import java.util.concurrent.ConcurrentHashMap;
19+
20+
public class NettyServer {
21+
22+
public static void main(String[] args) {
23+
Map<Channel, List<String>> db = new ConcurrentHashMap<>();
24+
ServerBootstrap serverBootstrap = new ServerBootstrap()
25+
.group(new NioEventLoopGroup(), new NioEventLoopGroup())
26+
.channel(NioServerSocketChannel.class)
27+
.childHandler(new ChannelInitializer<SocketChannel>() {
28+
@Override
29+
protected void initChannel(SocketChannel ch) throws Exception {
30+
ch.pipeline().addLast(new LineBasedFrameDecoder(1024))
31+
.addLast(new StringDecoder())
32+
.addLast(new StringEncoder())
33+
.addLast(new ResponseHandler())
34+
.addLast(new DbHandler(db));
35+
}
36+
});
37+
38+
serverBootstrap.bind(8080).addListener(future -> {
39+
if (future.isSuccess()) {
40+
System.out.println("Server started successfully on port 8080");
41+
} else {
42+
System.err.println("Failed to start server: " + future.cause());
43+
}
44+
});
45+
}
46+
47+
static class ResponseHandler extends SimpleChannelInboundHandler<String> {
48+
@Override
49+
protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
50+
System.out.println(msg);
51+
String message = "wo " + msg + "\n";
52+
ctx.channel().writeAndFlush(message);
53+
// 将消息继续传递
54+
ctx.fireChannelRead(msg);
55+
}
56+
57+
@Override
58+
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
59+
System.out.println("Channel registered: " + ctx.channel().remoteAddress());
60+
ctx.fireChannelRegistered();
61+
}
62+
63+
@Override
64+
public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
65+
System.out.println("Channel unregistered: " + ctx.channel().remoteAddress());
66+
ctx.fireChannelUnregistered();
67+
}
68+
69+
@Override
70+
public void channelActive(ChannelHandlerContext ctx) throws Exception {
71+
System.out.println("Channel active: " + ctx.channel().remoteAddress());
72+
ctx.fireChannelActive();
73+
}
74+
}
75+
76+
static class DbHandler extends SimpleChannelInboundHandler<String> {
77+
private final Map<Channel, List<String>> db;
78+
79+
public DbHandler(Map<Channel, List<String>> db) {
80+
this.db = db;
81+
}
82+
83+
@Override
84+
protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
85+
List<String> messageList = db.computeIfAbsent(ctx.channel(), k -> new ArrayList<>());
86+
messageList.add(msg);
87+
}
88+
89+
@Override
90+
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
91+
System.out.println("Channel registered: " + ctx.channel().remoteAddress());
92+
}
93+
94+
@Override
95+
public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
96+
System.out.println("Channel unregistered: " + ctx.channel().remoteAddress());
97+
}
98+
99+
@Override
100+
public void channelActive(ChannelHandlerContext ctx) throws Exception {
101+
System.out.println("Channel active: " + ctx.channel().remoteAddress());
102+
}
103+
104+
@Override
105+
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
106+
List<String> messageList = db.get(ctx.channel());
107+
System.out.println(messageList);
108+
}
109+
}
110+
111+
}

pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<modules>
1919
<module>demo-aspectj</module>
2020
<module>demo-consul</module>
21+
<module>demo-netty</module>
2122
<module>demo-mybatis</module>
2223
<module>demo-redis-spring-data</module>
2324
<module>demo-reflection</module>
@@ -41,6 +42,7 @@
4142
<mybatis-spring.version>3.0.3</mybatis-spring.version>
4243
<commons.lang.version>2.6</commons.lang.version>
4344
<aspectj.version>1.9.4</aspectj.version>
45+
<netty.version>4.1.119.Final</netty.version>
4446
<logback.version>1.5.6</logback.version>
4547
<lombok.version>1.18.30</lombok.version>
4648
<!-- test -->

0 commit comments

Comments
 (0)