ELK入门(二)- springboot整合ES

这篇具有很好参考价值的文章主要介绍了ELK入门(二)- springboot整合ES。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

springboot整合elasticsearch

引用依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.ohb.springboot</groupId>
    <artifactId>springboot-es</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.knife4j.version>4.0.0</project.knife4j.version>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.13</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--引入elasticsearch-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        
        

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.24</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

配置文件(application.yml)

spring:
  elasticsearch:
    uris: http://192.168.198.129:9200
  data:
    elasticsearch:
      repositories:
        enabled: true

  application:
    name: springboot-es
server:
  port: 9070

knife4j:
  enable: true
  openapi:
    title: knife4测试elasticsearch
    description: 测试接口
    email: hbo@djs.com
    concat: ohb

配置类

package com.ohb.springboot.es.config;


import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.RestClients;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;

/**
 * Elasticsearch配置
 */
public class ElasticsearchConfig extends AbstractElasticsearchConfiguration {

    @Value("${spring.elasticsearch.uris}")
    private String uris;

    @Override
    public RestHighLevelClient elasticsearchClient() {
       ClientConfiguration configuration=
               ClientConfiguration.builder()
                       .connectedTo(uris)
                       .build();
       return RestClients.create(configuration).rest();
    }
}

文档类

package com.ohb.springboot.es.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.DateFormat;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

import java.math.BigDecimal;
import java.time.LocalDate;

/**
 * 创建文档实体类
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Document(indexName = "book_index",createIndex = true)
public class BookDoc {
    @Id
    @Field(type = FieldType.Text)
    private String id;

    @Field(type = FieldType.Text)
    private String title;

    @Field(type = FieldType.Text)
    private String author;

    @Field(type = FieldType.Text)
    private String publisher;

    @Field(type = FieldType.Double)
    private BigDecimal price;

    @Field(type = FieldType.Text)
    private String content;

 	@Field(type = FieldType.Date,format = DateFormat.year_month_day)
    @JsonFormat(pattern = "yyyy-MM-dd",timezone = "GMT+8")
    private LocalDate publishDate;  //此处要使用LocalDate类型,否则会报转换错误
}

DTO类

package com.ohb.springboot.es.dto;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;

@Data
@NoArgsConstructor
@AllArgsConstructor
@ApiModel("DTO")
public class HttpResp {
    @ApiModelProperty("DTO返回代码")
    private int code;

    @ApiModelProperty("DTO返回信息")
    private String msg;

    @ApiModelProperty("dto返回时间")
    private Date time;

    @ApiModelProperty("dto返回数据")
    private Object results;
}

定义elasticsearch操作数据接口

package com.ohb.springboot.es.dao;


import com.ohb.springboot.es.entity.BookDoc;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

public interface IBookRepository extends ElasticsearchRepository<BookDoc,String> {
}

ELK入门(二)- springboot整合ES,elk,elk,spring boot,elasticsearch

服务层接口(service)

接口
package com.ohb.springboot.es.service;
import com.ohb.springboot.es.entity.BookDoc;

import java.util.List;

public interface IBookService {

    List<BookDoc> findAll();
    void addBookDoc(BookDoc bookDoc);
}

实现类
package com.ohb.springboot.es.service.impl;

import com.ohb.springboot.es.dao.IBookDocRepository;
import com.ohb.springboot.es.entity.BookDoc;
import com.ohb.springboot.es.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class BookServiceImpl implements IBookService {

    @Autowired
    private IBookDocRepository ibdr;

    /**
     转换为List集合
     */
    @Override
    public List<BookDoc> findAll() {
        List<BookDoc> list = new ArrayList<>();
        ibdr.findAll().forEach((bc) -> list.add(bc));
        return list;
    }

    @Override
    public void addBookDoc(BookDoc bookDoc) {
    }
}

控制层(controller)

package com.ohb.springboot.es.controller;

import com.ohb.springboot.es.dto.HttpResp;
import com.ohb.springboot.es.service.IBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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;

import java.util.Date;
@Api(tags = "图书接口类")
@RestController
@RequestMapping("/book/api")
public class BookController {

    @Autowired
    private IBookService ibs;

    @ApiOperation("查询所有图书信息")
    @GetMapping("/findAllBooks")
    public HttpResp findAllBooks() {
      return new HttpResp(20001, "查询所有图书成功", new Date(), ibs.findAll());
    }
}

测试

package com.ohb.springboot.es.dao;

import com.ohb.springboot.es.entity.Book;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.math.BigDecimal;
import java.time.LocalDate;

import static org.junit.Assert.*;

@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class IBookRepositoryTest {

    @Autowired
    private IBookRepository bookRepository;

    @Test
    public void saveBook(){
            bookRepository.save(new BookDoc("wnhz001","西游记","吴承恩","神话出版社", BigDecimal.valueOf(33.5),"历经千险,终成大道", LocalDate.now()));
    }

    @Test
    public void findAll(){
        bookRepository.findAll().forEach(System.out::println);
    }

}
 .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::               (v2.6.13)

2023-03-08 17:25:50.048  INFO 6936 --- [           main] c.o.s.es.dao.IBookRepositoryTest         : Starting IBookRepositoryTest using Java 1.8.0_311 on XTZJ-20220114OE with PID 6936 (started by Administrator in D:\wnhz-workspace\springboot\springboot-es)
2023-03-08 17:25:50.053  INFO 6936 --- [           main] c.o.s.es.dao.IBookRepositoryTest         : No active profile set, falling back to 1 default profile: "default"
2023-03-08 17:25:51.683  INFO 6936 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Elasticsearch repositories in DEFAULT mode.
2023-03-08 17:25:51.824  INFO 6936 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 128 ms. Found 1 Elasticsearch repository interfaces.
2023-03-08 17:25:51.840  INFO 6936 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Reactive Elasticsearch repositories in DEFAULT mode.
2023-03-08 17:25:51.847  INFO 6936 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 6 ms. Found 0 Reactive Elasticsearch repository interfaces.
2023-03-08 17:25:58.622  WARN 6936 --- [/O dispatcher 1] org.elasticsearch.client.RestClient      : request [GET http://192.168.198.129:9200/] returned 1 warnings: [299 Elasticsearch-7.17.7-78dcaaa8cee33438b91eca7f5c7f56a70fec9e80 "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.17/security-minimal-setup.html to enable security."]
2023-03-08 17:25:58.910  WARN 6936 --- [           main] org.elasticsearch.client.RestClient      : request [HEAD http://192.168.198.129:9200/book_index?ignore_throttled=false&ignore_unavailable=false&expand_wildcards=open%2Cclosed&allow_no_indices=false] returned 2 warnings: [299 Elasticsearch-7.17.7-78dcaaa8cee33438b91eca7f5c7f56a70fec9e80 "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.17/security-minimal-setup.html to enable security."],[299 Elasticsearch-7.17.7-78dcaaa8cee33438b91eca7f5c7f56a70fec9e80 "[ignore_throttled] parameter is deprecated because frozen indices have been deprecated. Consider cold or frozen tiers in place of frozen indices."]
2023-03-08 17:25:59.096  INFO 6936 --- [           main] c.o.s.es.dao.IBookRepositoryTest         : Started IBookRepositoryTest in 10.118 seconds (JVM running for 13.203)
2023-03-08 17:25:59.977  WARN 6936 --- [           main] org.elasticsearch.client.RestClient      : request [POST http://192.168.198.129:9200/book_index/_search?typed_keys=true&max_concurrent_shard_requests=5&ignore_unavailable=false&expand_wildcards=open&allow_no_indices=true&ignore_throttled=true&search_type=query_then_fetch&batched_reduce_size=512] returned 2 warnings: [299 Elasticsearch-7.17.7-78dcaaa8cee33438b91eca7f5c7f56a70fec9e80 "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.17/security-minimal-setup.html to enable security."],[299 Elasticsearch-7.17.7-78dcaaa8cee33438b91eca7f5c7f56a70fec9e80 "[ignore_throttled] parameter is deprecated because frozen indices have been deprecated. Consider cold or frozen tiers in place of frozen indices."]
2023-03-08 17:26:00.235  WARN 6936 --- [           main] org.elasticsearch.client.RestClient      : request [POST http://192.168.198.129:9200/book_index/_search?typed_keys=true&max_concurrent_shard_requests=5&ignore_unavailable=false&expand_wildcards=open&allow_no_indices=true&ignore_throttled=true&search_type=query_then_fetch&batched_reduce_size=512] returned 2 warnings: [299 Elasticsearch-7.17.7-78dcaaa8cee33438b91eca7f5c7f56a70fec9e80 "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.17/security-minimal-setup.html to enable security."],[299 Elasticsearch-7.17.7-78dcaaa8cee33438b91eca7f5c7f56a70fec9e80 "[ignore_throttled] parameter is deprecated because frozen indices have been deprecated. Consider cold or frozen tiers in place of frozen indices."]


Book(id=wnhz001, title=西游记, author=吴承恩, publisher=神话出版社, price=33.5, content=历经千险,终成大道, publishDate=Wed Mar 08 17:25:39 CST 2023)

Process finished with exit code 0

ELK入门(二)- springboot整合ES,elk,elk,spring boot,elasticsearch

  • knife4测试

ELK入门(二)- springboot整合ES,elk,elk,spring boot,elasticsearch文章来源地址https://www.toymoban.com/news/detail-834641.html

到了这里,关于ELK入门(二)- springboot整合ES的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • ELK实战,Linux版docker安装ElasticSearch、ES-head、Logstash、Kiabana入门,无坑详细图解

            项目需要,记录一次ELK日志分析系统无坑初始安装过程,并给大家整理出了操作elasticsearch的主要命令,elasticsearch!伙伴们都懂得哦!别的不多说,看过内容概览,直接开整!!! 1-1 修改/etc/security/limits.conf limits.conf文件限制着用户可以使用的最大文件数,最大线

    2023年04月09日
    浏览(32)
  • SpringBoot整合ELK教程

    ELK 即 Elasticsearch、Logstash、Kibana,组合起来可以搭建线上日志系统,本文主要讲解使用 ELK 来收集测试框架产生的日志。 Elasticsearch:用于存储收集到的日志信息; Logstash:用于收集日志,测试框架应用整合了 Logstash 以后会把日志发送给 Logstash,Logstash再把日志转发给Elasticsear

    2024年01月21日
    浏览(32)
  • 知识点13--spring boot整合elasticsearch以及ES高亮

    本章知识点沿用知识点12的项目,介绍如何使用spring boot整合ES,没有ES的去我主页 各类型大数据集群搭建文档--大数据原生集群本地测试环境搭建三 中可以看到ES如何搭建 不管你有没有ES,最好是没有,因为一定要知道一点,一定要去官网查一下你当前用的spring boot data es的版

    2024年02月12日
    浏览(37)
  • ELK日志分析--ES(Elasticsearch)--(一)

    Elasticsearch:存储、搜索和分析 Elasticsearch是Elastic Stack核心的分布式搜索和分析引擎。Logstash和Beats有助于收集,聚合和丰富你的数据并将其存储在Elasticsearch中。使用Kibana,你可以交互式地探索,可视化和共享对数据的见解,并管理和监视堆栈。Elasticsearch是发生索引,搜索和分

    2024年02月02日
    浏览(47)
  • Spring Boot 整合Elasticsearch入门

    Spring Data Elasticsearch 是 Spring Data 项目的子项目,提供了 Elasticsearch 与 Spring 的集成。实现了 Spring Data Repository 风格的 Elasticsearch 文档交互风格,让你轻松进行 Elasticsearch 客户端开发。 应粉丝要求特地将 Elasticsearch 整合到 Spring Boot  中去。本来打算整合到 kono 脚手架中,但是转

    2024年04月13日
    浏览(35)
  • springboot整合ELK+kafka采集日志

    在分布式的项目中,各功能模块产生的日志比较分散,同时为满足性能要求,同一个微服务会集群化部署,当某一次业务报错后,如果不能确定产生的节点,那么只能逐个节点去查看日志文件;logback中RollingFileAppender,ConsoleAppender这类同步化记录器也降低系统性能,综上一些

    2024年02月15日
    浏览(37)
  • elasticsearch+logstash+kibana整合(ELK的使用)第一课

    进入 如图,一共四个地方

    2024年02月08日
    浏览(62)
  • ELK 处理 Spring Boot 日志

    来源:ibm.com/developerworks/cn/java /build-elk-and-use-it-for-springboot -and-nginx/index.html ELK 简介 Logstash Elasticsearch Kibana ELK 实现方案 ELK 平台搭建 安装 Logstash 安装 Elasticsearch 安装 Kibana 在 Spring Boot 中使用 ELK 修改并部署 Spring Boot 项目 配置 Shipper 角色 Logstash 配置 Indexer 角色 Logstash 查看效果

    2024年02月07日
    浏览(30)
  • 【微服务】Spring Boot集成ELK实用案例

    推荐一款我一直在用国内很火的 AI网站 ,包含 GPT3.5/4.0、文心一言、通义千问、智谱AI等多个AI模型 ,支持PC、APP、VScode插件同步使用,点击链接跳转-ChatGPT4.0中文版 在现代软件开发中,微服务架构已成为一种流行趋势。随之而来的挑战之一是如何有效地管理和分析分布在各个

    2024年02月20日
    浏览(28)
  • SpringBoot应用整合并使用Docker安装ELK实现日志收集

    ELK即Elasticsearch、Logstash、Kibana,组合起来可以搭建线上日志系统,本文主要讲解使用ELK来收集SpringBoot应用产生的日志。 Elasticsearch:用于存储收集到的日志信息; Logstash:用于收集日志,SpringBoot应用整合了Logstash以后会把日志发送给Logstash,Logstash再把日志转发给Elasticsearch; Kiban

    2023年04月08日
    浏览(34)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包