|
@@ -0,0 +1,79 @@
|
|
|
+package com.persagy.apm.energy.report.workorder.config;
|
|
|
+
|
|
|
+import org.apache.commons.lang.StringUtils;
|
|
|
+import org.elasticsearch.client.Client;
|
|
|
+import org.elasticsearch.client.transport.TransportClient;
|
|
|
+import org.elasticsearch.common.settings.Settings;
|
|
|
+import org.elasticsearch.common.transport.TransportAddress;
|
|
|
+import org.elasticsearch.transport.client.PreBuiltTransportClient;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
|
|
+import org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchAutoConfiguration;
|
|
|
+import org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration;
|
|
|
+import org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositoriesAutoConfiguration;
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
+import org.springframework.core.convert.support.DefaultConversionService;
|
|
|
+import org.springframework.core.env.Environment;
|
|
|
+import org.springframework.data.elasticsearch.config.ElasticsearchConfigurationSupport;
|
|
|
+import org.springframework.data.elasticsearch.core.ElasticsearchEntityMapper;
|
|
|
+import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
|
|
|
+import org.springframework.data.elasticsearch.core.EntityMapper;
|
|
|
+import org.springframework.data.elasticsearch.core.convert.ElasticsearchCustomConversions;
|
|
|
+
|
|
|
+import javax.annotation.PostConstruct;
|
|
|
+import java.net.InetAddress;
|
|
|
+import java.net.UnknownHostException;
|
|
|
+import java.util.Arrays;
|
|
|
+
|
|
|
+@Configuration
|
|
|
+@AutoConfigureAfter({ElasticsearchAutoConfiguration.class, ElasticsearchDataAutoConfiguration.class, ElasticsearchRepositoriesAutoConfiguration.class})
|
|
|
+public class ElasticsearchCustomConfig extends ElasticsearchConfigurationSupport {
|
|
|
+ @Autowired
|
|
|
+ private Environment environment;
|
|
|
+
|
|
|
+ @PostConstruct
|
|
|
+ void init() {
|
|
|
+ System.setProperty("es.set.netty.runtime.available.processors", "false");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ @Override
|
|
|
+ public EntityMapper entityMapper() {
|
|
|
+ ElasticsearchEntityMapper entityMapper = new ElasticsearchEntityMapper(
|
|
|
+ elasticsearchMappingContext(), new DefaultConversionService());
|
|
|
+ entityMapper.setConversions(elasticsearchCustomConversions());
|
|
|
+ return entityMapper;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ @Override
|
|
|
+ public ElasticsearchCustomConversions elasticsearchCustomConversions() {
|
|
|
+ return new ElasticsearchCustomConversions(Arrays.asList(new WorkorderToMap(), new MapToWorkorder()));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean(name = {"elasticsearchOperations", "elasticsearchTemplate"})
|
|
|
+ public ElasticsearchTemplate elasticsearchTemplate() throws UnknownHostException {
|
|
|
+ return new ElasticsearchTemplate(elasticsearchClient(), entityMapper());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public Client elasticsearchClient() throws UnknownHostException {
|
|
|
+ String clusterName = environment.getProperty("spring.data.elasticsearch.cluster-name");
|
|
|
+ String clusterNodes = environment.getProperty("spring.data.elasticsearch.cluster-nodes");
|
|
|
+ if (StringUtils.isBlank(clusterName)) {
|
|
|
+ clusterName = "elasticsearch";
|
|
|
+ }
|
|
|
+ if (StringUtils.isBlank(clusterNodes)) {
|
|
|
+ clusterNodes = "127.0.0.1:9300";
|
|
|
+ }
|
|
|
+ Settings settings = Settings.builder().put("cluster.name", clusterName).build();
|
|
|
+ TransportClient client = new PreBuiltTransportClient(settings);
|
|
|
+ String[] split = clusterNodes.split(",");
|
|
|
+ for (int i = 0; i < split.length; i++) {
|
|
|
+ String[] addrs = split[i].split(":");
|
|
|
+ client.addTransportAddress(new TransportAddress(InetAddress.getByName(addrs[0]), Integer.valueOf(addrs[1])));
|
|
|
+ }
|
|
|
+ return client;
|
|
|
+ }
|
|
|
+}
|