All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.alibaba.schedulerx.worker.util.ConsoleUtil Maven / Gradle / Ivy
package com.alibaba.schedulerx.worker.util;
import java.io.BufferedReader;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import com.alibaba.schedulerx.common.domain.AppGroupInfo;
import com.alibaba.schedulerx.common.domain.JSONResult;
import com.alibaba.schedulerx.common.util.ConfigUtil;
import com.alibaba.schedulerx.common.util.JsonUtil;
import com.alibaba.schedulerx.worker.domain.WorkerConstants;
import com.alibaba.schedulerx.worker.log.LogFactory;
import com.alibaba.schedulerx.worker.log.Logger;
import com.google.common.collect.Lists;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
/**
* console接口获取信息
*
* @author 陆昆
* @create 2019-03-13 下午4:49
**/
public class ConsoleUtil {
private static final Logger LOGGER = LogFactory.getLogger(ConsoleUtil.class);
private static final String APP_GROUP_ID_URL = "/worker/v1/appgroup/getId";
private static final String APP_GROUP_URL = "/worker/v1/appgroup/get";
private static final String CONSOLE_LIST_URL = "/worker/v1/meta/consolelist";
public static AppGroupInfo getAppGroup(String groupId, String appKey, String namespace, String namespaceSource) {
AppGroupInfo appGroupInfo = null;
Map parameterMap = new HashMap<>();
parameterMap.put("groupId", groupId);
String domain = ConfigUtil.getWorkerConfig().getString(WorkerConstants.WORKER_DOMAIN_NAME);
if (StringUtils.isNotBlank(domain)) {
String url;
if (namespace != null) {
url = "http://" + domain + APP_GROUP_URL + "?groupId=" + groupId + "&namespace=" + namespace;
if (StringUtils.isNotBlank(namespaceSource)) {
url += "&namespaceSource=" + namespaceSource;
}
} else {
url = "http://" + domain + APP_GROUP_URL + "?groupId=" + groupId;
}
try {
url += "&appKey=" + URLEncoder.encode((appKey == null) ? "" : appKey, "UTF-8");
HttpResponse jsonResponse = Unirest.get(url).asJson();
if (jsonResponse != null) {
JSONResult jsonResult = JsonUtil.fromJson(jsonResponse.getBody().toString(), JSONResult.class);
if (jsonResult != null) {
appGroupInfo = JsonUtil.fromJson(JsonUtil.toJson(jsonResult.getData()), AppGroupInfo.class);
}
}
} catch (Throwable e) {
LOGGER.warn("getAppGroup groupid={} error, url={} {}", groupId, url, e.getMessage());
}
}
return appGroupInfo;
}
public static long getAppGroupId(String groupId, String appKey, String namespace, String namespaceSource) {
long appGroupId = 0L;
Map parameterMap = new HashMap<>();
parameterMap.put("groupId", groupId);
String domain = ConfigUtil.getWorkerConfig().getString(WorkerConstants.WORKER_DOMAIN_NAME);
if (StringUtils.isNotBlank(domain)) {
String url;
if (namespace != null) {
url = "http://" + domain + APP_GROUP_ID_URL + "?groupId=" + groupId + "&namespace=" + namespace;
if (StringUtils.isNotBlank(namespaceSource)) {
url += "&namespaceSource=" + namespaceSource;
}
} else {
url = "http://" + domain + APP_GROUP_ID_URL + "?groupId=" + groupId;
}
try {
url += "&appKey=" + URLEncoder.encode((appKey == null) ? "" : appKey, "UTF-8");
HttpResponse jsonResponse = Unirest.get(url).asJson();
if (jsonResponse != null) {
JSONResult jsonResult = JsonUtil.fromJson(jsonResponse.getBody().toString(), JSONResult.class);
if (jsonResult != null) {
appGroupId = NumberUtils.toLong((String)jsonResult.getData(), 0);
}
}
} catch (Throwable e) {
LOGGER.error("GetAppGroupId Error, Message:{}, url={}", e.getMessage(), url);
throw new RuntimeException("GetAppGroupId failed.", e);
}
}
return appGroupId;
}
/**
* 从地址服务器获取vip列表
*
* @return
*/
public static List getVipServerList(String addressServerDomain, String port) {
List serverList = Lists.newArrayList();
String url = null;
try {
url = String.format("http://%s:%s/schedulerx2/consolelist", addressServerDomain,
port);
HttpResponse response = Unirest.get(url).asString();
if (response != null && response.getStatus() == HttpURLConnection.HTTP_OK) {
BufferedReader reader = new BufferedReader(new StringReader(response.getBody()));
String line;
while ((line = reader.readLine()) != null) {
if (StringUtils.isNotBlank(line)) {
serverList.add(StringUtils.trim(line));
}
}
}
return serverList;
} catch (Exception ex) {
LOGGER.warn("Fetch domainName from address server error, url={}.", url);
return serverList;
}
}
private static String getAddressServerPort() {
String addressPort = System.getProperty(WorkerConstants.ADDRESS_SERVER_PORT);
if (StringUtils.isBlank(addressPort)) {
addressPort = (String)ConfigUtil.getWorkerConfig().getProperty(WorkerConstants.ADDRESS_SERVER_PORT);
}
if (StringUtils.isBlank(addressPort)) {
addressPort = System.getenv(WorkerConstants.ADDRESS_SERVER_PORT.replace(".", "_"));
}
if (StringUtils.isBlank(addressPort) || Integer.parseInt(addressPort) <= 0) {
addressPort = "8080";
}
ConfigUtil.getWorkerConfig().setProperty(WorkerConstants.ADDRESS_SERVER_PORT, addressPort);
return addressPort;
}
public static String getAddressServerDomain() {
// set优先
String domainName = ConfigUtil.getWorkerConfig().getString(WorkerConstants.ADDRESS_SERVER_DOMAIN);
// EDAS应用会通过-D配置
if (StringUtils.isBlank(domainName)) {
domainName = System.getProperty(WorkerConstants.ADDRESS_SERVER_DOMAIN);
}
if (StringUtils.isBlank(domainName)) {
domainName = System.getenv(WorkerConstants.ADDRESS_SERVER_DOMAIN.replace(".", "_"));
}
if (StringUtils.isNotBlank(domainName)) {
ConfigUtil.getWorkerConfig().setProperty(WorkerConstants.ADDRESS_SERVER_DOMAIN, domainName);
}
return domainName;
}
/**
* 直接从地址服务获取domainName
*
* @return
*/
public static String getDomainFromHttpServer() {
String addressDomain = getAddressServerDomain();
if (StringUtils.isBlank(addressDomain)) {
return StringUtils.EMPTY;
}
List serverList = getVipServerList(addressDomain, getAddressServerPort());
if (CollectionUtils.isNotEmpty(serverList)) {
int index = 0;
if (serverList.size() > 1) {
Random random = new Random();
index = random.nextInt(serverList.size());
}
return serverList.get(index);
}
return StringUtils.EMPTY;
}
@SuppressWarnings("unchecked")
public static Map fetchMetaInfoFromConsole(String namespace, String namespaceSource,
List groupIds, List appKeys) throws Exception {
String url = null;
try {
String domain = ConfigUtil.getWorkerConfig().getString(WorkerConstants.WORKER_DOMAIN_NAME);
if (StringUtils.isBlank(namespaceSource)) {
url = "http://" + domain + "/worker/v1/meta/get?namespace=" + namespace + "&groups=" + StringUtils.join(
groupIds, ",")+"&appKeys="+URLEncoder.encode(StringUtils.join(appKeys, ","), "UTF-8");
} else {
url = "http://" + domain + "/worker/v1/meta/get?namespace=" + namespace + "&namespaceSource="
+ namespaceSource + "&groups=" + StringUtils.join(groupIds, ",")+"&appKeys="+URLEncoder.encode(StringUtils.join(appKeys, ","), "UTF-8");
}
url += "&supportLogService=true&version=" + ConfigUtil.getWorkerConfig().getString(WorkerConstants.WORKER_VERSION);
HttpResponse response = Unirest.get(url).asJson();
if (response != null && response.getStatus() == HttpURLConnection.HTTP_OK) {
JSONResult jsonResult = JsonUtil.fromJson(response.getBody().toString(), JSONResult.class);
if (jsonResult.isSuccess()) {
return (Map)jsonResult.getData();
} else {
throw new IllegalArgumentException(jsonResult.getMessage());
}
}
} catch (Exception ex) {
LOGGER.error("fetch metaInfo from console error, url={}", url, ex);
throw ex;
}
return null;
}
public static String getRandomConsoleIp(String domain) {
String console = null;
String url = "http://" + domain + CONSOLE_LIST_URL;
try {
HttpResponse response = Unirest.get(url).asJson();
if (response != null && response.getStatus() == HttpURLConnection.HTTP_OK) {
JSONResult jsonResult = JsonUtil.fromJson(response.getBody().toString(), JSONResult.class);
List consoleList = (List) jsonResult.getData();
if (CollectionUtils.isNotEmpty(consoleList)) {
int index = new Random().nextInt(consoleList.size());
console = consoleList.get(index);
}
}
} catch (Exception e) {
LOGGER.error("get consolelist error, url={}", url, e);
}
return console;
}
}