Skip to content

Commit

Permalink
restClient默认连接console api接口重构 #24
Browse files Browse the repository at this point in the history
  • Loading branch information
chembohuang committed Mar 1, 2019
1 parent 615ec8d commit c9a998b
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand All @@ -44,6 +42,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.vip.pallas.client.env.LoadEnv;
import com.vip.pallas.client.thread.CleanRestClientTask;
import com.vip.pallas.client.thread.QueryConsoleTask;

Expand Down Expand Up @@ -138,7 +137,8 @@ private static PallasRestClient createPallasRestclient(String clientToken,
int retryCount = 3;
while (QueryConsoleTask.getPsListByToken(clientToken) == null && retryCount-- > 0) {
TimeUnit.SECONDS.sleep(1);
log.error("can't get a valid pallas-search list from pallas-console with token: {}, init pallas-client failed.", clientToken);
log.error("can't get a valid pallas-search list from {} with token: {}, init pallas-client failed.",
LoadEnv.consoleQueryUrl, clientToken);
}
PallasRestClient pallasRestClient;
List<HttpHost> psHostList = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.vip.pallas.client.env;

import java.util.Iterator;
import java.util.ServiceLoader;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class LoadEnv {

private static final Logger log = LoggerFactory.getLogger(LoadEnv.class);

public static String consoleQueryUrl;

private static LoadEnv envInstance;

public static final String CONSOLE_QUERY_URL_ENV_KEY = "VIP_PALLAS_CONSOLE_QUERY_URL";

public static final String QUERY_URL_SUFFIX = "/pallas/ss/query_pslist_and_domain.json";

static {
initEnvImpl();
}

public static String addSuffixIfNecessary(String consoleQueryUrl) {
if (!consoleQueryUrl.endsWith(".json")) {
consoleQueryUrl += consoleQueryUrl.endsWith("/") ? StringUtils.substringAfter(QUERY_URL_SUFFIX, "/") : QUERY_URL_SUFFIX;
}
log.warn("console url located to: {}", consoleQueryUrl);
return consoleQueryUrl;
}

public static LoadEnv envInstance() {
return envInstance;
}

private static void initEnvImpl() {
try {
// discover LoadEnv
ServiceLoader<LoadEnv> loader = ServiceLoader.load(LoadEnv.class);
Iterator<LoadEnv> iter = loader.iterator();
while (iter.hasNext()) {
envInstance = iter.next();
log.info("LoadEnv implementation found: {}", envInstance.getClass().getName());
return;
}
// log error, fallback
envInstance = new LoadOpenEnv();
log.error("LoadEnv implementation not found, fallback to: {}", envInstance.getClass().getName());
} catch (Throwable e) {
// log error, fallback
envInstance = new LoadOpenEnv();
log.error("LoadEnv implementation not found, fallback to: {}", envInstance.getClass().getName());
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.vip.pallas.client.env;

public class LoadOpenEnv extends LoadEnv {

static {
consoleQueryUrl = System.getProperty(CONSOLE_QUERY_URL_ENV_KEY, "http://localhost:8080");
consoleQueryUrl = addSuffixIfNecessary(consoleQueryUrl);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.vip.pallas.client.PallasRestClientBuilder;
import com.vip.pallas.client.env.LoadEnv;
import com.vip.pallas.client.util.HttpClient;
import com.vip.pallas.utils.IPUtils;

Expand All @@ -39,9 +40,6 @@ public class QueryConsoleTask implements Runnable {

private static final String PARAMS = "{\"token\":\"%s\", \"ip\":\"" + IPUtils.localIp4Str() + "\"}";

public static String consoleQueryUrl = System.getProperty("VIP_PALLAS_CONSOLE_QUERY_URL",
"http://localhost:8080/pallas/ss/query_pslist_and_domain.json");

public static volatile Map<String, String> esDomainMap = new ConcurrentHashMap<>();

public static volatile Map<String, List<String>> psListMap = new ConcurrentHashMap<>();
Expand All @@ -59,7 +57,7 @@ public void run() {
try {
String token = iterator.next();
JSONObject jsonObject = JSON.parseObject(
HttpClient.httpPost(consoleQueryUrl,
HttpClient.httpPost(LoadEnv.consoleQueryUrl,
String.format(PARAMS, token)));
if (jsonObject != null) {
JSONObject data = jsonObject.getJSONObject("data");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,34 @@

package com.vip.pallas.client.aspectj.integrationtest.service;

import java.io.IOException;
import java.util.Collections;
import java.util.Random;
import java.util.concurrent.TimeUnit;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpException;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.config.SocketConfig;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.localserver.LocalServerTestBase;
import org.apache.http.nio.entity.NStringEntity;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpRequestHandler;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
import org.junit.Before;

import com.vip.pallas.client.PallasRestClient;
import com.vip.pallas.client.PallasRestClientBuilder;
import com.vip.pallas.client.thread.QueryConsoleTask;
import java.io.IOException;
import java.util.Collections;
import java.util.Random;
import java.util.concurrent.TimeUnit;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpException;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.config.SocketConfig;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.localserver.LocalServerTestBase;
import org.apache.http.nio.entity.NStringEntity;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpRequestHandler;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
import org.junit.Before;

import com.vip.pallas.client.PallasRestClient;
import com.vip.pallas.client.PallasRestClientBuilder;
import com.vip.pallas.client.env.LoadEnv;

public class MockHttpService extends LocalServerTestBase {

Expand Down Expand Up @@ -109,7 +109,7 @@ public void handle(HttpRequest request, HttpResponse response, HttpContext conte
});
target = start();
serverBootstrap.setSocketConfig(SocketConfig.custom().setSoTimeout(61000).build());
QueryConsoleTask.consoleQueryUrl = "http://localhost:" + target.getPort() + "/getPsListAndEsDomain";
LoadEnv.consoleQueryUrl = "http://localhost:" + target.getPort() + "/getPsListAndEsDomain";
String serverUrl = "http://localhost:" + target.getPort();
System.out.println("server listen at: " + serverUrl);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.junit.rules.ExpectedException;

import com.vip.pallas.client.PallasRestClient;
import com.vip.pallas.client.env.LoadEnv;
import com.vip.pallas.client.thread.QueryConsoleTask;

public class PallasRestClientTest extends LocalServerTestBase {
Expand Down Expand Up @@ -98,7 +99,7 @@ public void handle(HttpRequest request, HttpResponse response, HttpContext conte
});
target = start();
serverBootstrap.setSocketConfig(SocketConfig.custom().setSoTimeout(61000).build());
QueryConsoleTask.consoleQueryUrl = "http://localhost:" + target.getPort() + "/getPsListAndEsDomain";
LoadEnv.consoleQueryUrl = "http://localhost:" + target.getPort() + "/getPsListAndEsDomain";
String serverUrl = "http://localhost:" + target.getPort();
System.out.println("server listen at: " + serverUrl);
}
Expand Down

0 comments on commit c9a998b

Please sign in to comment.