SpringBoot集成ES详解

发布时间:2023-05-19

一、SpringBoot集成ES部署

在集成ES前,先要安装和配置ES环境。对于MacOS和Linux系统用户,可以使用Homebrew来安装ES,使用以下命令即可:

brew update
brew install elasticsearch

对于Windows用户,可以从官网下载zip包,解压后运行bin/elasticsearch.bat即可。

二、SpringBoot集成ES版本要求

在集成中,需要根据自己的SpringBoot版本和ES版本来选择合适的ES Client API。大致对应关系如下:

ES6 -> 版本 <= 2.0.0.RELEASE
ES7 -> 版本 >= 2.1.0.RELEASE, <= 2.3.3.RELEASE
ES8 -> 版本 >= 2.4.0.RELEASE

同时,需要在pom.xml中引入对应的dependencies:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

三、SpringBoot集成ES日志

在开发过程中,通常需要对ES操作的日志进行记录和查看。可以在log4j.properties或logback.xml中进行配置:

#log4j.properties
log4j.logger.org.elasticsearch=INFO,es
log4j.additivity.org.elasticsearch=false
log4j.appender.es=org.apache.log4j.FileAppender
log4j.appender.es.file=elasticsearch.log
log4j.appender.es.threshold=debug
log4j.appender.es.layout=org.apache.log4j.PatternLayout
log4j.appender.es.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [%t] %-5p %c - %m%n

四、SpringBoot集成ES集群

ES可以通过搭建集群来实现高可用和负载均衡。在集成中,通过在application.yml中进行配置实现集群:

spring:
  data:
    elasticsearch:
      cluster-nodes: 
        - cluster-node1:9300
        - cluster-node2:9301
        - cluster-node3:9302

五、SpringBoot集成ES6

如果使用ES6版本,则需要引入高版本的API依赖:

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>6.x.x</version>
</dependency>

同时,ES6版本默认设置了"strict"模式,需要在Connfiguration中进行配置:

@Configuration
public class RestClientConfig extends AbstractElasticsearchConfiguration {
    @Override
    public RestHighLevelClient elasticsearchClient() {
        final ClientConfiguration clientConfiguration = ClientConfiguration.builder()
            .connectedTo("localhost:9200", "localhost:9291")
            .build();
        return RestClients.create(clientConfiguration).rest();
    }
    @Override
    public ElasticsearchRestTemplate elasticsearchRestTemplate() {
        return new ElasticsearchRestTemplate(elasticsearchClient());
    }
    @Override
    public ElasticsearchOperations elasticsearchOperations() {
        return new ElasticsearchRestTemplate(elasticsearchClient());
    }
    @Override
    public EntityMapper entityMapper() {
    }
}

六、SpringBoot集成ES7

在ES7中,Client API将要被废弃,取而代之的是高速的Java HTTP客户端,可以通过以下方式进行引用:

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-java-client</artifactId>
    <version>7.x.x</version>
</dependency>

通过配置高速的Java HTTP客户端,可以实现更好的查询性能。

七、SpringBoot集成ES8

在ES8中,将继续提高查询性能和去除Client API,因此需要升级到更高的API依赖版本:

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-client-sniffer</artifactId>
    <version>7.8.0</version>
</dependency>

同时,由于ES7中已经废除了TransportClient,因此代码需要进行迁移,大致思路如下:

private RestHighLevelClient restHighLevelClient;
private RestClient restClient;
private static final RequestOptions COMMON_OPTIONS;
static {
    RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
    COMMON_OPTIONS = builder.build();
}
public ElasticsearchClient() {
    restHighLevelClient = new RestHighLevelClient(
            RestClient.builder(
                    new HttpHost("192.168.1.2", 9200, "http"),
                    new HttpHost("192.168.1.3", 9200, "http")));
    restClient = restHighLevelClient.getLowLevelClient();
}

八、SpringBoot集成ES生成索引map

可以通过Spring Data专门提供的ElasticsearchEntityTypeMappper实现即时的索引更新和映射生成:

@Configuration
public class ElasticsearchClientConfig extends AbstractElasticsearchConfiguration {
    @Override
    public RestHighLevelClient elasticsearchClient() {
        final ClientConfiguration configuration = ClientConfiguration.builder()   
                .connectedTo("localhost:9200")
                .build();
        return RestClients.create(configuration).rest();
    }
    @Bean
    public ElasticsearchOperations elasticsearchOperations() {
        ElasticsearchRestTemplate elasticsearchTemplate = new ElasticsearchRestTemplate(elasticsearchClient());
        Map<String, Object> indexSettings = new HashMap<>();
        indexSettings.put("index.number_of_shards", 1);
        indexSettings.put("index.number_of_replicas", 0);
        elasticsearchTemplate.deleteIndex("book");
        elasticsearchTemplate.createIndex(Book.class, indexSettings);
        elasticsearchTemplate.putMapping(Book.class);
        elasticsearchTemplate.refresh(Book.class);
        return elasticsearchTemplate;
    }
}

九、SpringBoot集成ES用户名密码

当需要进行安全控制和授权时,可以通过在application.yml中进行配置并进行权限控制:

spring:
  data:
    elasticsearch:
      repositories:
        enabled: true
      properties:
        http:
          auth:
            password: elasticsearch_password
            username: elasticsearch_username

同时,需要在ElasticsearchTemplate中进行身份验证:

public ElasticsearchTemplate esTemplate(RestHighLevelClient elasticsearchClient) {
    final ClientConfiguration configuration = 
            ClientConfiguration.builder()
                    .connectedTo("localhost:9200")
                    .build();
    return new ElasticsearchRestTemplate(
            elasticsearchClient, new DefaultEntityMapper(),
            new CustomElasticsearchExceptionTranslator(),
            new SearchTemplateEngine());
}
@Bean
public RestHighLevelClient elasticsearchClient() {
    return RestClients.create(
            ClientConfiguration.builder()
                    .connectedTo("localhost:9200")
                    .usingSsl()
                    .withBasicAuth("elastic", "password")
                    .build())
            .rest();
}

总结

本篇文章详细介绍了如何使用SpringBoot集成ES,并从多个方面进行了详细阐述,包括部署、版本要求、日志、集群、ES6、ES7、ES8、生成索引map、用户名密码等。希望可以对读者了解和使用ES有所帮助。