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:9200data:elasticsearch:repositories:enabled: trueapplication:name: springboot-es
server:port: 9070knife4j:enable: trueopenapi:title: knife4测试elasticsearchdescription: 测试接口email: hbo@djs.comconcat: 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;@Overridepublic 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> {
}

image-20230311112826280

服务层接口(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 {@Autowiredprivate IBookDocRepository ibdr;/**转换为List集合*/@Overridepublic List<BookDoc> findAll() {List<BookDoc> list = new ArrayList<>();ibdr.findAll().forEach((bc) -> list.add(bc));return list;}@Overridepublic 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 {@Autowiredprivate 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 {@Autowiredprivate IBookRepository bookRepository;@Testpublic void saveBook(){bookRepository.save(new BookDoc("wnhz001","西游记","吴承恩","神话出版社", BigDecimal.valueOf(33.5),"历经千险,终成大道", LocalDate.now()));}@Testpublic 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

image-20230308173855180

  • knife4测试

image-20230311122456638

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/700815.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

TT语音×个推:流失预测准确率超90%,撬动存量增长个推GeTui 2024-02-23 09:50 浙江

当移动互联网进入存量时代&#xff0c;如何更高效地进行用户全生命周期管理、提升用户生命周期价值&#xff0c;变得尤为重要。TT语音是国内领先的兴趣社交平台&#xff0c;累计注册用户数高达数亿。为了进一步盘活存量用户价值&#xff0c;TT语音从2019年起便与个推合作&#…

Java-8函数式编程设计-Functional-Interface

Java 8函数式编程设计-Functional-Interface 我自己的理解&#xff0c;函数式编程对用户最大的价值是促使开发者养成模块化编程的习惯&#xff0c;代码可读性和维护性提高很多。 通过阅读JDK 8的 java.util.function 和 java.util.stream 包源码&#xff0c;意在理解Java的函数…

AcWing 872:最大公约数 ← 递归及非递归解法等

【题目来源】https://www.acwing.com/problem/content/874/【题目描述】 给定 n 对正整数 ai,bi&#xff0c;请你求出每对数的最大公约数。【输入格式】 第一行包含整数 n。 接下来 n 行&#xff0c;每行包含一个整数对 ai,bi。【输出格式】 输出共 n 行&#xff0c;每行输出一…

三分钟快速搭建家纺行业小程序商城:轻松实现电子商务梦想

随着互联网的普及和移动设备的广泛使用&#xff0c;越来越多的商业活动正在向数字化转型。在这个过程中&#xff0c;小程序商城作为一种新型的电子商务模式&#xff0c;正逐渐受到商家的青睐。本文将通过具体步骤&#xff0c;指导读者如何开发一个纺织辅料小程序商城。 一、选择…

使用c++filt 还原RTTI返回的类型名称

考虑下面的代码 const char ori[] "feelling good"; auto copy ori; std::cout << typeid(ori).name() << \n; std::cout << typeid(copy).name() << \n; 在g 编译后输出 A14_c PKc typeid操作符会返回一个const type_info&对象&a…

EarMaster Pro 7 简体中文破解版下载 v7.2.0.42 电脑版

软件介绍 EarMaster Pro 简体中文破解版是一款由丹麦皇家音乐学院官方制作的多功能音乐品鉴教育软件&#xff0c;软件具有丰富的功能&#xff0c;它可以自定义培训课程&#xff0c;针对性地训练音准、节奏、和声等音乐要素&#xff0c;用户可以根据自身需求和水平选择不同难度…

【探索Linux】—— 强大的命令行工具 P.23(线程池 —— 简单模拟)

阅读导航 引言一、线程池简单介绍二、Linux下线程池代码⭕Makefile文件⭕ . h 头文件✅Task.hpp✅thread.hpp✅threadPool.hpp ⭕ . cpp 文件✅testMain.cpp 三、线程池的优点温馨提示 引言 在Linux下&#xff0c;线程池是一种常见的并发编程模型&#xff0c;它能够有效地管理…

代码随想录算法训练营(动态规划2)| 62.不同路径 63. 不同路径 II

62.不同路径 本题大家掌握动态规划的方法就可以。 数论方法 有点非主流&#xff0c;很难想到。 leetcode题目链接 题目链接/文章讲解 视频讲解 二叉树的深度搜索&#xff0c;找到总共有几个叶子节点 这个想理解好难啊 //每个节点都是从左向右&#xff0c;然后回溯&#xff0c…

一键搭建Tex书写环境【免费、开源】

安装 # 安装 vscode scoop install extras/vscode # 安装 cmder(windows下超好用的命令行工具&#xff0c;可选) scoop install main/cmder-full # 安装 miktex scoop install main/miktex配置 将我的VSCode配置文件导入到 VSCode 即可。 VSCode for Latex 配置文件 具体步…

neo4j创建新数据库

根据网上提供的教程&#xff0c;neo4j并没有提供创建数据库的命令&#xff0c;其只有一个默认数据库graph.db&#xff0c;该数据库中的所有数据将存储在neo4j安装路径下的data/databases/graph.db目录中。 因此&#xff0c;我们猜想&#xff0c;如果我们将默认数据库的名字修改…

Qt:把文件夹从A移动到B

QT 文件复制&#xff0c;移动(剪切)操作_qt剪切文件到指定路径-CSDN博客 如何移动一个文件&#xff1f; QString old_nameQString("D:\\c\\c优秀源码学习.txt");QString new_nameQString("D:\\c优秀源码学习.txt");bool x QFile::rename(old_name,new_n…

【TypeScript基础知识点】的讲解

TypeScript基础知识点 TypeScript基础知识点 TypeScript基础知识点 TypeScript 是一种由 Microsoft 开发和维护的开源编程语言&#xff0c;它是 JavaScript 的一个超集&#xff0c;添加了可选的静态类型和基于类的面向对象编程&#xff0c;以下是一些 TypeScript 的基础知识点…

【C++】模板初阶 | 泛型编程 | 函数模板 | 类模板

目录 1. 泛型编程 2. 函数模板 2.1 函数模板概念 2.2 函数模板格式 2.3 函数模板的原理 2.4 函数模板的实例化 2.5 模板参数的匹配原则 3. 类模板 3.1 类模板的定义格式 3.2 类模板的实例化 【本节目标】 1. 泛型编程 2. 函数模板 3. 类模板 1. 泛型编程 如何实现一…

SpringCloud(16)之SpringCloud OpenFeign和Ribbon

一、Spring Cloud OpenFeign介绍 Feign [feɪn] 译文 伪装。Feign是一个轻量级的Http封装工具对象,大大简化了Http请求,它的使用方法 是定义一个接口&#xff0c;然后在上面添加注解。不需要拼接URL、参数等操作。项目主页&#xff1a;GitHub - OpenFeign/feign: Feign makes w…

车载测试面试:各大车企面试题汇总(含答案)

本博主协助百名人员成功进军车载行业 TBOX 深圳 涉及过T-BOX测试吗Ota升级涉及的台架环境是什么样的&#xff1f;上车实测之前有没有一个仿真环境台架环境都什么零部件T-BOX了解多少Linux和shell有接触吗 单片机uds诊断是在实车上座的吗 uds在实车上插的那口 诊断仪器是哪个…

java课设之简易版客房管理系统(mvc三层架构)

&#xff08;一&#xff09;、系统概述&#xff1a; 客房管理系统是一个用于管理酒店客房信息的程序&#xff0c;主要功能包括客房信息录入、客房状态查询、客房订单管理&#xff0c;客房的预定功能。 &#xff08;二&#xff09;、功能说明&#xff1a; 1.登录&#xff1a;管理…

如何在Linux部署OpenGauss数据管理系统并实现公网访问内网数据

文章目录 前言1. Linux 安装 openGauss2. Linux 安装cpolar3. 创建openGauss主节点端口号公网地址4. 远程连接openGauss5. 固定连接TCP公网地址6. 固定地址连接测试 前言 openGauss是一款开源关系型数据库管理系统&#xff0c;采用木兰宽松许可证v2发行。openGauss内核深度融合…

数字化转型导师坚鹏:如何制定政府数字化转型年度培训规划

如何制定政府数字化转型年度培训规划 ——以推动政府数字化转型战略落地为核心&#xff0c;实现知行果合一 课程背景&#xff1a; 很多政府都在开展政府数字化转型培训工作&#xff0c;目前存在以下问题急需解决&#xff1a; 缺少针对性的政府数字化转型年度培训规划 不清…

【pytorch】pytorch模型可复现设置

文章目录 序言1. 可复现设置代码2. 可复现设置代码解析2.1 消除python与numpy的随机性2.2 消除torch的随机性2.3 消除DataLoader的随机性2.4 消除cuda的随机性2.5 避免pytorch使用不确定性算法2.6 使用pytorch-lightning2.7 特殊情况 序言 为了让模型在同一设备每次训练的结果…

复旦大学MBA聚劲联合会:洞见智慧,拓宽思维格局及国际化视野

12月2日&#xff0c;“焕拥时代 俱创未来”聚劲联合会俱创会年度盛典暨俱乐部募新仪式圆满收官。16家复旦MBA俱乐部、200余名同学、校友、各界同仁齐聚复旦管院&#xff0c;一起在精彩纷呈的圆桌论坛里激荡思想&#xff0c;在活力四射的俱乐部风采展示中凝聚力量。      以…