Initial code import
[msb/apigateway.git] / apiroute / apiroute-service / src / main / java / org / openo / msb / wrapper / util / MicroServiceUtil.java
1 /**\r
2 * Copyright (C) 2016 ZTE, Inc. and others. All rights reserved. (ZTE)\r
3 *\r
4 * Licensed under the Apache License, Version 2.0 (the "License");\r
5 * you may not use this file except in compliance with the License.\r
6 * You may obtain a copy of the License at\r
7 *\r
8 * http://www.apache.org/licenses/LICENSE-2.0\r
9 *\r
10 * Unless required by applicable law or agreed to in writing, software\r
11 * distributed under the License is distributed on an "AS IS" BASIS,\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13 * See the License for the specific language governing permissions and\r
14 * limitations under the License.\r
15 */\r
16 \r
17 package org.openo.msb.wrapper.util;\r
18 \r
19 import java.util.regex.Pattern;\r
20 \r
21 import javax.servlet.http.HttpServletRequest;\r
22 \r
23 import org.apache.commons.lang3.StringUtils;\r
24 \r
25 \r
26 public class MicroServiceUtil {\r
27     public static final String PREFIX_PATH = "discover:microservices";\r
28     \r
29     public static final String PREFIX_PATH_PORT = "discover:";\r
30 \r
31     public static final String SUFFIX_PATH_INFO = "info";\r
32 \r
33     public static final String REDIS_KEY_PATTERN =\r
34             "discover:microservices:(?<servicename>[^:]+)(:(?<version>[^:]*))?:info";\r
35 \r
36     public static final String REQUEST_SUCCESS = "SUCCESS";\r
37 \r
38     public static final String REQUEST_FAIL = "FAIL";\r
39 \r
40     public static final String ROUTE_PATH_LOADBALANCE = "lb"; // 负载均衡路径名\r
41 \r
42    \r
43     public static String getPrefixedKey(String... paths) {\r
44         StringBuffer sb = new StringBuffer();\r
45         \r
46         if(paths[0].trim().equals("") || paths[0].equals(String.valueOf(JedisUtil.serverPort))){\r
47             sb.append(PREFIX_PATH);\r
48         }\r
49         else{\r
50            sb.append(PREFIX_PATH_PORT).append(paths[0]); \r
51         }\r
52         \r
53         for (int i = 1; i < paths.length; i++) {\r
54             sb.append(":");\r
55             sb.append(paths[i]);\r
56         }\r
57         return sb.toString();\r
58     }\r
59 \r
60     \r
61     public static String getServiceInfoKey(String serverPort,String serviceName, String version) {\r
62         return getPrefixedKey(serverPort,serviceName, version, SUFFIX_PATH_INFO);\r
63     }\r
64 \r
65  \r
66     \r
67     \r
68     \r
69     public static Pattern getRedisKeyPattern() {\r
70         return Pattern.compile(REDIS_KEY_PATTERN);\r
71     }\r
72 \r
73     public static String getRealIp(HttpServletRequest request) {\r
74         String ip = request.getHeader("X-Forwarded-For");\r
75         if (StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)) {\r
76             // After the reverse proxy can have multiple IP value for many times, the first IP is the real IP\r
77             int index = ip.indexOf(",");\r
78             if (index != -1) {\r
79                 return ip.substring(0, index);\r
80             } else {\r
81                 return ip;\r
82             }\r
83         }\r
84         ip = request.getHeader("X-Real-IP");\r
85 \r
86         if (StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)) {\r
87             return ip;\r
88         }\r
89         \r
90 \r
91         return request.getRemoteAddr();\r
92 \r
93     }\r
94 \r
95 \r
96 }\r