1. @Slf4j
    2. @Component
    3. public class EsClientUtil {
    4. private static EsClientUtil instance;
    5. private static String esAddress;
    6. public static String esAddressNoStatic;
    7. private static RestHighLevelClient client = null;
    8. private static RestClientBuilder restClientBuilder;
    9. private EsClientUtil() {
    10. esAddress = esAddressNoStatic;
    11. }
    12. public synchronized static EsClientUtil getInstance() {
    13. if (instance == null) {
    14. instance = new EsClientUtil();
    15. }
    16. return instance;
    17. }
    18. /**
    19. * 获取es客户端连接
    20. */
    21. public synchronized static RestHighLevelClient getClient() throws BusinessException {
    22. if(client == null){
    23. if (StringUtils.isEmpty(esAddress)) {
    24. throw new BusinessException(ModelCatalogCenterErrorCode.ERROR_PARAM, "es ip is null");
    25. }
    26. ArrayList<HttpHost> hostList = new ArrayList<>();
    27. Arrays.stream(esAddress.split(",")).forEach(host -> getHostList(host, hostList));
    28. restClientBuilder = RestClient.builder(hostList.toArray(new HttpHost[0]));
    29. restClientBuilder.setHttpClientConfigCallback(httpClientBuilder -> {
    30. httpClientBuilder.setKeepAliveStrategy((response, context) -> Duration.ofMinutes(5).toMillis());
    31. httpClientBuilder.disableAuthCaching();
    32. return httpClientBuilder;
    33. });
    34. client = new RestHighLevelClient(restClientBuilder);
    35. }
    36. return client;
    37. }
    38. public void createIndex(String index, String type, String mapping,Map settings) {
    39. RestHighLevelClient client = getClient();
    40. CreateIndexRequest request = new CreateIndexRequest(index);
    41. if(null != settings){
    42. request.settings(settings);
    43. }
    44. request.mapping(type,mapping, XContentType.JSON);
    45. try {
    46. client.indices().create(request, RequestOptions.DEFAULT);
    47. } catch (IOException e) {
    48. //log.error(DgdeviceLoggerHelper.message(ErrorCode.ES_OPERATION_FAILD, " es createIndex method error..."), e);
    49. }
    50. }
    51. /**
    52. * @param host
    53. * @param hostList
    54. * @return
    55. */
    56. private static void getHostList(String host, ArrayList<HttpHost> hostList) {
    57. String[] address = host.split(":");
    58. if (address.length == 2) {
    59. hostList.add(new HttpHost(address[0], Integer.parseInt(address[1]), "http"));
    60. }
    61. }
    62. public void createIndexByDynamicTemplate(String indexName, String dynamicTemplates) {
    63. if(StringUtils.isBlank(esAddress) || StringUtils.isBlank(dynamicTemplates)){
    64. throw new BusinessException("esAddress or dynamicTemplates is null ,createDeviceIndex failure");
    65. }
    66. String url = "http://"+esAddress+"/"+indexName;
    67. try {
    68. HttpClientUtil.doPutJson(url,dynamicTemplates);
    69. } catch (Exception e) {
    70. //log.error(DgdeviceLoggerHelper.message(ErrorCode.ES_OPERATION_FAILD, " es createDeviceIndex method error..."), e);
    71. }
    72. }
    73. /**
    74. * @param index 索引名称 判断es中是否存在该索引
    75. */
    76. public boolean exsitIndex(String index) {
    77. RestHighLevelClient client = getClient();
    78. boolean result = false;
    79. GetIndexRequest request = new GetIndexRequest();
    80. request.indices(index);
    81. request.local(false);
    82. request.humanReadable(true);
    83. request.includeDefaults(false);
    84. try {
    85. result = client.indices().exists(request, RequestOptions.DEFAULT);
    86. } catch (IOException e) {
    87. //log.error(DgdeviceLoggerHelper.message(ErrorCode.ES_OPERATION_FAILD, " es exsitIndex method error..."), e);
    88. }
    89. return result;
    90. }
    91. }