Spring Data Elasticsearch: Understanding path.home - java

I am following Spring Data Elasticsearch Turorial.
In section 2.3. Java Configuration, there is:
#Value("${elasticsearch.home:/usr/local/Cellar/elasticsearch/5.6.0}")
private String elasticsearchHome;
which is used in:
#Bean
public Client client() {
Settings elasticsearchSettings = Settings.builder()
.put("client.transport.sniff", true)
.put("path.home", elasticsearchHome)
.put("cluster.name", clusterName).build();
TransportClient client = new PreBuiltTransportClient(elasticsearchSettings);
client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
return client;
}
First, I din't understand why the client needs the installation directory of Elasticsearch. Second, I don't have Elasticsearch installed on the client computer. It's installed on a remote server. So what should I do with this settig - path.home?
(Of course I will change "127.0.0.1" too).

Related

Java Springboot on HTTPS: how to change default http port 8080 to another value

Hi Java and Springboot Gurus,
This regarding a project I have created in Java using springboot.
I was able to successfully create and setup an https project in spring boot.
Then to set the HTTPS port I have just to set via commandline instead in application.properties via this command in the terminal:
$ java -Dserver.port=6001 -jar myapp-0.0.1.jar
I made it like this so that I can easily create other instances that will be running in other ports let say 6002, 6003 and so on...
But the problem is that, there is this default HTTP Port that tomcat sets:
2019-08-16 02:42:36.768 INFO 6 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 6001 (https) 8080 (http)
I want to get rid of this or perhaps assign it to another port e.g. 6081.
I have read this article, and its setting it up programmatically.
But I want to avoid that.
I just want it to be something I can add as parameter in the command line like -Dserver.port=6001. To avoid further complicating my project as much as I can.
I tried doing this:
$ java -Dserver.port=6001 -Dserver.http.port=6081 -jar myapp-0.0.1.jar
... but it doesn't work.
Your ideas and inputs will be much appreciated!
Have you tried setting server.port=-1 in your application.properties. See Spring Boot doc.
[Update]
You may redirect your HTTP to HTTPS.
To achieve this you need another connector. SpringBoot does not allow to configure multiple connector with application.properties. So we can write a connector programmatically. Connector example for Tomcat:
#Configuration
public class MyTomcatConnector {
#Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {
#Override
protected void postProcessContext(Context context) {
SecurityConstraint securityConstraint = new SecurityConstraint();
securityConstraint.setUserConstraint("CONFIDENTIAL");
SecurityCollection collection = new SecurityCollection();
collection.addPattern("/*");
securityConstraint.addCollection(collection);
context.addConstraint(securityConstraint);
}
};
tomcat.addAdditionalTomcatConnectors(initiateHttpConnector());
return tomcat;
}
private Connector initiateHttpConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
connector.setPort(8080);
connector.setSecure(false);
connector.setRedirectPort(6001);
return connector;
}
}

APR with Embedded Tomcat - Spring

I am creating a RESTApi using spring framework. My IDE is STS. Embedded Tomcat concept is pretty useful and easy to implement. Just export a jar, run it and boom your service is up and running, but embedded Tomcat is using Nio HTTP Connector. I did some research and find out that APR HTTP Connector is much more better than Nio and I want to use APR. Still there is an option : export a war file, deploy it into a tomcat and configure its HTTP connector. But I really like embedded tomcat and I am wondering is it possible to change its HTTP Connector from Nio to APR ?
We can enable APR in springboot embeded tomcat by overiding the org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory and providing new org.apache.catalina.connector.Connector
with org.apache.coyote.http11.Http11AprProtocol protocol.
The below code might help to get it done.
#Bean
public TomcatServletWebServerFactory servletContainerFactoryProd() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
#Override
protected TomcatWebServer getTomcatWebServer(Tomcat tomcat) {
// to create new directories and files and add them to Context
return super.getTomcatWebServer(tomcat);
}
};
Connector connector = new Connector("org.apache.coyote.http11.Http11AprProtocol");
Http11AprProtocol protocol = (Http11AprProtocol) connector.getProtocolHandler();
connector.setProperty("compression", "on");
connector.setProperty("compressableMimeType", "text/html,text/xml,text/plain,application/json,application/xml");
// can also enable ssl and provide certificate details
tomcat.addAdditionalTomcatConnectors(connector);
return tomcat;
}

ElasticSearch Java Transport Client NoNodeAvailableException on Ubuntu 14.04

I am running Elasticsearch v. 2.3.2, using Java 7. Following is the printout from curl http://172.31.11.83:9200:
{
"name" : "ip-172-31-11-83",
"cluster_name" : "elasticsearch",
"version" : {
"number" : "2.3.2",
"build_hash" : "b9e4a6acad4008027e4038f6abed7f7dba346f94",
"build_timestamp" : "2016-04-21T16:03:47Z",
"build_snapshot" : false,
"lucene_version" : "5.5.0"
},
"tagline" : "You Know, for Search"
}
... and I am using the following in my Java code:
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>2.3.2</version>
</dependency>
I have ports 9200 and 9300 open in my firewall rules for my ES server, and can successfully execute said Java code from my laptop (Mac OSX). Following is the code snippet that starts off the process (this works fine):
Settings settings = Settings.settingsBuilder()
.put("cluster.name", "elasticsearch").build();
esClient =TransportClient.builder().settings(settings).build().addTransportAddress(new
InetSocketTransportAddress(new InetSocketAddress(InetAddress.getByName("172.31.11.83"), 9300)));
Then later, I try to issue an index request (this fails when I run the code on Ubuntu 14.04:
adminClient = esClient.admin().indices();
IndicesExistsResponse response = adminClient.exists(request).actionGet();
My elasticsearch.yml file contains the following network settings:
network.bind_host: 0
network.publish_host: 172.31.11.83
transport.tcp.port: 9300
http.port: 9200
I have also tried with network.bind_host: 172.31.11.83 to no avail. Using curl, I can get to port 9200 from all machines. The cluster name reported by curl is "elasticsearch".
When I start ES, I see the following in the elasticsearch.log:
publish_address {172.31.11.83:9300}, bound_addresses {[::]:9300}
And yet, the exception I get is as follows:
NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{172.31.11.83}{172.31.11.83:9300}]]
at org.elasticsearch.client.transport.TransportClientNodesService.ensureNodesAreAvailable(TransportClientNodesService.java:290)
at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:207)
at org.elasticsearch.client.transport.support.TransportProxyClient.execute(TransportProxyClient.java:55)
at org.elasticsearch.client.transport.TransportClient.doExecute(TransportClient.java:283)
at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:347)
at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:336)
at org.elasticsearch.client.support.AbstractClient$IndicesAdmin.execute(AbstractClient.java:1178)
at org.elasticsearch.client.support.AbstractClient$IndicesAdmin.exists(AbstractClient.java:1198)
Again, this exact code works from my local machine. Any thoughts?
Having identical issue..
Upgraded elastic from 1.7 to 2.3.2 on same AWS kit
Ubunto 14.0.4
Elastic binding transport on 9300 as before
Security group has port open (not changed)
Now remote clients cannot connect via transport layer - same error as above.
The only thing that has changed in my setup is the version of Elasticsearch
ok I solved this. It appears 2.3.2 doesn't default TCP bind in same way as 1.7.0
I had to set this in my elasticsearch.yml :
network.bind_host: {AWS private IP address)

ElasticSearch 2.0 Transport Client - No Node Available exception

[Using ElasticSearch version 2.0]
In etc/hosts file "esnode" is mapped to IP address(some other machine where ES is running) as shown
192.168.2.219 esnode
The Transport Client code is ::
public Client getClient() {
if ((this.client == null)) {
try {
Settings settings = Settings.settingsBuilder()
.put("cluster.name", "myclustername").build();
TransportClient tClient = TransportClient.builder().settings(settings).build();
String[] nodes = "esnode:9300".split(COMMA);
for (String node : nodes) {
String[] hostPort = node.split(COLON);
tClient.addTransportAddress(new InetSocketTransportAddress(
InetAddress.getByName(hostPort[0]), Integer.parseInt(hostPort[1])));
}
this.client = tClient;
} catch (Exception e) {
e.printStackTrace();
}
}
return this.client;
}
This client code runs but when executing the below code :
this.getClient().prepareGet(indexName, typeName, String.valueOf(id)).get();
The exception is thrown:
NoNodeAvailableException[None of the configured nodes are available: []]
at org.elasticsearch.client.transport.TransportClientNodesService.ensureNodesAreAvailable(TransportClientNodesService.java:280)
at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:197)
at org.elasticsearch.client.transport.support.TransportProxyClient.execute(TransportProxyClient.java:55)
at org.elasticsearch.client.transport.TransportClient.doExecute(TransportClient.java:272)
at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:347)
at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:85)
at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:59)
at org.elasticsearch.action.ActionRequestBuilder.get(ActionRequestBuilder.java:67)
I have also tried using IPAddress instead of host name. The above code runs properly if
esnode is mapped to 127.0.0.1
Can somebody help...
Setup elasticsearch host ip address to network.host value in elasticsearch.yml
network.host: es_host_ip
This is solve TransportClient NoNodeAvailableException issue.
Check if your elasticsearch server have also version 2.0, if no, upgrade. Client and server must have the same version to work, I don't know why but this solved my problem.
Cheers,
Other reason could be, your Elasticsearch Java client is a different version from your Elasticsearch server.
Elasticsearch Java client version is nothing but your elasticsearch jar version in your code base.
For example: In my code it's elasticsearch-2.4.0.jar
To verify Elasticsearch server version,
$ /Users/kkolipaka/elasticsearch/bin/elasticsearch -version Version:
5.2.2, Build: f9d9b74/2017-02-24T17:26:45.835Z, JVM: 1.8.0_111
As you can see, I've downloaded latest version of Elastic server 5.2.2 but forgot to update the ES Java API client version 2.4.0 https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/client.html

Embedded Tomcat enable SSL

I'm trying to setup SSL for embedded Tomcat. Both connectors starts but I only get response on http. On https I get in chrome a "No data received message" when I try http://localhost:9000/
The port is open:
I've tried telnet
telnet localhost 9000
and I have a connection.
I've also tried
openssl s_client -connect localhost:9000
and GET / method
and my servlet prints me the expected result in console. I do not understand why I get this error in browsers(chrome and Firefox)
My OS is Ubuntu 14.04 and I've tried with both Java 7 and Java 8 having the same result. Tomcat version is 8.0.23 from Maven repo
The code is:
public class Main {
public static void main(String[] args) throws Exception {
Tomcat tomcat = new Tomcat();
Service service = tomcat.getService();
service.addConnector(getSslConnector());
File base = new File(System.getProperty("java.io.tmpdir"));
Context rootCtx = tomcat.addContext("/", base.getAbsolutePath());
Tomcat.addServlet(rootCtx, "emptyServlet", new EmptyServlet());
rootCtx.addServletMapping("/*", "emptyServlet");
tomcat.start();
tomcat.getServer().await();
}
private static Connector getSslConnector() {
Connector connector = new Connector();
connector.setPort(9000);
connector.setSecure(true);
connector.setScheme("https");
connector.setAttribute("keyAlias", "tomcat");
connector.setAttribute("keystorePass", "password");
connector.setAttribute("keystoreType", "JKS");
connector.setAttribute("keystoreFile",
"keystore.jks");
connector.setAttribute("clientAuth", "false");
connector.setAttribute("protocol", "HTTP/1.1");
connector.setAttribute("sslProtocol", "TLS");
connector.setAttribute("maxThreads", "200");
connector.setAttribute("protocol", "org.apache.coyote.http11.Http11AprProtocol");
connector.setAttribute("SSLEnabled", true);
return connector;
}
}
The keystore you can find it on github
I've already tried different keystores but with the same result. Also the keystore looks good: keytool -list -keystore keystore.jks seems to be as expected.
Thanks in advance
It turned out to be my fault. The service was up and running but I kept on trying on http://localhost:9000 not https://locahost:9000 in my browser

Categories