diff --git a/hippo4j-example/hippo4j-spring-boot-starter-es-monitor-example/src/main/resources/application.properties b/hippo4j-example/hippo4j-spring-boot-starter-es-monitor-example/src/main/resources/application.properties index 33e86166..76febf01 100644 --- a/hippo4j-example/hippo4j-spring-boot-starter-es-monitor-example/src/main/resources/application.properties +++ b/hippo4j-example/hippo4j-spring-boot-starter-es-monitor-example/src/main/resources/application.properties @@ -5,10 +5,10 @@ server.servlet.context-path=/example spring.profiles.active=dev spring.application.name=dynamic-threadpool-example -es.host = xxx -es.scheme = xxx -es.userName = xxx -es.password = xxx +es.thread-pool-state.host = ip1:port,ip2:port +es.thread-pool-state.scheme = http +es.thread-pool-state.userName = xxx +es.thread-pool-state.password = xxx es.thread-pool-state.index.name = thread-pool-state spring.dynamic.thread-pool.item-id=test diff --git a/hippo4j-monitor/hippo4j-monitor-es/src/main/java/cn/hippo4j/monitor/es/EsClientHolder.java b/hippo4j-monitor/hippo4j-monitor-es/src/main/java/cn/hippo4j/monitor/es/EsClientHolder.java index c43ff9f0..d100c2a1 100644 --- a/hippo4j-monitor/hippo4j-monitor-es/src/main/java/cn/hippo4j/monitor/es/EsClientHolder.java +++ b/hippo4j-monitor/hippo4j-monitor-es/src/main/java/cn/hippo4j/monitor/es/EsClientHolder.java @@ -32,10 +32,10 @@ public class EsClientHolder { private static RestHighLevelClient initRestClient() { try { Environment environment = ApplicationContextHolder.getInstance().getEnvironment(); - host = environment.getProperty("es.host"); - scheme = environment.getProperty("es.schema"); - userName = environment.getProperty("es.userName"); - password = environment.getProperty("es.password"); + host = environment.getProperty("es.thread-pool-state.host"); + scheme = environment.getProperty("es.thread-pool-state.schema"); + userName = environment.getProperty("es.thread-pool-state.userName"); + password = environment.getProperty("es.thread-pool-state.password"); List hosts = parseHosts(); @@ -70,9 +70,9 @@ public class EsClientHolder { private static List parseHosts() { String[] hostAndPorts = host.split(","); - List hosts = Lists.newArrayList(); for (String hostAndPort : hostAndPorts) { + hostAndPort = hostAndPort.trim(); hosts.add(new HttpHost(hostAndPort.split(":")[0], Integer.parseInt(hostAndPort.split(":")[1]), scheme)); } return hosts; diff --git a/hippo4j-monitor/hippo4j-monitor-es/src/main/java/cn/hippo4j/monitor/es/EsMonitorHandler.java b/hippo4j-monitor/hippo4j-monitor-es/src/main/java/cn/hippo4j/monitor/es/EsMonitorHandler.java index aeed1d5c..66bdd3c5 100644 --- a/hippo4j-monitor/hippo4j-monitor-es/src/main/java/cn/hippo4j/monitor/es/EsMonitorHandler.java +++ b/hippo4j-monitor/hippo4j-monitor-es/src/main/java/cn/hippo4j/monitor/es/EsMonitorHandler.java @@ -60,7 +60,7 @@ public class EsMonitorHandler extends AbstractDynamicThreadPoolMonitor { BeanUtil.copyProperties(poolRunStateInfo, esThreadPoolRunStateInfo); Environment environment = ApplicationContextHolder.getInstance().getEnvironment(); - String indexName = environment.getProperty("es.index.name", "thread-pool-state"); + String indexName = environment.getProperty("es.thread-pool-state.index.name", "thread-pool-state"); String applicationName = environment.getProperty("spring.application.name", "application"); if (!this.isExists(indexName)) { @@ -71,7 +71,7 @@ public class EsMonitorHandler extends AbstractDynamicThreadPoolMonitor { } esThreadPoolRunStateInfo.setApplicationName(applicationName); - esThreadPoolRunStateInfo.setId("thread-pool-state-" + System.currentTimeMillis()); + esThreadPoolRunStateInfo.setId(indexName + "-" + System.currentTimeMillis()); this.log2Es(esThreadPoolRunStateInfo, indexName); } @@ -85,7 +85,7 @@ public class EsMonitorHandler extends AbstractDynamicThreadPoolMonitor { request.source(stateJson, XContentType.JSON); IndexResponse response = client.index(request, RequestOptions.DEFAULT); - log.info("write thread-pool state to es:{}", stateJson); + log.info("write thread-pool state to es:{}, id is ", response.getId()); } catch (Exception ex) { log.error("es index error, the exception was thrown in create index. name:{},type:{},id:{}. {} ", indexName, @@ -96,6 +96,7 @@ public class EsMonitorHandler extends AbstractDynamicThreadPoolMonitor { } public synchronized boolean isExists(String index) { + //cache check result if (Objects.isNull(isIndexExist)) { boolean exists = false; GetIndexRequest request = new GetIndexRequest(); @@ -104,14 +105,14 @@ public class EsMonitorHandler extends AbstractDynamicThreadPoolMonitor { RestHighLevelClient client = EsClientHolder.getClient(); exists = client.indices().exists(request, RequestOptions.DEFAULT); } catch (IOException e) { - e.printStackTrace(); + log.error("check es index fail"); } isIndexExist = new AtomicBoolean(exists); } return isIndexExist.get(); } - public boolean createIndex(String index, String type, String mapping, Integer shards, Integer replicas, String alias) { + public void createIndex(String index, String type, String mapping, Integer shards, Integer replicas, String alias) { RestHighLevelClient client = EsClientHolder.getClient(); boolean acknowledged = false; CreateIndexRequest request = new CreateIndexRequest(index); @@ -130,7 +131,7 @@ public class EsMonitorHandler extends AbstractDynamicThreadPoolMonitor { CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT); acknowledged = createIndexResponse.isAcknowledged(); } catch (IOException e) { - e.printStackTrace(); + log.error("create es index exception", e); } if (acknowledged) { log.info("create es index success"); @@ -139,8 +140,6 @@ public class EsMonitorHandler extends AbstractDynamicThreadPoolMonitor { log.error("create es index fail"); throw new RuntimeException("cannot auto create thread-pool state es index"); } - return acknowledged; - } @Override