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/incubator-shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new 6819a5c [ISSUE #3033] [type:new feature] support config netty thread
pool in HttpClient (#3034)
6819a5c is described below
commit 6819a5c4799258efbea388823f7ca125b3e335b3
Author: dragon-zhang <[email protected]>
AuthorDate: Sat Mar 12 11:23:43 2022 +0800
[ISSUE #3033] [type:new feature] support config netty thread pool in
HttpClient (#3034)
* [ISSUE #3033] [type:new feature] support config netty thread pool in
HttpClient
* fix code style
* fix bug
---
.../src/main/resources/application.yml | 8 +-
.../httpclient/config/HttpClientProperties.java | 149 ++++++++++++++++++++-
.../httpclient/HttpClientPluginConfiguration.java | 9 +-
3 files changed, 163 insertions(+), 3 deletions(-)
diff --git a/shenyu-bootstrap/src/main/resources/application.yml
b/shenyu-bootstrap/src/main/resources/application.yml
index 08e38fc..8a7b801 100644
--- a/shenyu-bootstrap/src/main/resources/application.yml
+++ b/shenyu-bootstrap/src/main/resources/application.yml
@@ -118,7 +118,13 @@ shenyu:
# handshakeTimeout:
# closeNotifyFlushTimeout:
# closeNotifyReadTimeout:
- # defaultConfigurationType:
+ # defaultConfigurationType:
+ # threadPool:
+ # prefix: shenyu
+ # selectCount: 1
+ # workerCount: 8
+ # daemon: true
+ # preferNative: true
instance:
enabled: false
registerType: zookeeper #etcd #consul
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 49050de..2f8b85e 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
@@ -20,7 +20,9 @@ package org.apache.shenyu.plugin.httpclient.config;
import org.apache.commons.lang3.StringUtils;
import org.apache.shenyu.common.exception.ShenyuException;
import org.springframework.util.ResourceUtils;
+import reactor.netty.ReactorNetty;
import reactor.netty.resources.ConnectionProvider;
+import reactor.netty.resources.LoopResources;
import reactor.netty.tcp.SslProvider;
import javax.net.ssl.KeyManagerFactory;
@@ -90,6 +92,11 @@ public class HttpClientProperties {
private Pool pool = new Pool();
/**
+ * ThreadPool configuration for Netty HttpClient.
+ */
+ private ThreadPool threadPool = new ThreadPool();
+
+ /**
* Proxy configuration for Netty HttpClient.
*/
private Proxy proxy = new Proxy();
@@ -271,7 +278,25 @@ public class HttpClientProperties {
public void setPool(final Pool pool) {
this.pool = pool;
}
-
+
+ /**
+ * Gets thread pool.
+ *
+ * @return the thread pool
+ */
+ public ThreadPool getThreadPool() {
+ return threadPool;
+ }
+
+ /**
+ * Sets thread pool.
+ *
+ * @param threadPool the thread pool
+ */
+ public void setThreadPool(final ThreadPool threadPool) {
+ this.threadPool = threadPool;
+ }
+
/**
* Gets proxy.
*
@@ -488,6 +513,128 @@ public class HttpClientProperties {
DISABLED
}
}
+
+ /**
+ * The type Thread Pool.
+ */
+ public static class ThreadPool {
+
+ /**
+ * The the event loop thread name prefix, defaults to shenyu.
+ */
+ private String prefix = "shenyu";
+
+ /**
+ * The selector thread count, defaults to 1.
+ */
+ private Integer selectCount =
Integer.parseInt(System.getProperty(ReactorNetty.IO_SELECT_COUNT, "1"));
+
+ /**
+ * The worker thread count, defaults to available processor (but with
a minimum value of 4).
+ */
+ private Integer workerCount = LoopResources.DEFAULT_IO_WORKER_COUNT;
+
+ /**
+ * Whether the thread created by the thread pool is a daemon thread.
+ */
+ private Boolean daemon = true;
+
+ /**
+ * Whether the native transport (epoll, kqueue) will be preferred,
+ * fallback it will be preferred when available.
+ */
+ private Boolean preferNative = LoopResources.DEFAULT_NATIVE;
+
+ /**
+ * Gets prefix.
+ *
+ * @return the prefix
+ */
+ public String getPrefix() {
+ return prefix;
+ }
+
+ /**
+ * Sets prefix.
+ *
+ * @param prefix the prefix
+ */
+ public void setPrefix(final String prefix) {
+ this.prefix = prefix;
+ }
+
+ /**
+ * Gets select count.
+ *
+ * @return the select count
+ */
+ public Integer getSelectCount() {
+ return selectCount;
+ }
+
+ /**
+ * Sets select count.
+ *
+ * @param selectCount the select count
+ */
+ public void setSelectCount(final Integer selectCount) {
+ this.selectCount = selectCount;
+ }
+
+ /**
+ * Gets worker count.
+ *
+ * @return the worker count
+ */
+ public Integer getWorkerCount() {
+ return workerCount;
+ }
+
+ /**
+ * Sets worker count.
+ *
+ * @param workerCount the worker count
+ */
+ public void setWorkerCount(final Integer workerCount) {
+ this.workerCount = workerCount;
+ }
+
+ /**
+ * Gets daemon.
+ *
+ * @return the daemon
+ */
+ public Boolean getDaemon() {
+ return daemon;
+ }
+
+ /**
+ * Sets daemon.
+ *
+ * @param daemon the daemon
+ */
+ public void setDaemon(final Boolean daemon) {
+ this.daemon = daemon;
+ }
+
+ /**
+ * Gets prefer native.
+ *
+ * @return the prefer native
+ */
+ public Boolean getPreferNative() {
+ return preferNative;
+ }
+
+ /**
+ * Sets prefer native.
+ *
+ * @param preferNative the prefer native
+ */
+ public void setPreferNative(final Boolean preferNative) {
+ this.preferNative = preferNative;
+ }
+ }
/**
* The type 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 6f3c5b6..72fd1ce 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
@@ -39,6 +39,7 @@ import
org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.netty.http.client.HttpClient;
import reactor.netty.resources.ConnectionProvider;
+import reactor.netty.resources.LoopResources;
import reactor.netty.tcp.ProxyProvider;
import java.security.cert.X509Certificate;
@@ -108,10 +109,16 @@ public class HttpClientPluginConfiguration {
connection.addHandlerLast(new
WriteTimeoutHandler(properties.getWriteTimeout(), TimeUnit.MILLISECONDS));
connection.addHandlerLast(new
ReadTimeoutHandler(properties.getReadTimeout(), TimeUnit.MILLISECONDS));
});
+ HttpClientProperties.ThreadPool threadPool =
properties.getThreadPool();
+ if (StringUtils.isNotEmpty(threadPool.getPrefix())) {
+ LoopResources resources =
LoopResources.create(threadPool.getPrefix(),
+ threadPool.getSelectCount(),
threadPool.getWorkerCount(), threadPool.getDaemon());
+ tcpClient = tcpClient.runOn(resources,
threadPool.getPreferNative());
+ }
return tcpClient;
});
HttpClientProperties.Ssl ssl = properties.getSsl();
- if (StringUtils.isNotEmpty(ssl.getKeyStorePath())
+ if (StringUtils.isNotEmpty(ssl.getKeyStorePath())
||
ArrayUtils.isNotEmpty(ssl.getTrustedX509CertificatesForTrustManager())
|| ssl.isUseInsecureTrustManager()) {
httpClient = httpClient.secure(sslContextSpec -> {