use of com.vip.saturn.job.exception.SaturnExecutorException in project Saturn by vipshop.
the class SaturnExecutor method discover.
private Map<String, String> discover() throws Exception {
if (SystemEnvProperties.VIP_SATURN_CONSOLE_URI_LIST.isEmpty()) {
throw new Exception("Please configure the parameter " + SystemEnvProperties.NAME_VIP_SATURN_CONSOLE_URI + " with env or -D");
}
int size = SystemEnvProperties.VIP_SATURN_CONSOLE_URI_LIST.size();
for (int i = 0; i < size; i++) {
String consoleUri = SystemEnvProperties.VIP_SATURN_CONSOLE_URI_LIST.get(i);
String url = consoleUri + "/rest/v1/discovery?namespace=" + namespace;
CloseableHttpClient httpClient = null;
try {
httpClient = HttpClientBuilder.create().build();
HttpGet httpGet = new HttpGet(url);
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(5000).setSocketTimeout(10000).build();
httpGet.setConfig(requestConfig);
CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
StatusLine statusLine = httpResponse.getStatusLine();
String responseBody = EntityUtils.toString(httpResponse.getEntity());
Integer statusCode = statusLine != null ? statusLine.getStatusCode() : null;
if (statusLine != null && statusCode.intValue() == HttpStatus.SC_OK) {
Map<String, String> discoveryInfo = JsonUtils.getGson().fromJson(responseBody, new TypeToken<Map<String, String>>() {
}.getType());
String connectionString = discoveryInfo.get(DISCOVER_INFO_ZK_CONN_STR);
if (StringUtils.isBlank(connectionString)) {
LogUtils.warn(log, LogEvents.ExecutorEvent.INIT, "ZK connection string is blank!");
continue;
}
LogUtils.info(log, LogEvents.ExecutorEvent.INIT, "Discover successfully. Url: {}, discovery info: {}", url, discoveryInfo);
return discoveryInfo;
} else {
handleDiscoverException(responseBody, statusCode);
}
} catch (SaturnExecutorException e) {
LogUtils.error(log, LogEvents.ExecutorEvent.INIT, e.getMessage(), e);
if (e.getCode() != SaturnExecutorExceptionCode.UNEXPECTED_EXCEPTION) {
throw e;
}
} catch (Throwable t) {
LogUtils.error(log, LogEvents.ExecutorEvent.INIT, "Fail to discover from Saturn Console. Url: {}", url, t);
} finally {
if (httpClient != null) {
try {
httpClient.close();
} catch (IOException e) {
LogUtils.error(log, LogEvents.ExecutorEvent.INIT, "Fail to close httpclient", e);
}
}
}
}
List<String> context = buildContext();
String msg = "Fail to discover from Saturn Console! Please make sure that you have added the target namespace on Saturn Console, namespace:%s, context:%s";
throw new Exception(String.format(msg, namespace, context));
}
use of com.vip.saturn.job.exception.SaturnExecutorException in project Saturn by vipshop.
the class SaturnExecutor method discoverZK.
private String discoverZK() throws Exception {
if (SystemEnvProperties.VIP_SATURN_CONSOLE_URI_LIST.isEmpty()) {
throw new Exception("Please configure the parameter " + SystemEnvProperties.NAME_VIP_SATURN_CONSOLE_URI + " with env or -D");
}
int size = SystemEnvProperties.VIP_SATURN_CONSOLE_URI_LIST.size();
for (int i = 0; i < size; i++) {
String consoleUri = SystemEnvProperties.VIP_SATURN_CONSOLE_URI_LIST.get(i);
String url = consoleUri + "/rest/v1/discoverZk?namespace=" + namespace;
CloseableHttpClient httpClient = null;
try {
httpClient = HttpClientBuilder.create().build();
HttpGet httpGet = new HttpGet(url);
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(5000).setSocketTimeout(10000).build();
httpGet.setConfig(requestConfig);
CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
StatusLine statusLine = httpResponse.getStatusLine();
String responseBody = EntityUtils.toString(httpResponse.getEntity());
Integer statusCode = statusLine != null ? statusLine.getStatusCode() : null;
if (statusLine != null && statusCode.intValue() == HttpStatus.SC_OK) {
String connectionString = JSON.parseObject(responseBody, String.class);
if (StringUtils.isBlank(connectionString)) {
log.warn("ZK connection string is blankļ¼");
continue;
}
log.info("Discover zk connection string successfully. Url: {}, zk connection string: {}", url, connectionString);
return connectionString;
} else {
handleDiscoverException(responseBody, statusCode);
}
} catch (SaturnExecutorException e) {
log.error(e.getMessage());
if (e.getCode() != SaturnExecutorExceptionCode.UNEXPECTED_EXCEPTION) {
throw e;
}
} catch (Exception e) {
log.error("Fail to discover zk connection. Url: " + url, e);
} finally {
if (httpClient != null) {
try {
httpClient.close();
} catch (IOException e) {
log.error("Fail to close httpclient.", e);
}
}
}
}
throw new Exception("Fail to discover zk connection string! Please make sure that you have added your namespace on Saturn Console.");
}
Aggregations