示例#1
0
  static Map<Long, RouteRule> route2RouteRule(
      List<Route> routes, Map<String, List<String>> clusters) {
    Map<Long, RouteRule> rules = new HashMap<Long, RouteRule>();
    // route -> RouteRule
    if (routes != null && routes.size() > 0) {
      for (Route route : routes) {
        rules.put(route.getId(), RouteRule.parseQuitely(route));
      }
    }
    // expand the cluster parameters into conditions of routerule
    if (clusters != null && clusters.size() > 0) {
      Map<Long, RouteRule> rrs = new HashMap<Long, RouteRule>();
      for (Map.Entry<Long, RouteRule> entry : rules.entrySet()) {
        RouteRule rr = entry.getValue();

        Map<String, RouteRule.MatchPair> when =
            RouteRuleUtils.expandCondition(
                rr.getWhenCondition(), "consumer.cluster", "consumer.host", clusters);
        Map<String, RouteRule.MatchPair> then =
            RouteRuleUtils.expandCondition(
                rr.getThenCondition(), "provider.cluster", "provider.host", clusters);

        rrs.put(entry.getKey(), RouteRule.createFromCondition(when, then));
      }
      rules = rrs;
    }
    return rules;
  }
示例#2
0
  public static boolean matchRoute(
      String consumerAddress,
      String consumerQueryUrl,
      Route route,
      Map<String, List<String>> clusters) {
    RouteRule rule = RouteRule.parseQuitely(route);
    Map<String, RouteRule.MatchPair> when =
        RouteRuleUtils.expandCondition(
            rule.getWhenCondition(), "consumer.cluster", "consumer.host", clusters);
    Map<String, String> consumerSample = ParseUtils.parseQuery("consumer.", consumerQueryUrl);

    final int index = consumerAddress.lastIndexOf(":");
    String consumerHost = null;
    if (index != -1) {
      consumerHost = consumerAddress.substring(0, index);
    } else {
      consumerHost = consumerAddress;
    }
    consumerSample.put("consumer.host", consumerHost);

    return RouteRuleUtils.isMatchCondition(when, consumerSample, consumerSample);
  }