use of io.servicecomb.foundation.common.net.IpPort in project java-chassis by ServiceComb.
the class ServiceRegistryClientImpl method getSchema.
@Override
public String getSchema(String microserviceId, String schemaId) {
Holder<GetSchemaResponse> holder = new Holder<>();
IpPort ipPort = IpPortManager.INSTANCE.get();
CountDownLatch countDownLatch = new CountDownLatch(1);
RestUtils.get(ipPort, Const.MS_API_PATH + Const.MICROSERVICE_PATH + "/" + microserviceId + Const.SCHEMA_PATH + "/" + schemaId, new RequestParam(), syncHandler(countDownLatch, GetSchemaResponse.class, holder));
try {
countDownLatch.await();
} catch (Exception e) {
LOGGER.error("query schema exist {}/{} failed", microserviceId, schemaId, e);
}
if (holder.value != null) {
return holder.value.getSchema();
}
return null;
}
use of io.servicecomb.foundation.common.net.IpPort in project java-chassis by ServiceComb.
the class ServiceRegistryClientImpl method findServiceInstance.
@Override
public List<MicroserviceInstance> findServiceInstance(String selfMicroserviceId, String appId, String serviceName, String versionRule) {
Holder<FindInstancesResponse> holder = new Holder<>();
IpPort ipPort = IpPortManager.INSTANCE.get();
StringBuilder url = new StringBuilder(Const.MS_API_PATH);
url.append(Const.INSTANCES_PATH);
CountDownLatch countDownLatch = new CountDownLatch(1);
RestUtils.get(ipPort, url.toString(), new RequestParam().addQueryParam("appId", appId).addQueryParam("serviceName", serviceName).addQueryParam("version", versionRule).addHeader("X-ConsumerId", selfMicroserviceId), syncHandler(countDownLatch, FindInstancesResponse.class, holder));
try {
countDownLatch.await();
if (holder.value == null) {
// error
return null;
}
List<MicroserviceInstance> list = holder.value.getInstances();
if (list == null) {
return new ArrayList<>();
}
return list;
} catch (Exception e) {
LOGGER.error("find microservice instance {}/{}/{} failed", appId, serviceName, versionRule, e);
}
return null;
}
use of io.servicecomb.foundation.common.net.IpPort in project java-chassis by ServiceComb.
the class ServiceRegistryClientImpl method getAllMicroservices.
@Override
public List<Microservice> getAllMicroservices() {
Holder<GetAllServicesResponse> holder = new Holder<>();
IpPort ipPort = IpPortManager.INSTANCE.get();
CountDownLatch countDownLatch = new CountDownLatch(1);
RestUtils.get(ipPort, Const.MS_API_PATH + Const.MICROSERVICE_PATH, new RequestParam(), syncHandler(countDownLatch, GetAllServicesResponse.class, holder));
try {
countDownLatch.await();
if (holder.value != null) {
return holder.value.getServices();
}
} catch (Exception e) {
LOGGER.error("query all microservices failed", e);
}
return null;
}
use of io.servicecomb.foundation.common.net.IpPort in project java-chassis by ServiceComb.
the class ServiceRegistryClientImpl method updateInstanceProperties.
@Override
public boolean updateInstanceProperties(String microserviceId, String microserviceInstanceId, Map<String, String> instanceProperties) {
Holder<HttpClientResponse> holder = new Holder<>();
IpPort ipPort = IpPortManager.INSTANCE.get();
StringBuilder url = new StringBuilder(Const.MS_API_PATH);
url.append(Const.MICROSERVICE_PATH).append("/").append(microserviceId).append(Const.INSTANCES_PATH).append("/").append(microserviceInstanceId).append(Const.PROPERTIES_PATH);
try {
UpdatePropertiesRequest request = new UpdatePropertiesRequest();
request.setProperties(instanceProperties);
byte[] body = JsonUtils.writeValueAsBytes(request);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("update properties of microservice instance: {}", new String(body, Charset.defaultCharset()));
}
CountDownLatch countDownLatch = new CountDownLatch(1);
RestUtils.put(ipPort, url.toString(), new RequestParam().setBody(body), syncHandler(countDownLatch, HttpClientResponse.class, holder));
countDownLatch.await();
if (holder.value != null) {
if (holder.value.statusCode() == Status.OK.getStatusCode()) {
return true;
}
LOGGER.warn(holder.value.statusMessage());
}
} catch (Exception e) {
LOGGER.error("update properties of microservice instance {}/{} failed", microserviceId, microserviceInstanceId, e);
}
return false;
}
use of io.servicecomb.foundation.common.net.IpPort in project java-chassis by ServiceComb.
the class RestUtils method httpDo.
public static void httpDo(RequestContext requestContext, Handler<RestResponse> responseHandler) {
HttpClientWithContext vertxHttpClient = HttpClientPool.INSTANCE.getClient();
vertxHttpClient.runOnContext(httpClient -> {
IpPort ipPort = requestContext.getIpPort();
HttpMethod httpMethod = requestContext.getMethod();
RequestParam requestParam = requestContext.getParams();
if (ipPort == null) {
LOGGER.error("request address is null");
responseHandler.handle(new RestResponse(requestContext, null));
return;
}
StringBuilder url = new StringBuilder(requestContext.getUri());
String queryParams = requestParam.getQueryParams();
if (!queryParams.isEmpty()) {
url.append(url.lastIndexOf("?") > 0 ? "&" : "?").append(queryParams);
}
HttpClientRequest httpClientRequest = httpClient.request(httpMethod, ipPort.getPort(), ipPort.getHostOrIp(), url.toString(), response -> {
responseHandler.handle(new RestResponse(requestContext, response));
});
httpClientRequest.setTimeout(ServiceRegistryConfig.INSTANCE.getRequestTimeout()).exceptionHandler(e -> {
LOGGER.error("{} {} fail, endpoint is {}:{}, message: {}", httpMethod, url.toString(), ipPort.getHostOrIp(), ipPort.getPort(), e.getMessage());
responseHandler.handle(new RestResponse(requestContext, null));
});
addDefaultHeaders(httpClientRequest);
if (requestParam.getHeaders() != null && requestParam.getHeaders().size() > 0) {
for (Map.Entry<String, String> header : requestParam.getHeaders().entrySet()) {
httpClientRequest.putHeader(header.getKey(), header.getValue());
}
}
if (requestParam.getCookies() != null && requestParam.getCookies().size() > 0) {
StringBuilder stringBuilder = new StringBuilder();
for (Map.Entry<String, String> cookie : requestParam.getCookies().entrySet()) {
stringBuilder.append(cookie.getKey()).append("=").append(cookie.getValue()).append("; ");
}
httpClientRequest.putHeader("Cookie", stringBuilder.toString());
}
if (httpMethod != HttpMethod.GET && requestParam.getBody() != null && requestParam.getBody().length > 0) {
httpClientRequest.end(Buffer.buffer(requestParam.getBody()));
} else {
httpClientRequest.end();
}
});
}
Aggregations