2 * Copyright 2016 2015-2016 ZTE, Inc. and others. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.openo.msb.wrapper.util;
18 import java.util.regex.Pattern;
20 import javax.servlet.http.HttpServletRequest;
22 import org.apache.commons.lang3.StringUtils;
25 public class MicroServiceUtil {
26 public static final String PREFIX_PATH = "discover:microservices";
28 public static final String PREFIX_PATH_PORT = "discover:";
30 public static final String SUFFIX_PATH_INFO = "info";
32 public static final String REDIS_KEY_PATTERN =
33 "discover:microservices:(?<servicename>[^:]+)(:(?<version>[^:]*))?:info";
35 public static final String REQUEST_SUCCESS = "SUCCESS";
37 public static final String REQUEST_FAIL = "FAIL";
39 public static final String ROUTE_PATH_LOADBALANCE = "lb"; // 负载均衡路径名
42 public static String getPrefixedKey(String... paths) {
43 StringBuffer sb = new StringBuffer();
45 if(paths[0].trim().equals("") || paths[0].equals(String.valueOf(JedisUtil.serverPort))){
46 sb.append(PREFIX_PATH);
49 sb.append(PREFIX_PATH_PORT).append(paths[0]);
52 for (int i = 1; i < paths.length; i++) {
60 public static String getServiceInfoKey(String serverPort,String serviceName, String version) {
61 return getPrefixedKey(serverPort,serviceName, version, SUFFIX_PATH_INFO);
68 public static Pattern getRedisKeyPattern() {
69 return Pattern.compile(REDIS_KEY_PATTERN);
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(",");
78 return ip.substring(0, index);
83 ip = request.getHeader("X-Real-IP");
85 if (StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)) {
90 return request.getRemoteAddr();