2 * Copyright (C) 2016 ZTE, Inc. and others. All rights reserved. (ZTE)
\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
8 * http://www.apache.org/licenses/LICENSE-2.0
\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
17 package org.openo.msb.wrapper.util;
\r
19 import java.util.regex.Pattern;
\r
21 import javax.servlet.http.HttpServletRequest;
\r
23 import org.apache.commons.lang3.StringUtils;
\r
26 public class MicroServiceUtil {
\r
27 public static final String PREFIX_PATH = "discover:microservices";
\r
29 public static final String PREFIX_PATH_PORT = "discover:";
\r
31 public static final String SUFFIX_PATH_INFO = "info";
\r
33 public static final String REDIS_KEY_PATTERN =
\r
34 "discover:microservices:(?<servicename>[^:]+)(:(?<version>[^:]*))?:info";
\r
36 public static final String REQUEST_SUCCESS = "SUCCESS";
\r
38 public static final String REQUEST_FAIL = "FAIL";
\r
40 public static final String ROUTE_PATH_LOADBALANCE = "lb"; // 负载均衡路径名
\r
43 public static String getPrefixedKey(String... paths) {
\r
44 StringBuffer sb = new StringBuffer();
\r
46 if(paths[0].trim().equals("") || paths[0].equals(String.valueOf(JedisUtil.serverPort))){
\r
47 sb.append(PREFIX_PATH);
\r
50 sb.append(PREFIX_PATH_PORT).append(paths[0]);
\r
53 for (int i = 1; i < paths.length; i++) {
\r
55 sb.append(paths[i]);
\r
57 return sb.toString();
\r
61 public static String getServiceInfoKey(String serverPort,String serviceName, String version) {
\r
62 return getPrefixedKey(serverPort,serviceName, version, SUFFIX_PATH_INFO);
\r
69 public static Pattern getRedisKeyPattern() {
\r
70 return Pattern.compile(REDIS_KEY_PATTERN);
\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
79 return ip.substring(0, index);
\r
84 ip = request.getHeader("X-Real-IP");
\r
86 if (StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)) {
\r
91 return request.getRemoteAddr();
\r