Search in sources :

Example 1 with QueryConfigurationsResponse

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

the class ConfigCenterClient method queryConfigurations.

@Override
public QueryConfigurationsResponse queryConfigurations(QueryConfigurationsRequest request) {
    String dimensionsInfo = buildDimensionsInfo(request, true);
    QueryConfigurationsResponse queryConfigurationsResponse = new QueryConfigurationsResponse();
    Map<String, Object> configurations = new HashMap<>();
    String uri = null;
    String address = addressManager.address();
    try {
        uri = address + "/configuration/items?dimensionsInfo=" + HttpUtils.encodeURLParam(dimensionsInfo) + "&revision=" + request.getRevision();
        Map<String, String> headers = new HashMap<>();
        headers.put("x-environment", request.getEnvironment());
        HttpRequest httpRequest = new HttpRequest(uri, headers, null, HttpRequest.GET);
        HttpResponse httpResponse = httpTransport.doRequest(httpRequest);
        if (httpResponse.getStatusCode() == HttpStatus.SC_OK) {
            Map<String, Map<String, Object>> allConfigMap = HttpUtils.deserialize(httpResponse.getContent(), new TypeReference<Map<String, Map<String, Object>>>() {
            });
            if (allConfigMap.get(REVISION) != null) {
                queryConfigurationsResponse.setRevision((String) allConfigMap.get(REVISION).get("version"));
            }
            if (allConfigMap.get(APPLICATION_CONFIG) != null) {
                configurations.putAll(allConfigMap.get(APPLICATION_CONFIG));
            }
            if (allConfigMap.get(buildDimensionsInfo(request, false)) != null) {
                configurations.putAll(allConfigMap.get(buildDimensionsInfo(request, false)));
            }
            if (allConfigMap.get(buildDarkLaunchDimensionsInfo(request)) != null) {
                configurations.putAll(allConfigMap.get(buildDarkLaunchDimensionsInfo(request)));
            }
            if (allConfigMap.get(dimensionsInfo) != null) {
                configurations.putAll(allConfigMap.get(dimensionsInfo));
            }
            queryConfigurationsResponse.setConfigurations(configurations);
            queryConfigurationsResponse.setChanged(true);
            addressManager.recordSuccessState(address);
            return queryConfigurationsResponse;
        } else if (httpResponse.getStatusCode() == HttpStatus.SC_NOT_MODIFIED) {
            queryConfigurationsResponse.setChanged(false);
            addressManager.recordSuccessState(address);
            return queryConfigurationsResponse;
        } else if (httpResponse.getStatusCode() == HttpStatus.SC_BAD_REQUEST) {
            throw new OperationException("Bad request for query configurations.");
        } else {
            addressManager.recordFailState(address);
            throw new OperationException("read response failed. status:" + httpResponse.getStatusCode() + "; message:" + httpResponse.getMessage() + "; content:" + httpResponse.getContent());
        }
    } catch (IOException e) {
        addressManager.recordFailState(address);
        LOGGER.error("query configuration from {} failed, message={}", uri, e.getMessage());
        throw new OperationException("", e);
    }
}
Also used : HttpRequest(org.apache.servicecomb.http.client.common.HttpRequest) QueryConfigurationsResponse(org.apache.servicecomb.config.center.client.model.QueryConfigurationsResponse) HashMap(java.util.HashMap) HttpResponse(org.apache.servicecomb.http.client.common.HttpResponse) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map) OperationException(org.apache.servicecomb.config.center.client.exception.OperationException)

Example 2 with QueryConfigurationsResponse

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

the class ConfigCenterConfigurationSourceImpl method firstPull.

private QueryConfigurationsRequest firstPull(ConfigCenterClient configCenterClient) {
    QueryConfigurationsRequest queryConfigurationsRequest = createQueryConfigurationsRequest();
    try {
        QueryConfigurationsResponse response = configCenterClient.queryConfigurations(queryConfigurationsRequest);
        if (response.isChanged()) {
            configConverter.updateData(response.getConfigurations());
            updateConfiguration(WatchedUpdateResult.createIncremental(configConverter.getCurrentData(), null, null));
            queryConfigurationsRequest.setRevision(response.getRevision());
        }
    } catch (Exception e) {
        if (ConfigCenterConfig.INSTANCE.firstPullRequired()) {
            throw e;
        }
        LOGGER.warn("first pull failed, and ignore {}", e.getMessage());
    }
    return queryConfigurationsRequest;
}
Also used : QueryConfigurationsResponse(org.apache.servicecomb.config.center.client.model.QueryConfigurationsResponse) QueryConfigurationsRequest(org.apache.servicecomb.config.center.client.model.QueryConfigurationsRequest)

Aggregations

QueryConfigurationsResponse (org.apache.servicecomb.config.center.client.model.QueryConfigurationsResponse)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 OperationException (org.apache.servicecomb.config.center.client.exception.OperationException)1 QueryConfigurationsRequest (org.apache.servicecomb.config.center.client.model.QueryConfigurationsRequest)1 HttpRequest (org.apache.servicecomb.http.client.common.HttpRequest)1 HttpResponse (org.apache.servicecomb.http.client.common.HttpResponse)1