This is an automated email from the ASF dual-hosted git repository.
xiaoyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new 38182edc93 [type:feat] support http2 server (#5125)
38182edc93 is described below
commit 38182edc9321750e68c86473acafb15a052d8316
Author: moremind <[email protected]>
AuthorDate: Fri Sep 8 12:13:52 2023 +0800
[type:feat] support http2 server (#5125)
* [type:feat] support http2 ssl
* [type:feat] support http2 ssl
* [type:feat] support http2 ssl
* [type:feat] support http2 ssl
* [type:feat] support http2 ssl
---------
Co-authored-by: xiaoyu <[email protected]>
---
.../httpclient/config/HttpClientProperties.java | 4 +--
.../starter/gateway/ShenyuConfigurationTest.java | 2 ++
.../plugin/httpclient/HttpClientFactory.java | 39 ++++++++++++++--------
.../httpclient/HttpClientPluginConfiguration.java | 7 ++--
.../HttpClientPluginConfigurationTest.java | 9 +++--
5 files changed, 38 insertions(+), 23 deletions(-)
diff --git
a/shenyu-plugin/shenyu-plugin-httpclient/src/main/java/org/apache/shenyu/plugin/httpclient/config/HttpClientProperties.java
b/shenyu-plugin/shenyu-plugin-httpclient/src/main/java/org/apache/shenyu/plugin/httpclient/config/HttpClientProperties.java
index b0048c3565..778e89b3e7 100644
---
a/shenyu-plugin/shenyu-plugin-httpclient/src/main/java/org/apache/shenyu/plugin/httpclient/config/HttpClientProperties.java
+++
b/shenyu-plugin/shenyu-plugin-httpclient/src/main/java/org/apache/shenyu/plugin/httpclient/config/HttpClientProperties.java
@@ -764,7 +764,7 @@ public class HttpClientProperties {
* Installs the netty InsecureTrustManagerFactory. This is insecure
and not
* suitable for production.
*/
- private boolean useInsecureTrustManager;
+ private boolean useInsecureTrustManager = Boolean.FALSE;
/**
* Trusted certificates for verifying the remote endpoint's
certificate.
@@ -812,7 +812,7 @@ public class HttpClientProperties {
/**
* The default ssl configuration type. Defaults to JDK Provider.
*/
- private SslProvider defaultConfigurationType = SslProvider.JDK;
+ private SslProvider defaultConfigurationType;
/**
* Is use insecure trust manager boolean.
diff --git
a/shenyu-spring-boot-starter/shenyu-spring-boot-starter-gateway/src/test/java/org/apache/shenyu/springboot/starter/gateway/ShenyuConfigurationTest.java
b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-gateway/src/test/java/org/apache/shenyu/springboot/starter/gateway/ShenyuConfigurationTest.java
index 945eaa985d..619e66f1b0 100644
---
a/shenyu-spring-boot-starter/shenyu-spring-boot-starter-gateway/src/test/java/org/apache/shenyu/springboot/starter/gateway/ShenyuConfigurationTest.java
+++
b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-gateway/src/test/java/org/apache/shenyu/springboot/starter/gateway/ShenyuConfigurationTest.java
@@ -26,6 +26,7 @@ import org.apache.shenyu.web.loader.ShenyuLoaderService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
+import org.springframework.boot.autoconfigure.web.ServerProperties;
import
org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import
org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory;
@@ -55,6 +56,7 @@ public class ShenyuConfigurationTest {
.withBean(ShenyuConfigurationTest.class)
.withBean(DefaultServerCodecConfigurer.class)
.withBean(DefaultErrorAttributes.class)
+ .withBean(ServerProperties.class)
.withPropertyValues(
"debug=true",
"shenyu.cross.enabled=true",
diff --git
a/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-httpclient/src/main/java/org/apache/shenyu/springboot/starter/plugin/httpclient/HttpClientFactory.java
b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-httpclient/src/main/java/org/apache/shenyu/springboot/starter/plugin/httpclient/HttpClientFactory.java
index 2468ca43d2..088aefbe31 100644
---
a/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-httpclient/src/main/java/org/apache/shenyu/springboot/starter/plugin/httpclient/HttpClientFactory.java
+++
b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-httpclient/src/main/java/org/apache/shenyu/springboot/starter/plugin/httpclient/HttpClientFactory.java
@@ -26,11 +26,14 @@ import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shenyu.plugin.httpclient.config.HttpClientProperties;
import org.springframework.beans.factory.config.AbstractFactoryBean;
+import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.properties.PropertyMapper;
+import reactor.netty.http.Http11SslContextSpec;
+import reactor.netty.http.Http2SslContextSpec;
+import reactor.netty.http.HttpProtocol;
import reactor.netty.http.client.HttpClient;
import reactor.netty.resources.ConnectionProvider;
import reactor.netty.resources.LoopResources;
-import reactor.netty.tcp.DefaultSslContextSpec;
import reactor.netty.tcp.SslProvider;
import reactor.netty.transport.ProxyProvider;
@@ -44,10 +47,15 @@ public class HttpClientFactory extends
AbstractFactoryBean<HttpClient> {
private final HttpClientProperties properties;
private final LoopResources loopResources;
+
+ private final ServerProperties serverProperties;
- public HttpClientFactory(final HttpClientProperties httpClientProperties,
final LoopResources loopResources) {
+ public HttpClientFactory(final HttpClientProperties httpClientProperties,
+ final LoopResources loopResources,
+ final ServerProperties serverProperties) {
this.properties = httpClientProperties;
this.loopResources = loopResources;
+ this.serverProperties = serverProperties;
}
@Override
@@ -78,18 +86,18 @@ public class HttpClientFactory extends
AbstractFactoryBean<HttpClient> {
}
private void setSsl(final SslProvider.SslContextSpec sslContextSpec, final
HttpClientProperties.Ssl ssl) {
- SslProvider.ProtocolSslContextSpec spec =
DefaultSslContextSpec.forClient()
- .configure(sslContextBuilder -> {
- X509Certificate[] trustedX509Certificates =
ssl.getTrustedX509CertificatesForTrustManager();
- if (ArrayUtils.isNotEmpty(trustedX509Certificates)) {
-
sslContextBuilder.trustManager(trustedX509Certificates);
- } else if (ssl.isUseInsecureTrustManager()) {
-
sslContextBuilder.trustManager(InsecureTrustManagerFactory.INSTANCE);
- }
- sslContextBuilder.keyManager(ssl.getKeyManagerFactory());
-
sslContextBuilder.sslProvider(ssl.getDefaultConfigurationType());
- });
- sslContextSpec.sslContext(spec)
+ SslProvider.ProtocolSslContextSpec clientSslContext =
(serverProperties.getHttp2().isEnabled())
+ ? Http2SslContextSpec.forClient() :
Http11SslContextSpec.forClient();
+ clientSslContext.configure(sslContextBuilder -> {
+ X509Certificate[] trustedX509Certificates =
ssl.getTrustedX509CertificatesForTrustManager();
+ if (ArrayUtils.isNotEmpty(trustedX509Certificates)) {
+ sslContextBuilder.trustManager(trustedX509Certificates);
+ } else if (ssl.isUseInsecureTrustManager()) {
+
sslContextBuilder.trustManager(InsecureTrustManagerFactory.INSTANCE);
+ }
+ sslContextBuilder.keyManager(ssl.getKeyManagerFactory());
+ });
+ sslContextSpec.sslContext(clientSslContext)
.handshakeTimeout(ssl.getHandshakeTimeout())
.closeNotifyFlushTimeout(ssl.getCloseNotifyFlushTimeout())
.closeNotifyReadTimeout(ssl.getCloseNotifyReadTimeout());
@@ -102,6 +110,9 @@ public class HttpClientFactory extends
AbstractFactoryBean<HttpClient> {
ConnectionProvider connectionProvider = buildConnectionProvider(pool);
HttpClient httpClient = HttpClient.create(connectionProvider)
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS,
properties.getConnectTimeout());
+ if (serverProperties.getHttp2().isEnabled()) {
+ httpClient = httpClient.protocol(HttpProtocol.HTTP11,
HttpProtocol.H2);
+ }
HttpClientProperties.Proxy proxy = properties.getProxy();
if (StringUtils.isNotEmpty(proxy.getHost())) {
httpClient = setHttpClientProxy(httpClient, proxy);
diff --git
a/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-httpclient/src/main/java/org/apache/shenyu/springboot/starter/plugin/httpclient/HttpClientPluginConfiguration.java
b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-httpclient/src/main/java/org/apache/shenyu/springboot/starter/plugin/httpclient/HttpClientPluginConfiguration.java
index aff9487d4d..7c6c27d259 100644
---
a/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-httpclient/src/main/java/org/apache/shenyu/springboot/starter/plugin/httpclient/HttpClientPluginConfiguration.java
+++
b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-httpclient/src/main/java/org/apache/shenyu/springboot/starter/plugin/httpclient/HttpClientPluginConfiguration.java
@@ -24,6 +24,7 @@ import
org.apache.shenyu.plugin.httpclient.config.HttpClientProperties;
import org.springframework.beans.factory.ObjectProvider;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -71,13 +72,15 @@ public class HttpClientPluginConfiguration {
*
* @param properties the properties
* @param provider the loop resources bean provider
+ * @param serverProperties the server properties
* @return the http client
*/
@Bean
@ConditionalOnMissingBean({HttpClient.class, HttpClientFactory.class})
public HttpClientFactory httpClient(final HttpClientProperties properties,
- final ObjectProvider<LoopResources> provider)
{
- return new HttpClientFactory(properties, provider.getIfAvailable());
+ final ObjectProvider<LoopResources>
provider,
+ final ServerProperties
serverProperties) {
+ return new HttpClientFactory(properties, provider.getIfAvailable(),
serverProperties);
}
diff --git
a/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-httpclient/src/test/java/org/apache/shenyu/springboot/starter/plugin/httpclient/HttpClientPluginConfigurationTest.java
b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-httpclient/src/test/java/org/apache/shenyu/springboot/starter/plugin/httpclient/HttpClientPluginConfigurationTest.java
index 2047392cfb..f2d852c20a 100644
---
a/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-httpclient/src/test/java/org/apache/shenyu/springboot/starter/plugin/httpclient/HttpClientPluginConfigurationTest.java
+++
b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-httpclient/src/test/java/org/apache/shenyu/springboot/starter/plugin/httpclient/HttpClientPluginConfigurationTest.java
@@ -23,12 +23,12 @@ import static
org.junit.jupiter.api.Assertions.assertNotNull;
import java.time.Duration;
-import io.netty.handler.ssl.SslProvider;
import org.apache.shenyu.plugin.api.ShenyuPlugin;
import org.apache.shenyu.plugin.httpclient.config.HttpClientProperties;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
+import org.springframework.boot.autoconfigure.web.ServerProperties;
import
org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Configuration;
@@ -49,7 +49,8 @@ public class HttpClientPluginConfigurationTest {
public void before() {
applicationContextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(HttpClientPluginConfiguration.class))
- .withBean(HttpClientPluginConfigurationTest.class);
+ .withBean(HttpClientPluginConfigurationTest.class)
+ .withBean(ServerProperties.class);
}
@Test
@@ -71,8 +72,7 @@ public class HttpClientPluginConfigurationTest {
"shenyu.httpclient.ssl.X509Certificate[]=[]",
"shenyu.httpclient.ssl.handshakeTimeout=10000",
"shenyu.httpclient.ssl.closeNotifyFlushTimeout=3000",
- "shenyu.httpclient.ssl.closeNotifyReadTimeout=0",
-
"shenyu.httpclient.ssl.SslProvider.DefaultConfigurationType=1"
+ "shenyu.httpclient.ssl.closeNotifyReadTimeout=0"
)
.run(context -> {
HttpClientProperties properties =
context.getBean("httpClientProperties", HttpClientProperties.class);
@@ -92,7 +92,6 @@ public class HttpClientPluginConfigurationTest {
assertNotNull(properties.getSsl().getTrustedX509Certificates());
assertThat(properties.getSsl().getCloseNotifyFlushTimeout(),
is(Duration.ofMillis(3000)));
assertThat(properties.getSsl().getCloseNotifyReadTimeout(), is(Duration.ZERO));
-
assertThat(properties.getSsl().getDefaultConfigurationType(),
is(SslProvider.JDK));
});
}