官网
https://www.elastic.co/cn/
版本
https://docs.spring.io/spring-data/elasticsearch/docs/4.4.10/reference/html/
我们选用的是elasticsearch 7.17.9版本,对应的,我们需要升级springboot版本,对应的中间件都需要升级
组件版本说明
Springboot: 2.7.10
spring-data-elasticsearch: 4.4.10
spring-boot-starter-data-elasticsearch: 2.7.10
实现代码地址
https://github.com/OrderDong/microservice-boot
分支:microservice-boot-1.0.6-es
microservice-boot-eleastic模块
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<!--elastic search 7.17.9 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<!-- spring-data-elasticsearch 包含此包
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.17.9</version>
</dependency>-->
<!-- Reactive Infrastructure -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webflux</artifactId>
</dependency>
<dependency>
<groupId>io.projectreactor.netty</groupId>
<artifactId>reactor-netty-http</artifactId>
</dependency>
application.yml
spring:
# Jackson 配置项
jackson:
# serialization:
# write-dates-as-timestamps: true # 设置 Date 的格式,使用时间戳
# write-date-timestamps-as-nanoseconds: false # 设置不使用 nanoseconds 的格式。例如说 1611460870.401,而是直接 1611460870401
# write-durations-as-timestamps: true # 设置 Duration 的格式,使用时间戳
# fail-on-empty-beans: false # 允许序列化无属性的 Bean
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
application:
name: microservice-boot-es
elasticsearch:
uris: 10.255.20.231:9200
server:
port: 8023
tomcat:
uri-encoding: UTF-8
max-connections: 40000
Repository
package org.lwd.microservice.boot.es.dao;
import org.lwd.microservice.boot.es.entity.VisitLog;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* @author weidong
* @version V1.0.0
* @since 2023/7/26
*/
@Repository
public interface VisitLogRepository extends ElasticsearchRepository<VisitLog, String> {
//自定义规则
List<VisitLog> findByUserLoginId(Integer userLoginId);
}
VisitLog模型定义
package org.lwd.microservice.boot.es.entity;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import java.io.Serializable;
import java.util.Date;
/**
* <p>
* 系统访问日志
* </p>
*
* @author lwd
* @since 2023-07-26
*/
@Getter
@Setter
@Document(indexName = "visit_log")
public class VisitLog implements Serializable {
private static final long serialVersionUID = 1L;
/**
*访问id
*/
@Id
private Integer id;
/**
* 不同业务表明
*/
private String tableName;
/**
*登录账号
*/
private String userLoginId;
/**
*服务ip
*/
private String serverIpAddress;
/**
*服务名
*/
private String serverHostName;
/**
*请求url
*/
private String initialRequest;
/**
*消息内容
*/
private String msgContent;
/**
*创建时间
*/
private Date createTime;
}
controller使用
中间服务层定义掠过了。。。。
package org.lwd.microservice.boot.es.controller;
import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.client.RestHighLevelClient;
import org.lwd.microservice.boot.es.entity.VisitLog;
import org.lwd.microservice.boot.es.service.VisitLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.*;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.data.elasticsearch.core.query.StringQuery;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.util.List;
/**
* @author weidong
* @version V1.0.0
* @since 2023/7/26
*/
@Slf4j
@RestController
@RequestMapping("/es")
public class VisitLogController {
@Autowired
VisitLogService visitLogService;
//rest模版操作
@Autowired
ElasticsearchOperations elasticsearchOperations;
//响应式模版操作
@Autowired
ReactiveElasticsearchOperations reactiveElasticsearchOperations;
@PostMapping("/saveVisitLog")
@ResponseBody
public String saveVisitLog(@RequestBody VisitLog visitLog){
VisitLog saveVisitLog = visitLogService.saveVisitLog(visitLog);
log.info("----saveVisitLog----:{}", JSON.toJSONString(saveVisitLog));
return JSON.toJSONString(saveVisitLog);
}
@GetMapping("/getVisitLogAll")
@ResponseBody
public String getVisitLogAll(){
List<VisitLog> logList = visitLogService.findAll();
log.info("----getVisitLogAll----:{}", JSON.toJSONString(logList));
return JSON.toJSONString(logList);
}
//自定义查询
@GetMapping("/getVisitLogByUserLoginId")
@ResponseBody
public String getVisitLogByUserLoginId(Integer userLoginId){
List<VisitLog> logList = visitLogService.findByUserLoginId(userLoginId);
log.info("----getVisitLogAll----:{}", JSON.toJSONString(logList));
return JSON.toJSONString(logList);
}
}
测试http请求
TestEs.http
## 新增日志
POST http://localhost:8023/es/saveVisitLog
Content-Type: application/json
{
"id": 3,
"tableName": "VisitLog",
"userLoginId": 3,
"serverIpAddress": "127.0.0.1",
"serverHostName": "weidong",
"initialRequest": "http://localhost:8023",
"msgContent": "test es add3",
"createTime": "2023-07-27 16:34:36"
}
### 获取所有数据-不分页
GET http://localhost:8023/es/getVisitLogAll
### 获取所有数据-自定义查询规则
GET http://localhost:8023/es/getVisitLogByUserLoginId?userLoginId=1
结果
kibana结果
@ID
如果不赋值给ID,那么es会自动生成一个字符串,但是,获取数据可能出问题文章来源:https://www.toymoban.com/news/detail-613914.html
文章来源地址https://www.toymoban.com/news/detail-613914.html
外传
😜 原创不易,如若本文能够帮助到您的同学
🎉 支持我:关注我+点赞👍+收藏⭐️
📝 留言:探讨问题,看到立马回复
💬 格言:己所不欲勿施于人 扬帆起航、游历人生、永不言弃!🔥
到了这里,关于Elasticsearch 整合springboot-Elasticsearch文章二的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!