本篇內(nèi)容主要講解“nacos中RaftProxy的原理和作用是什么”,感興趣的朋友不妨來看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“nacos中RaftProxy的原理和作用是什么”吧!
創(chuàng)新互聯(lián)公司服務(wù)項(xiàng)目包括長(zhǎng)治網(wǎng)站建設(shè)、長(zhǎng)治網(wǎng)站制作、長(zhǎng)治網(wǎng)頁制作以及長(zhǎng)治網(wǎng)絡(luò)營(yíng)銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,長(zhǎng)治網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到長(zhǎng)治省份的部分城市,未來相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
本文主要研究一下nacos的RaftProxy
nacos-1.1.3/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftProxy.java
@Component public class RaftProxy { public void proxyGET(String server, String api, Mapparams) throws Exception { // do proxy if (!server.contains(UtilsAndCommons.IP_PORT_SPLITER)) { server = server + UtilsAndCommons.IP_PORT_SPLITER + RunningConfig.getServerPort(); } String url = "http://" + server + RunningConfig.getContextPath() + api; HttpClient.HttpResult result = HttpClient.httpGet(url, null, params); if (result.code != HttpURLConnection.HTTP_OK) { throw new IllegalStateException("leader failed, caused by: " + result.content); } } public void proxy(String server, String api, Map params, HttpMethod method) throws Exception { // do proxy if (!server.contains(UtilsAndCommons.IP_PORT_SPLITER)) { server = server + UtilsAndCommons.IP_PORT_SPLITER + RunningConfig.getServerPort(); } String url = "http://" + server + RunningConfig.getContextPath() + api; HttpClient.HttpResult result; switch (method) { case GET: result = HttpClient.httpGet(url, null, params); break; case POST: result = HttpClient.httpPost(url, null, params); break; case DELETE: result = HttpClient.httpDelete(url, null, params); break; default: throw new RuntimeException("unsupported method:" + method); } if (result.code != HttpURLConnection.HTTP_OK) { throw new IllegalStateException("leader failed, caused by: " + result.content); } } public void proxyPostLarge(String server, String api, String content, Map headers) throws Exception { // do proxy if (!server.contains(UtilsAndCommons.IP_PORT_SPLITER)) { server = server + UtilsAndCommons.IP_PORT_SPLITER + RunningConfig.getServerPort(); } String url = "http://" + server + RunningConfig.getContextPath() + api; HttpClient.HttpResult result = HttpClient.httpPostLarge(url, headers, content); if (result.code != HttpURLConnection.HTTP_OK) { throw new IllegalStateException("leader failed, caused by: " + result.content); } } }
RaftProxy提供了proxyGET、proxy、proxyPostLarge三個(gè)方法,其中proxy接收了HttpMethod,可以處理GET、POST、DELETE
nacos-1.1.3/naming/src/main/java/com/alibaba/nacos/naming/misc/HttpClient.java
public class HttpClient { private static final int TIME_OUT_MILLIS = 10000; private static final int CON_TIME_OUT_MILLIS = 5000; private static AsyncHttpClient asyncHttpClient; private static CloseableHttpClient postClient; static { AsyncHttpClientConfig.Builder builder = new AsyncHttpClientConfig.Builder(); builder.setMaximumConnectionsTotal(-1); builder.setMaximumConnectionsPerHost(128); builder.setAllowPoolingConnection(true); builder.setFollowRedirects(false); builder.setIdleConnectionTimeoutInMs(TIME_OUT_MILLIS); builder.setConnectionTimeoutInMs(CON_TIME_OUT_MILLIS); builder.setCompressionEnabled(true); builder.setIOThreadMultiplier(1); builder.setMaxRequestRetry(0); builder.setUserAgent(UtilsAndCommons.SERVER_VERSION); asyncHttpClient = new AsyncHttpClient(builder.build()); HttpClientBuilder builder2 = HttpClients.custom(); builder2.setUserAgent(UtilsAndCommons.SERVER_VERSION); builder2.setConnectionTimeToLive(CON_TIME_OUT_MILLIS, TimeUnit.MILLISECONDS); builder2.setMaxConnPerRoute(-1); builder2.setMaxConnTotal(-1); builder2.disableAutomaticRetries(); postClient = builder2.build(); } //...... public static HttpResult httpPost(String url, Listheaders, Map paramValues) { return httpPost(url, headers, paramValues, "UTF-8"); } public static HttpResult httpPost(String url, List headers, Map paramValues, String encoding) { try { HttpPost httpost = new HttpPost(url); RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(5000).setConnectTimeout(5000).setSocketTimeout(5000).setRedirectsEnabled(true).setMaxRedirects(5).build(); httpost.setConfig(requestConfig); List nvps = new ArrayList (); for (Map.Entry entry : paramValues.entrySet()) { nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } httpost.setEntity(new UrlEncodedFormEntity(nvps, encoding)); HttpResponse response = postClient.execute(httpost); HttpEntity entity = response.getEntity(); String charset = encoding; if (entity.getContentType() != null) { HeaderElement[] headerElements = entity.getContentType().getElements(); if (headerElements != null && headerElements.length > 0 && headerElements[0] != null && headerElements[0].getParameterByName("charset") != null) { charset = headerElements[0].getParameterByName("charset").getValue(); } } return new HttpResult(response.getStatusLine().getStatusCode(), IOUtils.toString(entity.getContent(), charset), Collections. emptyMap()); } catch (Throwable e) { return new HttpResult(500, e.toString(), Collections. emptyMap()); } } public static HttpResult httpPostLarge(String url, Map headers, String content) { try { HttpClientBuilder builder = HttpClients.custom(); builder.setUserAgent(UtilsAndCommons.SERVER_VERSION); builder.setConnectionTimeToLive(500, TimeUnit.MILLISECONDS); CloseableHttpClient httpClient = builder.build(); HttpPost httpost = new HttpPost(url); for (Map.Entry entry : headers.entrySet()) { httpost.setHeader(entry.getKey(), entry.getValue()); } httpost.setEntity(new StringEntity(content, ContentType.create("application/json", "UTF-8"))); HttpResponse response = httpClient.execute(httpost); HttpEntity entity = response.getEntity(); HeaderElement[] headerElements = entity.getContentType().getElements(); String charset = headerElements[0].getParameterByName("charset").getValue(); return new HttpResult(response.getStatusLine().getStatusCode(), IOUtils.toString(entity.getContent(), charset), Collections. emptyMap()); } catch (Exception e) { return new HttpResult(500, e.toString(), Collections. emptyMap()); } } //...... }
httpPost的connectionRequestTimeout為5秒,connectTimeout為5秒,socketTimeout為5秒;httpPostLarge設(shè)置了500毫秒的connectionTimeToLive
RaftProxy提供了proxyGET、proxy、proxyPostLarge三個(gè)方法,其中proxy接收了HttpMethod,可以處理GET、POST、DELETE
到此,相信大家對(duì)“nacos中RaftProxy的原理和作用是什么”有了更深的了解,不妨來實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!