首先明确一点,SpringBoot自带的ES模板,不建议使用,建议使用Rest Client。如果业务简单,且无特殊要求,可以使用SpringBoot的模板ElasticsearchRepository来搞定。这个非常简单,这里不作介绍,有需要看最底下的连接

ElasticsearchRepository

  • 优点: 简单,SpringBoot无缝对接,配置简单

  • 缺点: 基于即将废弃的TransportClient, 不能支持复杂的业务

 

SpringBoot整合Elasticsearch的Java Rest Client_ide

 

创建SpringBoot项目

SpringBoot整合Elasticsearch的Java Rest Client_ide_02

IDEA 创建SpringBoot工程如果不会创建,可以参考:

    https://www.jianshu.com/p/2101d176555b

maven 和 gradle都可以,建议使用 JDK使用1.8 因为Elasticsearch 的Java High Level REST Client 对java的版本要求是8。你们可以去官网查证。

【参考:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-getting-started-maven.html

添加Rest Client依赖,增加配置

maven工程:

<!-- Java Low Level REST Client -->
<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-client</artifactId>
    <version>6.3.2</version>
</dependency>

 <!-- Java High Level REST Client -->
<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>6.3.2</version>
</dependency>

gradle 工程

dependencies {
    compile 'org.elasticsearch.client:elasticsearch-rest-client:6.3.2'
    compile 'org.elasticsearch.client:elasticsearch-rest-high-level-client:6.3.2'
}

 

编写单例Rest Low Level Client 和Rest High Level Client的bean

想用Spring 的IOC管理ES的连接客户端,可分下面几步

  • 配置ES节点

  • 配置Rest Client

  • 配置Rest High Level Client

  • 使用IOC注入

 

       根据我从其他网站上查询的资料,Rest Client是长连接,而且内部有默认的线程池管理,因此一般无需自定义线程池管理连接。如果不对请指正

基于以上结论。先把连接点全部配置到配置文件中.(为了省事,直接一个数组搞定,有心的朋友可以注入成list+对象)

  1. 配置节点

elasticsearch.ip=127.0.0.1:9200,127.0.0.2:9200,127.0.0.3:9200,127.0.0.4:9200,127.0.0.5:9200,127.0.0.6:9200

 

  1. 编写Config类,配置Rest Client和Rest High Level Client

请找一个pack,创建ElasticsearchRestClient 类

/**
 * this file to you under the Apache License,
 * version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at:
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 * Create By @author zing  @date 2018/7/18 17:20
 */
@Slf4j@Configurationpublic class ElasticsearchRestClient {    

    /**
     * 使用冒号隔开ip和端口
     */
    @Value("${elasticsearch.ip}")
    String[] ipAddress;

    private static final int ADDRESS_LENGTH = 2;

    private static final String HTTP_SCHEME = "http";

    @Bean
    public RestClient restClient() {
        HttpHost[] hosts = Arrays.stream(ipAddress)
                .map(this::makeHttpHost)
                .filter(Objects::nonNull)
                .toArray(HttpHost[]::new);
        log.debug("hosts:{}", Arrays.toString(hosts));
        return RestClient
                .builder(hosts)
                .build();
    }

    @Bean(name = "highLevelClient")    
    public RestHighLevelClient highLevelClient(@Autowired RestClient restClient) {
        return new RestHighLevelClient(restClient);
    }    

   private HttpHost makeHttpHost(String s) {
        assert StringUtils.isNotEmpty(s);

        String[] address = s.split(":");
        if (address.length == ADDRESS_LENGTH) {
            String ip = address[0];
            int port = Integer.parseInt(address[1]);
            return new HttpHost(ip, port, HTTP_SCHEME);
        } else {
            return null;
        }
    }
}

注:@Slf4j注解是lombok的日志注解,可以自行删除,切换成其他日志方式;Stream不会的朋友可以写成for循环,速度大约能快些

业务使用

强烈建议从TransportClient迁移到RestClient,因为业务压测发现TransportClient存在并发瓶颈。
在service里然后使用之前创建的highLevelClient呢?
demo如下

@Servicepublic 
class XXXServiceImpl implements XXXService {    

   @Autowired
    RestHighLevelClient highLevelClient;
    @Override
    public boolean testEsRestClient(){
        SearchRequest searchRequest = new SearchRequest("gdp_tops*");
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.query(QueryBuilders.termQuery("city", "北京市"));
        sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
        searchRequest.source(sourceBuilder);        try {
            SearchResponse response = highLevelClient.search(searchRequest);
            Arrays.stream(response.getHits().getHits())
                    .forEach(i -> {
                        System.out.println(i.getIndex());
                        System.out.println(i.getSource());
                        System.out.println(i.getType());
                    });
            System.out.println(response.getHits().totalHits);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
}

如果你没有Query DSL的基础,建议先学习ES的基本查询方法,如果有时间我可能会写一些,但是网上已经有N多入门教程。

ps:

ES 没有事务概念,ES存储暂时可以理解成每次请求是原子性的,如果涉及多种数据库操作,如:存完ES再存MySQL,且要保证一致性的话,可以考虑使用消息队列做失败补偿,如果需要及时返回的存储,最好不要同时操作两种数据库。否则则需要手动控制事务失败后的恢复。相对麻烦很多

RestClient其他详细使用方法参考我前面的文章:

Elasticsearch Java Rest Client 上手指南(上)

Elasticsearch Java Rest Client 上手指南(下)

 

作者:MaxZing

 

 



        SpringBoot整合Elasticsearch的Java Rest Client_spring_03