基于spring data的Elastic Search的配置示例_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 基于spring data的Elastic Search的配置示例

基于spring data的Elastic Search的配置示例

 2018/3/15 18:47:17  笨小孩在早起  程序员俱乐部  我要评论(0)
  • 摘要:基于springdata的ElasticSearch的配置示例importjava.net.InetAddress;importjava.net.UnknownHostException;importjava.util.ArrayList;importorg.apache.commons.lang3.tuple.ImmutablePair;importorg.apache.commons.lang3.tuple.Pair;importorg.elasticsearch.client
  • 标签:配置 Spring
基于spring data的Elastic Search的配置示例


class="java">
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;

import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;

import com.xxx.cms.config.props.EsProps;
xxx
@Configuration
@EnableElasticsearchRepositories(
		basePackages={"com.xxx.cms.elasticsearch.repositories"}
		)
public class ElasticSearchConfig {

	private Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	Environment env;
	
	@Bean
    public ElasticsearchOperations elasticsearchTemplate() {
        return new ElasticsearchTemplate(elasticsearchClient());
    }
	
	@Bean
	public EsProps esProps(){
		
		EsProps esProps = new EsProps();
		esProps.setInit(Boolean.valueOf(env.getProperty("hs.cms.srv.es.init")));
		return esProps;
	}
	
	@Bean
	public Client elasticsearchClient(){
		
		String esNodes = env.getProperty("hs.cms.srv.es.cluster.nodes"),
				clusterName = env.getProperty("hs.cms.srv.es.cluster.name");
		ArrayList<Pair<String, Integer>> nodePairs = new ArrayList<>();
		for (String nodeStr : esNodes.split(",")) {
			
			String[] nodeSpliArr = nodeStr.split(":");
			Pair<String, Integer> pair = new ImmutablePair<>(nodeSpliArr[0], Integer.valueOf(nodeSpliArr[1]));
			nodePairs.add(pair);
		}
		
		Settings settings = Settings.settingsBuilder()
                .put("cluster.name", clusterName)
                .build();
		TransportClient client = TransportClient.builder()
                .settings(settings)
                .build();
		for (Pair<String, Integer> pair : nodePairs) {
			
			try {
				client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(pair.getLeft()), pair.getRight()));
			} catch (UnknownHostException e) {
				
				logger.error("初始化Es配置出错", e);
				return null;
			}
		}
		return client;
	}
	
}
发表评论
用户名: 匿名