|
|
@@ -14,36 +14,41 @@ import org.elasticsearch.common.settings.Settings;
|
|
|
import org.elasticsearch.node.InternalSettingsPreparer;
|
|
|
import org.elasticsearch.node.Node;
|
|
|
import org.elasticsearch.transport.Netty4Plugin;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
|
|
+import javax.annotation.PostConstruct;
|
|
|
import java.util.Collections;
|
|
|
|
|
|
+@Component
|
|
|
public class EmbeddedElasticSearchConfig {
|
|
|
|
|
|
- static {
|
|
|
- System.setProperty("es.set.netty.runtime.available.processors", "false");
|
|
|
- }
|
|
|
+ @Value("${spring.elasticsearch.embedded.enabled}")
|
|
|
+ private boolean embeddedElasticsearchEnabled;
|
|
|
+
|
|
|
+ @Value("${spring.elasticsearch.embedded.port}")
|
|
|
+ private Integer esPort;
|
|
|
|
|
|
- public static boolean embeddedEnable() {
|
|
|
- try {
|
|
|
- Class.forName("cc.iotkit.temporal.es.config.ElasticsearchConfiguration");
|
|
|
- } catch (ClassNotFoundException e) {
|
|
|
- return false;
|
|
|
+ @PostConstruct
|
|
|
+ private void start(){
|
|
|
+ if (embeddedElasticsearchEnabled) {
|
|
|
+ startEmbeddedElasticSearch(esPort);
|
|
|
}
|
|
|
- return !"true".equals(System.getProperty("disabledEmbeddedEs"));
|
|
|
+
|
|
|
}
|
|
|
|
|
|
@SneakyThrows
|
|
|
- public static void startEmbeddedElasticSearch() {
|
|
|
- EmbeddedElasticSearch embeddedElasticSearch = new EmbeddedElasticSearch(new ConfigProperty());
|
|
|
+ public void startEmbeddedElasticSearch(Integer port) {
|
|
|
+ EmbeddedElasticSearch embeddedElasticSearch = new EmbeddedElasticSearch(new ConfigProperty(), port);
|
|
|
embeddedElasticSearch.start();
|
|
|
}
|
|
|
|
|
|
- public static class ConfigProperty {
|
|
|
+ public class ConfigProperty {
|
|
|
|
|
|
public Settings.Builder applySetting(Settings.Builder settings) {
|
|
|
String dataPath = "./data/elasticsearch";
|
|
|
String homePath = "./";
|
|
|
- int port = 9200;
|
|
|
+ int port = esPort;
|
|
|
String host = "0.0.0.0";
|
|
|
return settings.put("network.host", host)
|
|
|
.put("http.port", port)
|
|
|
@@ -56,7 +61,7 @@ public class EmbeddedElasticSearchConfig {
|
|
|
public static class EmbeddedElasticSearch extends Node {
|
|
|
|
|
|
@SneakyThrows
|
|
|
- public EmbeddedElasticSearch(ConfigProperty properties) {
|
|
|
+ public EmbeddedElasticSearch(ConfigProperty properties, Integer port) {
|
|
|
super(InternalSettingsPreparer.prepareEnvironment(
|
|
|
properties.applySetting(
|
|
|
Settings.builder()
|
|
|
@@ -65,7 +70,7 @@ public class EmbeddedElasticSearchConfig {
|
|
|
.put("transport.type", Netty4Plugin.NETTY_TRANSPORT_NAME)
|
|
|
.put("http.type", Netty4Plugin.NETTY_HTTP_TRANSPORT_NAME)
|
|
|
.put("network.host", "0.0.0.0")
|
|
|
- .put("http.port", 9200)
|
|
|
+ .put("http.port", port)
|
|
|
).build(), Collections.emptyMap(), null, () -> "default"),
|
|
|
Collections.singleton(Netty4Plugin.class), false);
|
|
|
}
|