SpringCloud gateway 自定义请求的 httpClient

SpringCloud gateway 自定义请求的 httpClient

说明对应的版本是3.1.4版本

gateway作为服务代理,对http协议请求转发时,可以通过配置参数配置发起http请求设置。

先将httpclient的基本配置如下:

spring:
  main:
    allow-bean-definition-overriding: true
  application:
    name: paas-gateway-server
  cloud:
    gateway:
      httpclient:
        pool:
          type: ELASTIC
          max-idle-time: 30S
        connect-timeout: 2000 # 全局变量
        response-timeout: 10s #全局变量

Gateway 代码http请求默认是使用http连接池的,而且默认使用连接池类型是ELASTIC,支持三种类型

ELASTIC, 弹性根据请求自动调整,最大连接数没有限制,在并发的大的时候会耗尽系统的资源。
FIXED, 固定大小,对流量可控情况下可以使用
DISABLED;禁用

具体参数可以参考官网文档

Gateway使用的netty非阻塞式响应式协议,gateway的配置类GatewayAutoConfiguration中NettyConfiguration,关于NettyRoutingFilter是GlobalFilter的有个实现,而且order顺序是最大值(值越小约先执行)。NettyRoutingFilter最后执行请求的filter。http的配置是在类GatewayAutoConfiguration中HttpClientFactory.creatInstance具体代码如下:

    protected ConnectionProvider buildConnectionProvider(HttpClientProperties properties) {
        Pool pool = properties.getPool();
        ConnectionProvider connectionProvider;
        if (pool.getType() == PoolType.DISABLED) {
            connectionProvider = ConnectionProvider.newConnection();
        } else {
            reactor.netty.resources.ConnectionProvider.Builder builder = ConnectionProvider.builder(pool.getName());
            if (pool.getType() == PoolType.FIXED) {
                ((reactor.netty.resources.ConnectionProvider.Builder)((reactor.netty.resources.ConnectionProvider.Builder)builder.maxConnections(pool.getMaxConnections())).pendingAcquireMaxCount(-1)).pendingAcquireTimeout(Duration.ofMillis(pool.getAcquireTimeout()));
            } else {
                ((reactor.netty.resources.ConnectionProvider.Builder)((reactor.netty.resources.ConnectionProvider.Builder)builder.maxConnections(2147483647)).pendingAcquireTimeout(Duration.ofMillis(0L))).pendingAcquireMaxCount(-1);
            }

            if (pool.getMaxIdleTime() != null) {
                builder.maxIdleTime(pool.getMaxIdleTime());
            }

            if (pool.getMaxLifeTime() != null) {
                builder.maxLifeTime(pool.getMaxLifeTime());
            }

            builder.evictInBackground(pool.getEvictionInterval());
            builder.metrics(pool.isMetrics());
            connectionProvider = builder.build();
        }

        return connectionProvider;
    }
    }

默认使用的ELASRIC,reactor 默认的最大线程池是500。

  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值