Created
July 15, 2016 18:01
-
-
Save guilhermeblanco/8f9ec4ac82624db4e3254256d32bbbf6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.eblock.api; | |
import java.io.InputStream; | |
import java.net.InetAddress; | |
import java.net.UnknownHostException; | |
import org.elasticsearch.client.Client; | |
import org.elasticsearch.client.transport.TransportClient; | |
import org.elasticsearch.common.settings.Settings; | |
import org.elasticsearch.common.transport.InetSocketTransportAddress; | |
import org.elasticsearch.shield.ShieldPlugin; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.ComponentScan; | |
import org.springframework.context.annotation.PropertySource; | |
import org.springframework.core.env.Environment; | |
import org.springframework.util.StringUtils; | |
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; | |
@SpringBootApplication | |
@ComponentScan(basePackages = {"com.domain.api"}) | |
@PropertySource("classpath:application.properties") | |
public class Application extends WebMvcConfigurerAdapter { | |
@Autowired | |
private Environment env; | |
@Bean | |
public Client elasticSearchClient() | |
{ | |
final String clusterName = env.getProperty("elasticsearch.cluster.name"); | |
final String hostname = env.getProperty("elasticsearch.transport.hostname"); | |
final String pingSchedule = env.getProperty("elasticsearch.transport.pingSchedule"); | |
final int port = Integer.parseInt(env.getProperty("elasticsearch.transport.port")); | |
final boolean enableSSL = Boolean.parseBoolean(env.getProperty("elasticsearch.transport.ssl")); | |
final String shieldUser = String.format( | |
"%s:%s", | |
env.getProperty("elasticsearch.shield.username"), | |
env.getProperty("elasticsearch.shield.password") | |
); | |
final TransportClient.Builder transportClientBuilder = TransportClient.builder(); | |
final Settings.Builder settingsBuilder = Settings.builder() | |
.put("action.bulk.compress", false) | |
.put("cluster.name", clusterName) | |
.put("transport.sniff", false) | |
.put("transport.ping_schedule", pingSchedule) | |
.put("client.transport.ignore_cluster_name", false) | |
.put("request.headers.X-Found-Cluster", clusterName) | |
.put("shield.enabled", ! enableSSL) | |
; | |
if (enableSSL) { | |
transportClientBuilder.addPlugin(ShieldPlugin.class); | |
settingsBuilder | |
.put("shield.user", shieldUser) | |
.put("shield.transport.ssl", enableSSL) | |
; | |
} | |
final TransportClient client = transportClientBuilder | |
.settings(settingsBuilder.build()) | |
.build() | |
; | |
try { | |
for (InetAddress address : InetAddress.getAllByName(hostname)) { | |
client.addTransportAddress(new InetSocketTransportAddress(address, port)); | |
} | |
} catch (UnknownHostException exception) { | |
throw new RuntimeException("Unknown host", exception); | |
} | |
return client; | |
} | |
public static void main(String[] args) { | |
SpringApplication.run(Application.class, args); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
elasticsearch.cluster.name={my_cluster_name} | |
elasticsearch.transport.hostname={my_cluster_name}.{my_cluster_region}.aws.found.io | |
elasticsearch.transport.port=9343 | |
elasticsearch.transport.ssl=true | |
elasticsearch.transport.pingSchedule=5s | |
elasticsearch.shield.user={my_shield_username} | |
elasticsearch.shield.password={my_shield_password} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<parent> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-parent</artifactId> | |
<version>1.3.3.RELEASE</version> | |
<relativePath/> | |
</parent> | |
<properties> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
<java.version>1.8</java.version> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-jetty</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-websocket</artifactId> | |
<exclusions> | |
<exclusion> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-tomcat</artifactId> | |
</exclusion> | |
</exclusions> | |
</dependency> | |
<!-- Elasticsearch --> | |
<dependency> | |
<groupId>org.elasticsearch</groupId> | |
<artifactId>elasticsearch</artifactId> | |
<version>2.3.3</version> | |
</dependency> | |
<dependency> | |
<groupId>org.elasticsearch.plugin</groupId> | |
<artifactId>shield</artifactId> | |
<version>2.3.3</version> | |
</dependency> | |
<!-- Testing --> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-test</artifactId> | |
<scope>test</scope> | |
</dependency> | |
<repositories> | |
<repository> | |
<id>elasticsearch-releases</id> | |
<name>elasticsearch</name> | |
<url>http://maven.elasticsearch.org/releases</url> | |
<releases> | |
<enabled>true</enabled> | |
</releases> | |
<snapshots> | |
<enabled>false</enabled> | |
</snapshots> | |
</repository> | |
</repositories> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-maven-plugin</artifactId> | |
<configuration> | |
<executable>true</executable> | |
</configuration> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
Author
guilhermeblanco
commented
Jul 15, 2016
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment