modify something

pull/451/head
maxisvest 3 years ago
parent bea61eadb3
commit 544401824d

@ -5,10 +5,10 @@ server.servlet.context-path=/example
spring.profiles.active=dev spring.profiles.active=dev
spring.application.name=dynamic-threadpool-example spring.application.name=dynamic-threadpool-example
es.host = xxx es.thread-pool-state.host = ip1:port,ip2:port
es.scheme = xxx es.thread-pool-state.scheme = http
es.userName = xxx es.thread-pool-state.userName = xxx
es.password = xxx es.thread-pool-state.password = xxx
es.thread-pool-state.index.name = thread-pool-state es.thread-pool-state.index.name = thread-pool-state
spring.dynamic.thread-pool.item-id=test spring.dynamic.thread-pool.item-id=test

@ -32,10 +32,10 @@ public class EsClientHolder {
private static RestHighLevelClient initRestClient() { private static RestHighLevelClient initRestClient() {
try { try {
Environment environment = ApplicationContextHolder.getInstance().getEnvironment(); Environment environment = ApplicationContextHolder.getInstance().getEnvironment();
host = environment.getProperty("es.host"); host = environment.getProperty("es.thread-pool-state.host");
scheme = environment.getProperty("es.schema"); scheme = environment.getProperty("es.thread-pool-state.schema");
userName = environment.getProperty("es.userName"); userName = environment.getProperty("es.thread-pool-state.userName");
password = environment.getProperty("es.password"); password = environment.getProperty("es.thread-pool-state.password");
List<HttpHost> hosts = parseHosts(); List<HttpHost> hosts = parseHosts();
@ -70,9 +70,9 @@ public class EsClientHolder {
private static List<HttpHost> parseHosts() { private static List<HttpHost> parseHosts() {
String[] hostAndPorts = host.split(","); String[] hostAndPorts = host.split(",");
List<HttpHost> hosts = Lists.newArrayList(); List<HttpHost> hosts = Lists.newArrayList();
for (String hostAndPort : hostAndPorts) { for (String hostAndPort : hostAndPorts) {
hostAndPort = hostAndPort.trim();
hosts.add(new HttpHost(hostAndPort.split(":")[0], Integer.parseInt(hostAndPort.split(":")[1]), scheme)); hosts.add(new HttpHost(hostAndPort.split(":")[0], Integer.parseInt(hostAndPort.split(":")[1]), scheme));
} }
return hosts; return hosts;

@ -60,7 +60,7 @@ public class EsMonitorHandler extends AbstractDynamicThreadPoolMonitor {
BeanUtil.copyProperties(poolRunStateInfo, esThreadPoolRunStateInfo); BeanUtil.copyProperties(poolRunStateInfo, esThreadPoolRunStateInfo);
Environment environment = ApplicationContextHolder.getInstance().getEnvironment(); 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"); String applicationName = environment.getProperty("spring.application.name", "application");
if (!this.isExists(indexName)) { if (!this.isExists(indexName)) {
@ -71,7 +71,7 @@ public class EsMonitorHandler extends AbstractDynamicThreadPoolMonitor {
} }
esThreadPoolRunStateInfo.setApplicationName(applicationName); esThreadPoolRunStateInfo.setApplicationName(applicationName);
esThreadPoolRunStateInfo.setId("thread-pool-state-" + System.currentTimeMillis()); esThreadPoolRunStateInfo.setId(indexName + "-" + System.currentTimeMillis());
this.log2Es(esThreadPoolRunStateInfo, indexName); this.log2Es(esThreadPoolRunStateInfo, indexName);
} }
@ -85,7 +85,7 @@ public class EsMonitorHandler extends AbstractDynamicThreadPoolMonitor {
request.source(stateJson, XContentType.JSON); request.source(stateJson, XContentType.JSON);
IndexResponse response = client.index(request, RequestOptions.DEFAULT); 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) { } catch (Exception ex) {
log.error("es index error, the exception was thrown in create index. name:{},type:{},id:{}. {} ", log.error("es index error, the exception was thrown in create index. name:{},type:{},id:{}. {} ",
indexName, indexName,
@ -96,6 +96,7 @@ public class EsMonitorHandler extends AbstractDynamicThreadPoolMonitor {
} }
public synchronized boolean isExists(String index) { public synchronized boolean isExists(String index) {
//cache check result
if (Objects.isNull(isIndexExist)) { if (Objects.isNull(isIndexExist)) {
boolean exists = false; boolean exists = false;
GetIndexRequest request = new GetIndexRequest(); GetIndexRequest request = new GetIndexRequest();
@ -104,14 +105,14 @@ public class EsMonitorHandler extends AbstractDynamicThreadPoolMonitor {
RestHighLevelClient client = EsClientHolder.getClient(); RestHighLevelClient client = EsClientHolder.getClient();
exists = client.indices().exists(request, RequestOptions.DEFAULT); exists = client.indices().exists(request, RequestOptions.DEFAULT);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); log.error("check es index fail");
} }
isIndexExist = new AtomicBoolean(exists); isIndexExist = new AtomicBoolean(exists);
} }
return isIndexExist.get(); 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(); RestHighLevelClient client = EsClientHolder.getClient();
boolean acknowledged = false; boolean acknowledged = false;
CreateIndexRequest request = new CreateIndexRequest(index); CreateIndexRequest request = new CreateIndexRequest(index);
@ -130,7 +131,7 @@ public class EsMonitorHandler extends AbstractDynamicThreadPoolMonitor {
CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT); CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
acknowledged = createIndexResponse.isAcknowledged(); acknowledged = createIndexResponse.isAcknowledged();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); log.error("create es index exception", e);
} }
if (acknowledged) { if (acknowledged) {
log.info("create es index success"); log.info("create es index success");
@ -139,8 +140,6 @@ public class EsMonitorHandler extends AbstractDynamicThreadPoolMonitor {
log.error("create es index fail"); log.error("create es index fail");
throw new RuntimeException("cannot auto create thread-pool state es index"); throw new RuntimeException("cannot auto create thread-pool state es index");
} }
return acknowledged;
} }
@Override @Override

Loading…
Cancel
Save