HttpClient HttpRoutePlanner接口确定请求目标路由

 更新时间:2023年10月16日 09:30:31   作者:codecraft  
这篇文章主要为大家介绍了使用HttpClient HttpRoutePlanner接口确定请求目标路由,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

本文主要研究一下HttpClient的HttpRoutePlanner

HttpRoutePlanner

org/apache/http/conn/routing/HttpRoutePlanner.java

/**
 * Encapsulates logic to compute a {@link HttpRoute} to a target host.
 * Implementations may for example be based on parameters, or on the
 * standard Java system properties.
 * <p>
 * Implementations of this interface must be thread-safe. Access to shared
 * data must be synchronized as methods of this interface may be executed
 * from multiple threads.
 * </p>
 *
 * @since 4.0
 */
public interface HttpRoutePlanner {

    /**
     * Determines the route for a request.
     *
     * @param target    the target host for the request.
     *                  Implementations may accept {@code null}
     *                  if they can still determine a route, for example
     *                  to a default target or by inspecting the request.
     * @param request   the request to execute
     * @param context   the context to use for the subsequent execution.
     *                  Implementations may accept {@code null}.
     *
     * @return  the route that the request should take
     *
     * @throws HttpException    in case of a problem
     */
    HttpRoute determineRoute(HttpHost target,
                                    HttpRequest request,
                                    HttpContext context) throws HttpException;

}
HttpRoutePlanner接口定义了determineRoute方法,用于决定该请求的目标route

DefaultRoutePlanner

org/apache/http/impl/conn/DefaultRoutePlanner.java

/**
 * Default implementation of an {@link HttpRoutePlanner}. It will not make use of
 * any Java system properties, nor of system or browser proxy settings.
 *
 * @since 4.3
 */
@Contract(threading = ThreadingBehavior.IMMUTABLE_CONDITIONAL)
public class DefaultRoutePlanner implements HttpRoutePlanner {
    private final SchemePortResolver schemePortResolver;
    public DefaultRoutePlanner(final SchemePortResolver schemePortResolver) {
        super();
        this.schemePortResolver = schemePortResolver != null ? schemePortResolver :
            DefaultSchemePortResolver.INSTANCE;
    }
    @Override
    public HttpRoute determineRoute(
            final HttpHost host,
            final HttpRequest request,
            final HttpContext context) throws HttpException {
        Args.notNull(request, "Request");
        if (host == null) {
            throw new ProtocolException("Target host is not specified");
        }
        final HttpClientContext clientContext = HttpClientContext.adapt(context);
        final RequestConfig config = clientContext.getRequestConfig();
        final InetAddress local = config.getLocalAddress();
        HttpHost proxy = config.getProxy();
        if (proxy == null) {
            proxy = determineProxy(host, request, context);
        }
        final HttpHost target;
        if (host.getPort() <= 0) {
            try {
                target = new HttpHost(
                        host.getHostName(),
                        this.schemePortResolver.resolve(host),
                        host.getSchemeName());
            } catch (final UnsupportedSchemeException ex) {
                throw new HttpException(ex.getMessage());
            }
        } else {
            target = host;
        }
        final boolean secure = target.getSchemeName().equalsIgnoreCase("https");
        return proxy == null
                        ? new HttpRoute(target, local, secure)
                        : new HttpRoute(target, local, proxy, secure);
    }
    /**
     * This implementation returns null.
     *
     * @throws HttpException may be thrown if overridden
     */
    protected HttpHost determineProxy(
            final HttpHost target,
            final HttpRequest request,
            final HttpContext context) throws HttpException {
        return null;
    }
}
DefaultRoutePlanner实现了HttpRoutePlanner接口,其determineRoute方法在host的port小于等于0时通过schemePortResolver.resolve来确定port

SchemePortResolver

org/apache/http/conn/SchemePortResolver.java

/**
 * Strategy for default port resolution for protocol schemes.
 *
 * @since 4.3
 */
public interface SchemePortResolver {
    /**
     * Returns the actual port for the host based on the protocol scheme.
     */
    int resolve(HttpHost host) throws UnsupportedSchemeException;
}
SchemePortResolver接口定义了resolve方法,用于根据协议来返回真正的port

DefaultSchemePortResolver

org/apache/http/impl/conn/DefaultSchemePortResolver.java

@Contract(threading = ThreadingBehavior.IMMUTABLE)
public class DefaultSchemePortResolver implements SchemePortResolver {
    public static final DefaultSchemePortResolver INSTANCE = new DefaultSchemePortResolver();
    @Override
    public int resolve(final HttpHost host) throws UnsupportedSchemeException {
        Args.notNull(host, "HTTP host");
        final int port = host.getPort();
        if (port > 0) {
            return port;
        }
        final String name = host.getSchemeName();
        if (name.equalsIgnoreCase("http")) {
            return 80;
        } else if (name.equalsIgnoreCase("https")) {
            return 443;
        } else {
            throw new UnsupportedSchemeException(name + " protocol is not supported");
        }
    }
}
DefaultSchemePortResolver方法针对http返回80,针对https返回443,其他的抛出UnsupportedSchemeException

小结

HttpClient的HttpRoutePlanner接口定义了determineRoute方法,用于决定该请求的目标route;

DefaultRoutePlanner实现了HttpRoutePlanner接口,其determineRoute方法在host的port小于等于0时通过schemePortResolver.resolve来确定port(http返回80,https返回443)。

以上就是HttpClient HttpRoutePlanner接口确定请求目标路由的详细内容,更多关于HttpClient HttpRoutePlanner接口的资料请关注脚本之家其它相关文章!

相关文章

  • Java JDBC连接Kerberos认证的HIVE和Impala方式

    Java JDBC连接Kerberos认证的HIVE和Impala方式

    本文主要介绍了HiveJDBC和ImpalaJDBC的使用方法,包括版本对应、Maven安装、主机名配置、端口开通、JDBC连接和Kerberos认证等
    2025-02-02
  • Spring框架中TaskExecutor类型Bean冲突导致的自动注入失败问题的解决步骤

    Spring框架中TaskExecutor类型Bean冲突导致的自动注入失败问题的解决步骤

    Spring中多个TaskExecutor Bean冲突导致注入失败,本文给大家介绍的解决方案包括使用@Qualifier或@Primary明确注入目标、重命名Bean、排除自动配置,确保版本兼容以避免冲突,需要的朋友可以参考下
    2025-07-07
  • += 和 ++ 操作符区别简单介绍

    += 和 ++ 操作符区别简单介绍

    这篇文章主要介绍了+= 和 ++ 操作符区别简单介绍的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2016-09-09
  • Spring boot框架JWT实现用户账户密码登录验证流程

    Spring boot框架JWT实现用户账户密码登录验证流程

    这篇文章主要介绍了Springboot框架JWT实现用户账户密码登录验证,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-06-06
  • Struts2 Result 参数详解

    Struts2 Result 参数详解

    这篇文章主要讲解Struts2 Result的参数,讲的比较详细,希望能给大家做一个参考。
    2016-06-06
  • Flyway详解及Springboot集成Flyway的详细教程

    Flyway详解及Springboot集成Flyway的详细教程

    Flayway是一款数据库版本控制管理工具,,支持数据库版本自动升级,Migrations可以写成sql脚本,也可以写在java代码里。这篇文章主要介绍了Flyway详解及Springboot集成Flyway的详细教程的相关资料,需要的朋友可以参考下
    2020-07-07
  • MyBatis+MyBatisPlus中遇到的一些坑及解决

    MyBatis+MyBatisPlus中遇到的一些坑及解决

    这篇文章主要介绍了MyBatis+MyBatisPlus中遇到的一些坑及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-03-03
  • Java字符串与正则表达式操作方法详解

    Java字符串与正则表达式操作方法详解

    在Java编程中正则表达式是一个强大的工具,用于处理字符串和进行模式匹配,这篇文章主要介绍了Java字符串与正则表达式操作的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2025-09-09
  • Spring Data Redis 中的 opsFor 方法深入解析

    Spring Data Redis 中的 opsFor 方法深入解析

    opsFor是RedisTemplate类中的一系列方法,用于获取特定数据类型的操作接口,这些方法返回的是 Operations 对象,每个对象都提供了针对特定 Redis 数据类型的操作方法,本文重点介绍Spring Data Redis 中的opsFor方法,感兴趣的朋友一起看看吧
    2025-12-12
  • 解读@Data注解父子类继承的问题

    解读@Data注解父子类继承的问题

    在Java开发中,使用Lombok库的@Data注解简化了代码,但在父子类继承关系中使用@Data注解时会遇到问题,主要问题是:当父类和子类都使用@Data注解时,会导致equals方法不对称,解决方案是在子类中使用@EqualsAndHashCode注解
    2024-11-11

最新评论