Skip to content

Commit 0ba1edb

Browse files
author
YunaiV
committed
开始 Netty 入门示例
1 parent 558c495 commit 0ba1edb

File tree

20 files changed

+608
-0
lines changed

20 files changed

+608
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
<parent>
6+
<artifactId>lab-67-netty-demo</artifactId>
7+
<groupId>cn.iocoder.springboot.labs</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>lab-67-netty-demo-client</artifactId>
13+
14+
<properties>
15+
<!-- 依赖相关配置 -->
16+
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
17+
<!-- 插件相关配置 -->
18+
<maven.compiler.target>1.8</maven.compiler.target>
19+
<maven.compiler.source>1.8</maven.compiler.source>
20+
</properties>
21+
22+
<dependencyManagement>
23+
<dependencies>
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-starter-parent</artifactId>
27+
<version>${spring.boot.version}</version>
28+
<type>pom</type>
29+
<scope>import</scope>
30+
</dependency>
31+
</dependencies>
32+
</dependencyManagement>
33+
34+
<dependencies>
35+
<!-- 实现对 Spring MVC 的自动化配置 -->
36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-starter-web</artifactId>
39+
</dependency>
40+
41+
<!-- Netty 依赖 -->
42+
<dependency>
43+
<groupId>io.netty</groupId>
44+
<artifactId>netty-all</artifactId>
45+
<version>4.1.50.Final</version>
46+
</dependency>
47+
48+
<!-- 引入 netty-demo-common 封装 -->
49+
<dependency>
50+
<groupId>cn.iocoder.springboot.labs</groupId>
51+
<artifactId>lab-67-netty-demo-common</artifactId>
52+
<version>1.0-SNAPSHOT</version>
53+
</dependency>
54+
</dependencies>
55+
56+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package cn.iocoder.springboot.lab67.nettyclientdemo;
2+
3+
public class NettyClientApplication {
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package cn.iocoder.springboot.lab67.nettyclientdemo.config;
2+
3+
import cn.iocoder.springboot.lab67.nettycommondemo.dispacher.MessageDispatcher;
4+
import cn.iocoder.springboot.lab67.nettycommondemo.dispacher.MessageHandlerContainer;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.Configuration;
7+
8+
@Configuration
9+
public class NettyClientConfig {
10+
11+
@Bean
12+
public MessageDispatcher messageDispatcher() {
13+
return new MessageDispatcher();
14+
}
15+
16+
@Bean
17+
public MessageHandlerContainer messageHandlerContainer() {
18+
return new MessageHandlerContainer();
19+
}
20+
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
<parent>
6+
<artifactId>lab-67-netty-demo</artifactId>
7+
<groupId>cn.iocoder.springboot.labs</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>lab-67-netty-demo-common</artifactId>
13+
14+
<properties>
15+
<!-- 插件相关配置 -->
16+
<maven.compiler.target>1.8</maven.compiler.target>
17+
<maven.compiler.source>1.8</maven.compiler.source>
18+
</properties>
19+
20+
<dependencies>
21+
<!-- Netty 依赖 -->
22+
<dependency>
23+
<groupId>io.netty</groupId>
24+
<artifactId>netty-all</artifactId>
25+
<version>4.1.50.Final</version>
26+
</dependency>
27+
28+
<!-- FastJSON 依赖 -->
29+
<dependency>
30+
<groupId>com.alibaba</groupId>
31+
<artifactId>fastjson</artifactId>
32+
<version>1.2.71</version>
33+
</dependency>
34+
35+
<!-- 引入 Spring 相关依赖 -->
36+
<dependency>
37+
<groupId>org.springframework</groupId>
38+
<artifactId>spring-aop</artifactId>
39+
<version>5.2.5.RELEASE</version>
40+
</dependency>
41+
<dependency>
42+
<groupId>org.springframework</groupId>
43+
<artifactId>spring-context</artifactId>
44+
<version>5.2.5.RELEASE</version>
45+
</dependency>
46+
47+
<!-- 引入 SLF4J 依赖 -->
48+
<dependency>
49+
<groupId>org.slf4j</groupId>
50+
<artifactId>slf4j-api</artifactId>
51+
<version>1.7.30</version>
52+
</dependency>
53+
</dependencies>
54+
55+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package cn.iocoder.springboot.lab67.nettycommondemo.codec;
2+
3+
public class Invocation {
4+
5+
/**
6+
* 类型 - 心跳请求
7+
*/
8+
public static final String TYPE_HEARTBEAT_REQUEST = "HEARTBEAT_REQUEST";
9+
/**
10+
* 类型 - 心跳响应
11+
*/
12+
public static final String TYPE_HEARTBEAT_RESPONSE = "HEARTBEAT_RESPONSE";
13+
14+
/**
15+
* 类型
16+
*/
17+
private String type;
18+
/**
19+
* 消息
20+
*/
21+
private String message;
22+
23+
public String getType() {
24+
return type;
25+
}
26+
27+
public Invocation setType(String type) {
28+
this.type = type;
29+
return this;
30+
}
31+
32+
public String getMessage() {
33+
return message;
34+
}
35+
36+
public Invocation setMessage(String message) {
37+
this.message = message;
38+
return this;
39+
}
40+
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package cn.iocoder.springboot.lab67.nettycommondemo.codec;
2+
3+
import com.alibaba.fastjson.JSON;
4+
import io.netty.buffer.ByteBuf;
5+
import io.netty.channel.ChannelHandlerContext;
6+
import io.netty.handler.codec.ByteToMessageDecoder;
7+
import io.netty.handler.codec.CorruptedFrameException;
8+
9+
import java.util.List;
10+
11+
public class InvocationDecoder extends ByteToMessageDecoder {
12+
13+
@Override
14+
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
15+
// 标记当前读取位置
16+
in.markReaderIndex();
17+
// 判断是否能够读取 length 长度
18+
if (in.readableBytes() <= 4) {
19+
return;
20+
}
21+
// 读取长度
22+
int length = in.readInt();
23+
if (length < 0) {
24+
throw new CorruptedFrameException("negative length: " + length);
25+
}
26+
// 如果 message 不够可读,则退回到原读取位置
27+
if (in.readableBytes() < length) {
28+
in.resetReaderIndex();
29+
return;
30+
}
31+
// 读取内容
32+
ByteBuf byteBuf = in.readRetainedSlice(length);
33+
// 解析成 Invocation
34+
byte[] content = byteBuf.array();
35+
Invocation invocation = JSON.parseObject(content, Invocation.class);
36+
out.add(invocation);
37+
}
38+
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package cn.iocoder.springboot.lab67.nettycommondemo.codec;
2+
3+
import com.alibaba.fastjson.JSON;
4+
import io.netty.buffer.ByteBuf;
5+
import io.netty.channel.ChannelHandlerContext;
6+
import io.netty.handler.codec.MessageToByteEncoder;
7+
8+
public class InvocationEncoder extends MessageToByteEncoder<Invocation> {
9+
10+
@Override
11+
protected void encode(ChannelHandlerContext ctx, Invocation invocation, ByteBuf out) {
12+
// 将 Invocation 转换成 byte[] 数组
13+
byte[] content = JSON.toJSONBytes(invocation);
14+
// 写入 length
15+
out.writeInt(content.length);
16+
// 写入内容
17+
out.writeBytes(content);
18+
}
19+
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package cn.iocoder.springboot.lab67.nettycommondemo.dispacher;
2+
3+
public interface Message {
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package cn.iocoder.springboot.lab67.nettycommondemo.dispacher;
2+
3+
import cn.iocoder.springboot.lab67.nettycommondemo.codec.Invocation;
4+
import com.alibaba.fastjson.JSON;
5+
import io.netty.channel.ChannelHandler;
6+
import io.netty.channel.ChannelHandlerContext;
7+
import io.netty.channel.SimpleChannelInboundHandler;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
10+
@ChannelHandler.Sharable
11+
public class MessageDispatcher extends SimpleChannelInboundHandler<Invocation> {
12+
13+
@Autowired
14+
private MessageHandlerContainer messageHandlerContainer;
15+
16+
@Override
17+
protected void channelRead0(ChannelHandlerContext ctx, Invocation invocation) {
18+
// 获得 type 对应的 MessageHandler 处理器
19+
MessageHandler messageHandler = messageHandlerContainer.getMessageHandler(invocation.getType());
20+
// 解析消息
21+
Class<? extends Message> messageClass = MessageHandlerContainer.getMessageClass(messageHandler);
22+
Message message = JSON.parseObject(invocation.getMessage(), messageClass);
23+
// 执行逻辑
24+
// noinspection unchecked
25+
messageHandler.execute(ctx.channel(), message);
26+
}
27+
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package cn.iocoder.springboot.lab67.nettycommondemo.dispacher;
2+
3+
import io.netty.channel.Channel;
4+
5+
public interface MessageHandler<T extends Message> {
6+
7+
/**
8+
* 执行处理消息
9+
*
10+
* @param channel 通道
11+
* @param message 消息
12+
*/
13+
void execute(Channel channel, T message);
14+
15+
/**
16+
* @return 消息类型,即每个 Message 实现类上的 TYPE 静态字段
17+
*/
18+
String getType();
19+
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package cn.iocoder.springboot.lab67.nettycommondemo.dispacher;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.aop.framework.AopProxyUtils;
6+
import org.springframework.beans.factory.InitializingBean;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.context.ApplicationContext;
9+
10+
import java.lang.reflect.ParameterizedType;
11+
import java.lang.reflect.Type;
12+
import java.util.HashMap;
13+
import java.util.Map;
14+
import java.util.Objects;
15+
16+
public class MessageHandlerContainer implements InitializingBean {
17+
18+
private Logger logger = LoggerFactory.getLogger(getClass());
19+
20+
/**
21+
* 消息类型与 MessageHandler 的映射
22+
*/
23+
private final Map<String, MessageHandler> handlers = new HashMap<>();
24+
25+
@Autowired
26+
private ApplicationContext applicationContext;
27+
28+
@Override
29+
public void afterPropertiesSet() throws Exception {
30+
// 通过 ApplicationContext 获得所有 MessageHandler Bean
31+
applicationContext.getBeansOfType(MessageHandler.class).values() // 获得所有 MessageHandler Bean
32+
.forEach(messageHandler -> handlers.put(messageHandler.getType(), messageHandler)); // 添加到 handlers 中
33+
logger.info("[afterPropertiesSet][消息处理器数量:{}]", handlers.size());
34+
}
35+
36+
protected MessageHandler getMessageHandler(String type) {
37+
MessageHandler handler = handlers.get(type);
38+
if (handler == null) {
39+
throw new IllegalArgumentException(String.format("类型(%s) 找不到匹配的 MessageHandler 处理器", type));
40+
}
41+
return handler;
42+
}
43+
44+
public static Class<? extends Message> getMessageClass(MessageHandler handler) {
45+
// 获得 Bean 对应的 Class 类名。因为有可能被 AOP 代理过。
46+
Class<?> targetClass = AopProxyUtils.ultimateTargetClass(handler);
47+
// 获得接口的 Type 数组
48+
Type[] interfaces = targetClass.getGenericInterfaces();
49+
Class<?> superclass = targetClass.getSuperclass();
50+
while ((Objects.isNull(interfaces) || 0 == interfaces.length) && Objects.nonNull(superclass)) { // 此处,是以父类的接口为准
51+
interfaces = superclass.getGenericInterfaces();
52+
superclass = targetClass.getSuperclass();
53+
}
54+
if (Objects.nonNull(interfaces)) {
55+
// 遍历 interfaces 数组
56+
for (Type type : interfaces) {
57+
// 要求 type 是泛型参数
58+
if (type instanceof ParameterizedType) {
59+
ParameterizedType parameterizedType = (ParameterizedType) type;
60+
// 要求是 MessageHandler 接口
61+
if (Objects.equals(parameterizedType.getRawType(), MessageHandler.class)) {
62+
Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
63+
// 取首个元素
64+
if (Objects.nonNull(actualTypeArguments) && actualTypeArguments.length > 0) {
65+
return (Class<Message>) actualTypeArguments[0];
66+
} else {
67+
throw new IllegalStateException(String.format("类型(%s) 获得不到消息类型", handler));
68+
}
69+
}
70+
}
71+
}
72+
}
73+
throw new IllegalStateException(String.format("类型(%s) 获得不到消息类型", handler));
74+
}
75+
76+
}

0 commit comments

Comments
 (0)