2 * Copyright 2016-2017 ZTE, Inc. and others.
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
14 package org.onap.msb.sdclient.wrapper.util;
16 import javax.servlet.http.HttpServletRequest;
18 import org.apache.commons.lang3.StringUtils;
21 * @ClassName: ApiRouteUtil
22 * @Description: TODO(ApiRoute工具类)
23 * @author tanghua10186366
24 * @date 2015年9月29日 下午3:19:51
27 public class DiscoverUtil {
30 public static final String CONSUL_DEFAULT_PORT = "8500";
32 public static final String APIGATEWAY_SERVINCE = "apigateway";
34 public static final String ROUTER_SERVINCE = "router";
36 public static final String APIGATEWAY_SERVINCE_ALL = "all";
38 public static final String APIGATEWAY_SERVINCE_DEFAULT = "default";
40 public static final String VISUAL_RANGE_IN = "1";
42 public static final String VISUAL_RANGE_OUT = "0";
44 public static final String SERVICENAME_LINE_NAMESPACE = "-";
46 public static final String SPLIT_LINE = "|";
48 public static final String EXTERNAL_NODE_NAME = "externalService";
51 public static final String REQUEST_SUCCESS = "SUCCESS";
53 public static final String REQUEST_FAIL = "FAIL";
55 public static final String VISUAL_RANGE_LIST = "0,1";
57 public static final String PROTOCOL_LIST = "REST,HTTP,MQ,FTP,SNMP,UI,TCP,UDP,PORTAL";
59 public static final String LB_POLICY_LIST = "round-robin,ip_hash,least_conn,client_custom";
61 public static final String LB_PARAMS_LIST = "weight,max_fails,fail_timeout";
63 public static final String CHECK_TYPE_LIST = "HTTP,TCP,TTL";
65 public static final String CHECK_HA_ROLE_LIST = "active,standby";
67 public static final String CONSUL_CATALOG_URL = "/v1/catalog";
69 public static final String CONSUL_AGENT_URL = "/v1/agent/service";
71 public static final String CONSUL_AGENT_TTL_URL = "/v1/agent/check/pass/";
73 public static final String CONSUL_HEALTH_URL = "/v1/health/service/";
75 public static final String[] PUBLISH_PROTOCOL = {"TCP", "UDP", "HTTP", "REST", "UI", "PORTAL"};
77 public static final String[] HTTP_PROTOCOL = {"HTTP", "REST", "UI", "PORTAL"};
79 public static final String TCP_UDP_PORT_RANGE_START = "28001";
81 public static final String TCP_UDP_PORT_RANGE_END = "30000";
83 public static final String CONSUL_ADDRESSS = "127.0.0.1:8500";
85 public static final String CONSUL_REGISTER_MODE = "catalog";
87 // public static boolean isProtocol_tcp_udp(String protocol){
88 // return "TCP".equals(protocol) || "UDP".equals(protocol);
92 public static String getRealIp(HttpServletRequest request) {
93 String ip = request.getHeader("X-Forwarded-For");
94 if (StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)) {
95 // 多次反向代理后会有多个ip值,第一个ip才是真实ip
96 int index = ip.indexOf(",");
98 return ip.substring(0, index);
103 ip = request.getHeader("X-Real-IP");
105 if (StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)) {
110 return request.getRemoteAddr();
114 public static boolean checkVisualRangeIn(String visualRange) {
115 return checkExist(visualRange, DiscoverUtil.VISUAL_RANGE_IN, "|");
118 public static boolean checkVisualRangeOut(String visualRange) {
119 return checkExist(visualRange, DiscoverUtil.VISUAL_RANGE_OUT, "|");
122 public static boolean checkExist(String list, String value, String separator) {
123 String[] listArray = StringUtils.split(list, separator);
125 for (int i = 0; i < listArray.length; i++) {
126 if (value.equals(listArray[i]))
132 public static boolean checkExist(String[] list, String value) {
133 for (int i = 0; i < list.length; i++) {
134 if (value.equals(list[i]))
140 public static boolean contain(String[] array, String value[]) {
141 for (int i = 0; i < array.length; i++) {
142 for (int n = 0; n < value.length; n++) {
143 if (array[i].equals(value[n])) {