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模块
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-validationartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-elasticsearchartifactId>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webfluxartifactId>
dependency>
<dependency>
<groupId>io.projectreactor.nettygroupId>
<artifactId>reactor-netty-httpartifactId>
dependency>
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
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);
}
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;
/**
*
* 系统访问日志
*
*
* @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;
}
中间服务层定义掠过了。。。。
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);
}
}
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
如果不赋值给ID,那么es会自动生成一个字符串,但是,获取数据可能出问题
原创不易,如若本文能够帮助到您的同学
支持我:关注我+点赞+收藏⭐️
留言:探讨问题,看到立马回复
格言:己所不欲勿施于人 扬帆起航、游历人生、永不言弃!