nacos中DistroFilter的作用是什么
本篇文章為大家展示了nacos中DistroFilter的作用是什么,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
CanDistro
nacos-1.1.3/naming/src/main/java/com/alibaba/nacos/naming/web/CanDistro.java
@Retention(RetentionPolicy.RUNTIME)
public@interfaceCanDistro{
}
-
CanDistro用于標識一個方法需要判斷是否應該根據distro被重定向
DistroFilter
nacos-1.1.3/naming/src/main/java/com/alibaba/nacos/naming/web/DistroFilter.java
publicclassDistroFilterimplementsFilter{
privatestaticfinalintPROXY_CONNECT_TIMEOUT=2000;
privatestaticfinalintPROXY_READ_TIMEOUT=2000;
@Autowired
privateDistroMapperdistroMapper;
@Autowired
privateSwitchDomainswitchDomain;
@Autowired
privateFilterBasefilterBase;
@Override
publicvoidinit(FilterConfigfilterConfig)throwsServletException{
}
@Override
publicvoiddoFilter(ServletRequestservletRequest,ServletResponseservletResponse,FilterChainfilterChain)throwsIOException,ServletException{
HttpServletRequestreq=(HttpServletRequest)servletRequest;
HttpServletResponseresp=(HttpServletResponse)servletResponse;
StringurlString=req.getRequestURI();
if(StringUtils.isNotBlank(req.getQueryString())){
urlString+="?"+req.getQueryString();
}
try{
Stringpath=newURI(req.getRequestURI()).getPath();
StringserviceName=req.getParameter(CommonParams.SERVICE_NAME);
//Forclientunder0.8.0:
if(StringUtils.isBlank(serviceName)){
serviceName=req.getParameter("dom");
}
Methodmethod=filterBase.getMethod(req.getMethod(),path);
if(method==null){
thrownewNoSuchMethodException(req.getMethod()+""+path);
}
StringgroupName=req.getParameter(CommonParams.GROUP_NAME);
if(StringUtils.isBlank(groupName)){
groupName=Constants.DEFAULT_GROUP;
}
//usegroupName@@serviceNameasnewservicename:
StringgroupedServiceName=serviceName;
if(StringUtils.isNotBlank(serviceName)&&!serviceName.contains(Constants.SERVICE_INFO_SPLITER)){
groupedServiceName=groupName+Constants.SERVICE_INFO_SPLITER+serviceName;
}
//proxyrequesttootherserverifnecessary:
if(method.isAnnotationPresent(CanDistro.class)&&!distroMapper.responsible(groupedServiceName)){
StringuserAgent=req.getHeader("User-Agent");
if(StringUtils.isNotBlank(userAgent)&&userAgent.contains(UtilsAndCommons.NACOS_SERVER_HEADER)){
//Thisrequestissentfrompeerserver,shouldnotberedirectedagain:
Loggers.SRV_LOG.error("receiveinvalidredirectrequestfrompeer{}",req.getRemoteAddr());
resp.sendError(HttpServletResponse.SC_BAD_REQUEST,
"receiveinvalidredirectrequestfrompeer"+req.getRemoteAddr());
return;
}
List<String>headerList=newArrayList<>(16);
Enumeration<String>headers=req.getHeaderNames();
while(headers.hasMoreElements()){
StringheaderName=headers.nextElement();
headerList.add(headerName);
headerList.add(req.getHeader(headerName));
}
HttpClient.HttpResultresult=
HttpClient.request("http://"+distroMapper.mapSrv(groupedServiceName)+urlString,headerList,
StringUtils.isBlank(req.getQueryString())?HttpClient.translateParameterMap(req.getParameterMap()):newHashMap<>(2)
,PROXY_CONNECT_TIMEOUT,PROXY_READ_TIMEOUT,"UTF-8",req.getMethod());
try{
resp.setCharacterEncoding("UTF-8");
resp.getWriter().write(result.content);
resp.setStatus(result.code);
}catch(Exceptionignore){
Loggers.SRV_LOG.warn("[DISTRO-FILTER]requestfailed:"+distroMapper.mapSrv(groupedServiceName)+urlString);
}
return;
}
OverrideParameterRequestWrapperrequestWrapper=OverrideParameterRequestWrapper.buildRequest(req);
requestWrapper.addParameter(CommonParams.SERVICE_NAME,groupedServiceName);
filterChain.doFilter(requestWrapper,resp);
}catch(AccessControlExceptione){
resp.sendError(HttpServletResponse.SC_FORBIDDEN,"accessdenied:"+UtilsAndCommons.getAllExceptionMsg(e));
return;
}catch(NoSuchMethodExceptione){
resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED,"nosuchapi:"+e.getMessage());
return;
}catch(Exceptione){
resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
"Serverfailed,"+UtilsAndCommons.getAllExceptionMsg(e));
return;
}
}
@Override
publicvoiddestroy(){
}
}
-
DistroFilter實現了servlet的Filter接口;其doFilter方法會從servletRequest中讀取serviceName、method、groupName等,然后判斷method是否標注CanDistro,如果是而且distroMapper不負責該service則構建http請求然后將結果寫回Filter;如果不需要重定向則繼續filterChain.doFilter
HttpClient.request
nacos-1.1.3/naming/src/main/java/com/alibaba/nacos/naming/misc/HttpClient.java
publicclassHttpClient{
privatestaticfinalintTIME_OUT_MILLIS=10000;
privatestaticfinalintCON_TIME_OUT_MILLIS=5000;
privatestaticAsyncHttpClientasyncHttpClient;
privatestaticCloseableHttpClientpostClient;
//......
publicstaticHttpResultrequest(Stringurl,List<String>headers,Map<String,String>paramValues,intconnectTimeout,intreadTimeout,Stringencoding,Stringmethod){
HttpURLConnectionconn=null;
try{
StringencodedContent=encodingParams(paramValues,encoding);
url+=(null==encodedContent)?"":("?"+encodedContent);
conn=(HttpURLConnection)newURL(url).openConnection();
conn.setConnectTimeout(connectTimeout);
conn.setReadTimeout(readTimeout);
conn.setRequestMethod(method);
conn.addRequestProperty("Client-Version",UtilsAndCommons.SERVER_VERSION);
conn.addRequestProperty("User-Agent",UtilsAndCommons.SERVER_VERSION);
setHeaders(conn,headers,encoding);
conn.connect();
returngetResult(conn);
}catch(Exceptione){
Loggers.SRV_LOG.warn("Exceptionwhilerequest:{},caused:{}",url,e);
returnnewHttpResult(500,e.toString(),Collections.<String,String>emptyMap());
}finally{
if(conn!=null){
conn.disconnect();
}
}
}
privatestaticHttpResultgetResult(HttpURLConnectionconn)throwsIOException{
intrespCode=conn.getResponseCode();
InputStreaminputStream;
if(HttpURLConnection.HTTP_OK==respCode){
inputStream=conn.getInputStream();
}else{
inputStream=conn.getErrorStream();
}
Map<String,String>respHeaders=newHashMap<String,String>(conn.getHeaderFields().size());
for(Map.Entry<String,List<String>>entry:conn.getHeaderFields().entrySet()){
respHeaders.put(entry.getKey(),entry.getValue().get(0));
}
StringgzipEncoding="gzip";
if(gzipEncoding.equals(respHeaders.get(HttpHeaders.CONTENT_ENCODING))){
inputStream=newGZIPInputStream(inputStream);
}
HttpResultresult=newHttpResult(respCode,IOUtils.toString(inputStream,getCharset(conn)),respHeaders);
inputStream.close();
returnresult;
}
publicstaticclassHttpResult{
finalpublicintcode;
finalpublicStringcontent;
finalprivateMap<String,String>respHeaders;
publicHttpResult(intcode,Stringcontent,Map<String,String>respHeaders){
this.code=code;
this.content=content;
this.respHeaders=respHeaders;
}
publicStringgetHeader(Stringname){
returnrespHeaders.get(name);
}
}
//......
}
-
HttpClient的request方法直接使用jdk的HttpURLConnection進行請求,返回結果封裝為HttpResult,其content即為響應的body
小結
DistroFilter實現了servlet的Filter接口;其doFilter方法會從servletRequest中讀取serviceName、method、groupName等,然后判斷method是否標注CanDistro,如果是而且distroMapper不負責該service則構建http請求然后將結果寫回Filter;如果不需要重定向則繼續filterChain.doFilter
總結
以上是生活随笔為你收集整理的nacos中DistroFilter的作用是什么的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 什么是云服务器ECS?云服务器ECS详解
- 下一篇: locker server启动