|
| 1 | +package io.ymq.elasticsearch.config; |
| 2 | + |
| 3 | + |
| 4 | +import org.elasticsearch.client.Client; |
| 5 | +import org.elasticsearch.client.transport.TransportClient; |
| 6 | +import org.elasticsearch.common.settings.Settings; |
| 7 | +import org.elasticsearch.common.transport.InetSocketTransportAddress; |
| 8 | +import org.springframework.beans.factory.annotation.Value; |
| 9 | +import org.springframework.context.annotation.Bean; |
| 10 | +import org.springframework.context.annotation.Configuration; |
| 11 | +import org.springframework.data.elasticsearch.core.ElasticsearchOperations; |
| 12 | +import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; |
| 13 | +import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories; |
| 14 | + |
| 15 | +import java.net.InetAddress; |
| 16 | + |
| 17 | +/** |
| 18 | + * 描述: |
| 19 | + * |
| 20 | + * @author yanpenglei |
| 21 | + * @create 2017-11-01 17:19 |
| 22 | + **/ |
| 23 | +@Configuration |
| 24 | +@EnableElasticsearchRepositories(basePackages = "io.ymq.elasticsearch") |
| 25 | +public class ElasticsearchConfig { |
| 26 | + /** |
| 27 | + * elk集群地址 |
| 28 | + */ |
| 29 | + @Value("${elasticsearch.ip}") |
| 30 | + private String hostName; |
| 31 | + /** |
| 32 | + * 端口 |
| 33 | + */ |
| 34 | + @Value("${elasticsearch.port}") |
| 35 | + private String port; |
| 36 | + /** |
| 37 | + * 集群名称 |
| 38 | + */ |
| 39 | + @Value("${elasticsearch.cluster.name}") |
| 40 | + private String clusterName; |
| 41 | + |
| 42 | + /** |
| 43 | + * 连接池 |
| 44 | + */ |
| 45 | + @Value("${elasticsearch.pool}") |
| 46 | + private String poolSize; |
| 47 | + |
| 48 | + @Bean |
| 49 | + public Client client() throws Exception { |
| 50 | + |
| 51 | + Settings esSettings = Settings.builder().put("cluster.name", clusterName) |
| 52 | + .put("client.transport.sniff", true)//增加嗅探机制,找到ES集群 |
| 53 | + .put("thread_pool.search.size", Integer.parseInt(poolSize))//增加线程池个数,暂时设为5 |
| 54 | + .build(); |
| 55 | + |
| 56 | + return TransportClient.builder() |
| 57 | + .settings(esSettings) |
| 58 | + .build() |
| 59 | + .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(hostName), Integer.parseInt(port))); |
| 60 | + } |
| 61 | + |
| 62 | + @Bean |
| 63 | + public ElasticsearchOperations elasticsearchTemplate() throws Exception { |
| 64 | + return new ElasticsearchTemplate(client()); |
| 65 | + } |
| 66 | + |
| 67 | + |
| 68 | +} |
0 commit comments