首页 > 其他分享 >ElasticSearch学习 - (五)SpringBoot集成ElasticSearch5.X

ElasticSearch学习 - (五)SpringBoot集成ElasticSearch5.X

时间:2022-10-11 17:02:58浏览次数:57  
标签:SpringBoot InetSocketTransportAddress ElasticSearch5 client elasticsearch org im


步骤:

  1. 添加pom文件依赖
  2. 配置ElasticSearch
  3. 使用ElasticSearch的java客户端

一、添加pom文件依赖

<properties>
<elasticsearch.version>5.6.4</elasticsearch.version>
</properties>

<!-- elasticsearch -->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>${elasticsearch.version}</version>
</dependency>

<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>${elasticsearch.version}</version>
</dependency>

<!-- elasticsearch依赖,版本需要小于等于2.7 -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.7</version>
</dependency>

二、配置ElasticSearch

package com.ahut.config;

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

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

/**
*
* @ClassName: ElasticSearchConfig
* @Description: elasticsearch配置
* @author cheng
* @date
@Configuration
public class ElasticSearchConfig

/**
*
* @Title: transportClient
* @Description: 配置elasticsearch
* @return
* @throws
@Bean
public TransportClient transportClient() throws UnknownHostException {
// 一定要注意,9300为elasticsearch的tcp端口
InetSocketTransportAddress master = new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300);
InetSocketTransportAddress node1 = new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9301);
InetSocketTransportAddress node2 = new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9302);
// 集群名称
Settings settings = Settings.builder().put("cluster.name", "rick").build();
TransportClient client = new PreBuiltTransportClient(settings);
// 添加
client.addTransportAddresses(master, node1, node2);
return

三、使用ElasticSearch的java客户端

dao层中注入:

/**
* ElasticSearch客户端
*/
@Autowired
private

工具类中注入:

/**
* ElasticSearch客户端,静态字段不同直接装配,这里通过set方法装配
*/
private static TransportClient client;

/**
*
* @Title: setClient
* @Description: spring注入静态字段
* @param
@Autowired
public void setClient


标签:SpringBoot,InetSocketTransportAddress,ElasticSearch5,client,elasticsearch,org,im
From: https://blog.51cto.com/u_15824687/5747155

相关文章

  • SpringBoot学习-(十三)SpringBoot中建立WebSocket连接(STOMP)
    STOMP协议介绍STOMP,StreamingTextOrientatedMessageProtocol,是流文本定向消息协议,是一种为MOM(MessageOrientedMiddleware,面向消息的中间件)设计的简单文本协议。它提......
  • ElasticSearch学习 - (一)windows下安装ElasticSearch
    ElasticSearch版本5.6.4要求jdk版本必须为1.8以上1、从官网下载elasticsearch地址:​​https://www.elastic.co/downloads/elasticsearch​​2、解压zip到文件目录3、开启Ela......
  • SpringBoot学习-(十九)SpringBoot定时器#Schedule
    定时器概述后台项目开发中经常会用到定时器,现在实现定时器的方式也是多种多样。下面列举几种常见的定时器实现方式:Quartz:Quartz的使用相当广泛,它是一个功能强大的调度器,当然......
  • SpringBoot学习-(十五)SpringBoot热部署
    热部署最重要的功能就是自动应用代码更改到最新的App上面去。原理是在发现代码有更改之后,重新启动应用,但是比速度比手动停止后再启动还要更快,更快指的不是节省出来的手工操......
  • 运行springboot是左下角没有services窗口解决方案
    结果成功:参考博文:http://t.zoukankan.com/sxdcgaq8080-p-14543088.html......
  • SpringBoot整合ssh
    背景:测试环境连接生产环境的数据库,无法本地调试环境: JDK8Maven:3.6.3Springboot:2.1.4jsch:0.1.55Jsch百度百科介绍:JSch是SSH2的一个纯Java实现。它允许你连......
  • springboot2中多环境配置@@ 无法解析maven中的设置
    Maven(pom中设置环境)SpringBoot(yml中设置多环境)都具备对环境的开发控制maven优先度高于springboot,springboot基于maven的坐标配置需要在pom.xml中配置多环境开......
  • springboot~对mybatis的start包进行单元测试
    一个start包,它不需要有springboot启动类,它只提供一切公用的功能,被其它包依赖就行了,通过META-INF/spring.factories或者META-INF/spring/org.springframework.boot.autoconf......
  • springboot H2 linux下搭建使用
    这次研究是H2数据库了,关键还是再Linux下进行搭建部署的,被这个数据库快弄死了弄了4天时间,现在大致可以用了,还有些细节需要修正。我这边使用的是springboot集成模式。直接使......
  • Springboot 日志框架
    1、概述项目中日志系统是必不可少的的。目前比较流行的日志框架有log4j、logback等。可能大家还不知道,这两个框架的作者是同一个人,Logback旨在作为流行的log4j项目的后......