In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how Spring Cloud developers to solve service conflicts and examples of rampant, I believe that most people do not know much, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
I. background
In this article, "example Analysis of Spring Cloud developers solving Service conflicts and instance rampant", it is mentioned that service metadata is used to achieve isolation and routing. A friend asked whether it could be achieved directly through IP. In this article, let's discuss this problem with you.
II. Feasibility analysis
In order to achieve isolation and routing through IP, there is a very key point that needs to be solved, that is, how to achieve IP recognition, which means how to distinguish which IP is on the server and which IP is native to the developer.
As shown in the picture above, we can still find patterns that can be identified, so this can be done!
Developer native IP-is actually the client IP, that is, the IP:172.16.20.2 of the original requestor
Server IP-can be understood as the IP of the machine where the service on the server resides: 172.16.20.1
Third, routing rule logic
The main goals are as follows:
When an ordinary user visits a page on the server, all the requested routes only call the instance on the server
When developing An access, all requested routes call the instance started locally by Development A first, and if not, the instance on the server is called.
When developing B access is the same as above, all requested routes have priority to call the instance started locally by developer B, and if not, the instance on the server will be called.
After finding the identification rules of IP, the following three routing rules are derived to achieve the above goals.
Give priority to matching the service instance of the original requester's IP
Furthermore, it matches the service instance of the machine IP where the upstream service resides.
If neither of the above logic matches, use polling to find an instance.
I will not describe in detail how to write specific custom load balancer objects here. You can refer to my previous article, "example Analysis of Spring Cloud developers solving Service conflicts and instance rampant"
4. Obtain the IP of the original requestor
The code snippet to get the original IP is as follows. You just need to add a filter to the gateway to get the IP, and then add it to the header and pass it on.
/ * obtain Ip address * / private String getIpAddr (HttpServletRequest request) {String ip = request.getHeader ("X-Forwarded-For"); if (isEmptyIP (ip)) {ip = request.getHeader ("Proxy-Client-IP"); if (isEmptyIP (ip)) {ip = request.getHeader ("WL-Proxy-Client-IP") If (isEmptyIP (ip)) {ip = request.getHeader ("HTTP_CLIENT_IP"); if (isEmptyIP (ip)) {ip = request.getHeader ("HTTP_X_FORWARDED_FOR"); if (isEmptyIP (ip)) {ip = request.getRemoteAddr () If ("127.0.0.1" .equals (ip) | | "0IP try {ip = InetAddress.getLocalHost (). GetHostAddress ()) configured locally according to the network card. } catch (UnknownHostException e) {log.error ("InetAddress.getLocalHost ()-error", e) } else if (ip.length () > 15) {String [] ips = ip.split (","); for (int index = 0; index < ips.length; index++) {String strIp = ips [index] If (! isEmptyIP (ip)) {ip = strIp; break;}} return ip;} private boolean isEmptyIP (String ip) {if (StrUtil.isEmpty (ip) | | UNKNOWN_STR.equalsIgnoreCase (ip)) {return true;} return false;}
Add the original IP to the HTTP_X_FORWARDED_FOR of header and pass it to the downstream service
RequestContext ctx = RequestContext.getCurrentContext (); HttpServletRequest request = ctx.getRequest (); String sourceIp = getIpAddr (request); ctx.getZuulRequestHeaders (). Put ("HTTP_X_FORWARDED_FOR", sourceIp); 5. Get the IP of the machine where the server resides.
Just use the InetAddress that comes with JDK.
String localIp = InetAddress.getLocalHost (). GetHostAddress () these are all the contents of the article "how Spring Cloud developers resolve service conflicts and instances". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.