Initial code import
[msb/apigateway.git] / apiroute / apiroute-service / src / main / java / org / openo / msb / wrapper / util / MicroServiceUtil.java
diff --git a/apiroute/apiroute-service/src/main/java/org/openo/msb/wrapper/util/MicroServiceUtil.java b/apiroute/apiroute-service/src/main/java/org/openo/msb/wrapper/util/MicroServiceUtil.java
new file mode 100644 (file)
index 0000000..67fd60f
--- /dev/null
@@ -0,0 +1,96 @@
+/**\r
+* Copyright (C) 2016 ZTE, Inc. and others. All rights reserved. (ZTE)\r
+*\r
+* Licensed under the Apache License, Version 2.0 (the "License");\r
+* you may not use this file except in compliance with the License.\r
+* You may obtain a copy of the License at\r
+*\r
+* http://www.apache.org/licenses/LICENSE-2.0\r
+*\r
+* Unless required by applicable law or agreed to in writing, software\r
+* distributed under the License is distributed on an "AS IS" BASIS,\r
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+* See the License for the specific language governing permissions and\r
+* limitations under the License.\r
+*/\r
+\r
+package org.openo.msb.wrapper.util;\r
+\r
+import java.util.regex.Pattern;\r
+\r
+import javax.servlet.http.HttpServletRequest;\r
+\r
+import org.apache.commons.lang3.StringUtils;\r
+\r
+\r
+public class MicroServiceUtil {\r
+    public static final String PREFIX_PATH = "discover:microservices";\r
+    \r
+    public static final String PREFIX_PATH_PORT = "discover:";\r
+\r
+    public static final String SUFFIX_PATH_INFO = "info";\r
+\r
+    public static final String REDIS_KEY_PATTERN =\r
+            "discover:microservices:(?<servicename>[^:]+)(:(?<version>[^:]*))?:info";\r
+\r
+    public static final String REQUEST_SUCCESS = "SUCCESS";\r
+\r
+    public static final String REQUEST_FAIL = "FAIL";\r
+\r
+    public static final String ROUTE_PATH_LOADBALANCE = "lb"; // 负载均衡路径名\r
+\r
+   \r
+    public static String getPrefixedKey(String... paths) {\r
+        StringBuffer sb = new StringBuffer();\r
+        \r
+        if(paths[0].trim().equals("") || paths[0].equals(String.valueOf(JedisUtil.serverPort))){\r
+            sb.append(PREFIX_PATH);\r
+        }\r
+        else{\r
+           sb.append(PREFIX_PATH_PORT).append(paths[0]); \r
+        }\r
+        \r
+        for (int i = 1; i < paths.length; i++) {\r
+            sb.append(":");\r
+            sb.append(paths[i]);\r
+        }\r
+        return sb.toString();\r
+    }\r
+\r
+    \r
+    public static String getServiceInfoKey(String serverPort,String serviceName, String version) {\r
+        return getPrefixedKey(serverPort,serviceName, version, SUFFIX_PATH_INFO);\r
+    }\r
+\r
\r
+    \r
+    \r
+    \r
+    public static Pattern getRedisKeyPattern() {\r
+        return Pattern.compile(REDIS_KEY_PATTERN);\r
+    }\r
+\r
+    public static String getRealIp(HttpServletRequest request) {\r
+        String ip = request.getHeader("X-Forwarded-For");\r
+        if (StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)) {\r
+            // After the reverse proxy can have multiple IP value for many times, the first IP is the real IP\r
+            int index = ip.indexOf(",");\r
+            if (index != -1) {\r
+                return ip.substring(0, index);\r
+            } else {\r
+                return ip;\r
+            }\r
+        }\r
+        ip = request.getHeader("X-Real-IP");\r
+\r
+        if (StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)) {\r
+            return ip;\r
+        }\r
+        \r
+\r
+        return request.getRemoteAddr();\r
+\r
+    }\r
+\r
+\r
+}\r