nacos中RaftProxy的原理和作用是什么
发表于:2025-12-03 作者:千家信息网编辑
千家信息网最后更新 2025年12月03日,本篇内容主要讲解"nacos中RaftProxy的原理和作用是什么",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"nacos中RaftProxy的原理和作用
千家信息网最后更新 2025年12月03日nacos中RaftProxy的原理和作用是什么
本篇内容主要讲解"nacos中RaftProxy的原理和作用是什么",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"nacos中RaftProxy的原理和作用是什么"吧!
序
本文主要研究一下nacos的RaftProxy
RaftProxy
nacos-1.1.3/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftProxy.java
@Componentpublic class RaftProxy { public void proxyGET(String server, String api, Map params) 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三个方法,其中proxy接收了HttpMethod,可以处理GET、POST、DELETE
HttpClient
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, List headers, 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设置了500毫秒的connectionTimeToLive
小结
RaftProxy提供了proxyGET、proxy、proxyPostLarge三个方法,其中proxy接收了HttpMethod,可以处理GET、POST、DELETE
到此,相信大家对"nacos中RaftProxy的原理和作用是什么"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
作用
原理
方法
三个
内容
UTF-8
处理
学习
实用
更深
兴趣
实用性
实际
小结
操作简单
更多
朋友
网站
频道
查询
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
论网络安全新技术
服务器是别人做的 怎么防护
工商银行软件开发中心和腾讯
sql语句中备份数据库表
和平精英咋退出服务器
和平精英竞赛服务器没有反应
nr数据库代表什么
互联网根服务器大小
软件开发用什么语言编写
东营市网络安全管控
sql数据库日志文件满了
数据库查询表数据个数
完美连接服务器次数过多禁止进入
钉钉网络安全认证证书号是多少
pc服务器价格3万左右
数据库原理与应用李玲玲
软件开发科技公司 党建
英文网络安全词汇
维普期刊全文数据库
腾讯客服电话服务器问题
网络安全知识竞赛活动
海南超频服务器联系方式
软件开发王老师
黄浦区第三方软件开发诚信经营
河北广兆网络技术有限公司
软件开发会议管理
世界反兴奋剂机构数据库
网络技术的在校目标
小公司软件开发能力哪来的
初中网络安全知识答题