Search in sources :

Example 11 with HttpResponse

use of org.apache.servicecomb.http.client.common.HttpResponse in project java-chassis by ServiceComb.

the class ServiceCenterClient method registerMicroserviceInstance.

@Override
public RegisteredMicroserviceInstanceResponse registerMicroserviceInstance(MicroserviceInstance instance) {
    try {
        CreateMicroserviceInstanceRequest request = new CreateMicroserviceInstanceRequest();
        request.setInstance(instance);
        HttpResponse response = httpClient.postHttpRequest("/registry/microservices/" + instance.getServiceId() + "/instances", null, HttpUtils.serialize(request));
        if (response.getStatusCode() == HttpStatus.SC_OK) {
            return HttpUtils.deserialize(response.getContent(), RegisteredMicroserviceInstanceResponse.class);
        }
        sendUnAuthorizedEvent(response);
        throw new OperationException("register service instance fails, statusCode = " + response.getStatusCode() + "; message = " + response.getMessage() + "; content = " + response.getContent());
    } catch (IOException e) {
        throw new OperationException("register service instance fails", e);
    }
}
Also used : CreateMicroserviceInstanceRequest(org.apache.servicecomb.service.center.client.model.CreateMicroserviceInstanceRequest) HttpResponse(org.apache.servicecomb.http.client.common.HttpResponse) IOException(java.io.IOException) OperationException(org.apache.servicecomb.service.center.client.exception.OperationException)

Example 12 with HttpResponse

use of org.apache.servicecomb.http.client.common.HttpResponse in project java-chassis by ServiceComb.

the class ServiceCenterClient method getServiceCenterInstances.

@Override
public MicroserviceInstancesResponse getServiceCenterInstances() {
    try {
        HttpResponse response = httpClient.getHttpRequest("/registry/health", null, null);
        if (response.getStatusCode() == HttpStatus.SC_OK) {
            return HttpUtils.deserialize(response.getContent(), MicroserviceInstancesResponse.class);
        }
        sendUnAuthorizedEvent(response);
        throw new OperationException("get service-center instances fails, statusCode = " + response.getStatusCode() + "; message = " + response.getMessage() + "; content = " + response.getContent());
    } catch (IOException e) {
        throw new OperationException("get service-center instances fails", e);
    }
}
Also used : HttpResponse(org.apache.servicecomb.http.client.common.HttpResponse) IOException(java.io.IOException) OperationException(org.apache.servicecomb.service.center.client.exception.OperationException)

Example 13 with HttpResponse

use of org.apache.servicecomb.http.client.common.HttpResponse in project java-chassis by ServiceComb.

the class ServiceCenterClient method queryServiceId.

@Override
public RegisteredMicroserviceResponse queryServiceId(Microservice microservice) {
    try {
        URIBuilder uriBuilder = new URIBuilder("/registry/existence");
        uriBuilder.setParameter("type", "microservice");
        uriBuilder.setParameter("appId", microservice.getAppId());
        uriBuilder.setParameter("serviceName", microservice.getServiceName());
        uriBuilder.setParameter("version", microservice.getVersion());
        uriBuilder.setParameter("env", microservice.getEnvironment());
        HttpResponse response = httpClient.getHttpRequest(uriBuilder.build().toString(), null, null);
        if (response.getStatusCode() == HttpStatus.SC_OK) {
            return HttpUtils.deserialize(response.getContent(), RegisteredMicroserviceResponse.class);
        }
        sendUnAuthorizedEvent(response);
        LOGGER.info("Query serviceId fails, statusCode = " + response.getStatusCode() + "; message = " + response.getMessage() + "; content = " + response.getContent());
        return null;
    } catch (IOException e) {
        throw new OperationException("query serviceId fails", e);
    } catch (URISyntaxException e) {
        throw new OperationException("build url failed.", e);
    }
}
Also used : HttpResponse(org.apache.servicecomb.http.client.common.HttpResponse) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) OperationException(org.apache.servicecomb.service.center.client.exception.OperationException) URIBuilder(org.apache.http.client.utils.URIBuilder)

Example 14 with HttpResponse

use of org.apache.servicecomb.http.client.common.HttpResponse in project java-chassis by ServiceComb.

the class ServiceCenterClient method getMicroserviceInstance.

@Override
public MicroserviceInstance getMicroserviceInstance(String serviceId, String instanceId) {
    try {
        HttpResponse response = httpClient.getHttpRequest("/registry/microservices/" + serviceId + "/instances/" + instanceId, null, null);
        if (response.getStatusCode() == HttpStatus.SC_OK) {
            MicroserviceInstanceResponse instanceResponse = HttpUtils.deserialize(response.getContent(), MicroserviceInstanceResponse.class);
            return instanceResponse.getInstance();
        }
        sendUnAuthorizedEvent(response);
        throw new OperationException("get service instance message fails, statusCode = " + response.getStatusCode() + "; message = " + response.getMessage() + "; content = " + response.getContent());
    } catch (IOException e) {
        throw new OperationException("get service instance message fails", e);
    }
}
Also used : HttpResponse(org.apache.servicecomb.http.client.common.HttpResponse) IOException(java.io.IOException) OperationException(org.apache.servicecomb.service.center.client.exception.OperationException) RegisteredMicroserviceInstanceResponse(org.apache.servicecomb.service.center.client.model.RegisteredMicroserviceInstanceResponse) MicroserviceInstanceResponse(org.apache.servicecomb.service.center.client.model.MicroserviceInstanceResponse)

Example 15 with HttpResponse

use of org.apache.servicecomb.http.client.common.HttpResponse in project java-chassis by ServiceComb.

the class ServiceCenterClient method sendHeartBeat.

@Override
public boolean sendHeartBeat(String serviceId, String instanceId) {
    try {
        HttpResponse response = httpClient.putHttpRequest("/registry/microservices/" + serviceId + "/instances/" + instanceId + "/heartbeat", null, null);
        if (response.getStatusCode() == HttpStatus.SC_OK) {
            return true;
        }
        sendUnAuthorizedEvent(response);
        throw new OperationException("heartbeats fails, statusCode = " + response.getStatusCode() + "; message = " + response.getMessage() + "; content = " + response.getContent());
    } catch (IOException e) {
        throw new OperationException("heartbeats fails ", e);
    }
}
Also used : HttpResponse(org.apache.servicecomb.http.client.common.HttpResponse) IOException(java.io.IOException) OperationException(org.apache.servicecomb.service.center.client.exception.OperationException)

Aggregations

HttpResponse (org.apache.servicecomb.http.client.common.HttpResponse)37 IOException (java.io.IOException)21 OperationException (org.apache.servicecomb.service.center.client.exception.OperationException)19 Test (org.junit.Test)15 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)5 Microservice (org.apache.servicecomb.service.center.client.model.Microservice)4 HttpRequest (org.apache.servicecomb.http.client.common.HttpRequest)3 MicroserviceInstancesResponse (org.apache.servicecomb.service.center.client.model.MicroserviceInstancesResponse)3 RegisteredMicroserviceResponse (org.apache.servicecomb.service.center.client.model.RegisteredMicroserviceResponse)3 HashMap (java.util.HashMap)2 MicroserviceInstance (org.apache.servicecomb.service.center.client.model.MicroserviceInstance)2 RegisteredMicroserviceInstanceResponse (org.apache.servicecomb.service.center.client.model.RegisteredMicroserviceInstanceResponse)2 SchemaInfo (org.apache.servicecomb.service.center.client.model.SchemaInfo)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 EventBus (com.google.common.eventbus.EventBus)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 URIBuilder (org.apache.http.client.utils.URIBuilder)1 OperationException (org.apache.servicecomb.config.center.client.exception.OperationException)1