Deery
Deery

Reputation: 1

How can I specify elasticsearch spring boot configuration for Azure managed Service (without port)

I created an Azure managed Service for ElasticSearch. Now I want to access it with my Spring Boot Elastic Application.

NOTE: I hide the real URL with the placeholder MyCustomAzureURLwithoutPort.com. In reality it is something like https://myclustername.es.REGION.azure.elastic-cloud.com

My ElasticConfig is:

@Configuration
@EnableElasticsearchRepositories(basePackages = "org.elastic.repositories")
@ComponentScan(basePackages = { "org.elastic" })
public class ElasticsearchClientConfig extends AbstractElasticsearchConfiguration {

    @Override
    @Bean
    public RestHighLevelClient elasticsearchClient() {


        final ClientConfiguration clientConfiguration = 
                ClientConfiguration
                .builder()
                .connectedTo("https://MyCustomAzureURLwithoutPort.com")
                .withBasicAuth(user, pw)
                .build();

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

But I keep getting an error like this:

11:41:59.972 [] [] [main]
                ERROR o.s.boot.SpringApplication - Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'productsearchappApplication': Invocation of init method failed; nested exception is org.springframework.dao.DataAccessResourceFailureException: https:MyCustomAzureURLwithoutPort.com; nested exception is java.lang.RuntimeException: https:MyCustomAzureURLwithoutPort.com
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:160)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:415)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1786)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:897)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:879)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:405)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
    at io.pratik.elasticsearch.productsearchapp.ProductsearchappApplication.main(ProductsearchappApplication.java:38)
Caused by: org.springframework.dao.DataAccessResourceFailureException: https:MyCustomAzureURLwithoutPort.com; nested exception is java.lang.RuntimeException: https:MyCustomAzureURLwithoutPort.com
    at org.springframework.data.elasticsearch.core.ElasticsearchExceptionTranslator.translateExceptionIfPossible(ElasticsearchExceptionTranslator.java:76)
    at org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate.translateException(ElasticsearchRestTemplate.java:378)
    at org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate.execute(ElasticsearchRestTemplate.java:361)
    at org.springframework.data.elasticsearch.core.DefaultIndexOperations.doRefresh(DefaultIndexOperations.java:175)
    at org.springframework.data.elasticsearch.core.AbstractDefaultIndexOperations.refresh(AbstractDefaultIndexOperations.java:159)
    at io.pratik.elasticsearch.productsearchapp.ProductsearchappApplication.buildIndex(ProductsearchappApplication.java:50)
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
    at java.base/java.lang.reflect.Method.invoke(Method.java:577)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:389)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:333)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:157)
    ... 19 common frames omitted
Caused by: java.lang.RuntimeException: https:MyCustomAzureURLwithoutPort.com
    at org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate.translateException(ElasticsearchRestTemplate.java:376)
    ... 28 common frames omitted
Caused by: java.io.IOException: https:MyCustomAzureURLwithoutPort.com
    at org.elasticsearch.client.RestClient.extractAndWrapCause(RestClient.java:828)
    at org.elasticsearch.client.RestClient.performRequest(RestClient.java:248)
    at org.elasticsearch.client.RestClient.performRequest(RestClient.java:235)
    at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1514)
    at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:1484)
    at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:1454)
    at org.elasticsearch.client.IndicesClient.refresh(IndicesClient.java:553)
    at org.springframework.data.elasticsearch.core.DefaultIndexOperations.lambda$doRefresh$9(DefaultIndexOperations.java:175)
    at org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate.execute(ElasticsearchRestTemplate.java:359)
    ... 27 common frames omitted
Caused by: java.net.UnknownHostException: https:MyCustomAzureURLwithoutPort.com
    at java.base/java.net.InetAddress$CachedAddresses.get(InetAddress.java:948)
    at java.base/java.net.InetAddress.getAllByName0(InetAddress.java:1638)
    at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1504)
    at org.apache.http.impl.conn.SystemDefaultDnsResolver.resolve(SystemDefaultDnsResolver.java:45)
    at org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager$InternalAddressResolver.resolveRemoteAddress(PoolingNHttpClientConnectionManager.java:664)
    at org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager$InternalAddressResolver.resolveRemoteAddress(PoolingNHttpClientConnectionManager.java:635)
    at org.apache.http.nio.pool.AbstractNIOConnPool.processPendingRequest(AbstractNIOConnPool.java:474)
    at org.apache.http.nio.pool.AbstractNIOConnPool.lease(AbstractNIOConnPool.java:280)
    at org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager.requestConnection(PoolingNHttpClientConnectionManager.java:295)
    at org.apache.http.impl.nio.client.AbstractClientExchangeHandler.requestConnection(AbstractClientExchangeHandler.java:377)
    at org.apache.http.impl.nio.client.DefaultClientExchangeHandlerImpl.start(DefaultClientExchangeHandlerImpl.java:129)
    at org.apache.http.impl.nio.client.InternalHttpAsyncClient.execute(InternalHttpAsyncClient.java:141)
    at org.elasticsearch.client.RestClient.performRequest(RestClient.java:244)
    ... 34 common frames omitted
Disconnected from the target VM, address: '127.0.0.1:52567', transport: 'socket'

I also tried the URL with Port 443 ( https://myclustername.es.REGION.azure.elastic-cloud.com:443), that's what the Stacktrace is copied from, but this didn't worked either.

Anyone have an Idea? Need the BasicAuth and connect to this URL without Port.

Thanks!

Upvotes: 0

Views: 475

Answers (1)

frascu
frascu

Reputation: 828

Elastic cloud uses 9243 and 443. Therefore, try with 9243.

You can find the reason here: Why does Elastic Cloud provides a specialized port (9243) instead of only using the default one (443)?

Upvotes: 1

Related Questions