Skip to content

Commit bb79410

Browse files
2.3.20 snapshot (#40)
1. 默认关闭非http协议 #39 hotfix: #39 2. 修复bug:ServletPrintWriter.write(int) 被转成数字
1 parent 6f63768 commit bb79410

11 files changed

+148
-71
lines changed

README.md

+8-6
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ github地址 : https://github.com/wangzihaogithub/spring-boot-protocol
149149
<dependency>
150150
<groupId>com.github.wangzihaogithub</groupId>
151151
<artifactId>spring-boot-protocol</artifactId>
152-
<version>2.3.19</version>
152+
<version>2.3.20</version>
153153
</dependency>
154154
```
155155

@@ -161,7 +161,7 @@ github地址 : https://github.com/wangzihaogithub/spring-boot-protocol
161161
<dependency>
162162
<groupId>com.github.wangzihaogithub</groupId>
163163
<artifactId>netty-servlet</artifactId>
164-
<version>2.3.19</version>
164+
<version>2.3.20</version>
165165
</dependency>
166166
```
167167

@@ -317,7 +317,7 @@ github地址 : https://github.com/wangzihaogithub/spring-boot-protocol
317317
<dependency>
318318
<groupId>com.github.wangzihaogithub</groupId>
319319
<artifactId>spring-boot-protocol</artifactId>
320-
<version>2.3.19</version>
320+
<version>2.3.20</version>
321321
</dependency>
322322

323323
2.编写代码
@@ -346,7 +346,8 @@ github地址 : https://github.com/wangzihaogithub/spring-boot-protocol
346346
开启h2c
347347
server:
348348
netty:
349-
enable-h2c: true
349+
http-servlet:
350+
enable-h2c: true
350351
351352
或 HttpServletProtocol#setEnableH2c(true)
352353
@@ -379,6 +380,7 @@ github地址 : https://github.com/wangzihaogithub/spring-boot-protocol
379380
"/test", Unpooled.EMPTY_BUFFER);
380381
http2Client.writeAndFlush(request).onSuccess(e -> {
381382
System.out.println(e);
383+
e.release();
382384
});
383385
}
384386

@@ -395,7 +397,7 @@ github地址 : https://github.com/wangzihaogithub/spring-boot-protocol
395397
<dependency>
396398
<groupId>com.github.wangzihaogithub</groupId>
397399
<artifactId>spring-boot-protocol</artifactId>
398-
<version>2.3.19</version>
400+
<version>2.3.20</version>
399401
</dependency>
400402

401403
2.编写代码
@@ -484,7 +486,7 @@ github地址 : https://github.com/wangzihaogithub/spring-boot-protocol
484486
<dependency>
485487
<groupId>com.github.wangzihaogithub</groupId>
486488
<artifactId>spring-boot-protocol</artifactId>
487-
<version>2.3.19</version>
489+
<version>2.3.20</version>
488490
</dependency>
489491
490492
2.编写启动类

pom.xml

+2-2
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.19</version>
7+
<version>2.3.20</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.19</tag>
52+
<tag>v2.3.20</tag>
5353
</scm>
5454

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

src/main/java/com/github/netty/protocol/HttpServletProtocol.java

+10-13
Original file line numberDiff line numberDiff line change
@@ -54,25 +54,13 @@
5454
*/
5555
public class HttpServletProtocol extends AbstractProtocol {
5656
private static final LoggerX LOGGER = LoggerFactoryX.getLogger(HttpServletProtocol.class);
57-
private static final boolean EXIST_JAVAX_WEBSOCKET;
5857
private static final ByteBuf OUT_OF_MAX_CONNECTION_RESPONSE = Unpooled.copiedBuffer(
5958
"HTTP/1.1 503\r\n" +
6059
"Retry-After: 60\r\n" +
6160
"Connection: Close\r\n" +
6261
"Content-Length: 0\r\n" +
6362
"\r\n", Charset.forName("ISO-8859-1"));
6463

65-
static {
66-
boolean existJavaxWebsocket;
67-
try {
68-
Class.forName("javax.websocket.Endpoint");
69-
existJavaxWebsocket = true;
70-
} catch (Throwable e) {
71-
existJavaxWebsocket = false;
72-
}
73-
EXIST_JAVAX_WEBSOCKET = existJavaxWebsocket;
74-
}
75-
7664
private final ServletContext servletContext;
7765
private SslContextBuilder sslContextBuilder;
7866
private SslContext sslContext;
@@ -84,6 +72,7 @@ public class HttpServletProtocol extends AbstractProtocol {
8472
private boolean enableContentCompression = true;
8573
private boolean enableH2c = false;
8674
private boolean enableH2 = HttpConstants.EXIST_DEPENDENCY_H2;
75+
private boolean enableWebsocket = HttpConstants.EXIST_JAVAX_WEBSOCKET;
8776

8877
private int contentSizeThreshold = 8102;
8978
private String[] compressionMimeTypes = {"text/html", "text/xml", "text/plain",
@@ -337,7 +326,7 @@ public String getProtocolName() {
337326
name = name.concat("/h2");
338327
}
339328
}
340-
if (EXIST_JAVAX_WEBSOCKET) {
329+
if (enableWebsocket) {
341330
name = name.concat("/ws");
342331
if (ssl) {
343332
name = name.concat("/wss");
@@ -346,6 +335,14 @@ public String getProtocolName() {
346335
return name;
347336
}
348337

338+
public boolean isEnableWebsocket() {
339+
return enableWebsocket;
340+
}
341+
342+
public void setEnableWebsocket(boolean enableWebsocket) {
343+
this.enableWebsocket = enableWebsocket;
344+
}
345+
349346
public ServletContext getServletContext() {
350347
return servletContext;
351348
}

src/main/java/com/github/netty/protocol/servlet/ServletPrintWriter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ protected void clearError() {
6161

6262
@Override
6363
public void write(int c) {
64-
write(String.valueOf(c));
64+
write(String.valueOf((char) c));
6565
}
6666

6767
@Override

src/main/java/com/github/netty/protocol/servlet/util/HttpConstants.java

+12
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class HttpConstants {
2222
public static final AsciiString H2_EXT_STREAM_ID = AsciiString.cached("x-http2-stream-id");
2323
public static final AsciiString H2_EXT_SCHEME = AsciiString.cached("x-http2-scheme");
2424
public static final boolean EXIST_DEPENDENCY_H2;
25+
public static final boolean EXIST_JAVAX_WEBSOCKET;
2526

2627
static {
2728
boolean isExistH2;
@@ -34,4 +35,15 @@ public class HttpConstants {
3435
EXIST_DEPENDENCY_H2 = isExistH2;
3536
}
3637

38+
static {
39+
boolean existJavaxWebsocket;
40+
try {
41+
Class.forName("javax.websocket.Endpoint");
42+
existJavaxWebsocket = true;
43+
} catch (Throwable e) {
44+
existJavaxWebsocket = false;
45+
}
46+
EXIST_JAVAX_WEBSOCKET = existJavaxWebsocket;
47+
}
48+
3749
}

src/main/java/com/github/netty/springboot/NettyProperties.java

+38
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.github.netty.protocol.mysql.server.MysqlBackendBusinessHandler;
99
import com.github.netty.protocol.nrpc.codec.DataCodecUtil;
1010
import com.github.netty.protocol.servlet.util.HttpAbortPolicyWithReport;
11+
import com.github.netty.protocol.servlet.util.HttpConstants;
1112
import io.netty.handler.logging.LogLevel;
1213
import io.netty.util.ResourceLeakDetector;
1314
import org.springframework.boot.context.properties.ConfigurationProperties;
@@ -248,6 +249,15 @@ public static class HttpServlet {
248249
* 是否开启h2c upgrade: h2c
249250
*/
250251
private boolean enableH2c = false;
252+
/**
253+
* 是否开启h2 upgrade: h2
254+
* 为null则会去取servlet.http.enabled
255+
*/
256+
private Boolean enableH2 = null;
257+
/**
258+
* 是否开启Websocket upgrade: ws
259+
*/
260+
private boolean enableWebsocket = HttpConstants.EXIST_JAVAX_WEBSOCKET;
251261
/**
252262
* 定时刷新缓冲区数据时间间隔(毫秒)
253263
* 当同时连接的客户端数量上千的时候开启(开启减少系统调用次数,批量写数据),否则不建议开启(因为http协议是阻塞协议,不快速返回数据会导致客户端不进行下次请求,反而降低吞吐量).
@@ -307,6 +317,22 @@ public static class HttpServlet {
307317
*/
308318
private boolean startupFailExit = true;
309319

320+
public Boolean getEnableH2() {
321+
return enableH2;
322+
}
323+
324+
public void setEnableH2(Boolean enableH2) {
325+
this.enableH2 = enableH2;
326+
}
327+
328+
public boolean isEnableWebsocket() {
329+
return enableWebsocket;
330+
}
331+
332+
public void setEnableWebsocket(boolean enableWebsocket) {
333+
this.enableWebsocket = enableWebsocket;
334+
}
335+
310336
public boolean isEnableH2c() {
311337
return enableH2c;
312338
}
@@ -622,6 +648,10 @@ public static class Nrpc {
622648
*/
623649
@NestedConfigurationProperty
624650
private final ServerThreadPool threadPool = new ServerThreadPool();
651+
/**
652+
* 是否开启rpc代理
653+
*/
654+
private boolean enabled = false;
625655
/**
626656
* 编码-fastjson最快,jdk需要实现序列化接口
627657
*/
@@ -679,6 +709,14 @@ public static class Nrpc {
679709
*/
680710
private String serverDefaultVersion = "";
681711

712+
public boolean isEnabled() {
713+
return enabled;
714+
}
715+
716+
public void setEnabled(boolean enabled) {
717+
this.enabled = enabled;
718+
}
719+
682720
public Codec getCodec() {
683721
return codec;
684722
}

src/main/java/com/github/netty/springboot/SpringUtil.java

+9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import io.netty.handler.ssl.ClientAuth;
55
import io.netty.handler.ssl.SslContextBuilder;
66
import org.springframework.beans.factory.BeanFactory;
7+
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
78
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
89
import org.springframework.beans.factory.support.DefaultSingletonBeanRegistry;
910
import org.springframework.boot.web.server.Ssl;
@@ -21,6 +22,14 @@
2122

2223
public class SpringUtil {
2324

25+
public static <T> T getBean(BeanFactory beanFactory, Class<T> requiredType) {
26+
try {
27+
return beanFactory.getBean(requiredType);
28+
} catch (NoSuchBeanDefinitionException e) {
29+
return null;
30+
}
31+
}
32+
2433
public static boolean isSingletonBean(BeanFactory beanFactory, String beanName) {
2534
if (beanFactory instanceof DefaultSingletonBeanRegistry
2635
&& ((DefaultSingletonBeanRegistry) beanFactory).isSingletonCurrentlyInCreation(beanName)) {

0 commit comments

Comments
 (0)