Skip to content

Commit f13fde0

Browse files
发布v2.3.28
1.dubbo代理支持前后端心跳包 2.servlet-urlpattern优化匹配策略
1 parent 4f70e13 commit f13fde0

20 files changed

+421
-360
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ github地址 : https://github.com/wangzihaogithub/spring-boot-protocol
152152
<dependency>
153153
<groupId>com.github.wangzihaogithub</groupId>
154154
<artifactId>spring-boot-protocol</artifactId>
155-
<version>2.3.27</version>
155+
<version>2.3.28</version>
156156
</dependency>
157157
```
158158

@@ -164,7 +164,7 @@ github地址 : https://github.com/wangzihaogithub/spring-boot-protocol
164164
<dependency>
165165
<groupId>com.github.wangzihaogithub</groupId>
166166
<artifactId>netty-servlet</artifactId>
167-
<version>2.3.27</version>
167+
<version>2.3.28</version>
168168
</dependency>
169169
```
170170

@@ -320,7 +320,7 @@ github地址 : https://github.com/wangzihaogithub/spring-boot-protocol
320320
<dependency>
321321
<groupId>com.github.wangzihaogithub</groupId>
322322
<artifactId>spring-boot-protocol</artifactId>
323-
<version>2.3.27</version>
323+
<version>2.3.28</version>
324324
</dependency>
325325

326326
2.编写代码
@@ -400,7 +400,7 @@ github地址 : https://github.com/wangzihaogithub/spring-boot-protocol
400400
<dependency>
401401
<groupId>com.github.wangzihaogithub</groupId>
402402
<artifactId>spring-boot-protocol</artifactId>
403-
<version>2.3.27</version>
403+
<version>2.3.28</version>
404404
</dependency>
405405

406406
2.编写代码
@@ -489,7 +489,7 @@ github地址 : https://github.com/wangzihaogithub/spring-boot-protocol
489489
<dependency>
490490
<groupId>com.github.wangzihaogithub</groupId>
491491
<artifactId>spring-boot-protocol</artifactId>
492-
<version>2.3.27</version>
492+
<version>2.3.28</version>
493493
</dependency>
494494
495495
2.编写启动类

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>com.github.wangzihaogithub</groupId>
66
<artifactId>spring-boot-protocol</artifactId>
7-
<version>2.3.27</version>
7+
<version>2.3.28</version>
88
<packaging>jar</packaging>
99

1010
<name>Spring Boot Protocol</name>
@@ -49,7 +49,7 @@
4949
<connection>scm:git:https://github.com/wangzihaogithub/spring-boot-protocol.git</connection>
5050
<developerConnection>scm:git:git@github.com:wangzihaogithub/spring-boot-protocol.git</developerConnection>
5151
<url>git@github.com:wangzihaogithub/spring-boot-protocol.git</url>
52-
<tag>v2.3.27</tag>
52+
<tag>v2.3.28</tag>
5353
</scm>
5454

5555
<!-- 开发者信息 -->

src/main/java/com/github/netty/protocol/dubbo/Application.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ public class Application {
2626
* 是否是默认应用
2727
*/
2828
private boolean defaultApplication;
29+
/**
30+
* 后端心跳间隔毫秒
31+
*/
32+
private int heartbeatIntervalMs = Constant.DEFAULT_HEARTBEAT;
2933

3034
public Application() {
3135
}
@@ -58,6 +62,14 @@ public void setName(String name) {
5862
this.name = name;
5963
}
6064

65+
public int getHeartbeatIntervalMs() {
66+
return heartbeatIntervalMs;
67+
}
68+
69+
public void setHeartbeatIntervalMs(int heartbeatIntervalMs) {
70+
this.heartbeatIntervalMs = heartbeatIntervalMs;
71+
}
72+
6173
public String getAttachmentApplicationName() {
6274
return attachmentApplicationName;
6375
}

src/main/java/com/github/netty/protocol/dubbo/Body.java

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
package com.github.netty.protocol.dubbo;
22

3+
import com.github.netty.protocol.dubbo.packet.BodyEvent;
4+
import com.github.netty.protocol.dubbo.packet.BodyFail;
5+
import com.github.netty.protocol.dubbo.packet.BodyRequest;
6+
import com.github.netty.protocol.dubbo.packet.BodyResponse;
37
import io.netty.buffer.ByteBuf;
48

9+
import java.io.ByteArrayInputStream;
10+
import java.io.IOException;
11+
import java.util.Map;
12+
13+
import static com.github.netty.protocol.dubbo.Constant.*;
14+
515
public abstract class Body {
616
ByteBuf bodyBytes;
717
int markReaderIndex;
@@ -20,4 +30,87 @@ public boolean release() {
2030
return false;
2131
}
2232
}
33+
34+
public static Body readBody(ByteBuf buffer, Header header) throws IOException, ClassNotFoundException {
35+
// request and serialization flag.
36+
byte flag = header.flag;
37+
byte status = header.status;
38+
int bodyLength = header.bodyLength;
39+
boolean flagResponse = (flag & FLAG_REQUEST) == 0;
40+
byte serializationProtoId = (byte) (flag & SERIALIZATION_MASK);
41+
if (flagResponse) {
42+
// decode response.
43+
if (status == OK) {
44+
if ((flag & FLAG_EVENT) != 0) {
45+
return readEvent(buffer, bodyLength, serializationProtoId);
46+
} else {
47+
try (Serialization.ObjectInput in = Serialization.codeOfDeserialize(serializationProtoId, buffer, bodyLength)) {
48+
byte responseWith = buffer.readByte();
49+
BodyResponse packetResponse;
50+
switch (responseWith) {
51+
case RESPONSE_NULL_VALUE:
52+
packetResponse = new BodyResponse(null, null, null);
53+
break;
54+
case RESPONSE_VALUE:
55+
packetResponse = new BodyResponse(in.readObject(), null, null);
56+
break;
57+
case RESPONSE_WITH_EXCEPTION:
58+
packetResponse = new BodyResponse(null, in.readThrowable(), null);
59+
break;
60+
case RESPONSE_NULL_VALUE_WITH_ATTACHMENTS:
61+
packetResponse = new BodyResponse(null, null, in.readAttachments());
62+
break;
63+
case RESPONSE_VALUE_WITH_ATTACHMENTS:
64+
packetResponse = new BodyResponse(in.readObject(), null, in.readAttachments());
65+
break;
66+
case RESPONSE_WITH_EXCEPTION_WITH_ATTACHMENTS:
67+
packetResponse = new BodyResponse(null, in.readThrowable(), in.readAttachments());
68+
break;
69+
default:
70+
throw new IOException("Unknown result flag, expect '0' '1' '2' '3' '4' '5', but received: " + responseWith);
71+
}
72+
return packetResponse;
73+
}
74+
}
75+
} else {
76+
try (Serialization.ObjectInput in = Serialization.codeOfDeserialize(serializationProtoId, buffer, bodyLength)) {
77+
return new BodyFail(in.readUTF());
78+
}
79+
}
80+
} else {
81+
// decode request.
82+
if ((flag & FLAG_EVENT) != 0) {
83+
return readEvent(buffer, bodyLength, serializationProtoId);
84+
} else {
85+
try (Serialization.ObjectInput in = Serialization.codeOfDeserialize(serializationProtoId, buffer, bodyLength)) {
86+
String dubboVersion = in.readUTF();
87+
String path = in.readUTF();
88+
String version = in.readUTF();
89+
String methodName = in.readUTF();
90+
String parameterTypesDesc = in.readUTF();
91+
int countArgs = countArgs(parameterTypesDesc);
92+
Object[] args = new Object[countArgs];
93+
for (int i = 0; i < countArgs; i++) {
94+
args[i] = in.readArg();
95+
}
96+
Map<String, Object> attachments = in.readAttachments();
97+
return new BodyRequest(dubboVersion, path, version, methodName, parameterTypesDesc, attachments, args);
98+
}
99+
}
100+
}
101+
}
102+
103+
public static BodyEvent readEvent(ByteBuf buffer, int bodyLength, byte serializationProtoId) throws IOException, ClassNotFoundException {
104+
Object data;
105+
byte[] payload = Serialization.getPayload(buffer, bodyLength);
106+
if (Serialization.isHeartBeat(payload, serializationProtoId)) {
107+
data = null;
108+
} else {
109+
try (Serialization.ObjectInput input = Serialization.codeOfDeserialize(serializationProtoId, new ByteArrayInputStream(payload))) {
110+
data = input.readEvent();
111+
}
112+
}
113+
return new BodyEvent(data);
114+
}
115+
23116
}

src/main/java/com/github/netty/protocol/dubbo/Constant.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ public class Constant {
1212
public static final byte RESPONSE_WITH_EXCEPTION_WITH_ATTACHMENTS = 3;
1313
public static final byte RESPONSE_VALUE_WITH_ATTACHMENTS = 4;
1414
public static final byte RESPONSE_NULL_VALUE_WITH_ATTACHMENTS = 5;
15+
/**
16+
* na.
17+
*/
18+
public static final byte STATUS_NA = 0;
1519
/**
1620
* ok.
1721
*/
@@ -67,13 +71,18 @@ public class Constant {
6771
protected static final byte MAGIC_1 = (byte) MAGIC;
6872
// message flag.
6973
protected static final byte FLAG_REQUEST = (byte) 0x80;
70-
// protected static final byte FLAG_TWOWAY = (byte) 0x40;
74+
protected static final byte FLAG_TWOWAY = (byte) 0x40;
7175
protected static final byte FLAG_EVENT = (byte) 0x20;
7276
protected static final int SERIALIZATION_MASK = 0x1f;
77+
protected static final int DEFAULT_HEARTBEAT = 60 * 1000;
78+
79+
protected static final String HEARTBEAT_EVENT = null;
80+
protected static final String MOCK_HEARTBEAT_EVENT = "H";
81+
protected static final String READONLY_EVENT = "R";
7382

7483
public static String statusToString(byte status) {
7584
switch (status) {
76-
case 0:
85+
case STATUS_NA:
7786
return "NA";
7887
case OK:
7988
return "OK";

src/main/java/com/github/netty/protocol/dubbo/DubboDecoder.java

Lines changed: 2 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
package com.github.netty.protocol.dubbo;
22

3-
import com.github.netty.protocol.dubbo.packet.BodyFail;
4-
import com.github.netty.protocol.dubbo.packet.BodyHeartBeat;
5-
import com.github.netty.protocol.dubbo.packet.BodyRequest;
6-
import com.github.netty.protocol.dubbo.packet.BodyResponse;
73
import io.netty.buffer.ByteBuf;
84
import io.netty.channel.ChannelHandlerContext;
95
import io.netty.handler.codec.ByteToMessageDecoder;
106

11-
import java.io.ByteArrayInputStream;
12-
import java.io.IOException;
137
import java.util.List;
14-
import java.util.Map;
158

169
import static com.github.netty.protocol.dubbo.Constant.*;
1710

@@ -34,7 +27,7 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> ou
3427
case READ_HEADER: {
3528
if (buffer.readableBytes() >= HEADER_LENGTH) {
3629
try {
37-
this.packet = new DubboPacket(readHeader(buffer));
30+
this.packet = new DubboPacket(Header.readHeader(buffer));
3831
} catch (Exception e) {
3932
exception(ctx, buffer, e);
4033
throw e;
@@ -51,7 +44,7 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> ou
5144
ByteBuf body = buffer.readRetainedSlice(this.packet.header.bodyLength);
5245
int markReaderIndex = body.readerIndex();
5346
try {
54-
this.packet.body = readBody(body);
47+
this.packet.body = Body.readBody(body, packet.header);
5548
} catch (Exception e) {
5649
exception(ctx, buffer, e);
5750
this.packet.release();
@@ -76,103 +69,6 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> ou
7669
} while (hasNext);
7770
}
7871

79-
protected Header readHeader(ByteBuf buffer) {
80-
int readerIndex = buffer.readerIndex();
81-
82-
// request and serialization flag. -62 isHeartBeat
83-
byte flag = buffer.getByte(readerIndex + 2);
84-
byte status = buffer.getByte(readerIndex + 3);
85-
long requestId = buffer.getLong(readerIndex + 4);
86-
// 8 - 1-request/0-response
87-
byte type = buffer.getByte(readerIndex + 8);
88-
int bodyLength = buffer.getInt(readerIndex + 12);
89-
90-
ByteBuf headerBytes = buffer.readRetainedSlice(HEADER_LENGTH);
91-
return new Header(headerBytes, flag, status, requestId, type, bodyLength);
92-
}
93-
94-
protected Body readBody(ByteBuf buffer) throws IOException, ClassNotFoundException {
95-
// request and serialization flag.
96-
byte flag = packet.header.flag;
97-
byte status = packet.header.status;
98-
int bodyLength = packet.header.bodyLength;
99-
boolean flagResponse = (flag & FLAG_REQUEST) == 0;
100-
byte serializationProtoId = (byte) (flag & SERIALIZATION_MASK);
101-
if (flagResponse) {
102-
// decode response.
103-
if (status == OK) {
104-
if ((flag & FLAG_EVENT) != 0) {
105-
return readHeartBeat(buffer, bodyLength, serializationProtoId);
106-
} else {
107-
try (Serialization.ObjectInput in = Serialization.codeOfDeserialize(serializationProtoId, buffer, bodyLength)) {
108-
byte responseWith = buffer.readByte();
109-
BodyResponse packetResponse;
110-
switch (responseWith) {
111-
case RESPONSE_NULL_VALUE:
112-
packetResponse = new BodyResponse(null, null, null);
113-
break;
114-
case RESPONSE_VALUE:
115-
packetResponse = new BodyResponse(in.readObject(), null, null);
116-
break;
117-
case RESPONSE_WITH_EXCEPTION:
118-
packetResponse = new BodyResponse(null, in.readThrowable(), null);
119-
break;
120-
case RESPONSE_NULL_VALUE_WITH_ATTACHMENTS:
121-
packetResponse = new BodyResponse(null, null, in.readAttachments());
122-
break;
123-
case RESPONSE_VALUE_WITH_ATTACHMENTS:
124-
packetResponse = new BodyResponse(in.readObject(), null, in.readAttachments());
125-
break;
126-
case RESPONSE_WITH_EXCEPTION_WITH_ATTACHMENTS:
127-
packetResponse = new BodyResponse(null, in.readThrowable(), in.readAttachments());
128-
break;
129-
default:
130-
throw new IOException("Unknown result flag, expect '0' '1' '2' '3' '4' '5', but received: " + responseWith);
131-
}
132-
return packetResponse;
133-
}
134-
}
135-
} else {
136-
try (Serialization.ObjectInput in = Serialization.codeOfDeserialize(serializationProtoId, buffer, bodyLength)) {
137-
return new BodyFail(in.readUTF());
138-
}
139-
}
140-
} else {
141-
// decode request.
142-
if ((flag & FLAG_EVENT) != 0) {
143-
return readHeartBeat(buffer, bodyLength, serializationProtoId);
144-
} else {
145-
try (Serialization.ObjectInput in = Serialization.codeOfDeserialize(serializationProtoId, buffer, bodyLength)) {
146-
String dubboVersion = in.readUTF();
147-
String path = in.readUTF();
148-
String version = in.readUTF();
149-
String methodName = in.readUTF();
150-
String parameterTypesDesc = in.readUTF();
151-
int countArgs = countArgs(parameterTypesDesc);
152-
Object[] args = new Object[countArgs];
153-
for (int i = 0; i < countArgs; i++) {
154-
args[i] = in.readArg();
155-
}
156-
Map<String, Object> attachments = in.readAttachments();
157-
return new BodyRequest(dubboVersion, path, version, methodName, parameterTypesDesc, attachments, args);
158-
}
159-
}
160-
}
161-
}
162-
163-
protected BodyHeartBeat readHeartBeat(ByteBuf buffer, int bodyLength, byte serializationProtoId) throws IOException, ClassNotFoundException {
164-
Object data;
165-
byte[] payload = Serialization.getPayload(buffer, bodyLength);
166-
if (Serialization.isHeartBeat(payload, serializationProtoId)) {
167-
data = null;
168-
} else {
169-
try (Serialization.ObjectInput input = Serialization.codeOfDeserialize(serializationProtoId, new ByteArrayInputStream(payload))) {
170-
data = input.readEvent();
171-
}
172-
}
173-
return new BodyHeartBeat(data);
174-
}
175-
17672
protected <E extends Exception> void exception(ChannelHandlerContext ctx, ByteBuf buffer, E cause) throws Exception {
17773
buffer.release();
17874
ctx.close();

0 commit comments

Comments
 (0)