wangzhinan 5 lat temu
rodzic
commit
58e7169169

+ 1 - 16
business/es-service/pom.xml

@ -70,23 +70,8 @@
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>5.6.1</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>transport</artifactId>
            <version>5.6.1</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>x-pack-transport</artifactId>
            <version>5.6.1</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>6.2.3</version>
        </dependency>
        <!--   poi xml导入导出工具 end -->
    </dependencies>

+ 11 - 61
business/es-service/src/main/java/com/yihu/jw/es/es/ElasticFactory.java

@ -1,37 +1,26 @@
package com.yihu.jw.es.es;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.config.HttpClientConfig;
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.client.HttpAsyncClientBuilder;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.xpack.client.PreBuiltXPackTransportClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.concurrent.TimeUnit;
/**
 * Created by chenweida on 2017/6/5.
 */
@Component
public class ElasticFactory {
    private static JestClientFactory factory = null;
@ -42,8 +31,6 @@ public class ElasticFactory {
    private String tHost;// 59.61.92.90:9066,59.61.92.90:9068
    @Value("${es.clusterName}")
    private String clusterName;
    @Value("${es.securityUser}")
    private String securityUser;
//-----------------------------------jestClient----------------------------------------
    /**
@ -67,13 +54,13 @@ public class ElasticFactory {
        // Construct a new Jest client according to configuration via factory
        factory = new JestClientFactory();
        HttpClientConfig httpClientConfig = new HttpClientConfig
                .Builder(Arrays.asList(hostArray))//http://59.61.92.90:9065,http://59.61.92.90:9067
                .Builder(Arrays.asList(hostArray))
                .multiThreaded(true)
                .maxTotalConnection(50)// 最大链接
                .maxConnectionIdleTime(120, TimeUnit.SECONDS)//链接等待时间
                .connTimeout(60 * 1000)
                .maxConnectionIdleTime(10, TimeUnit.MINUTES)//链接等待时间
                .connTimeout(60 * 1000*10)
                // .discoveryEnabled(true)
                .readTimeout(60 * 1000)//60秒
                .readTimeout(60 * 1000*10)//60秒
                .build();
@ -101,16 +88,12 @@ public class ElasticFactory {
    private synchronized void initTranClient() throws UnknownHostException {
        if (transportClient == null) {
            String[] hosts = tHost.split(",");
            Settings settings = Settings.builder()
                    // es 集群的名称
                    .put("cluster.name",  StringUtils.isEmpty(clusterName) ? "jkzl" : clusterName)
                    .put("client.transport.sniff", "true")
                    //账号密码
                    .put("xpack.security.user", securityUser)
            Settings settings = Settings.settingsBuilder()
                    // .put("client.transport.sniff", true)//开启嗅探功能
                    .put("cluster.name", StringUtils.isEmpty(clusterName) ? "jkzl" : clusterName)//默认集群名字是jkzl
                    .build();
            transportClient = new PreBuiltXPackTransportClient(settings);
            transportClient = TransportClient.builder().settings(settings).build();
            for (String oneHost : hosts) {
                String[] hostAndport = oneHost.split(":");
@ -118,38 +101,5 @@ public class ElasticFactory {
            }
        }
    }
    //-----------------------------------restHighLevelClient----------------------------------------
    private RestHighLevelClient restHighLevelClient = null;
    public RestHighLevelClient getHighLevelClient() {
        try {
            initHighLevelClient();
            return restHighLevelClient;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    private synchronized void initHighLevelClient() throws UnknownHostException {
        //初始化ES操作客户端172.26.0.112:9200
        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        String user[] = securityUser.split(":");
        credentialsProvider.setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials(user[0], user[1]));  //es账号密码
        restHighLevelClient = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost(esHost, 9200)
                ).setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                    public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                        httpClientBuilder.disableAuthCaching();
                        return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
                    }
                })/*.setMaxRetryTimeoutMillis(2000)*/
        );
    }
}

+ 9 - 3
business/es-service/src/main/java/com/yihu/jw/es/es/Test.java

@ -1,3 +1,4 @@
/*
package com.yihu.jw.es.es;
import org.apache.http.HttpHost;
@ -28,9 +29,11 @@ import java.util.Map;
import static org.elasticsearch.index.query.QueryBuilders.matchQuery;
*/
/**
 * Created by Trick on 2019/12/18.
 */
 *//*
public class Test {
    private static RestHighLevelClient client = null;
@ -47,7 +50,9 @@ public class Test {
                        httpClientBuilder.disableAuthCaching();
                        return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
                    }
                })/*.setMaxRetryTimeoutMillis(2000)*/
                })*/
/*.setMaxRetryTimeoutMillis(2000)*//*
        );
    }
@ -63,7 +68,7 @@ public class Test {
//同步
//        BulkResponse bulkResponse = esClient.bulk(request);
        search();
        index();
    }
    static void index() throws Exception {
@ -100,3 +105,4 @@ public class Test {
    }
}
*/

+ 1 - 1
svr/svr-internet-hospital/src/main/resources/application.yml

@ -203,7 +203,7 @@ es:
  host:  http://172.26.0.112:9200
  tHost: 172.26.0.112:9300
  clusterName: jkzl
  securityUser: elastic:elastic
  securityUser: elastic:changeme
# 上传文件临时路径配置
FileTempPath: