Search in sources :

Example 1 with FindMicroserviceInstancesResponse

use of org.apache.servicecomb.service.center.client.model.FindMicroserviceInstancesResponse in project java-chassis by ServiceComb.

the class ServiceCenterClient method findMicroserviceInstance.

@Override
public FindMicroserviceInstancesResponse findMicroserviceInstance(String consumerId, String appId, String serviceName, String versionRule, String revision) {
    try {
        Map<String, String> headers = new HashMap<>();
        headers.put("X-ConsumerId", consumerId);
        HttpResponse response = httpClient.getHttpRequest("/registry/instances?appId=" + URLEncoder.encode(appId, "UTF-8") + "&serviceName=" + HttpUtils.encodeURLParam(serviceName) + "&version=" + HttpUtils.encodeURLParam(versionRule) + "&rev=" + HttpUtils.encodeURLParam(revision), headers, null);
        FindMicroserviceInstancesResponse result = new FindMicroserviceInstancesResponse();
        if (response.getStatusCode() == HttpStatus.SC_OK) {
            result.setModified(true);
            result.setRevision(response.getHeader("X-Resource-Revision"));
            result.setMicroserviceInstancesResponse(HttpUtils.deserialize(response.getContent(), MicroserviceInstancesResponse.class));
            return result;
        }
        if (response.getStatusCode() == HttpStatus.SC_NOT_MODIFIED) {
            result.setModified(false);
            return result;
        }
        sendUnAuthorizedEvent(response);
        throw new OperationException("get service instances list fails, statusCode = " + response.getStatusCode() + "; message = " + response.getMessage() + "; content = " + response.getContent());
    } catch (IOException e) {
        throw new OperationException("get service instances list fails", e);
    }
}
Also used : MicroserviceInstancesResponse(org.apache.servicecomb.service.center.client.model.MicroserviceInstancesResponse) FindMicroserviceInstancesResponse(org.apache.servicecomb.service.center.client.model.FindMicroserviceInstancesResponse) HashMap(java.util.HashMap) HttpResponse(org.apache.servicecomb.http.client.common.HttpResponse) FindMicroserviceInstancesResponse(org.apache.servicecomb.service.center.client.model.FindMicroserviceInstancesResponse) IOException(java.io.IOException) OperationException(org.apache.servicecomb.service.center.client.exception.OperationException)

Example 2 with FindMicroserviceInstancesResponse

use of org.apache.servicecomb.service.center.client.model.FindMicroserviceInstancesResponse in project java-chassis by ServiceComb.

the class ServiceCenterDiscovery method pullInstance.

private void pullInstance(SubscriptionKey k, SubscriptionValue v) {
    if (myselfServiceId == null) {
        // registration not ready
        return;
    }
    try {
        FindMicroserviceInstancesResponse instancesResponse = serviceCenterClient.findMicroserviceInstance(myselfServiceId, k.appId, k.serviceName, ALL_VERSION, v.revision);
        if (instancesResponse.isModified()) {
            List<MicroserviceInstance> instances = instancesResponse.getMicroserviceInstancesResponse().getInstances() == null ? Collections.emptyList() : instancesResponse.getMicroserviceInstancesResponse().getInstances();
            setMicroserviceInfo(instances);
            LOGGER.info("Instance changed event, " + "current: revision={}, instances={}; " + "origin: revision={}, instances={}; " + "appId={}, serviceName={}", instancesResponse.getRevision(), instanceToString(instances), v.revision, instanceToString(v.instancesCache), k.appId, k.serviceName);
            v.instancesCache = instances;
            v.revision = instancesResponse.getRevision();
            eventBus.post(new InstanceChangedEvent(k.appId, k.serviceName, v.instancesCache));
        }
    } catch (Exception e) {
        LOGGER.error("find service {}#{} instance failed.", k.appId, k.serviceName, e);
        failedInstances.add(k);
    }
}
Also used : InstanceChangedEvent(org.apache.servicecomb.service.center.client.DiscoveryEvents.InstanceChangedEvent) MicroserviceInstance(org.apache.servicecomb.service.center.client.model.MicroserviceInstance) FindMicroserviceInstancesResponse(org.apache.servicecomb.service.center.client.model.FindMicroserviceInstancesResponse)

Aggregations

FindMicroserviceInstancesResponse (org.apache.servicecomb.service.center.client.model.FindMicroserviceInstancesResponse)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 HttpResponse (org.apache.servicecomb.http.client.common.HttpResponse)1 InstanceChangedEvent (org.apache.servicecomb.service.center.client.DiscoveryEvents.InstanceChangedEvent)1 OperationException (org.apache.servicecomb.service.center.client.exception.OperationException)1 MicroserviceInstance (org.apache.servicecomb.service.center.client.model.MicroserviceInstance)1 MicroserviceInstancesResponse (org.apache.servicecomb.service.center.client.model.MicroserviceInstancesResponse)1