Spring Boot upgrade for Elastic Search application

68 views Asked by At

We want to upgrade java spring-boot application from spring-boot 2.6.2 to 3.1.2(3.+), Elasticsearch version: 7.10.1

I'm getting below error while application start up

Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled. 2023-12-06T16:52:08.687-06:00 ERROR 99236 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in com.test.pc.es.util.ElasticSearchUtil required a bean of type 'org.elasticsearch.client.RestHighLevelClient' that could not be found.

Action:

Consider defining a bean of type 'org.elasticsearch.client.RestHighLevelClient' in your configuration.

I tried configuring as below Attempt 1:

import lombok.AllArgsConstructor;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.erhlc.RestClients;

@AllArgsConstructor
public class ESClientConfig {

    private final ElasticSearchConfigProperties elasticsearchConfigProperties;

    @Bean
    public RestHighLevelClient elasticClient() {

        ClientConfiguration clientConfiguration = ClientConfiguration.builder()
                .connectedTo(elasticsearchConfigProperties.getServerUrl())
//                .usingSsl()  // Use SSL, if necessary
                .withBasicAuth(elasticsearchConfigProperties.getUsername(), elasticsearchConfigProperties.getPassword())
                .withConnectTimeout(5000) // Set connection timeout
                .withSocketTimeout(60000) // Set socket timeout
                .build();

        return RestClients.create(clientConfiguration).rest();
    }
}

Attempt 2:

import lombok.AllArgsConstructor;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.message.BasicHeader;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;

import java.nio.charset.StandardCharsets;

import static org.apache.http.HttpHeaders.AUTHORIZATION;


@AllArgsConstructor
public class ESClientConfig {

    private final ElasticSearchConfigProperties elasticsearchConfigProperties;

    @Bean
    public RestHighLevelClient elasticClient() {
        String auth = elasticsearchConfigProperties.getUsername() + ":" + elasticsearchConfigProperties.getPassword();
        byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(StandardCharsets.US_ASCII) );
        String authHeader = "Basic " + new String( encodedAuth );
        RestClientBuilder restClient = RestClient
                .builder(HttpHost.create(elasticsearchConfigProperties.getServerUrl()))
                .setDefaultHeaders(new Header[]{
                        new BasicHeader(AUTHORIZATION, authHeader),
                        new BasicHeader("Content-type", MediaType.APPLICATION_JSON_VALUE),
                })
                .setPathPrefix(elasticsearchConfigProperties.getPathPrefix());

        RestHighLevelClient hlrc = new RestHighLevelClient(restClient);

        return hlrc;
    }
}
0

There are 0 answers