Idea+maven+spring-cloud项目搭建系列--7整合Elasticsearch

前言:
本文是建立在已整合nacos 的基础上进行的扩展,如需要整合Nacos 可以参考:
https://blog.csdn.net/l123lgx/article/details/121624988

本文是建立在服务器已经部署Es和Kibana的基础只上进行的整合,如需要部署Es和Kibana可以参考:https://blog.csdn.net/l123lgx/article/details/121744965

1 Es 介绍:
Elasticsearch 是一个强大的开源搜索和分析引擎,可以让数据易于探索;

2 Spring-cloud–整合ES:

2.1 新建es spring-boot 项目作为ES服务

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2.1 pom 文件修改:
(1) es服务pom 修改

<?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>
    <!-- 1 引入父类pom -->
    <parent>
        <artifactId>bluegrass</artifactId>
        <groupId>org.lgx.bluegrass</groupId>
        <version>1.0-RELEASE</version>
    </parent>

    <groupId>org.lgx.bluegrass</groupId>
    <artifactId>bluegrass-es</artifactId>
    <!-- 修改版本-->
    <version>1.0-RELEASE</version>
    <name>bluegrass-es</name>
    <description>Demo project for Spring Boot</description>
    <!-- 打包方式-->
    <packaging>jar</packaging>

    <!-- 2 去除子类的变量引用统一使用 父类 -->
    <!--
    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
    </properties>
-->
    <dependencies>
        <!--增加api 依赖 -->
        <dependency>
            <groupId>org.lgx.bluegrass</groupId>
            <artifactId>bluegrass-api</artifactId>
            <version>1.0-RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </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>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- 增加nacos 配置 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!-- 增加es 配置 -->
       <!-- <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-elasticsearch</artifactId>
            <version>4.1.0</version>
        </dependency>-->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>7.8.1</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>7.8.1</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-client</artifactId>
            <version>7.8.1</version>
        </dependency>
    </dependencies>

    <!-- 3 移除依赖管理 -->
    <!--
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.7.RELEASE</version>
                <configuration>
                    <mainClass>org.lgx.bluegrass.bluegrasses.BluegrassEsApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

(2)父pom 增加es 子模块
在这里插入图片描述

3 配置文件参数处理:
3.1 注释原有的application.properties 的内容:
在这里插入图片描述

3.2 resources 新加bootstrap.yml文件
在这里插入图片描述

3.3 bootstrap.ym增加服务名称端口,及nacos 服务地址:

spring:
  application:
    # 应用名称
    name: bluegrass-es
  profiles:
    # 生效环境
    active: dev
  cloud:
    nacos:
      config:
        # 配置中心地址
        server-addr: nocos服务ip:8848
        # 配置中心文件后缀
        file-extension: yaml
        # 配置中心的命名空间id
        namespace: 03e54842-c34e-4a6c-a28c-0a55a2e0fb6c
        # 配置中心的配置文件分组
        group: DEFAULT_GROUP
      discovery:
        # 服务发现地址
        server-addr: nocos服务ip:8848
        # 注册的地址
        ip: 172.28.20.154
        # 需要注册到的命名空间
        namespace: 81321cc2-df37-4e6a-b4d8-7e7160248f9e
        # 注册服务的默认分组
        group: DEFAULT_GROUP

3.4 nacos 服务增加bluegrass-es 配置文件,配置端口及es 配置参数:
在这里插入图片描述

server:
  port: 9083
version:
  number: 123

es: 
  host-port: es服务Ip:9200
  hosts: es服务Ip
  port: 9200  
  connect-timeout: 10000
  socket-timeout: 100000
  username: elastic
  password: elastic123456
  agg:
    shardSize: 200000
management:
  health:
    elasticsearch:
    # 如禁止es的健康检查如下,默认均为开启状态
      enabled: false
  endpoints:
    web:
      exposure:
        include: '*'



3.5 启动类开启nacos:
在这里插入图片描述
启动类java:

package org.lgx.bluegrass.bluegrasses;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
@EnableDiscoveryClient
@RefreshScope
public class BluegrassEsApplication {
    @Value("${version.number:无版本号替换}")
    private String versionNumber;

    public static void main(String[] args) {
        SpringApplication.run(BluegrassEsApplication.class, args);
    }

    @RequestMapping(value = "/version-number", method = RequestMethod.GET)
    public String index() {
        return versionNumber;
    }

}

3.6 新建config 包并创建ElasticSearchConfig.java 配置类文件

 package org.lgx.bluegrass.bluegrasses.config;


import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class ElasticSearchConfig {
    @Value("${es.connect-timeout:-1}")
    private int connectTimeout;

    @Value("${es.socket-timeout:-1}")
    private int socketTimeout;

    @Value("${es.connection-request-timeout:-1}")
    private int connectionRequestTimeout;

    @Value("${es.password:aaa}")
    private String password;

    @Value("${es.username:aaa}")
    private String userName;

    @Value("${es.hosts:127.0.0.1}")
    private String hosts;

    @Value("${es.port:9200}")
    private int port;

    @Value("${es.max-retry-timeout-millis:300000}")
    private int maxRetryTimeoutMillis;


    public RestHighLevelClient elasticsearchClient() {
        String[] hosts = this.hosts.split(",");
        HttpHost[] httpHosts = new HttpHost[hosts.length];
        for(int i=0;i<hosts.length;i++) {
            httpHosts[i] = new HttpHost(hosts[i], port, "http");
        }

        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password));

        RestClientBuilder builder = RestClient.builder(httpHosts).setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {
            @Override
            public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) {
                requestConfigBuilder.setConnectTimeout(connectTimeout);
                requestConfigBuilder.setSocketTimeout(socketTimeout);
                requestConfigBuilder.setConnectionRequestTimeout(connectionRequestTimeout);
                return requestConfigBuilder;
            }
        }).setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
            @Override
            public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                httpClientBuilder.disableAuthCaching();
                return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
            }
        });

        RestHighLevelClient client = new RestHighLevelClient(builder);
        return client;
    }

    @Bean(name = {"restHighLevelClient"})
    public RestHighLevelClient client() {
        return this.elasticsearchClient();
    }


}

3.7 测试:新建controller 文件夹,新增 TestEsController

 package org.lgx.bluegrass.bluegrasses.controller;

import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentType;
import org.lgx.bluegrass.api.util.ExceptionFormatUtil;
import org.lgx.bluegrass.bluegrasses.constant.IndexMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description TODO
 * @Date 2021/12/6 18:29
 * @Author lgx
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("test")
public class TestEsController {
    @Autowired
    @Qualifier("restHighLevelClient")
    private RestHighLevelClient client;

    /**
     * test
     *
     * @param request
     * @return
     */
    @GetMapping(value = "/createIndex")
    public String getFinanceIns(HttpServletRequest request) {
        // 创建索引
        try {
            CreateIndexRequest createIndexRequest = new CreateIndexRequest("record");
            createIndexRequest.settings(Settings.builder()
                    .put("index.number_of_shards", 1)
                    .put("index.number_of_replicas", 1)
            );
            createIndexRequest.mapping(IndexMapping.RECORDS, XContentType.JSON);
            CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
            return createIndexResponse.index();
        }catch (Exception e){
            e.printStackTrace();
            log.error(ExceptionFormatUtil.buildErrorMessage(e));
            return "create index record error";
        }
    }

}

IndexMapping.java

package org.lgx.bluegrass.bluegrasses.constant;

/**
 * @Description TODO
 * @Date 2021/12/6 18:33
 * @Author lgx
 * @Version 1.0
 */
public class IndexMapping {
    public static final  String RECORDS = "{\n" +
            "\"properties\": {\n" +
            "      \"userId\": {\n" +
            "        \"type\": \"integer\"\n" +
            "      },\n" +
            "      \"age\": {\n" +
            "        \"type\": \"integer\"\n" +
            "      },\n" +
            "      \"createTime\": {\n" +
            "        \"type\": \"date\",\n" +
            "        \"format\": \"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis\"\n" +
            "      }\n" +
            "    }\n" +
            "  }";
}

3.8 发起创建所以请求:http://172.28.20.154:9083/test/createIndex
在这里插入图片描述

 登录kibana 查看索引:

在这里插入图片描述

参考:
maven:
https://search.maven.org/artifact/org.elasticsearch.client/elasticsearch-rest-high-level-client/7.8.1/jar

Java es api:
https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.8/java-rest-high-document-index.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值