01、ES实战:创建客户端

Java建立客户端

调用API方法前提,通过putty串行接口连接软件(选择SSH)开启安装在Linux环境下的elasticsearch5.1.2的服务。

需要注意的几点:

1、 cluster.name是安装elasticsearch时设置的名称;
2、 调用服务时xxx.xxx.xxx.xxx写的是虚拟机的ip;
3、 端口填写elasticsearch默认的9300端口;

public class ESClient(){
   
     
    public TransportClient CreateClient(){
   
     
        try{
   
     
            /*创建客户端*/
            //client startup
            //设置集群名称
            Settings settings = 
                Settings.builder().put("cluster.name", "elsearch")
                .put("client.transport.sniff", true)
                .build();
            //创建client
            TransportClient client = 
                new PreBuiltTransportClient(settings)
                .addTransportAddress(
                new InetSocketTransportAddress(
                InetAddress.getByName("xxx.xxx.xxx.xxx"),9300));
            return client;
        }catch(Exception e){
   
     
            e.printStackTrace();
        }
    }
}
基于 Spring Boot 建立 Elasticsearch 的初始化 Bean

客户端连接初始化 Bean

package com.ygsoft.matcloud.tcp.dap.server.tools.config;

import java.net.InetAddress;

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * ES Client
 * @author yinlilan
 * @version 1.0
 */
@Configuration
public class ElasticsearchConfig {
   
     

	private static final Logger LOGGER = LoggerFactory.getLogger(ElasticsearchConfig.class);

	/**
	 * elk集群地址
	 */
	@Value("${elasticsearch.ip}")
	private String hostName;
	/**
	 * 端口
	 */
	@Value("${elasticsearch.port}")
	private String port;
	/**
	 * 集群名称
	 */
	@Value("${elasticsearch.cluster.name}")
	private String clusterName;

	/**
	 * 连接池
	 */
	@Value("${elasticsearch.pool}")
	private String poolSize;

	@Bean
	public TransportClient init() {
   
     

		TransportClient transportClient = null;

		try {
   
     
			// 配置信息
			Settings esSetting = Settings.builder()
					.put("cluster.name", clusterName)
					.put("client.transport.sniff", true)// 增加嗅探机制,找到ES集群
					.put("thread_pool.search.size", Integer.parseInt(poolSize))// 增加线程池个数,暂时设为5
					.build();

			transportClient = new PreBuiltTransportClient(esSetting);
			InetSocketTransportAddress inetSocketTransportAddress = new InetSocketTransportAddress(
					InetAddress.getByName(hostName), Integer.valueOf(port));
			transportClient.addTransportAddresses(inetSocketTransportAddress);

		} catch (Exception e) {
   
     
			LOGGER.error("elasticsearch TransportClient create error!!!", e);
		}

		return transportClient;
	}
}

使用方式通过 @Autowired 注入的方式 使用

/**
 * ES 工具类
 * 
 * @author yinlilan
 * @version 1.0
 */
@Component
public class ElasticsearchUtils {
   
     

	private static final Logger LOGGER = LoggerFactory.getLogger(ElasticsearchUtils.class);

	@Autowired
	private TransportClient transportClient;

	private static TransportClient client;

	@PostConstruct
	public void init() {
   
     
		client = this.transportClient;
	}
	
    ...
}

版权声明:本文不是「本站」原创文章,版权归原作者所有 | 原文地址: