How to configure Spring Boot with elasticsearch 5.2.1? - elasticsearch

I am trying to connect my Spring Boot application to local elasticsearch 5.2.1 instance. When i use "org.springframework.boot:spring-boot-starter-data-elasticsearch" dependency, i face with "Received message from unsupported version: [2.0.0] minimal compatible version is: [5.0.0]". I think this is due to elasticsearch version is 2.4.4 in starter dependency. So to solve this error, i edit pom.xml properties by adding elasticsearch.version>5.2.1/elasticsearch.version> line. But this time i get
"java.lang.NoSuchMethodError: org.elasticsearch.client.transport.TransportClient.builder()"
To overcome this issue, i create custom config class like below:
#Configuration
public class ElasticsearchConfiguration {
#Bean
public Client client() throws UnknownHostException {
TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
return client;
}
#Bean
public ElasticsearchTemplate elasticsearchTemplate() throws UnknownHostException {
return new ElasticsearchTemplate(client());
}
}
This time i get apache.logging.log4j exceptions (check here) so i add necessary dependencies.
Finally i get below error and stucked there. Could anyone help me out with this?
nested exception is java.lang.NoClassDefFoundError:org/elasticsearch/action/count/CountRequestBuilder

The github page of spring-data-elasticsearch shows that it currently supports elasticsearch only up to version 2.4.0.
For now you have 3 options:
Wait and hope
checkout the pull request
checkout the dev branch 5.0.x-prep

You need to use Spring Boot 2. Check out my spring-boot-elasticsearch-5.x example.

You can use elasticsearch java api to create transport client instead of using spring-data-elasticsearch.

I tried the same and getting that error too for CountRequestBuilder, reason is that CountRequestBuilder class is deprecated and removed now from 5.x elastic search versions, that class is replaced by SearchRequestBuilder but unfortunately spring-data-elasticsearch don't provide this even in the latest release of its jar and that CountRequestBuilder is used in ElasticSearchTemplate.
I am also looking out for some solution. I will post if able to resolve.

Related

Created RestHighLevelClient for Elasticsearch won't use SSL

I am trying to connect to an existing Elasticsearch instance. The instance is set up and working fine, which I can confirm by accessing it through the browser under https://localhost:9200. I am using Spring Data Elasticsearch version 4.1.2 and I am following the official instructions from https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#elasticsearch.clients.rest.
Unfortunately, when starting the application, the initial health checks of the elasticsearch instance fail, because the code is trying to use http instead of https:
o.s.w.r.f.client.ExchangeFunctions : [377e90b0] HTTP HEAD http://localhost:9200/
Therefore I added the ".usingSsl()" to the ClientConfiguration, but it does not seem to have any effect and I just don't understand why. The "funny" thing is, if I implement the reactive client as described here (https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#elasticsearch.clients.reactive), https suddenly works and the applications starts perfectly fine. However, I want to use the regular high level client.
Can you help me change the configuration in a way so that it finally makes use of https instead of http?
#Configuration
class ElasticsearchClientConfig: AbstractElasticsearchConfiguration() {
#Bean
override fun elasticsearchClient(): RestHighLevelClient {
val clientConfiguration = ClientConfiguration.builder()
.connectedTo("localhost:9200")
.usingSsl()
.build();
return RestClients.create(clientConfiguration).rest();
}
}

WebSocket cannot connect to endpoint when running Spring-Boot 2.2 with lazy bean initialization?

I'm having trouble getting the client to connect to a WebSocket endpoint when the Spring-Boot 2.2 application is started in lazy-init mode.
I was able to get this Spring.io tutorial to work. It uses spring-boot-starter-parent version 2.1.6. I changed the pom.xml to use spring-boot-starter-parent version 2.2.0 and got it to work also.
But when I set spring.main.lazy-initialization=true in application.properties, the client does not connect to the server via WebSocket anymore when I click on the "Connect" button. In Chrome Developer Tool > Network > WebSocket, I see that the client sends a CONNECT request, but it never receives a "CONNECTED" response.
I've uploaded my project file to GitHub here:
https://github.com/hirokiterashima/spring-boot-stomp-messaging-websocket. The first commit is the 'complete' directory of the original project in the Spring.io tutorial, which uses Spring-Boot 2.1.6: https://github.com/spring-guides/gs-messaging-stomp-websocket/tree/master/complete. The second commit contains my changes to pom.xml to use Spring-Boot 2.2.0 and addition of application.properties file to enable lazy initialization. As you can see, all I did in the second commit was change to Spring Boot 2.2.0, updated the jQuery webjars dependency, and enabled lazy initialization. If you comment-out the spring.main.lazy-initialization line in application.properties, it will work.
Did anybody else come across a similar issue? What can I do to make this work?
Thanks for your help!
just register the following #Bean:
#Bean
public LazyInitializationExcludeFilter stompWebSocketHandlerMappingLazyInitializationExcludeFilter() {
return LazyInitializationExcludeFilter.forBeanTypes(HandlerMapping.class);
}
or
#Bean
public LazyInitializationExcludeFilter stompWebSocketHandlerMappingLazyInitializationExcludeFilter() {
return ((beanName, beanDefinition, beanType) -> beanName.equals("stompWebSocketHandlerMapping"));
}

nodeBuilder() is removed by Elasticsearch, but still spring-data-elasticsearch documentation contains configuration which uses nodeBuilder()

I was following the Spring-Data-Elasticseach documentaion and was following the configuration as mentioned in the above link.
#Configuration
#EnableElasticsearchRepositories(basePackages = "org/springframework/data/elasticsearch/repositories")
static class Config {
#Bean
public ElasticsearchOperations elasticsearchTemplate() {
return new ElasticsearchTemplate(nodeBuilder().local(true).node().client());
}
}
Since import for nodeBuilder() is not mentioned in the documentation I assumed it from org.elasticsearch.node.NodeBuilder.* as mentioned in elasticsearch Java API.
But in the later releases, the API got changed and NodeBuilder no longer exists. So why/how the spring documentation still using the NodeBuilder?
If that's an issue with the documentation, what's the right configuration?
The dependencies I am using
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
with the boot version 2.1.1.RELEASE
This looks like a documentation issue. I've raise DATAES-574 to have that fixed.
With Spring Boot 2.1, the usual way to create a Client bean is to set a spring.data.elasticsearch.cluster-nodes property. Behind the scenes this will create the Client as a org.elasticsearch.client.transport.TransportClient instance.
You can also define that bean yourself if you so wish.
In the future, TransportClient is also going to been deprecated by Elasticsearch. At that point you'll need to use the higher level REST API. See https://jira.spring.io/browse/DATAES-407 for the details.

Using JaxWsProxyFactoryBean and Spring how do I make an HTTPS connection through a proxy?

I am trying to use JaxWsProxyFactoryBean to connect to a TLS1.2 https service using spring boot and java config through a proxy. I have been unable to find any samples, or information about how to configure this. Im using wsdl2java to build all of the objects that eventually get returned from JaxWsProxyFactoryBean. Any help would be greatly appreciated. What I currently have:
private <T> T cxfClient(String address, String user, String pass, Class<T> serviceClass) {
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setAddress(address);
factory.setUsername(user);
factory.setPassword(pass);
return factory.create(serviceClass);
}
and
#Bean
public Client client() {
Client client = ClientProxy.getClient(lexisNexisClient());
HTTPConduit http = (HTTPConduit) client.getConduit();
http.getClient().setProxyServer("myproxy");
http.getClient().setProxyServerPort(myport);
http.getProxyAuthorization().setUserName("myuser");
http.getProxyAuthorization().setPassword("mypass");
return client;
}
I am currently getting an error:
Caused by: java.lang.NoSuchMethodError: org.apache.cxf.configuration.jsse.TLSClientParameters.getSslContext()Ljavax/net/ssl/SSLContext
Any help would be greatly appreciated.
Thanks,
Brian
Issue ended up being a dependency problem with CXF versions. Apparently the Ljavax/net/ssl/SSLContext means there is a dependency version conflict.
Thanks,
Brian

Neo4j-OGM triggers ClassCastException on any query after spring-boot-devtools restart

I'm starting a new project, and decided to try neo4j with springboot data neo4j and OGM. Everything is working just fine, but in my development env, the spring-boot-devtools is not helping much.
Every time that I change a java class, the Automatic restart triggers and then any query that I run throws a ClassCastException like
java.lang.ClassCastException: br.com.ncisaude.gr.dominio.usuario.Usuario cannot be cast to br.com.ncisaude.gr.dominio.usuario.Usuario
at com.sun.proxy.$Proxy133.findByEmail(Unknown Source)...
Obviously it is a classloader issue, because the classes are just the same.
I believe that neo4j OGM or spring-data-neo4j use serialization for caching or something like that and this is causing this Exception, but im not realy sure.
Someone knows a workarround for this? if it is cache related, there is any way to disable those cache?
I dont know if I should send an issue to neo4j ogm or spring-boot-neo4j, any insights on this?
I'm running spring boot version 1.5.3 with bolt driver 2.1.2. My configuration has nothing specific, its just the default springboot setup with neo4j.
#Configuration
#EnableSpringConfigured
#EnableTransactionManagement(mode = AdviceMode.ASPECTJ)
#EnableScheduling
#EntityScan("br.com.ncisaude.gr.dominio")
public class SpringConfig {
#Bean
#Profile("dev")
public org.neo4j.ogm.config.Configuration getConfiguration() {
org.neo4j.ogm.config.Configuration config = new org.neo4j.ogm.config.Configuration();
AutoIndexConfiguration autoIndexConfiguration = config.autoIndexConfiguration();
// Modo assert remove e cria todas as constraints
autoIndexConfiguration.setAutoIndex("assert");
DriverConfiguration driverConfiguration = config.driverConfiguration();
driverConfiguration.setURI("bolt://localhost");
driverConfiguration.setCredentials("neo4j", "******");
return config;
}
}
Thanks in Advance
[]s
Please see my reply to this issue here: https://github.com/neo4j/neo4j-ogm/issues/374

Resources