You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shenyu.apache.org by zh...@apache.org on 2023/09/26 03:47:02 UTC

[shenyu] branch master updated: [type:refactor] refactor http client properties (#5151)

This is an automated email from the ASF dual-hosted git repository.

zhangzicheng 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 fe5077c727 [type:refactor] refactor http client properties (#5151)
fe5077c727 is described below

commit fe5077c72791c62e5c03801907883690f3ba42a8
Author: moremind <he...@apache.org>
AuthorDate: Tue Sep 26 11:46:55 2023 +0800

    [type:refactor] refactor http client properties (#5151)
    
    * [type:refactor] refactor http client properties
    
    * [type:fix] fix k8s example error
    
    * [type:refactor] refactor http client
    
    * [type:refactor] refactor http client
    
    * [type:refactor] refactor http client
    
    * [type:refactor] refactor http client
    
    ---------
    
    Co-authored-by: dragon-zhang <zh...@apache.org>
    Co-authored-by: DamonXue <da...@gmail.com>
---
 .../src/main/resources/application.yml             |  5 +-
 .../httpclient/AbstractHttpClientPlugin.java       | 28 ++------
 .../plugin/httpclient/NettyHttpClientPlugin.java   | 10 +--
 .../shenyu/plugin/httpclient/WebClientPlugin.java  | 11 ++--
 .../httpclient/config/HttpClientProperties.java    | 76 ++++++++++++++++++++--
 .../httpclient/HttpClientPropertiesTest.java       |  2 +-
 .../plugin/httpclient/HttpClientFactory.java       | 56 ++++++++--------
 7 files changed, 125 insertions(+), 63 deletions(-)

diff --git a/shenyu-bootstrap/src/main/resources/application.yml b/shenyu-bootstrap/src/main/resources/application.yml
index 56eef4377e..c7b861e32c 100644
--- a/shenyu-bootstrap/src/main/resources/application.yml
+++ b/shenyu-bootstrap/src/main/resources/application.yml
@@ -168,7 +168,10 @@ shenyu:
 #      name: proxy
 #      maxConnections: 16
 #      acquireTimeout: 45000
-#      maxIdleTime: 3000
+#      maxIdleTime: 3000 # unit: ms
+#      maxLifeTime:  # unit: ms
+#      evictionInterval:  # unit: ms
+#      metrics: false
 #    proxy:
 #      host:
 #      port:
diff --git a/shenyu-plugin/shenyu-plugin-httpclient/src/main/java/org/apache/shenyu/plugin/httpclient/AbstractHttpClientPlugin.java b/shenyu-plugin/shenyu-plugin-httpclient/src/main/java/org/apache/shenyu/plugin/httpclient/AbstractHttpClientPlugin.java
index 096d710ca7..f293191046 100644
--- a/shenyu-plugin/shenyu-plugin-httpclient/src/main/java/org/apache/shenyu/plugin/httpclient/AbstractHttpClientPlugin.java
+++ b/shenyu-plugin/shenyu-plugin-httpclient/src/main/java/org/apache/shenyu/plugin/httpclient/AbstractHttpClientPlugin.java
@@ -39,7 +39,6 @@ import org.apache.shenyu.plugin.httpclient.exception.ShenyuTimeoutException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.core.io.buffer.DataBuffer;
-import org.springframework.http.HttpHeaders;
 import org.springframework.http.HttpStatus;
 import org.springframework.web.server.ResponseStatusException;
 import org.springframework.web.server.ServerWebExchange;
@@ -79,8 +78,7 @@ public abstract class AbstractHttpClientPlugin<R> implements ShenyuPlugin {
         final int retryTimes = (int) Optional.ofNullable(exchange.getAttribute(Constants.HTTP_RETRY)).orElse(0);
         final String retryStrategy = (String) Optional.ofNullable(exchange.getAttribute(Constants.RETRY_STRATEGY)).orElseGet(RetryEnum.CURRENT::getName);
         LogUtils.debug(LOG, () -> String.format("The request urlPath is: %s, retryTimes is : %s, retryStrategy is : %s", uri, retryTimes, retryStrategy));
-        final HttpHeaders httpHeaders = buildHttpHeaders(exchange);
-        final Mono<R> response = doRequest(exchange, exchange.getRequest().getMethodValue(), uri, httpHeaders, exchange.getRequest().getBody())
+        final Mono<R> response = doRequest(exchange, exchange.getRequest().getMethodValue(), uri, exchange.getRequest().getBody())
                 .timeout(duration, Mono.error(() -> new TimeoutException("Response took longer than timeout: " + duration)))
                 .doOnError(e -> LOG.error(e.getMessage(), e));
         if (RetryEnum.CURRENT.getName().equals(retryStrategy)) {
@@ -100,7 +98,7 @@ public abstract class AbstractHttpClientPlugin<R> implements ShenyuPlugin {
                     .flatMap((Function<Object, Mono<? extends Void>>) o -> chain.execute(exchange));
         }
         final Set<URI> exclude = Sets.newHashSet(uri);
-        return resend(response, exchange, duration, httpHeaders, exclude, retryTimes)
+        return resend(response, exchange, duration, exclude, retryTimes)
                 .onErrorMap(ShenyuException.class, th -> new ResponseStatusException(HttpStatus.SERVICE_UNAVAILABLE,
                         ShenyuResultEnum.CANNOT_FIND_HEALTHY_UPSTREAM_URL_AFTER_FAILOVER.getMsg(), th))
                 .onErrorMap(TimeoutException.class, th -> new ResponseStatusException(HttpStatus.GATEWAY_TIMEOUT, th.getMessage(), th))
@@ -110,12 +108,11 @@ public abstract class AbstractHttpClientPlugin<R> implements ShenyuPlugin {
     private Mono<R> resend(final Mono<R> clientResponse,
                            final ServerWebExchange exchange,
                            final Duration duration,
-                           final HttpHeaders httpHeaders,
                            final Set<URI> exclude,
                            final int retryTimes) {
         Mono<R> result = clientResponse;
         for (int i = 0; i < retryTimes; i++) {
-            result = resend(result, exchange, duration, httpHeaders, exclude);
+            result = resend(result, exchange, duration, exclude);
         }
         return result;
     }
@@ -123,7 +120,6 @@ public abstract class AbstractHttpClientPlugin<R> implements ShenyuPlugin {
     private Mono<R> resend(final Mono<R> response,
                            final ServerWebExchange exchange,
                            final Duration duration,
-                           final HttpHeaders httpHeaders,
                            final Set<URI> exclude) {
         // does it necessary to add backoff interval time ?
         return response.onErrorResume(th -> {
@@ -154,36 +150,22 @@ public abstract class AbstractHttpClientPlugin<R> implements ShenyuPlugin {
             final URI newUri = RequestUrlUtils.buildRequestUri(exchange, upstream.buildDomain());
             // in order not to affect the next retry call, newUri needs to be excluded
             exclude.add(newUri);
-            return doRequest(exchange, exchange.getRequest().getMethodValue(), newUri, httpHeaders, exchange.getRequest().getBody())
+            return doRequest(exchange, exchange.getRequest().getMethodValue(), newUri, exchange.getRequest().getBody())
                     .timeout(duration, Mono.error(() -> new TimeoutException("Response took longer than timeout: " + duration)))
                     .doOnError(e -> LOG.error(e.getMessage(), e));
         });
     }
 
-    /**
-     * Build the http request headers.
-     *
-     * @param exchange the current server exchange
-     * @return HttpHeaders
-     */
-    private HttpHeaders buildHttpHeaders(final ServerWebExchange exchange) {
-        final HttpHeaders headers = new HttpHeaders();
-        headers.addAll(exchange.getRequest().getHeaders());
-        headers.remove(HttpHeaders.HOST);
-        return headers;
-    }
-
     /**
      * Process the Web request.
      *
      * @param exchange    the current server exchange
      * @param httpMethod  http method, eg.POST
      * @param uri         the request uri
-     * @param httpHeaders the request header
      * @param body        the request body
      * @return {@code Mono<Void>} to indicate when request processing is complete
      */
     protected abstract Mono<R> doRequest(ServerWebExchange exchange, String httpMethod,
-                                         URI uri, HttpHeaders httpHeaders, Flux<DataBuffer> body);
+                                         URI uri, Flux<DataBuffer> body);
 
 }
diff --git a/shenyu-plugin/shenyu-plugin-httpclient/src/main/java/org/apache/shenyu/plugin/httpclient/NettyHttpClientPlugin.java b/shenyu-plugin/shenyu-plugin-httpclient/src/main/java/org/apache/shenyu/plugin/httpclient/NettyHttpClientPlugin.java
index 7333a52271..8afc06f8f3 100644
--- a/shenyu-plugin/shenyu-plugin-httpclient/src/main/java/org/apache/shenyu/plugin/httpclient/NettyHttpClientPlugin.java
+++ b/shenyu-plugin/shenyu-plugin-httpclient/src/main/java/org/apache/shenyu/plugin/httpclient/NettyHttpClientPlugin.java
@@ -55,10 +55,12 @@ public class NettyHttpClientPlugin extends AbstractHttpClientPlugin<HttpClientRe
     }
 
     @Override
-    protected Mono<HttpClientResponse> doRequest(final ServerWebExchange exchange, final String httpMethod, final URI uri,
-                                final HttpHeaders httpHeaders, final Flux<DataBuffer> body) {
-        return Mono.from(httpClient.headers(headers -> httpHeaders.forEach(headers::add))
-                .request(HttpMethod.valueOf(httpMethod)).uri(uri.toASCIIString())
+    protected Mono<HttpClientResponse> doRequest(final ServerWebExchange exchange, final String httpMethod,
+                                                 final URI uri, final Flux<DataBuffer> body) {
+        return Mono.from(httpClient.headers(headers -> {
+            exchange.getRequest().getHeaders().forEach(headers::add);
+            headers.remove(HttpHeaders.HOST);
+        }).request(HttpMethod.valueOf(httpMethod)).uri(uri.toASCIIString())
                 .send((req, nettyOutbound) -> nettyOutbound.send(body.map(dataBuffer -> ((NettyDataBuffer) dataBuffer).getNativeBuffer())))
                 .responseConnection((res, connection) -> {
                     exchange.getAttributes().put(Constants.CLIENT_RESPONSE_ATTR, res);
diff --git a/shenyu-plugin/shenyu-plugin-httpclient/src/main/java/org/apache/shenyu/plugin/httpclient/WebClientPlugin.java b/shenyu-plugin/shenyu-plugin-httpclient/src/main/java/org/apache/shenyu/plugin/httpclient/WebClientPlugin.java
index 3d7dd05ea3..3e0fce611d 100644
--- a/shenyu-plugin/shenyu-plugin-httpclient/src/main/java/org/apache/shenyu/plugin/httpclient/WebClientPlugin.java
+++ b/shenyu-plugin/shenyu-plugin-httpclient/src/main/java/org/apache/shenyu/plugin/httpclient/WebClientPlugin.java
@@ -52,15 +52,18 @@ public class WebClientPlugin extends AbstractHttpClientPlugin<ClientResponse> {
     }
     
     @Override
-    protected Mono<ClientResponse> doRequest(final ServerWebExchange exchange, final String httpMethod, final URI uri,
-                                             final HttpHeaders httpHeaders, final Flux<DataBuffer> body) {
+    protected Mono<ClientResponse> doRequest(final ServerWebExchange exchange, final String httpMethod,
+                                             final URI uri, final Flux<DataBuffer> body) {
         // springWebflux5.3 mark #exchange() deprecated. because #echange maybe make memory leak.
         // https://github.com/spring-projects/spring-framework/issues/25751
         // exchange is deprecated, so change to {@link WebClient.RequestHeadersSpec#exchangeToMono(Function)}
         return webClient.method(HttpMethod.valueOf(httpMethod)).uri(uri)
-                .headers(headers -> headers.addAll(httpHeaders))
+                .headers(headers -> {
+                    headers.addAll(exchange.getRequest().getHeaders());
+                    headers.remove(HttpHeaders.HOST);
+                })
                 .body((outputMessage, context) -> {
-                    MediaType mediaType = httpHeaders.getContentType();
+                    MediaType mediaType = exchange.getRequest().getHeaders().getContentType();
                     if (MediaType.TEXT_EVENT_STREAM.isCompatibleWith(mediaType)
                             || MediaType.MULTIPART_MIXED.isCompatibleWith(mediaType)
                             || MediaType.IMAGE_PNG.isCompatibleWith(mediaType)
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 778e89b3e7..caaf74a965 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
@@ -403,7 +403,7 @@ public class HttpClientProperties {
         /**
          * The channel pool map name, defaults to proxy.
          */
-        private String name = "proxy";
+        private String name = "shenyu-proxy";
 
         /**
          * Only for type FIXED, the maximum number of connections before starting pending
@@ -421,6 +421,21 @@ public class HttpClientProperties {
          * if NULL there is no max idle time.
          */
         private Long maxIdleTime;
+        
+        /**
+         * Time in millis after which the channel will be closed.
+         */
+        private Long maxLifeTime;
+        
+        /**
+         * Perform regular eviction checks in the background at a specified interval.
+         */
+        private Long evictionInterval;
+        
+        /**
+         * Enables channel pools metrics to be collected and registered in Micrometer.
+         */
+        private Boolean metrics = Boolean.FALSE;
 
         /**
          * Gets type.
@@ -499,9 +514,8 @@ public class HttpClientProperties {
          *
          * @return the maxIdleTime timeout
          */
-        public Duration getMaxIdleTime() {
-            return Optional.ofNullable(maxIdleTime)
-                    .map(it -> Duration.ofMillis(maxIdleTime)).orElse(Duration.ofMillis(3000L));
+        public Long getMaxIdleTime() {
+            return maxIdleTime;
         }
 
         /**
@@ -513,6 +527,60 @@ public class HttpClientProperties {
             this.maxIdleTime = maxIdleTime;
         }
         
+        /**
+         * Gets maxLifeTime timeout.
+         *
+         * @param maxLifeTime the maxLifeTime timeout
+         */
+        public void setMaxLifeTime(final Long maxLifeTime) {
+            this.maxLifeTime = maxLifeTime;
+        }
+        
+        /**
+         * Gets maxLifeTime timeout.
+         *
+         * @return the maxLifeTime timeout
+         */
+        public Long getMaxLifeTime() {
+            return maxLifeTime;
+        }
+        
+        /**
+         * Gets eviction interval.
+         *
+         * @return the eviction interval
+         */
+        public Long getEvictionInterval() {
+            return evictionInterval;
+        }
+        
+        /**
+         * Sets eviction interval.
+         *
+         * @param evictionInterval the eviction interval
+         */
+        public void setEvictionInterval(final Long evictionInterval) {
+            this.evictionInterval = evictionInterval;
+        }
+        
+        /**
+         * sets metrics.
+         *
+         * @param metrics the metrics
+         */
+        public void setMetrics(final Boolean metrics) {
+            this.metrics = metrics;
+        }
+        
+        /**
+         * Gets metrics.
+         *
+         * @return the metrics
+         */
+        public Boolean getMetrics() {
+            return metrics;
+        }
+        
         /**
          * The enum Pool type.
          */
diff --git a/shenyu-plugin/shenyu-plugin-httpclient/src/test/java/org/apache/shenyu/plugin/httpclient/HttpClientPropertiesTest.java b/shenyu-plugin/shenyu-plugin-httpclient/src/test/java/org/apache/shenyu/plugin/httpclient/HttpClientPropertiesTest.java
index 6776fb6891..d2a5f7c164 100644
--- a/shenyu-plugin/shenyu-plugin-httpclient/src/test/java/org/apache/shenyu/plugin/httpclient/HttpClientPropertiesTest.java
+++ b/shenyu-plugin/shenyu-plugin-httpclient/src/test/java/org/apache/shenyu/plugin/httpclient/HttpClientPropertiesTest.java
@@ -133,7 +133,7 @@ public class HttpClientPropertiesTest {
         Assertions.assertEquals(httpClientProperties.getPool().getMaxConnections(), ConnectionProvider.DEFAULT_POOL_MAX_CONNECTIONS);
         Assertions.assertEquals(httpClientProperties.getPool().getName(), "name");
         Assertions.assertEquals(httpClientProperties.getPool().getType(), HttpClientProperties.Pool.PoolType.ELASTIC);
-        Assertions.assertEquals(httpClientProperties.getPool().getMaxIdleTime(), Duration.ofMillis(0L));
+        Assertions.assertEquals(Duration.ofMillis(httpClientProperties.getPool().getMaxIdleTime()), Duration.ofMillis(0L));
     }
 
     @Test
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 088aefbe31..44ac56a8fc 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
@@ -25,6 +25,7 @@ import io.netty.handler.timeout.WriteTimeoutHandler;
 import org.apache.commons.lang3.ArrayUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.shenyu.plugin.httpclient.config.HttpClientProperties;
+import org.apache.shenyu.plugin.httpclient.config.HttpClientProperties.Pool;
 import org.springframework.beans.factory.config.AbstractFactoryBean;
 import org.springframework.boot.autoconfigure.web.ServerProperties;
 import org.springframework.boot.context.properties.PropertyMapper;
@@ -33,6 +34,7 @@ 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.ConnectionProvider.Builder;
 import reactor.netty.resources.LoopResources;
 import reactor.netty.tcp.SslProvider;
 import reactor.netty.transport.ProxyProvider;
@@ -40,6 +42,7 @@ import reactor.netty.transport.ProxyProvider;
 import java.security.cert.X509Certificate;
 import java.time.Duration;
 import java.util.Objects;
+import java.util.Optional;
 import java.util.concurrent.TimeUnit;
 
 public class HttpClientFactory extends AbstractFactoryBean<HttpClient> {
@@ -140,56 +143,57 @@ public class HttpClientFactory extends AbstractFactoryBean<HttpClient> {
     }
 
     private ConnectionProvider buildConnectionProvider(final HttpClientProperties.Pool pool) {
-        ConnectionProvider connectionProvider;
         if (pool.getType() == HttpClientProperties.Pool.PoolType.DISABLED) {
-            connectionProvider = ConnectionProvider.newConnection();
-        } else if (pool.getType() == HttpClientProperties.Pool.PoolType.FIXED) {
-            // reactor remove fixed pool by fixed method from 0.9.4
-            // reason: https://github.com/reactor/reactor-netty/issues/1499 and https://github.com/reactor/reactor-netty/issues/1960
-            connectionProvider = buildFixedConnectionPool(pool);
+            return ConnectionProvider.newConnection();
         } else {
-            // please see https://projectreactor.io/docs/netty/release/reference/index.html#_connection_pool_2
-            // reactor remove elastic pool by elastic method from 0.9.4
-            // reason: https://github.com/reactor/reactor-netty/issues/1499 and https://github.com/reactor/reactor-netty/issues/1960
-            connectionProvider = buildElasticConnectionPool(pool);
+            ConnectionProvider.Builder builder = ConnectionProvider.builder(pool.getName());
+            if (pool.getType() == HttpClientProperties.Pool.PoolType.FIXED) {
+                // reactor remove fixed pool by fixed method from 0.9.4
+                // reason: https://github.com/reactor/reactor-netty/issues/1499 and https://github.com/reactor/reactor-netty/issues/1960
+                this.buildFixedConnectionPool(pool, builder);
+            } else {
+                // please see https://projectreactor.io/docs/netty/release/reference/index.html#_connection_pool_2
+                // reactor remove elastic pool by elastic method from 0.9.4
+                // reason: https://github.com/reactor/reactor-netty/issues/1499 and https://github.com/reactor/reactor-netty/issues/1960
+                this.buildElasticConnectionPool(builder);
+            }
+            Optional.ofNullable(pool.getMaxIdleTime()).map(Duration::ofMillis).ifPresent(builder::maxIdleTime);
+            Optional.ofNullable(pool.getMaxLifeTime()).map(Duration::ofMillis).ifPresent(builder::maxLifeTime);
+            Optional.ofNullable(pool.getEvictionInterval()).map(Duration::ofMillis).ifPresent(builder::evictInBackground);
+            builder.metrics(pool.getMetrics());
+            return builder.build();
         }
-        return connectionProvider;
     }
 
     /**
      * build fixed connection pool.
      *
-     * @param pool connection pool params
-     * @return {@link ConnectionProvider}
+     * @param pool    connection pool params
+     * @param builder connection provider builder
      */
-    public static ConnectionProvider buildFixedConnectionPool(final HttpClientProperties.Pool pool) {
+    public void buildFixedConnectionPool(final Pool pool,
+                                         final Builder builder) {
         if (pool.getMaxConnections() <= 0) {
             throw new IllegalArgumentException("Max Connections value must be strictly positive");
         }
         if (pool.getAcquireTimeout() < 0) {
             throw new IllegalArgumentException("Acquire Timeout value must be positive");
         }
-        ConnectionProvider.Builder builder = ConnectionProvider.builder(pool.getName())
-                .maxConnections(pool.getMaxConnections())
+        builder.maxConnections(pool.getMaxConnections())
                 .pendingAcquireTimeout(Duration.ofMillis(pool.getAcquireTimeout()))
-                .maxIdleTime(pool.getMaxIdleTime());
-        return builder.build();
+                .pendingAcquireMaxCount(-1);
     }
 
     /**
      * build elastic connection provider pool.
      *
-     * @param pool connection pool params
-     * @return {@link ConnectionProvider} elastic pool
+     * @param builder connection provider builder
      */
-    public ConnectionProvider buildElasticConnectionPool(final HttpClientProperties.Pool pool) {
+    public void buildElasticConnectionPool(final Builder builder) {
         // about the args, please see https://projectreactor.io/docs/netty/release/reference/index.html#_connection_pool_2
-        ConnectionProvider.Builder builder = ConnectionProvider.builder(pool.getName())
-                .maxConnections(Integer.MAX_VALUE)
+        builder.maxConnections(Integer.MAX_VALUE)
                 .pendingAcquireTimeout(Duration.ofMillis(0))
-                .pendingAcquireMaxCount(-1)
-                .maxIdleTime(pool.getMaxIdleTime());
-        return builder.build();
+                .pendingAcquireMaxCount(-1);
     }
 
 }