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接口的资料请关注脚本之家其它相关文章!

相关文章

  • SpringBoot Jackson日期格式化统一配置的实现

    SpringBoot Jackson日期格式化统一配置的实现

    Spring项目中经常需要配置日期时间格式格式,本文主要介绍了SpringBoot Jackson日期格式化统一配置的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-08-08
  • @Bean注解和@Configuration、@Component注解组合使用的区别

    @Bean注解和@Configuration、@Component注解组合使用的区别

    这篇文章主要介绍了@Bean注解和@Configuration、@Component注解组合使用的区别,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-11-11
  • Java中的Opencv简介与开发环境部署方法

    Java中的Opencv简介与开发环境部署方法

    OpenCV是一个开源的计算机视觉和图像处理库,提供了丰富的图像处理算法和工具,它支持多种图像处理和计算机视觉算法,可以用于物体识别与跟踪、图像分割与边缘检测、图像特征提取与描述等应用,本文介绍Java中的Opencv简介与开发环境部署方法,感兴趣的朋友一起看看吧
    2025-01-01
  • J2ME编程中的几个重要概念介绍

    J2ME编程中的几个重要概念介绍

    本文介绍的是J2ME编程应用平台中的几个重要概念,希望对你有帮助,一起来看。
    2015-09-09
  • Java字节与字符流永久存储json数据

    Java字节与字符流永久存储json数据

    本篇文章给大家详细讲述了Java字节与字符流永久存储json数据的方法,以及代码分享,有兴趣的参考学习下。
    2018-02-02
  • Java 运算符详情

    Java 运算符详情

    这篇文章主要介绍了Java 运算符,Java 中的运算符与 C 语言基本一致。下面文章就围绕Java 中的运算符的相关资料展开内容,需要的朋友可以参考一下
    2021-11-11
  • Java 自定义注解及利用反射读取注解的实例

    Java 自定义注解及利用反射读取注解的实例

    下面小编就为大家带来一篇Java 自定义注解及利用反射读取注解的实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-08-08
  • Spring Bean是如何初始化的详解

    Spring Bean是如何初始化的详解

    Spring只Bean加载机制默认情况下是初始化容器的时候就会直接初始化,但是也取决于是否使用了懒加载,下面这篇文章主要给大家介绍了关于Spring Bean是如何初始化的相关资料,需要的朋友可以参考下
    2022-03-03
  • Java 8 Stream 处理数据方法汇总

    Java 8 Stream 处理数据方法汇总

    这篇文章主要介绍了Java 8 Stream处理数据,Stream是Java 8 新引入的一个包它让我们能用声明式的方式处理数据,Stream流式处理相较于传统方法简洁高效,也便于进行并发编程,更多相关内容需要的小伙伴可以参考下面文章内容
    2022-06-06
  • Springboot事务失效的几种情况解读

    Springboot事务失效的几种情况解读

    这篇文章主要介绍了Springboot事务失效的几种情况解读,因为Spring AOP默认使用动态代理,会给被代理的类生成一个代理类,事务相关的操作都通过代理来完成,使用内部方法调用时,使用的是实例调用,没有通过代理类调用方法,因此事务不会检测到失败,需要的朋友可以参考下
    2023-10-10

最新评论