springboot整合ES(Elasticsearch)整合Knife4j

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
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值