Search in sources :

Example 1 with HttpClientConfig

use of com.alibaba.nacos.common.http.HttpClientConfig in project nacos by alibaba.

the class ServerHttpAgent method httpDelete.

@Override
public HttpRestResult<String> httpDelete(String path, Map<String, String> headers, Map<String, String> paramValues, String encode, long readTimeoutMs) throws Exception {
    final long endTime = System.currentTimeMillis() + readTimeoutMs;
    String currentServerAddr = serverListMgr.getCurrentServerAddr();
    int maxRetry = this.maxRetry;
    HttpClientConfig httpConfig = HttpClientConfig.builder().setReadTimeOutMillis(Long.valueOf(readTimeoutMs).intValue()).setConTimeOutMillis(ConfigHttpClientManager.getInstance().getConnectTimeoutOrDefault(100)).build();
    do {
        try {
            Header newHeaders = Header.newInstance();
            if (headers != null) {
                newHeaders.addAll(headers);
            }
            Query query = Query.newInstance().initParams(paramValues);
            HttpRestResult<String> result = NACOS_RESTTEMPLATE.delete(getUrl(currentServerAddr, path), httpConfig, newHeaders, query, String.class);
            if (isFail(result)) {
                LOGGER.error("[NACOS ConnectException] currentServerAddr: {}, httpCode: {}", serverListMgr.getCurrentServerAddr(), result.getCode());
            } else {
                // Update the currently available server addr
                serverListMgr.updateCurrentServerAddr(currentServerAddr);
                return result;
            }
        } catch (ConnectException connectException) {
            LOGGER.error("[NACOS ConnectException httpDelete] currentServerAddr:{}, err : {}", serverListMgr.getCurrentServerAddr(), ExceptionUtil.getStackTrace(connectException));
        } catch (SocketTimeoutException stoe) {
            LOGGER.error("[NACOS SocketTimeoutException httpDelete] currentServerAddr:{}, err : {}", serverListMgr.getCurrentServerAddr(), ExceptionUtil.getStackTrace(stoe));
        } catch (Exception ex) {
            LOGGER.error("[NACOS Exception httpDelete] currentServerAddr: " + serverListMgr.getCurrentServerAddr(), ex);
            throw ex;
        }
        if (serverListMgr.getIterator().hasNext()) {
            currentServerAddr = serverListMgr.getIterator().next();
        } else {
            maxRetry--;
            if (maxRetry < 0) {
                throw new ConnectException("[NACOS HTTP-DELETE] The maximum number of tolerable server reconnection errors has been reached");
            }
            serverListMgr.refreshCurrentServerAddr();
        }
    } while (System.currentTimeMillis() <= endTime);
    LOGGER.error("no available server");
    throw new ConnectException("no available server");
}
Also used : HttpClientConfig(com.alibaba.nacos.common.http.HttpClientConfig) SocketTimeoutException(java.net.SocketTimeoutException) Header(com.alibaba.nacos.common.http.param.Header) Query(com.alibaba.nacos.common.http.param.Query) SocketTimeoutException(java.net.SocketTimeoutException) NacosException(com.alibaba.nacos.api.exception.NacosException) ConnectException(java.net.ConnectException) ConnectException(java.net.ConnectException)

Example 2 with HttpClientConfig

use of com.alibaba.nacos.common.http.HttpClientConfig in project nacos by alibaba.

the class ServerHttpAgent method httpGet.

@Override
public HttpRestResult<String> httpGet(String path, Map<String, String> headers, Map<String, String> paramValues, String encode, long readTimeoutMs) throws Exception {
    final long endTime = System.currentTimeMillis() + readTimeoutMs;
    String currentServerAddr = serverListMgr.getCurrentServerAddr();
    int maxRetry = this.maxRetry;
    HttpClientConfig httpConfig = HttpClientConfig.builder().setReadTimeOutMillis(Long.valueOf(readTimeoutMs).intValue()).setConTimeOutMillis(ConfigHttpClientManager.getInstance().getConnectTimeoutOrDefault(100)).build();
    do {
        try {
            Header newHeaders = Header.newInstance();
            if (headers != null) {
                newHeaders.addAll(headers);
            }
            Query query = Query.newInstance().initParams(paramValues);
            HttpRestResult<String> result = NACOS_RESTTEMPLATE.get(getUrl(currentServerAddr, path), httpConfig, newHeaders, query, String.class);
            if (isFail(result)) {
                LOGGER.error("[NACOS ConnectException] currentServerAddr: {}, httpCode: {}", serverListMgr.getCurrentServerAddr(), result.getCode());
            } else {
                // Update the currently available server addr
                serverListMgr.updateCurrentServerAddr(currentServerAddr);
                return result;
            }
        } catch (ConnectException connectException) {
            LOGGER.error("[NACOS ConnectException httpGet] currentServerAddr:{}, err : {}", serverListMgr.getCurrentServerAddr(), connectException.getMessage());
        } catch (SocketTimeoutException socketTimeoutException) {
            LOGGER.error("[NACOS SocketTimeoutException httpGet] currentServerAddr:{}, err : {}", serverListMgr.getCurrentServerAddr(), socketTimeoutException.getMessage());
        } catch (Exception ex) {
            LOGGER.error("[NACOS Exception httpGet] currentServerAddr: " + serverListMgr.getCurrentServerAddr(), ex);
            throw ex;
        }
        if (serverListMgr.getIterator().hasNext()) {
            currentServerAddr = serverListMgr.getIterator().next();
        } else {
            maxRetry--;
            if (maxRetry < 0) {
                throw new ConnectException("[NACOS HTTP-GET] The maximum number of tolerable server reconnection errors has been reached");
            }
            serverListMgr.refreshCurrentServerAddr();
        }
    } while (System.currentTimeMillis() <= endTime);
    LOGGER.error("no available server");
    throw new ConnectException("no available server");
}
Also used : HttpClientConfig(com.alibaba.nacos.common.http.HttpClientConfig) SocketTimeoutException(java.net.SocketTimeoutException) Header(com.alibaba.nacos.common.http.param.Header) Query(com.alibaba.nacos.common.http.param.Query) SocketTimeoutException(java.net.SocketTimeoutException) NacosException(com.alibaba.nacos.api.exception.NacosException) ConnectException(java.net.ConnectException) ConnectException(java.net.ConnectException)

Example 3 with HttpClientConfig

use of com.alibaba.nacos.common.http.HttpClientConfig in project nacos by alibaba.

the class HttpClient method httpPost.

/**
 * Request http post method.
 *
 * @param url         url
 * @param headers     headers
 * @param paramValues params
 * @param encoding    charset
 * @return {@link RestResult} as response
 */
public static RestResult<String> httpPost(String url, List<String> headers, Map<String, String> paramValues, String encoding) {
    try {
        Header header = Header.newInstance();
        if (CollectionUtils.isNotEmpty(headers)) {
            header.addAll(headers);
        }
        header.addParam(HttpHeaderConsts.ACCEPT_CHARSET, encoding);
        AuthHeaderUtil.addIdentityToHeader(header);
        HttpClientConfig httpClientConfig = HttpClientConfig.builder().setConTimeOutMillis(5000).setReadTimeOutMillis(5000).build();
        return APACHE_SYNC_NACOS_REST_TEMPLATE.postForm(url, httpClientConfig, header, paramValues, String.class);
    } catch (Throwable e) {
        return RestResult.<String>builder().withCode(500).withMsg(e.toString()).build();
    }
}
Also used : HttpClientConfig(com.alibaba.nacos.common.http.HttpClientConfig) Header(com.alibaba.nacos.common.http.param.Header)

Example 4 with HttpClientConfig

use of com.alibaba.nacos.common.http.HttpClientConfig in project nacos by alibaba.

the class HttpClient method request.

/**
 * Do http request.
 *
 * @param url            request url
 * @param headers        request headers
 * @param paramValues    request params
 * @param body           request body
 * @param connectTimeout timeout of connection
 * @param readTimeout    timeout of request
 * @param encoding       charset of request
 * @param method         http method
 * @return {@link RestResult} as response
 */
public static RestResult<String> request(String url, List<String> headers, Map<String, String> paramValues, String body, int connectTimeout, int readTimeout, String encoding, String method) {
    Header header = Header.newInstance();
    if (CollectionUtils.isNotEmpty(headers)) {
        header.addAll(headers);
    }
    header.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
    header.addParam(HttpHeaderConsts.CLIENT_VERSION_HEADER, VersionUtils.version);
    header.addParam(HttpHeaderConsts.USER_AGENT_HEADER, UtilsAndCommons.SERVER_VERSION);
    header.addParam(HttpHeaderConsts.REQUEST_SOURCE_HEADER, EnvUtil.getLocalAddress());
    header.addParam(HttpHeaderConsts.ACCEPT_CHARSET, encoding);
    AuthHeaderUtil.addIdentityToHeader(header);
    HttpClientConfig httpClientConfig = HttpClientConfig.builder().setConTimeOutMillis(connectTimeout).setReadTimeOutMillis(readTimeout).build();
    Query query = Query.newInstance().initParams(paramValues);
    query.addParam(FieldsConstants.ENCODING, ENCODING);
    query.addParam(FieldsConstants.NOFIX, NOFIX);
    try {
        return APACHE_SYNC_NACOS_REST_TEMPLATE.exchange(url, httpClientConfig, header, query, body, method, String.class);
    } catch (Exception e) {
        Loggers.SRV_LOG.warn("Exception while request: {}, caused: {}", url, e);
        return RestResult.<String>builder().withCode(500).withMsg(e.toString()).build();
    }
}
Also used : HttpClientConfig(com.alibaba.nacos.common.http.HttpClientConfig) Header(com.alibaba.nacos.common.http.param.Header) Query(com.alibaba.nacos.common.http.param.Query)

Example 5 with HttpClientConfig

use of com.alibaba.nacos.common.http.HttpClientConfig in project nacos by alibaba.

the class ServerHttpAgent method httpPost.

@Override
public HttpRestResult<String> httpPost(String path, Map<String, String> headers, Map<String, String> paramValues, String encode, long readTimeoutMs) throws Exception {
    final long endTime = System.currentTimeMillis() + readTimeoutMs;
    String currentServerAddr = serverListMgr.getCurrentServerAddr();
    int maxRetry = this.maxRetry;
    HttpClientConfig httpConfig = HttpClientConfig.builder().setReadTimeOutMillis(Long.valueOf(readTimeoutMs).intValue()).setConTimeOutMillis(ConfigHttpClientManager.getInstance().getConnectTimeoutOrDefault(3000)).build();
    do {
        try {
            Header newHeaders = Header.newInstance();
            if (headers != null) {
                newHeaders.addAll(headers);
            }
            HttpRestResult<String> result = NACOS_RESTTEMPLATE.postForm(getUrl(currentServerAddr, path), httpConfig, newHeaders, paramValues, String.class);
            if (isFail(result)) {
                LOGGER.error("[NACOS ConnectException] currentServerAddr: {}, httpCode: {}", currentServerAddr, result.getCode());
            } else {
                // Update the currently available server addr
                serverListMgr.updateCurrentServerAddr(currentServerAddr);
                return result;
            }
        } catch (ConnectException connectException) {
            LOGGER.error("[NACOS ConnectException httpPost] currentServerAddr: {}, err : {}", currentServerAddr, connectException.getMessage());
        } catch (SocketTimeoutException socketTimeoutException) {
            LOGGER.error("[NACOS SocketTimeoutException httpPost] currentServerAddr: {}, err : {}", currentServerAddr, socketTimeoutException.getMessage());
        } catch (Exception ex) {
            LOGGER.error("[NACOS Exception httpPost] currentServerAddr: " + currentServerAddr, ex);
            throw ex;
        }
        if (serverListMgr.getIterator().hasNext()) {
            currentServerAddr = serverListMgr.getIterator().next();
        } else {
            maxRetry--;
            if (maxRetry < 0) {
                throw new ConnectException("[NACOS HTTP-POST] The maximum number of tolerable server reconnection errors has been reached");
            }
            serverListMgr.refreshCurrentServerAddr();
        }
    } while (System.currentTimeMillis() <= endTime);
    LOGGER.error("no available server, currentServerAddr : {}", currentServerAddr);
    throw new ConnectException("no available server, currentServerAddr : " + currentServerAddr);
}
Also used : HttpClientConfig(com.alibaba.nacos.common.http.HttpClientConfig) SocketTimeoutException(java.net.SocketTimeoutException) Header(com.alibaba.nacos.common.http.param.Header) SocketTimeoutException(java.net.SocketTimeoutException) NacosException(com.alibaba.nacos.api.exception.NacosException) ConnectException(java.net.ConnectException) ConnectException(java.net.ConnectException)

Aggregations

HttpClientConfig (com.alibaba.nacos.common.http.HttpClientConfig)5 Header (com.alibaba.nacos.common.http.param.Header)5 NacosException (com.alibaba.nacos.api.exception.NacosException)3 Query (com.alibaba.nacos.common.http.param.Query)3 ConnectException (java.net.ConnectException)3 SocketTimeoutException (java.net.SocketTimeoutException)3