Spring boot 实战指南(三):整合Elasticsearch、swagger、redis、mq

news2025/4/7 19:00:14

文章目录

  • 一、Elasticsearch
    • 创建项目
    • 配置maven
    • 完善依赖
    • es连接配置
    • 实体映射
    • repository
    • service
    • controller
  • 二、swagger
    • 依赖
    • 启动类
    • 路径匹配配置
    • 配置类
    • controller注解
  • 三、redis

一、Elasticsearch

官方文档
Elasticsearch教程

自己搭建了一个简单的demo,仓库在这里,可以直接运行,不过需要自行安装Elasticsearch7.16.3并在application.yml改一下url。

创建项目

用spring initializr 快速搭建一个项目,sdk选的1.8,java版本是8。

spring boot版本选择2.7.x。

勾选这些依赖:

在这里插入图片描述

配置maven

file > settings > build,execution,deployment > build tools > maven

在这里插入图片描述

完善依赖

pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>tracy</groupId>
    <artifactId>claimnet-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>claimnet-client</name>
    <description>claimnet客户端</description>
    <properties>
        <java.version>8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

es连接配置

application.yml

spring:
  elasticsearch:
    rest:
      uris: http://es服务器ip:9200

实体映射

package tracy.claimnetclient.entity;

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

import java.io.Serializable;

@Data
@Document(indexName = "text",createIndex = false) //如果Elasticsearch库中原本不存在这个索引的数据,这里可以写true
public class Text implements Serializable {
    @Id //将下面这个属性标记为id
    @Field(name = "id",type = FieldType.Text)
    String id;
    @Field(name = "application_no",type = FieldType.Text)
    String applicationNo;
    @Field(name = "content",type = FieldType.Text)
    String content;
    @Field(name = "date",type = FieldType.Text)
    String date;
    @Field(name = "entity",type = FieldType.Text)
    String entity;
    @Field(name = "feature",type = FieldType.Text)
    String feature;
}

注意,这些属性和Elasticsearch数据库中对应文档中的属性是一一对应的。

repository

ElasticsearchRepository中提供了很多线程的方法,可以满足简单的增删改查需求:

package tracy.claimnetclient.repository;

import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import tracy.claimnetclient.entity.Text;
import java.util.List;

public interface TextRepository extends ElasticsearchRepository<Text,String> {//这里的String表示id的类型
	//PageRequest用来实现分页查询
    List<Text> findTextsByIdOrApplicationNoOrContent(String id, String app, String keyword, PageRequest pageRequest);
}

service

接口:

package tracy.claimnetclient.service;

import tracy.claimnetclient.entity.Text;
import tracy.claimnetclient.util.PairResult;

import java.util.List;

public interface TextService {
    List<Text> text(String keyword,
                    Integer curPage,
                    Integer pageSize);
}

实现:

package tracy.claimnetclient.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import tracy.claimnetclient.entity.Text;
import tracy.claimnetclient.repository.TextRepository;
import tracy.claimnetclient.util.PairResult;

import java.util.Arrays;
import java.util.List;

@Service
public class TextServiceImpl implements TextService{
    @Autowired
    private TextRepository textRepository;

    @Override
    public List<Text> text(String keyword, Integer curPage, Integer pageSize) {
        if(curPage==null)curPage=1;
        if(pageSize==null)pageSize=10;
        return textRepository.findTextsByIdOrApplicationNoOrContent(keyword,keyword,keyword, PageRequest.of(curPage-1,pageSize));
    }
}

controller

package tracy.claimnetclient.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tracy.claimnetclient.entity.Claim;
import tracy.claimnetclient.entity.Text;
import tracy.claimnetclient.service.ClaimService;
import tracy.claimnetclient.service.TextService;

import java.util.List;

@RestController
@RequestMapping("/search")
public class SearchController {
    @Autowired
    private TextService textService;

    @GetMapping("/text/{keyword}")
    public List<Text> text(@PathVariable("keyword")String keyword,
                           Integer curPage,
                           Integer pageSize) {
        return textService.text(keyword,curPage,pageSize);
    }
}

【OK】

二、swagger

参考

依赖

 <!-- 引入swagger相关的jar -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>

启动类

类上添加注解@EnableOpenApi

路径匹配配置

application.yml

spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

配置类

package tracy.claimnetclient.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.VendorExtension;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import java.util.ArrayList;

@Configuration
public class SwaggerConfiguration {
    @Bean
    public Docket docket() {
        //这里要配置controller包
        return new Docket(DocumentationType.OAS_30)
                .select().apis(RequestHandlerSelectors.basePackage("tracy.claimnetclient.controller"))
                .paths(PathSelectors.any()).build()
                .apiInfo(setApiInfo());
    }
    private ApiInfo setApiInfo() {
        //作者信息
        Contact contact = new Contact("tracy",
                "https://blog.csdn.net/Tracycoder?spm=1011.2415.3001.5343",
                "1409568085@qq.com");
        //项目描述
        ApiInfo info = new ApiInfo("Claimnet-client Restful Api", "", "v1.0",
                "https://blog.csdn.net/Tracycoder?spm=1011.2415.3001.5343", contact,
                "Apache 2.0", "", new ArrayList<VendorExtension>());
        return info;
    }
}

controller注解

使用类、方法、参数注解:

package tracy.claimnetclient.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tracy.claimnetclient.entity.Claim;
import tracy.claimnetclient.entity.Text;
import tracy.claimnetclient.service.ClaimService;
import tracy.claimnetclient.service.TextService;

import java.util.List;

@Api(tags = "检索模块") 
@RestController
@RequestMapping("/search")
public class SearchController {
    @Autowired
    private TextService textService;
    @Autowired
    private ClaimService claimService;

    @ApiOperation(value = "根据id、申请号、内容查询text索引文档", notes = "api/search/text", response = List.class)
    @GetMapping("/text/{keyword}")
    public List<Text> text(@ApiParam(value = "关键词", required = true) @PathVariable("keyword")String keyword,
                           @ApiParam(value = "当前页码") Integer curPage,
                           @ApiParam(value = "页大小") Integer pageSize) {
        return textService.text(keyword,curPage,pageSize);
    }
}

最后,启动项目,访问http://127.0.0.1:8080/swagger-ui/index.html。

三、redis

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

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

相关文章

分布式之CAP理论分析

写在前面 任何存在的事物都有其内在的特性&#xff0c;分布式也不例外&#xff0c;那么分布式都有什么特性呢&#xff1f;国外有大牛就帮我们总结了如下的三点&#xff1a; C:Consitency,一致性 A&#xff1a;Available&#xff0c;可用性 P&#xff1a;Partition tolerance&…

细讲一个 TCP 连接能发多少个 HTTP 请求(二)

第三个问题&#xff1a;一个 TCP 连接中 HTTP 请求发送可以一起发送么&#xff08;比如一起发三个请求&#xff0c;再三个响应一起接收&#xff09;&#xff1f; HTTP/1.1 存在一个问题&#xff0c;单个 TCP 连接在同一时刻只能处理一个请求&#xff0c;意思是说&#xff1a;两…

测试员都是背锅侠?测试人员避“锅”攻略,拿走不谢

最近发生了一起生产事故&#xff0c;究其根源&#xff0c;事故本身属于架构或者需求层面需要规避的问题&#xff0c;测试人员的责任其实是非常小的&#xff0c;但实际情况是&#xff1a;相关测试人员因此承担了很大的压力&#xff0c;成为质量问题的“背锅侠”。 实际上&#…

极验2代验证码分析

目标链接 aHR0cDovL3d3dy5qc2dzai5nb3YuY246NTg4ODgvbWluaS9uZXR3ZWIvU01MaWJyYXJ5LmpzcA接口分析 点击搜索就会跳出验证码&#xff0c;netWebServlet.json 的请求&#xff0c;会返回 challenge 和 gt 接着可以看响应请求图片信息的接口&#xff0c;可以看到请求参数包含cha…

iOS 组件化或SDK时对资源加载注意点

本文针对cocoapods进行打包的资源做个讲解&#xff0c;针对自身项目注意调整资源访问策略。 资源文件打包方式 使用pod lib create AppResourceModule 来进行演示。 use_frameworks! target AppResourceModule_Example dopod AppResourceModule, :path > ../ end podfil…

Python+Flask+MySQL开发的在线外卖订餐系统(附源码)

文章目录一、项目模块及功能介绍1、登录模块2、注册模块3、商家用户模块4、买家用户模块5、系统管理员模块源码二、项目结构三、环境依赖四、运行方法五、系统部分界面展示1、首页2、注册界面3、登录界面4、商家主界面5、商家菜单界面6、商家添加菜品界面7、商家修改菜品界面8、…

数值程序分析

原文来自微信公众号“编程语言Lab”&#xff1a;数值程序分析 搜索关注“编程语言Lab”公众号&#xff08;HW-PLLab&#xff09;获取编程语言更多技术内容&#xff01; 欢迎加入编程语言社区 SIG-程序分析&#xff0c;了解更多程序分析相关的技术内容。 加入方式&#xff1a;添…

实现自动化部署前端项目,从安装Jenkins到部署完成的整体配置 --适合初学Jenkins、想实现或者学习自动化部署的同学,知识点比较全面,过程写的清晰

前言 一、什么是Jenkins 二、Jenkins安装配置 Linux环境安装JDK Linux环境安装Maven Linux安装Jenkins 启动Jenkins jenkins配置 配置汉化版的jenkins 安装gitHub插件 gitHub配置 jenkins的配置 jenkins的gitHub配置 jenkins的java环境配置 小总结 两种情况 第…

RPCMon:一款基于ETW的RPC监控工具

关于RPCMon RPCMon是一款基于事件跟踪的WindowsRPC监控工具&#xff0c;该工具是一款GUI工具&#xff0c;可以帮助广大研究人员通过ETW&#xff08;Event Tracing for Windows&#xff09;扫描RPC通信。 RPCMon能够为广大研究人员提供进程之间RPC通信的高级视图&#xff0c;该…

谋变2023:家电巨头进击的“三大关口”

2022年的中国家电行业&#xff0c;无疑在艰难中前行。奥维云网&#xff08;AVC&#xff09;推总数据显示&#xff0c;2022年中国家电市场&#xff08;不含3C&#xff09;零售额为7081亿元&#xff0c;同比下滑7.4%。下滑背后的推力是多样的&#xff0c;包括地产市场下行、消费者…

服务端返回内容跨域CORS之后,也在chrome/edge浏览器里显示出响应信息

由于浏览器的同源策略&#xff0c;服务端返回的内容跨域&#xff0c;且没有允许跨域CORS的请求头之后&#xff0c;浏览器无法显示出服务端返回的信息&#xff0c;不方便问题排查。比如&#xff1a;Access to XMLHttpRequest at http://localhost:6001/service-app/query/common…

springcloud-工程创建(IDEA)

文章目录介绍springcloud 常用组件1.创建父工程2.删除父工程的src目录3.修改父工程的pom文件4 springcloud 版本依赖5.创建子模块6 子项目下创建启动类介绍 Spring Cloud 是一个基于 Spring Boot 实现的云应用开发工具&#xff0c;它为开发中的配置管理、服务发现、断路器、智…

最全面的内网open虚拟专用网络通道搭建过程

内网open虚拟专用网络通道搭建过程 文章目录内网open虚拟专用网络通道搭建过程前言一、环境准备二、安装open虚拟专用通道2.1、安装依赖包2.2、开始安装三、证书配置3.1、easy-rsa配置修改3.2、初始化与创建CA根证书3.3、生成服务端证书3.4、生成客户端证书3.5、创建服务端配置…

SpringCloud微服务~面试题

1. SpringCloud常见组件有哪些&#xff1f; 问题说明&#xff1a;这个题目主要考察对SpringCloud的组件基本了解 难易程度&#xff1a;简单 参考话术&#xff1a; SpringCloud包含的组件很多&#xff0c;有很多功能是重复的。其中最常用组件包括&#xff1a; 注册中心组件&…

【冲刺金三银四】2023年网络安全工程师面试题合集

以下为信息/网络安全各个方向涉及的面试题&#xff0c;星数越多代表问题出现的几率越大&#xff0c;祝各位都能找到满意的工作~ 【一一帮助网络安全提升点我一一】 ①网络安全学习路线 ②20份渗透测试电子书 ③安全攻防357页笔记 ④50份安全攻防面试指南 ⑤安全红队渗透工具包 …

市场调研计划书如何写?

想要做好一个产品&#xff0c;市场调研是必不可少的一步&#xff0c;也是第一步&#xff0c;那么如何进行市场调研呢&#xff1f;以下是我整理的一份市场调研计划书&#xff0c;希望能够帮助到大家&#xff01;&#xff01;&#xff01; 一、文档版本控制 主要记录文档的版本…

Reverse_SSH:一款基于SSH的反向Shell工具

关于Reverse_SSH Reverse_SSH上一款基于SSH的反向Shell工具&#xff0c;在该工具的帮助下&#xff0c;广大研究人员可以使用SSH来实现反向Shell&#xff0c;并同时拥有下列功能&#xff1a; 1、使用原生SSH语句管理和连接反向Shell&#xff1b; 2、动态、本地和远程转发&#…

[python入门㊷] - python存储数据

目录 ❤ json.dump()存储数据 ❤ json.laod()读取数据 ❤ 保存和读取用户生成的数据 ❤ 重构 JSON(JavaScript Object Notation)格式最初是为JavaScript开发的&#xff0c;但随后成了一种常见格式&#xff0c;被包括Python在内的众多语言采用 ❤ json.dump()存储数据…

基本TCP编程

1. 基本概念 TCP (即传输控制协议) 是一种面向连接的传输层协议,它能提供高可靠性通信 (即数据无误、数据无丢失、数据无失序、数据无重复到达的通信)。 2. 通信流程解析 TCP 通信的流程与打电话的过程相似,以下以一对情侣打电话的过程来展示TCP的通信流程: 其中服务端 …

4.4 序列化与反序列化

文章目录1.概述2.特点/应用场景3.涉及到的流对象4.代码实现序列化与反序列化4.1 步骤1&#xff1a;创建学生类Student24.2 步骤2&#xff1a;创建序列化测试类5.测试案例中常见的几种编译错误类型6.为什么反序列化版本号需要与序列化版本号一致&#xff1f;7.自动提示 生成UID …