Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2.3.20 snapshot #40

Merged
merged 5 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ github地址 : https://github.com/wangzihaogithub/spring-boot-protocol
<dependency>
<groupId>com.github.wangzihaogithub</groupId>
<artifactId>spring-boot-protocol</artifactId>
<version>2.3.19</version>
<version>2.3.20</version>
</dependency>
```

Expand All @@ -161,7 +161,7 @@ github地址 : https://github.com/wangzihaogithub/spring-boot-protocol
<dependency>
<groupId>com.github.wangzihaogithub</groupId>
<artifactId>netty-servlet</artifactId>
<version>2.3.19</version>
<version>2.3.20</version>
</dependency>
```

Expand Down Expand Up @@ -317,7 +317,7 @@ github地址 : https://github.com/wangzihaogithub/spring-boot-protocol
<dependency>
<groupId>com.github.wangzihaogithub</groupId>
<artifactId>spring-boot-protocol</artifactId>
<version>2.3.19</version>
<version>2.3.20</version>
</dependency>

2.编写代码
Expand Down Expand Up @@ -346,7 +346,8 @@ github地址 : https://github.com/wangzihaogithub/spring-boot-protocol
开启h2c
server:
netty:
enable-h2c: true
http-servlet:
enable-h2c: true

或 HttpServletProtocol#setEnableH2c(true)

Expand Down Expand Up @@ -379,6 +380,7 @@ github地址 : https://github.com/wangzihaogithub/spring-boot-protocol
"/test", Unpooled.EMPTY_BUFFER);
http2Client.writeAndFlush(request).onSuccess(e -> {
System.out.println(e);
e.release();
});
}

Expand All @@ -395,7 +397,7 @@ github地址 : https://github.com/wangzihaogithub/spring-boot-protocol
<dependency>
<groupId>com.github.wangzihaogithub</groupId>
<artifactId>spring-boot-protocol</artifactId>
<version>2.3.19</version>
<version>2.3.20</version>
</dependency>

2.编写代码
Expand Down Expand Up @@ -484,7 +486,7 @@ github地址 : https://github.com/wangzihaogithub/spring-boot-protocol
<dependency>
<groupId>com.github.wangzihaogithub</groupId>
<artifactId>spring-boot-protocol</artifactId>
<version>2.3.19</version>
<version>2.3.20</version>
</dependency>

2.编写启动类
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.github.wangzihaogithub</groupId>
<artifactId>spring-boot-protocol</artifactId>
<version>2.3.19</version>
<version>2.3.20</version>
<packaging>jar</packaging>

<name>Spring Boot Protocol</name>
Expand Down Expand Up @@ -49,7 +49,7 @@
<connection>scm:git:https://github.com/wangzihaogithub/spring-boot-protocol.git</connection>
<developerConnection>scm:git:git@github.com:wangzihaogithub/spring-boot-protocol.git</developerConnection>
<url>git@github.com:wangzihaogithub/spring-boot-protocol.git</url>
<tag>v2.3.19</tag>
<tag>v2.3.20</tag>
</scm>

<!-- 开发者信息 -->
Expand Down
23 changes: 10 additions & 13 deletions src/main/java/com/github/netty/protocol/HttpServletProtocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,13 @@
*/
public class HttpServletProtocol extends AbstractProtocol {
private static final LoggerX LOGGER = LoggerFactoryX.getLogger(HttpServletProtocol.class);
private static final boolean EXIST_JAVAX_WEBSOCKET;
private static final ByteBuf OUT_OF_MAX_CONNECTION_RESPONSE = Unpooled.copiedBuffer(
"HTTP/1.1 503\r\n" +
"Retry-After: 60\r\n" +
"Connection: Close\r\n" +
"Content-Length: 0\r\n" +
"\r\n", Charset.forName("ISO-8859-1"));

static {
boolean existJavaxWebsocket;
try {
Class.forName("javax.websocket.Endpoint");
existJavaxWebsocket = true;
} catch (Throwable e) {
existJavaxWebsocket = false;
}
EXIST_JAVAX_WEBSOCKET = existJavaxWebsocket;
}

private final ServletContext servletContext;
private SslContextBuilder sslContextBuilder;
private SslContext sslContext;
Expand All @@ -84,6 +72,7 @@ public class HttpServletProtocol extends AbstractProtocol {
private boolean enableContentCompression = true;
private boolean enableH2c = false;
private boolean enableH2 = HttpConstants.EXIST_DEPENDENCY_H2;
private boolean enableWebsocket = HttpConstants.EXIST_JAVAX_WEBSOCKET;

private int contentSizeThreshold = 8102;
private String[] compressionMimeTypes = {"text/html", "text/xml", "text/plain",
Expand Down Expand Up @@ -337,7 +326,7 @@ public String getProtocolName() {
name = name.concat("/h2");
}
}
if (EXIST_JAVAX_WEBSOCKET) {
if (enableWebsocket) {
name = name.concat("/ws");
if (ssl) {
name = name.concat("/wss");
Expand All @@ -346,6 +335,14 @@ public String getProtocolName() {
return name;
}

public boolean isEnableWebsocket() {
return enableWebsocket;
}

public void setEnableWebsocket(boolean enableWebsocket) {
this.enableWebsocket = enableWebsocket;
}

public ServletContext getServletContext() {
return servletContext;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ protected void clearError() {

@Override
public void write(int c) {
write(String.valueOf(c));
write(String.valueOf((char) c));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class HttpConstants {
public static final AsciiString H2_EXT_STREAM_ID = AsciiString.cached("x-http2-stream-id");
public static final AsciiString H2_EXT_SCHEME = AsciiString.cached("x-http2-scheme");
public static final boolean EXIST_DEPENDENCY_H2;
public static final boolean EXIST_JAVAX_WEBSOCKET;

static {
boolean isExistH2;
Expand All @@ -34,4 +35,15 @@ public class HttpConstants {
EXIST_DEPENDENCY_H2 = isExistH2;
}

static {
boolean existJavaxWebsocket;
try {
Class.forName("javax.websocket.Endpoint");
existJavaxWebsocket = true;
} catch (Throwable e) {
existJavaxWebsocket = false;
}
EXIST_JAVAX_WEBSOCKET = existJavaxWebsocket;
}

}
38 changes: 38 additions & 0 deletions src/main/java/com/github/netty/springboot/NettyProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.github.netty.protocol.mysql.server.MysqlBackendBusinessHandler;
import com.github.netty.protocol.nrpc.codec.DataCodecUtil;
import com.github.netty.protocol.servlet.util.HttpAbortPolicyWithReport;
import com.github.netty.protocol.servlet.util.HttpConstants;
import io.netty.handler.logging.LogLevel;
import io.netty.util.ResourceLeakDetector;
import org.springframework.boot.context.properties.ConfigurationProperties;
Expand Down Expand Up @@ -248,6 +249,15 @@ public static class HttpServlet {
* 是否开启h2c upgrade: h2c
*/
private boolean enableH2c = false;
/**
* 是否开启h2 upgrade: h2
* 为null则会去取servlet.http.enabled
*/
private Boolean enableH2 = null;
/**
* 是否开启Websocket upgrade: ws
*/
private boolean enableWebsocket = HttpConstants.EXIST_JAVAX_WEBSOCKET;
/**
* 定时刷新缓冲区数据时间间隔(毫秒)
* 当同时连接的客户端数量上千的时候开启(开启减少系统调用次数,批量写数据),否则不建议开启(因为http协议是阻塞协议,不快速返回数据会导致客户端不进行下次请求,反而降低吞吐量).
Expand Down Expand Up @@ -307,6 +317,22 @@ public static class HttpServlet {
*/
private boolean startupFailExit = true;

public Boolean getEnableH2() {
return enableH2;
}

public void setEnableH2(Boolean enableH2) {
this.enableH2 = enableH2;
}

public boolean isEnableWebsocket() {
return enableWebsocket;
}

public void setEnableWebsocket(boolean enableWebsocket) {
this.enableWebsocket = enableWebsocket;
}

public boolean isEnableH2c() {
return enableH2c;
}
Expand Down Expand Up @@ -622,6 +648,10 @@ public static class Nrpc {
*/
@NestedConfigurationProperty
private final ServerThreadPool threadPool = new ServerThreadPool();
/**
* 是否开启rpc代理
*/
private boolean enabled = false;
/**
* 编码-fastjson最快,jdk需要实现序列化接口
*/
Expand Down Expand Up @@ -679,6 +709,14 @@ public static class Nrpc {
*/
private String serverDefaultVersion = "";

public boolean isEnabled() {
return enabled;
}

public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

public Codec getCodec() {
return codec;
}
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/github/netty/springboot/SpringUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.netty.handler.ssl.ClientAuth;
import io.netty.handler.ssl.SslContextBuilder;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.beans.factory.support.DefaultSingletonBeanRegistry;
import org.springframework.boot.web.server.Ssl;
Expand All @@ -21,6 +22,14 @@

public class SpringUtil {

public static <T> T getBean(BeanFactory beanFactory, Class<T> requiredType) {
try {
return beanFactory.getBean(requiredType);
} catch (NoSuchBeanDefinitionException e) {
return null;
}
}

public static boolean isSingletonBean(BeanFactory beanFactory, String beanName) {
if (beanFactory instanceof DefaultSingletonBeanRegistry
&& ((DefaultSingletonBeanRegistry) beanFactory).isSingletonCurrentlyInCreation(beanName)) {
Expand Down
Loading