Explorar o código

es自动配置

15902849627 %!s(int64=3) %!d(string=hai) anos
pai
achega
3f03f7e476

+ 10 - 0
pom.xml

@@ -125,6 +125,16 @@
             <artifactId>spring-boot-configuration-processor</artifactId>
             <artifactId>spring-boot-configuration-processor</artifactId>
             <optional>true</optional>
             <optional>true</optional>
         </dependency>
         </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter</artifactId>
+            <optional>true</optional>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-autoconfigure</artifactId>
+            <optional>true</optional>
+        </dependency>
         <dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-test</artifactId>
             <artifactId>spring-boot-starter-test</artifactId>

+ 16 - 1
sckw-common/sckw-common-elasticsearch/pom.xml

@@ -18,11 +18,26 @@
         <maven.compiler.target>17</maven.compiler.target>
         <maven.compiler.target>17</maven.compiler.target>
         <maven.compiler.compilerVersion>17</maven.compiler.compilerVersion>
         <maven.compiler.compilerVersion>17</maven.compiler.compilerVersion>
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+        <elasticsearch.version>7.9.3</elasticsearch.version>
     </properties>
     </properties>
 
 
 
 
     <dependencies>
     <dependencies>
-
+        <dependency>
+            <groupId>org.elasticsearch.client</groupId>
+            <artifactId>elasticsearch-rest-high-level-client</artifactId>
+            <version>${elasticsearch.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.elasticsearch.client</groupId>
+            <artifactId>elasticsearch-rest-client</artifactId>
+            <version>${elasticsearch.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.elasticsearch</groupId>
+            <artifactId>elasticsearch</artifactId>
+            <version>${elasticsearch.version}</version>
+        </dependency>
     </dependencies>
     </dependencies>
 
 
 </project>
 </project>

+ 197 - 0
sckw-common/sckw-common-elasticsearch/src/main/java/com/sckw/elasticsearch/service/es/EsUtils.java

@@ -0,0 +1,197 @@
+package com.sckw.elasticsearch.service.es;
+
+import lombok.extern.slf4j.Slf4j;
+import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
+import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
+import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
+import org.elasticsearch.action.bulk.BulkRequest;
+import org.elasticsearch.action.bulk.BulkResponse;
+import org.elasticsearch.action.delete.DeleteRequest;
+import org.elasticsearch.action.search.ClearScrollRequest;
+import org.elasticsearch.action.search.SearchRequest;
+import org.elasticsearch.action.search.SearchResponse;
+import org.elasticsearch.action.support.IndicesOptions;
+import org.elasticsearch.client.RequestOptions;
+import org.elasticsearch.client.RestHighLevelClient;
+import org.elasticsearch.client.core.CountRequest;
+import org.elasticsearch.client.core.CountResponse;
+import org.elasticsearch.common.settings.Settings;
+import org.elasticsearch.common.xcontent.XContentType;
+import org.elasticsearch.search.SearchHits;
+import org.elasticsearch.search.aggregations.Aggregations;
+import org.springframework.util.CollectionUtils;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * @author yuzhongchuan
+ * @description COMMENT='ES相关工具类'
+ * @create 2023 -06 -06 10:01
+ */
+@Slf4j
+public class EsUtils {
+
+    private RestHighLevelClient client;
+
+    private static EsUtils esUtils;
+
+    public EsUtils(RestHighLevelClient client) {
+        esUtils = this;
+        this.client = client;
+    }
+
+    /**
+     * 创建索引
+     *
+     * @param idxName
+     * @param idxSql
+     * @throws Exception
+     */
+    public static void createIndex(String idxName,String idxSql){
+        if (!indexExist(idxName)) {
+            log.error(" idxName={} 已经存在,idxSql={}",idxName,idxSql);
+            return;
+        }
+        log.info(" idxName={} 创建索引,idxSql={}",idxName,idxSql);
+        CreateIndexRequest request = new CreateIndexRequest(idxName);
+        buildSetting(request);
+        request.mapping("_doc",idxSql, XContentType.JSON);
+    }
+
+    /**
+     * 删除索引
+     * @param idxName 索引名称
+     */
+    public static void deleteIndex(String idxName){
+        if (!indexExist(idxName)) {
+            log.error(" idxName={} 索引不存在",idxName);
+            return;
+        }
+        try {
+            esUtils.client.indices().delete(new DeleteIndexRequest(idxName),RequestOptions.DEFAULT);
+        } catch (IOException e) {
+            log.error("ES deleteIndex异常", e);
+        }
+    }
+
+    /**
+     * 设置分片
+     * @param request
+     */
+    public static void buildSetting(CreateIndexRequest request){
+        request.settings(Settings.builder().put("index.number_of_shards",9)
+                .put("index.number_of_replicas",1));
+    }
+
+    /**
+     * 断某个index是否存在
+     * @param idxName
+     * @return
+     */
+    public static boolean indexExist(String idxName) {
+        GetIndexRequest request = new GetIndexRequest();
+        request.local(false);
+        request.humanReadable(true);
+        request.includeDefaults(false);
+        request.indices(idxName);
+        request.indicesOptions(IndicesOptions.lenientExpandOpen());
+        try {
+            return esUtils.client.indices().exists(request,RequestOptions.DEFAULT);
+        } catch (IOException e) {
+            log.error("ES indexExist查询SearchRequest异常", e);
+            throw new RuntimeException(e);
+        }
+    }
+
+
+    /**
+     * 布尔查询
+     *
+     * @param searchRequest
+     * @return
+     */
+    public static SearchHits boolSearch(SearchRequest searchRequest) {
+        log.info("ES开始boolSearch查询SearchRequest:{}", searchRequest);
+        try {
+            SearchResponse searchResponse = esUtils.client.search(searchRequest, RequestOptions.DEFAULT);
+            return searchResponse.getHits();
+        } catch (Exception e) {
+            log.error("ES boolSearch查询SearchRequest异常", e);
+            return null;
+        }
+    }
+
+    /**
+     * 分组聚合查询
+     *
+     * @param searchRequest
+     * @return
+     */
+    public static Aggregations groupBySearch(SearchRequest searchRequest) {
+        log.info("ES开始groupBySearch查询SearchRequest:{}", searchRequest);
+        try {
+            SearchResponse searchResponse = esUtils.client.search(searchRequest, RequestOptions.DEFAULT);
+            return searchResponse.getAggregations();
+        } catch (Exception e) {
+            log.error("ES groupBySearch查询SearchRequest异常", e);
+            return null;
+        }
+    }
+
+    /**
+     * 清除滚屏
+     *
+     * @param scrollId
+     */
+    public static void clearScroll(String scrollId) {
+        if (scrollId != null) {
+            ClearScrollRequest clearScrollRequest = new ClearScrollRequest();
+            clearScrollRequest.addScrollId(scrollId);
+            try {
+                esUtils.client.clearScroll(clearScrollRequest, RequestOptions.DEFAULT);
+                log.info("ES清除滚屏成功");
+            } catch (Exception e) {
+                log.error("ES清除滚屏出错:" + e.getMessage());
+            }
+        }
+    }
+
+    /**
+     * 简单聚合查询
+     *
+     * @param countRequest
+     * @return
+     */
+    public static long count(CountRequest countRequest ){
+        try {
+
+            log.info("开始ES查询indexName:{}的count", Arrays.toString(countRequest.indices()));
+            CountResponse count = esUtils.client.count(countRequest, RequestOptions.DEFAULT);
+            log.info("ES查询到:count={}", count.getCount());
+            return count.getCount();
+        } catch (Exception e) {
+            log.error("ES count异常", e);
+            return 0;
+        }
+    }
+
+
+    /**
+     * 批量新增
+     *
+     * @param bulkRequest
+     * @return
+     */
+    public static boolean bulkIndex(BulkRequest bulkRequest) {
+        try {
+            esUtils.client.bulk(bulkRequest, RequestOptions.DEFAULT);
+            return true;
+        } catch (Exception e) {
+            log.error("批量插入数据异常:", e);
+            return false;
+        }
+    }
+
+}

+ 109 - 0
sckw-common/sckw-common-elasticsearch/src/main/java/com/sckw/elasticsearch/service/es/autoconfigure/EsAutoConfiguration.java

@@ -0,0 +1,109 @@
+package com.sckw.elasticsearch.service.es.autoconfigure;
+
+
+import com.sckw.elasticsearch.service.es.EsUtils;
+import com.sckw.elasticsearch.service.es.constants.EsConstant;
+import com.sckw.elasticsearch.service.es.properties.EsProperties;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.http.Header;
+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.impl.client.BasicCredentialsProvider;
+import org.apache.http.impl.nio.reactor.IOReactorConfig;
+import org.apache.http.message.BasicHeader;
+import org.elasticsearch.client.*;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
+import org.springframework.context.annotation.Bean;
+
+
+import java.util.concurrent.TimeUnit;
+
+/**
+ * @Author yzc
+ * @Description es自动装配
+ * @createTime 2022年03月03日 09:36:00
+ */
+@EnableConfigurationProperties(EsProperties.class)
+@Slf4j
+public class EsAutoConfiguration {
+
+    @Autowired
+    private EsProperties esProperties;
+
+
+    @Bean
+    @ConditionalOnClass(RestHighLevelClient.class)
+    @ConditionalOnMissingBean(EsUtils.class)
+    public EsUtils esUtils(RestHighLevelClient restHighLevelClient) {
+        log.info("---------------EsUtils 正在初始化");
+        return new EsUtils(restHighLevelClient);
+    }
+
+    @Bean
+    @ConditionalOnMissingBean(RestHighLevelClient.class)
+    public RestHighLevelClient restHighLevelClient() {
+        log.info("---------------es配置正在加载,当前配置为:{}", esProperties);
+        if (EsConstant.DEFAULT_HOST.equals(esProperties.getHostList())) {
+            throw new RuntimeException("es配置错误,无法加载");
+        }
+        //解析hostList配置信息
+        String[] split = esProperties.getHostList().split(",");
+        //创建HttpHost数组,其中存放es主机和端口的配置信息
+        HttpHost[] httpHostArray = new HttpHost[split.length];
+        for (int i = 0; i < split.length; i++) {
+            String item = split[i];
+            httpHostArray[i] = new HttpHost(item.split(":")[0], Integer.parseInt(item.split(":")[1]), esProperties.getScheme());
+        }
+        RestClientBuilder clientBuilder = RestClient.builder(httpHostArray);
+        // 1.设置请求头
+        Header[] defaultHeaders = {new BasicHeader("header", "value")};
+        clientBuilder.setDefaultHeaders(defaultHeaders);
+        // 2.设置失败监听器,每次节点失败都可以监听到,可以作额外处理
+        clientBuilder.setFailureListener(new RestClient.FailureListener() {
+            @Override
+            public void onFailure(Node node) {
+                super.onFailure(node);
+                log.error(node.getName() + "==节点失败了");
+            }
+        });
+        /** 3.配置节点选择器,客户端以循环方式将每个请求发送到每一个配置的节点上,
+         *发送请求的节点,用于过滤客户端,将请求发送到这些客户端节点,默认向每个配置节点发送,
+         *这个配置通常是用户在启用嗅探时向专用主节点发送请求(即只有专用的主节点应该被HTTP请求命中)
+         */
+        clientBuilder.setNodeSelector(NodeSelector.SKIP_DEDICATED_MASTERS);
+        /*
+         *4. 配置异步请求的线程数量,Apache Http Async Client默认启动一个调度程序线程,以及由连接管理器使用的许多工作线程
+         *(与本地检测到的处理器数量一样多,取决于Runtime.getRuntime().availableProcessors()返回的数量)。线程数可以修改如下,
+         *这里是修改为10个线程,默认1
+         */
+        clientBuilder.setHttpClientConfigCallback(httpAsyncClientBuilder -> httpAsyncClientBuilder.setDefaultIOReactorConfig(
+                IOReactorConfig.custom().setIoThreadCount(10).build()
+        ));
+        /**
+         *5. 配置连接超时和套接字超时
+         *配置请求超时,将连接超时(默认为1秒)和套接字超时(默认为30秒)增加,
+         *这里配置完应该相应地调整最大重试超时(默认为30秒),即上面的setMaxRetryTimeoutMillis,一般于最大的那个值一致即60000
+         */
+        clientBuilder.setRequestConfigCallback(requestConfigBuilder -> {
+            // 连接5秒超时,套接字连接60s超时
+            return requestConfigBuilder
+                    .setConnectTimeout(esProperties.getConnectTimeout())
+                    .setSocketTimeout(60000);
+        });
+
+        //6.设置ES需要基本身份验证的默认凭据提供程序及设置KeepAliveStrategy为3分钟
+        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
+        credentialsProvider.setCredentials(AuthScope.ANY,
+                new UsernamePasswordCredentials(esProperties.getUsername(), esProperties.getPassword()));
+        clientBuilder.setHttpClientConfigCallback(callback -> callback.setDefaultCredentialsProvider(credentialsProvider)
+                .setKeepAliveStrategy((response, context) -> TimeUnit.MINUTES.toMillis(3)));
+
+        //创建RestHighLevelClient客户端
+        return new RestHighLevelClient(clientBuilder);
+    }
+}

+ 11 - 0
sckw-common/sckw-common-elasticsearch/src/main/java/com/sckw/elasticsearch/service/es/constants/EsConstant.java

@@ -0,0 +1,11 @@
+package com.sckw.elasticsearch.service.es.constants;
+
+/**
+ * @Author yzc
+ * @Description es常量
+ * @createTime 2023 -06 -06 10:01
+ */
+public class EsConstant {
+
+    public static final String DEFAULT_HOST = "default:9200";
+}

+ 39 - 0
sckw-common/sckw-common-elasticsearch/src/main/java/com/sckw/elasticsearch/service/es/properties/EsProperties.java

@@ -0,0 +1,39 @@
+package com.sckw.elasticsearch.service.es.properties;
+
+import com.sckw.elasticsearch.service.es.constants.EsConstant;
+import lombok.Getter;
+import lombok.Setter;
+import lombok.ToString;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+
+/**
+ * @Author yzc
+ * @Description es配置
+ * @createTime 2023 -06 -06 10:01
+ */
+@Getter
+@Setter
+@ToString
+@ConfigurationProperties(prefix = "elasticsearch")
+public class EsProperties {
+    /**
+     * host地址
+     */
+    private String hostList = EsConstant.DEFAULT_HOST;
+    /**
+     * scheme
+     */
+    private String scheme = "https";
+    /**
+     * 用户名
+     */
+    private String username = "";
+    /**
+     * 密码
+     */
+    private String password = "";
+    /**
+     * 超时时间
+     */
+    private Integer connectTimeout = 10000;
+}

+ 2 - 0
sckw-common/sckw-common-elasticsearch/src/main/resources/META-INF/spring.factories

@@ -0,0 +1,2 @@
+org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
+com.sckw.elasticsearch.service.es.autoconfigure.EsAutoConfiguration

+ 1 - 0
sckw-common/sckw-common-elasticsearch/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports

@@ -0,0 +1 @@
+com.sckw.elasticsearch.service.es.autoconfigure.EsAutoConfiguration

+ 7 - 0
sckw-modules/sckw-example/pom.xml

@@ -40,6 +40,13 @@
             <artifactId>sckw-common-datasource</artifactId>
             <artifactId>sckw-common-datasource</artifactId>
             <version>1.0.0</version>
             <version>1.0.0</version>
         </dependency>
         </dependency>
+
+        <dependency>
+            <groupId>com.sckw</groupId>
+            <artifactId>sckw-common-elasticsearch</artifactId>
+            <version>1.0.0</version>
+        </dependency>
+
         <dependency>
         <dependency>
             <groupId>com.sckw</groupId>
             <groupId>com.sckw</groupId>
             <artifactId>sckw-common-excel</artifactId>
             <artifactId>sckw-common-excel</artifactId>

+ 34 - 0
sckw-modules/sckw-example/src/main/java/com/sckw/example/controller/EsController.java

@@ -0,0 +1,34 @@
+package com.sckw.example.controller;
+
+import com.sckw.elasticsearch.service.es.EsUtils;
+import lombok.RequiredArgsConstructor;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+
+/**
+ * @author: yzc
+ * @date: 2023-06-06 14:40
+ * @description:easy-es测试
+ */
+@RestController
+@RequestMapping("/testEs")
+@RequiredArgsConstructor
+public class EsController {
+
+
+    @GetMapping(value = "/createIndex")
+    public Boolean createIndex() {
+        EsUtils.createIndex("title","");
+        return true;
+    }
+
+
+    @GetMapping(value = "/deleteIndex")
+    public Boolean deleteIndex(@RequestParam(value = "indexName") String indexName) {
+        EsUtils.deleteIndex(indexName);
+        return true;
+    }
+
+}