use of org.apache.servicecomb.foundation.common.net.IpPort in project java-chassis by ServiceComb.
the class ServiceRegistryClientImpl method findServiceInstance.
@Override
public MicroserviceInstance findServiceInstance(String serviceId, String instanceId) {
try {
Holder<MicroserviceInstanceResponse> holder = new Holder<>();
IpPort ipPort = ipPortManager.getAvailableAddress();
CountDownLatch countDownLatch = new CountDownLatch(1);
restClientUtil.get(ipPort, String.format(Const.REGISTRY_API.MICROSERVICE_INSTANCE_OPERATION_ONE, serviceId, instanceId), new RequestParam().addHeader("X-ConsumerId", serviceId).addQueryParam("global", "true"), syncHandler(countDownLatch, MicroserviceInstanceResponse.class, holder));
countDownLatch.await();
if (null != holder.value) {
return holder.value.getInstance();
}
return null;
} catch (Exception e) {
LOGGER.error("get instance from sc failed");
return null;
}
}
use of org.apache.servicecomb.foundation.common.net.IpPort in project java-chassis by ServiceComb.
the class ServiceRegistryClientImpl method watch.
public void watch(String selfMicroserviceId, AsyncResultCallback<MicroserviceInstanceChangedEvent> callback, AsyncResultCallback<Void> onOpen, AsyncResultCallback<Void> onClose) {
Boolean alreadyWatch = watchServices.get(selfMicroserviceId);
if (alreadyWatch == null) {
synchronized (ServiceRegistryClientImpl.class) {
alreadyWatch = watchServices.get(selfMicroserviceId);
if (alreadyWatch == null) {
watchServices.put(selfMicroserviceId, true);
String url = String.format(Const.REGISTRY_API.MICROSERVICE_WATCH, selfMicroserviceId);
IpPort ipPort = ipPortManager.getAvailableAddress();
websocketClientUtil.open(ipPort, url, o -> {
onOpen.success(o);
LOGGER.info("watching microservice {} successfully, " + "the chosen service center address is {}:{}", selfMicroserviceId, ipPort.getHostOrIp(), ipPort.getPort());
}, c -> {
watchErrorHandler(new ClientException("connection is closed accidentally"), selfMicroserviceId, callback);
onClose.success(null);
}, bodyBuffer -> {
MicroserviceInstanceChangedEvent response;
try {
response = JsonUtils.readValue(bodyBuffer.getBytes(), MicroserviceInstanceChangedEvent.class);
} catch (Exception e) {
LOGGER.error("watcher handle microservice {} response failed, {}", selfMicroserviceId, bodyBuffer.toString());
return;
}
try {
callback.success(response);
} catch (Exception e) {
LOGGER.error("notify watcher failed, microservice {}", selfMicroserviceId, e);
}
}, e -> {
watchErrorHandler(e, selfMicroserviceId, callback);
onClose.success(null);
}, f -> {
watchErrorHandler(f, selfMicroserviceId, callback);
});
}
}
}
}
use of org.apache.servicecomb.foundation.common.net.IpPort in project java-chassis by ServiceComb.
the class ServiceRegistryClientImpl method updateMicroserviceInstanceStatus.
@Override
public boolean updateMicroserviceInstanceStatus(String microserviceId, String instanceId, MicroserviceInstanceStatus status) {
if (null == status) {
throw new IllegalArgumentException("null status is now allowed");
}
Holder<HttpClientResponse> holder = new Holder<>();
IpPort ipPort = ipPortManager.getAvailableAddress();
try {
LOGGER.debug("update status of microservice instance: {}", status);
String url = String.format(Const.REGISTRY_API.MICROSERVICE_INSTANCE_STATUS, microserviceId, instanceId);
Map<String, String[]> queryParams = new HashMap<>();
queryParams.put("value", new String[] { status.toString() });
CountDownLatch countDownLatch = new CountDownLatch(1);
restClientUtil.put(ipPort, url, new RequestParam().setQueryParams(queryParams), 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 status of microservice instance {}/{} failed", microserviceId, instanceId, e);
}
return false;
}
use of org.apache.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.getAvailableAddress();
CountDownLatch countDownLatch = new CountDownLatch(1);
restClientUtil.get(ipPort, Const.REGISTRY_API.MICROSERVICE_OPERATION_ALL, 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 emptyList();
}
use of org.apache.servicecomb.foundation.common.net.IpPort in project java-chassis by ServiceComb.
the class ServiceRegistryClientImpl method updateMicroserviceProperties.
@Override
public boolean updateMicroserviceProperties(String microserviceId, Map<String, String> serviceProperties) {
Holder<HttpClientResponse> holder = new Holder<>();
IpPort ipPort = ipPortManager.getAvailableAddress();
try {
UpdatePropertiesRequest request = new UpdatePropertiesRequest();
request.setProperties(serviceProperties);
byte[] body = JsonUtils.writeValueAsBytes(request);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("update properties of microservice: {}", new String(body, Charset.defaultCharset()));
}
CountDownLatch countDownLatch = new CountDownLatch(1);
restClientUtil.put(ipPort, String.format(Const.REGISTRY_API.MICROSERVICE_PROPERTIES, microserviceId), 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 {} failed", microserviceId, e);
}
return false;
}
Aggregations