Categories
Uncategorized

How Spring Cloud service developers to resolve conflicts and instances scurrying? (IP implementations)

I. Background

In my last article “How to Spring Cloud service developers to resolve conflicts and instances scurrying? “Mentioned metadata using the service to achieve isolation and routing, a friend asked can not be directly implemented by IP? This article and everyone will work together to discuss this issue

 

Second, feasibility analysis

To achieve to isolate over IP and routing, then there is a very critical point to be resolved is how to achieve IP identifiable, which means how to distinguish that IP is on the server, the IP is the developer of the machine

As shown above fact, we can still find the law can be identified, so this is the line!

    Developers Native IP – in fact, client IP, which is the original requester’s IP: 172.16.20.2

    Server IP – IP services can be understood as the machine where the server (around a bit): 172.16.20.1

 

Third, the routing logic rules

To achieve the following objectives:

    When ordinary users to access pages on the server, the server instances on all routes requests only call

    A developing access, priority call routing request all examples of development A native boot, if there is no instance on the server is called

    Above, all requests priority call routing to develop this instance B-initiated the development of B access, if there is no instance on the server is called

After finding the identification of IP law, deduced the following three routing rules to achieve the above goals

    Priority service instance matching the original requester’s IP

    Further examples of services matching service is located upstream of the machine IP

    Polling manner above two logic then not find a matching example

How specific custom load balancing objects I write here is not described in detail, you can refer to my previous article “How to Spring Cloud service developers to resolve conflicts and instances scurrying? “

 

Fourth, to obtain the IP of the original requester

Get original IP code fragment below, only you need to add a filter on the acquired IP gateway, and then added to the header which it has been passed down

/**
 * 获取Ip地址
 */
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) || "0:0:0:0:0:0:0:1".equals(ip)) {
                                // 根据网卡取本机配置的IP
                                try {
                                    ip = InetAddress.getLocalHost().getHostAddress();
                                } 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;
}

Added to the original IP header which is transmitted to the downstream services HTTP_X_FORWARDED_FOR

RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
String sourceIp = getIpAddr(request);
ctx.getZuulRequestHeaders().put("HTTP_X_FORWARDED_FOR", sourceIp);

 

Fifth, to obtain the IP server machine

JDK comes directly InetAddress on it

String localIp = InetAddress.getLocalHost().getHostAddress()

 

VI Summary

After the service instance development environment to achieve isolation, and policy routing over IP solutions can be implemented to develop completely aware, does not require configuration metadata, you do not need to preach their own version of the class of the argument.
    But this program actually has limitations

    Development server must be using only one to deploy all the services, because if the service upstream and downstream services not on the same IP will lose the ability to identify

    Because the network environment is complex, the client may not be able to get the real original IP

    Developers start the client / front end of the machine and start the background service job must be on the same computer; for example, if a front-end developer A boot-enabled client, backstage developers to debug B started service to die, because the original IP Sign up with a service instance does not match the IP

Finally, you may ask how the whole original IP link to pass along? Link transfer can refer to difficulties in my other article, "the log for troubleshooting? Distributed link tracking log to help you. "

 

Recommended Reading

    zuul latest Sentinel integrated gateway flow control assembly

    Ali registry Nacos production deployment

    Spring Boot custom configuration item inside the IDE automatically prompted

    How dynamic routing Spring Cloud Zuul do? Nacos very simple to achieve integration

Scan code attention has a surprise!

Leave a Reply