【Elasticsearch】安装并和SpringBoot进行整合

一、下载Elasticsearch并解压

https://www.elastic.co/cn/downloads/elasticsearch

 二、修改conf\elasticsearch.yml文件,末尾处加上以下代码,使ES支持跨域请求

http.cors.enabled: true
http.cors.allow-origin: "*"
network.host: 127.0.0.1

 三、双击bin目录下的elasticsearch.bat文件进行启动

我们会发现在ElasticSearch启动时,会占用两个端口9200和9300:
9200 是ES节点与外部通讯使用的端口。它是http协议的RESTful接口(各种CRUD操作都是走的该端口,如查询:http://localhost:9200/user/_search)
9300是ES节点之间通讯使用的端口。它是tcp通讯端口,集群间和TCPclient都走的它(java程序中使用ES时,在配置文件中要配置该端口)

 四、浏览器访问localhost:9200进行测试

五、下载Kibana并解压,步骤同上

https://www.elastic.co/cn/downloads/kibana

 六、双击bin目录下的kibana.bat文件进行启动

 七、Kibana默认端口是5601,浏览器进行访问

 八、SpringBoot项目中引入相关依赖

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <dependencies>
        <!--SpringBoot-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.7.4</version>
        </dependency>
        <!--Lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>
        <!--Commons-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>
        <!--elasticsearch-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
    </dependencies>

九、创建相关实体类

package cn.sdata.entity;

import lombok.Data;
import org.springframework.data.elasticsearch.annotations.Document;

import java.io.Serializable;

/**
 * @author lzw
 * @create 2022-10-25-15:02
 */
@Document(indexName = "user")
@Data
public class User implements Serializable {
    private static final long serialVersionUID = 551589397625941750L;
    private Long id;
    private String name;
    private String age;
}

十、创建一个接口实现ElasticsearchRepository

import cn.sdata.entity.User;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
/**
 * @author lzw
 * @create 2022-10-25-15:03
 */
public interface UserService extends ElasticsearchRepository<User,Long> {
}

十一、创建Controller进行添加、查询、删除测试

package cn.sdata.controller;

import cn.sdata.entity.User;
import cn.sdata.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author lzw
 * @create 2022-10-25-15:02
 */
@RestController
@RequestMapping("test")
public class TestController {
    @Autowired
    private UserService userService;

    //添加
    @GetMapping("save")
    public void test() {
        User user = new User();
        user.setId(System.currentTimeMillis());
        user.setAge("15");
        user.setName("王五");
        userService.save(user);
    }

    //查询
    @GetMapping("findAll")
    public Iterable<User> findAll() {
        Iterable<User> all = userService.findAll();
        for (User user : all) {
            System.out.println(user);
        }
        return all;
    }

    //删除
    @GetMapping("del")
    public void del() {
        userService.deleteAll();
    }
}

十二、演示部分

添加数据:

查询数据:

 

kibana查询索引:

kibana查询数据:

 清空数据并查询:

 

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值