ElasticSearch6.x 基于SpringBoot 实现ElasticSearch连接功能封装

第一步:基于SpringBoot2.1.2,依赖ElasticSearch jar 包


  
  		
			org.springframework.boot
			spring-boot-starter-data-elasticsearch
		
  

第二步:封装ElasticSearch 连接参数实体对象(ElasticSearchConfigEntity.java)

功能描述:声明ElasticSearchConfigEntity.java 为组件,实现读取application.properties 配置文件中涉及elasticsearch 服务器连接参数。

package com.zzg.ela.entity;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * ElasticSearch 实例化对象
 * @author zzg
 *
 */
@Component
public class ElasticSearchConfigEntity {
	// 集群名称
	@Value("${com.zzg.ela.cluster_name}")
	private String cluster_name;

	
	// 是否开启本地存储
	@Value("${com.zzg.ela.enable}")
	private String enable;
	
	// ip地址
	@Value("${com.zzg.ela.ip}")
	private String ip;
	
	// port 地址
	@Value("${com.zzg.ela.port}")
	private String port;
	
	// set 和 get
	public String getCluster_name() {
		return cluster_name;
	}

	public void setCluster_name(String cluster_name) {
		this.cluster_name = cluster_name;
	}

	public String getEnable() {
		return enable;
	}

	public void setEnable(String enable) {
		this.enable = enable;
	}

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public String getPort() {
		return port;
	}

	public void setPort(String port) {
		this.port = port;
	}
}

第三步:application.properties 配置涉及elasticsearch 服务器相关配置参数,截图如下:

ElasticSearch6.x 基于SpringBoot 实现ElasticSearch连接功能封装_第1张图片

第四步:编写ElasticSearch 配置参数对象(ElasticSearchConfig.java)

功能说明:注入ElasticSearchConfigEntity对象,实例化TransportClient对象。

package com.zzg.ela.config;

import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.annotation.PostConstruct;

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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.zzg.ela.entity.ElasticSearchConfigEntity;

/**
 * ElasticSearch 配置
 * 
 * @author zzg
 *
 */
@Configuration
public class ElasticSearchConfig {
	@Autowired
	private ElasticSearchConfigEntity entity;

	@PostConstruct
	void init() {
		System.setProperty("es.set.netty.runtime.available.processors", "false");
	}

	/**
	 * ela 连接实列化对象
	 * 
	 * @return
	 */
	@Bean
	public TransportClient getTransportClient() {
		// 9300是es的tcp服务端口
		TransportAddress node = null;

		try {
			node = new TransportAddress(InetAddress.getByName(entity.getIp()), Integer.valueOf(entity.getPort()));
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// 设置es节点的配置信息
		Settings settings = Settings.builder().put("cluster.name", entity.getCluster_name()).build();

		// 实例化es的客户端对象
		TransportClient client = new PreBuiltTransportClient(settings);
		client.addTransportAddress(node);

		return client;
	}
}

第五步:编写ElasticSearchUtil工具类对象

功能说明:注入TransportClient, 封装elasticsearch 搜索引擎的通用操作。

@Component
public class ElasticSearchIndexUtil {
	// 引入 Ela 连接实列化对象
	@Autowired
	private TransportClient client;

    /**其他功能待补充**/

}

 

你可能感兴趣的:(elasticsearch,学习笔记)