专栏首页码匠的流水账聊聊springboot jest autoconfigure
原创

聊聊springboot jest autoconfigure

本文主要研究一下springboot jest autoconfigure

JestProperties

spring-boot-autoconfigure-2.1.4.RELEASE-sources.jar!/org/springframework/boot/autoconfigure/elasticsearch/jest/JestProperties.java

@ConfigurationProperties(prefix = "spring.elasticsearch.jest")
public class JestProperties {
​
    /**
     * Comma-separated list of the Elasticsearch instances to use.
     */
    private List<String> uris = new ArrayList<>(
            Collections.singletonList("http://localhost:9200"));
​
    /**
     * Login username.
     */
    private String username;
​
    /**
     * Login password.
     */
    private String password;
​
    /**
     * Whether to enable connection requests from multiple execution threads.
     */
    private boolean multiThreaded = true;
​
    /**
     * Connection timeout.
     */
    private Duration connectionTimeout = Duration.ofSeconds(3);
​
    /**
     * Read timeout.
     */
    private Duration readTimeout = Duration.ofSeconds(3);
​
    /**
     * Proxy settings.
     */
    private final Proxy proxy = new Proxy();
​
    //......
​
    public static class Proxy {
​
        /**
         * Proxy host the HTTP client should use.
         */
        private String host;
​
        /**
         * Proxy port the HTTP client should use.
         */
        private Integer port;
​
        public String getHost() {
            return this.host;
        }
​
        public void setHost(String host) {
            this.host = host;
        }
​
        public Integer getPort() {
            return this.port;
        }
​
        public void setPort(Integer port) {
            this.port = port;
        }
​
    }
}
  • JestProperties提供了uris、username、password、multiThreaded(默认true)、connectionTimeout(默认3s)、readTimeout(默认3s)、proxy的配置

JestAutoConfiguration

spring-boot-autoconfigure-2.1.4.RELEASE-sources.jar!/org/springframework/boot/autoconfigure/elasticsearch/jest/JestAutoConfiguration.java

@Configuration
@ConditionalOnClass(JestClient.class)
@EnableConfigurationProperties(JestProperties.class)
@AutoConfigureAfter(GsonAutoConfiguration.class)
public class JestAutoConfiguration {
​
    private final JestProperties properties;
​
    private final ObjectProvider<Gson> gsonProvider;
​
    private final ObjectProvider<HttpClientConfigBuilderCustomizer> builderCustomizers;
​
    public JestAutoConfiguration(JestProperties properties, ObjectProvider<Gson> gson,
            ObjectProvider<HttpClientConfigBuilderCustomizer> builderCustomizers) {
        this.properties = properties;
        this.gsonProvider = gson;
        this.builderCustomizers = builderCustomizers;
    }
​
    @Bean(destroyMethod = "shutdownClient")
    @ConditionalOnMissingBean
    public JestClient jestClient() {
        JestClientFactory factory = new JestClientFactory();
        factory.setHttpClientConfig(createHttpClientConfig());
        return factory.getObject();
    }
​
    protected HttpClientConfig createHttpClientConfig() {
        HttpClientConfig.Builder builder = new HttpClientConfig.Builder(
                this.properties.getUris());
        PropertyMapper map = PropertyMapper.get();
        map.from(this.properties::getUsername).whenHasText().to((username) -> builder
                .defaultCredentials(username, this.properties.getPassword()));
        Proxy proxy = this.properties.getProxy();
        map.from(proxy::getHost).whenHasText().to((host) -> {
            Assert.notNull(proxy.getPort(), "Proxy port must not be null");
            builder.proxy(new HttpHost(host, proxy.getPort()));
        });
        map.from(this.gsonProvider::getIfUnique).whenNonNull().to(builder::gson);
        map.from(this.properties::isMultiThreaded).to(builder::multiThreaded);
        map.from(this.properties::getConnectionTimeout).whenNonNull()
                .asInt(Duration::toMillis).to(builder::connTimeout);
        map.from(this.properties::getReadTimeout).whenNonNull().asInt(Duration::toMillis)
                .to(builder::readTimeout);
        customize(builder);
        return builder.build();
    }
​
    private void customize(HttpClientConfig.Builder builder) {
        this.builderCustomizers.orderedStream()
                .forEach((customizer) -> customizer.customize(builder));
    }
​
}
  • JestAutoConfiguration在没有jestClient的bean情况下会通过JestProperties创建了HttpClientConfig,然后使用JestClientFactory创建JestClient,同时标记其destroyMethod为shutdownClient方法

JestClientFactory

jest-6.3.1-sources.jar!/io/searchbox/client/JestClientFactory.java

public class JestClientFactory {
​
    final static Logger log = LoggerFactory.getLogger(JestClientFactory.class);
    private HttpClientConfig httpClientConfig;
​
    public JestClient getObject() {
        JestHttpClient client = new JestHttpClient();
​
        if (httpClientConfig == null) {
            log.debug("There is no configuration to create http client. Going to create simple client with default values");
            httpClientConfig = new HttpClientConfig.Builder("http://localhost:9200").build();
        }
​
        client.setRequestCompressionEnabled(httpClientConfig.isRequestCompressionEnabled());
        client.setServers(httpClientConfig.getServerList());
        final HttpClientConnectionManager connectionManager = getConnectionManager();
        final NHttpClientConnectionManager asyncConnectionManager = getAsyncConnectionManager();
        client.setHttpClient(createHttpClient(connectionManager));
        client.setAsyncClient(createAsyncHttpClient(asyncConnectionManager));
​
        // set custom gson instance
        Gson gson = httpClientConfig.getGson();
        if (gson == null) {
            log.info("Using default GSON instance");
        } else {
            log.info("Using custom GSON instance");
            client.setGson(gson);
        }
​
        // set discovery (should be set after setting the httpClient on jestClient)
        if (httpClientConfig.isDiscoveryEnabled()) {
            log.info("Node Discovery enabled...");
            if (!Strings.isNullOrEmpty(httpClientConfig.getDiscoveryFilter())) {
                log.info("Node Discovery filtering nodes on \"{}\"", httpClientConfig.getDiscoveryFilter());
            }
            NodeChecker nodeChecker = createNodeChecker(client, httpClientConfig);
            client.setNodeChecker(nodeChecker);
            nodeChecker.startAsync();
            nodeChecker.awaitRunning();
        } else {
            log.info("Node Discovery disabled...");
        }
​
        // schedule idle connection reaping if configured
        if (httpClientConfig.getMaxConnectionIdleTime() > 0) {
            log.info("Idle connection reaping enabled...");
​
            IdleConnectionReaper reaper = new IdleConnectionReaper(httpClientConfig, new HttpReapableConnectionManager(connectionManager, asyncConnectionManager));
            client.setIdleConnectionReaper(reaper);
            reaper.startAsync();
            reaper.awaitRunning();
        } else {
            log.info("Idle connection reaping disabled...");
        }
​
        Set<HttpHost> preemptiveAuthTargetHosts = httpClientConfig.getPreemptiveAuthTargetHosts();
        if (!preemptiveAuthTargetHosts.isEmpty()) {
            log.info("Authentication cache set for preemptive authentication");
            client.setHttpClientContextTemplate(createPreemptiveAuthContext(preemptiveAuthTargetHosts));
        }
​
        client.setElasticsearchVersion(httpClientConfig.getElasticsearchVersion());
​
        return client;
    }
​
    public void setHttpClientConfig(HttpClientConfig httpClientConfig) {
        this.httpClientConfig = httpClientConfig;
    }
​
    //......
​
}
  • JestClientFactory的getObject方法首先创建JestHttpClient,然后设置HttpClient、AsyncClient
  • 如果isDiscoveryEnabled为true则会创建NodeChecker并执行Node Discovery
  • 如果maxConnectionIdleTime大于0则会创建IdleConnectionReaper,进行Idle connection reaping

小结

  • JestProperties提供了uris、username、password、multiThreaded(默认true)、connectionTimeout(默认3s)、readTimeout(默认3s)、proxy的配置
  • JestAutoConfiguration在没有jestClient的bean情况下会通过JestProperties创建了HttpClientConfig,然后使用JestClientFactory创建JestClient,同时标记其destroyMethod为shutdownClient方法
  • JestClientFactory的getObject方法首先创建JestHttpClient,然后设置HttpClient、AsyncClient;如果isDiscoveryEnabled为true则会创建NodeChecker并执行Node Discovery;如果maxConnectionIdleTime大于0则会创建IdleConnectionReaper,进行Idle connection reaping

doc

原创声明,本文系作者授权云+社区发表,未经许可,不得转载。

如有侵权,请联系 yunjia_community@tencent.com 删除。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • 聊聊springboot2的ScheduledTasksEndpoint

    本文主要研究下springboot2的ScheduledTasksEndpoint

    codecraft
  • 聊聊spring for kafka的AckMode

    本文主要讲述一下spring for kafka的consumer在spring.kafka.consumer.enable-auto-commit是false...

    codecraft
  • 聊聊spring cloud的AbstractLoadBalancingClient

    本文主要研究一下spring cloud的AbstractLoadBalancingClient

    codecraft
  • 【JAVA】多线程学习

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY 版权协议,转载请附上原文出处链接和本声明。

    韩旭051
  • 如何定位Obj-C野指针随机Crash(三):如何让Crash自报家门

    本文主要介绍如何利用OC Runtime的特性,让OC野指针对象主动抛出自己的信息,秒杀某些全系统栈Crash。 ? 陈其锋,腾讯SNG即通产品部音视频技术中心...

    腾讯Bugly
  • WPF 在image控件用鼠标拖拽出矩形

    今天有小伙伴问我一个问题,在image控件用鼠标拖拽出矩形,本文告诉大家如何使用鼠标画出矩形

    林德熙
  • js滚动加载(无限加载)(转)

    clientHeight比较好理解,是视窗的高度,就是我们在浏览器中所能看到内容的高度;

    山河木马
  • Python3 与 C# 并发编程之~ 进程篇中

    接着上面继续拓展,补充说说获取函数返回值。 上面是通过成功后的回调函数来获取返回值,这次说说自带的方法:

    逸鹏
  • android 自定义gallerey并实现预览功能

    自从Gallery被谷歌废弃以后,Google推荐使用ViewPager和HorizontalScrollView来实现Gallery的效果。的确Horizon...

    xiangzhihong
  • 聊聊BinlogConnectorReplicator的getTransactionRows

    本文主要研究一下BinlogConnectorReplicator的getTransactionRows

    codecraft

扫码关注云+社区

领取腾讯云代金券