In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is the principle and function of RaftProxy in nacos". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what is the principle and function of RaftProxy in nacos"?
Order
This paper mainly studies the RaftProxy of nacos.
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 provides three methods: proxyGET, proxy and proxyPostLarge. Proxy receives HttpMethod and can handle GET, POST and 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) .setRetsdirectorEnabled (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") .getParameterByName () }} 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 ());}} / /.}
The connectionRequestTimeout of httpPost is 5 seconds, connectTimeout is 5 seconds, and socketTimeout is 5 seconds. HttpPostLarge sets connectionTimeToLive of 500ms.
Summary
RaftProxy provides three methods: proxyGET, proxy and proxyPostLarge. Proxy receives HttpMethod and can handle GET, POST and DELETE.
At this point, I believe you have a deeper understanding of "what is the principle and function of RaftProxy in nacos". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.