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