springboot整合ES(Elasticsearch)整合Knife4j
资源下载路径:资源下载路径
该资源包括:elasticsearch的一个简单demo。有elasticsearch的window6.8.1版本。还有elasticsearch-head-master.还有elasticsearch-analysis-ik-5.6.8。还有elasticsearch-analysis-pinyin-6.8.1
ES是比较主流的技术,最近刚学习了下,写个小demo记录下。
搭建es
es下载
ES官网下载地址
https://www.elastic.co/cn/downloads/past-releases#elasticsearch
注意
配合head使用需要处理下跨域问题
在配置文件最后加上:
http.cors.enabled: true
http.cors.allow-origin: “*”
启动ES
进入到 D:\elasticsearch6.8\elasticsearch-6.8.1\bin 目录,执行:
elasticsearch
启动es成功!
elasticsearch-head-master下载
head-master下载git路径
https://github.com/mobz/elasticsearch-head.git
启动master
进入到 D:\elasticsearch6.8\elasticsearch-head-master 目录,输入:
grunt server
启动成功!
浏览器访问: http://localhost:9100/
至此ES搭建成功!
以下是java代码部分
目录结构
pom文件
<!--elasticsearch-->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>3.2.1.RELEASE</version>
</dependency>
<!--knife4j-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<!--在引用时请在maven中央仓库搜索最新版本号-->
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
properties配置文件
spring.application.name=es
server.port=2080
# ES配置
elasticsearch.host=127.0.0.1
elasticsearch.port=9300
elasticsearch.clustername=elasticsearch
elasticsearch.search.pool.size=5
ESConfig
package com.zjy.es.config;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
import java.net.InetAddress;
@Configuration
@EnableElasticsearchRepositories(basePackages = "com.zjy.es.dao")
public class ElasticsearchConfig {
/**日志对象*/
private static final Logger logger = LoggerFactory.getLogger(ElasticsearchConfig.class);
@Value("${elasticsearch.host}")
private String esHost;
@Value("${elasticsearch.port}")
private int esPort;
@Value("${elasticsearch.clustername}")
private String esClusterName;
@Value("${elasticsearch.search.pool.size}")
private Integer threadPoolSearchSize;
@Bean
public Client client() throws Exception {
Settings esSettings = Settings.builder()
.put("cluster.name", esClusterName)
//增加嗅探机制,找到ES集群,非集群置为false
.put("client.transport.sniff", true)
//增加线程池个数
.put("thread_pool.search.size", threadPoolSearchSize)
.build();
return new PreBuiltTransportClient(esSettings)
.addTransportAddress(new TransportAddress(InetAddress.getByName(esHost), esPort));
}
@Bean(name="elasticsearchTemplate")
public ElasticsearchOperations elasticsearchTemplateCustom() throws Exception {
ElasticsearchTemplate elasticsearchTemplate;
try {
elasticsearchTemplate = new ElasticsearchTemplate(client());
return elasticsearchTemplate;
} catch (Exception e) {
logger.error("初始化 ElasticsearchTemplate 失败");
return new ElasticsearchTemplate(client());
}
}
}
swaggerConfig
package com.zjy