Package com.alibaba.dubbo.registry.common.domain

Examples of com.alibaba.dubbo.registry.common.domain.Route


        }
    }
   
    @Test
    public void test_previewRoute() throws Exception {
        Route route = new Route();
        route.setId(1L);
        route.setService("hello.HelloService");
        route.setMatchRule("consumer.host=1.1.2.2,2.2.2.3");
        route.setFilterRule("provider.host=3.3.4.4&provider.application=morgan");
       
        {
            // no methods
            Map<String, String> preview = RouteUtils.previewRoute("hello.HelloService", "1.1.2.2:20880", "application=morgan", serviceUrls, route, clusters, null);
            Map<String, String> expected = new HashMap<String, String>();
            expected.put("dubbo://3.3.4.4:20880/hello.HelloService", "dubbo=2.0.0&version=1.0.0&revision=1.1.1&methods=*&application=morgan");
            assertEquals(expected, preview);
           
            // 2 methods
            preview = RouteUtils.previewRoute("hello.HelloService", "1.1.2.2", "application=morgan&methods=getPort,say", serviceUrls, route, clusters, null);
            expected = new HashMap<String, String>();
            expected.put("dubbo://3.3.4.4:20880/hello.HelloService", "dubbo=2.0.0&version=1.0.0&revision=1.1.1&methods=getPort,say&application=morgan");
            assertEquals(expected, preview);
           
            // ghost methods
            preview = RouteUtils.previewRoute("hello.HelloService", "1.1.2.2", "application=morgan&methods=getPort,say,ghostMethod", serviceUrls, route, clusters, null);
            expected = new HashMap<String, String>();
            expected.put("dubbo://3.3.4.4:20880/hello.HelloService", "dubbo=2.0.0&version=1.0.0&revision=1.1.1&methods=getPort,say,ghostMethod&application=morgan");
            assertEquals(expected, preview);
           
            // no route
            preview = RouteUtils.previewRoute("hello.HelloService", "1.2.3.4", "application=morgan", serviceUrls, route, clusters, null);
            assertEquals(serviceUrls_starMethods, preview);
           
            // no route, 2 methods
            preview = RouteUtils.previewRoute("hello.HelloService", "1.2.3.4", "application=morgan&methods=getPort,say", serviceUrls, route, clusters, null);
            assertEquals(serviceUrls, preview);
           
            // no route, ghost methods
            preview = RouteUtils.previewRoute("hello.HelloService", "1.2.3.4", "application=morgan&methods=getPort,say,ghostMethod", serviceUrls, route, clusters, null);
            assertEquals(serviceUrls_ghostMethods, preview);
        }
       
        // with cluster
        {
            route.setMatchRule("consumer.cluster = cluster1");
           
            // no method
            Map<String, String> preview = RouteUtils.previewRoute("hello.HelloService", "7.7.7.7:20880", "application=morgan", serviceUrls, route, clusters, null);
            Map<String, String> expected = new HashMap<String, String>();
            expected.put("dubbo://3.3.4.4:20880/hello.HelloService", "dubbo=2.0.0&version=1.0.0&revision=1.1.1&methods=*&application=morgan");
 
View Full Code Here


     * 显示路由详细信息
     * @param context
     */
    public void show(Map<String, Object> context) {
      try {
          Route route = routeService.findRoute(Long.parseLong((String)context.get("id")));
         
          if (route == null) {
              throw new IllegalArgumentException("The route is not existed.");
          }
          if(route.getService()!=null && !route.getService().isEmpty()){
              context.put("service", route.getService());
          }
         
          RouteRule routeRule= RouteRule.parse(route);
         
          @SuppressWarnings("unchecked")
          Map<String, RouteRule.MatchPair>[] paramArray = new Map[] {
              routeRule.getWhenCondition(), routeRule.getThenCondition()};
          String[][][] namesArray = new String[][][] {when_names, then_names };
         
          for(int i=0; i<paramArray.length; ++i) {
              Map<String, RouteRule.MatchPair> param = paramArray[i];
              String[][] names = namesArray[i];
              for(String[] name : names) {
                  RouteRule.MatchPair matchPair = param.get(name[0]);
                  if(matchPair == null) {
                      continue;
                  }
                 
                  if(!matchPair.getMatches().isEmpty()) {
                      String m = RouteRule.join(matchPair.getMatches());
                      context.put(name[1], m);
                  }
                  if(!matchPair.getUnmatches().isEmpty()) {
                      String u = RouteRule.join(matchPair.getUnmatches());
                      context.put(name[2], u);
                  }
              }
          }
          context.put("route", route);
          context.put("methods", CollectionUtils.sort(new ArrayList<String>(providerService.findMethodsByService(route.getService()))));
      } catch (ParseException e) {
      e.printStackTrace();
    }
    }
View Full Code Here

            if (filterRule.length() > MAX_RULE_LENGTH) {
                context.put("message", getMessage("Then rule is too long!"));
                return false;
            }
           
            Route route = new Route();
            route.setService(service);
            route.setName(name);
            route.setUsername((String)context.get("operator"));
            route.setOperator((String)context.get("operatorAddress"));
            route.setRule(routeRule.toString());
            if (StringUtils.isNotEmpty((String)context.get("priority"))) {
                route.setPriority(Integer.parseInt((String)context.get("priority")));
            }
            routeService.createRoute(route);
           
        }
       
View Full Code Here

            boolean black = false;
            if(blacks != null && blacks.length > 0){
                black = true;
            }

            Route oldRoute = routeService.findRoute(Long.valueOf(idStr));
            if(null == oldRoute) {
                context.put("message", getMessage("NoSuchRecord"));
                return false;
            }
            //判断参数,拼凑rule
            if (StringUtils.isNotEmpty((String)context.get("name"))) {
              String service = oldRoute.getService();
                if (context.get("operator") == null) {
                    context.put("message", getMessage("HaveNoServicePrivilege", service));
                    return false;
                }
               
                Map<String, String> when_name2valueList = new HashMap<String, String>();
                Map<String, String> notWhen_name2valueList = new HashMap<String, String>();
                for(String[] names : when_names) {
                    when_name2valueList.put(names[0], (String)context.get(names[1]));
                    notWhen_name2valueList.put(names[0], (String)context.get(names[2])); // value不为null的情况,这里处理,后面会保证
                }

                Map<String, String> then_name2valueList = new HashMap<String, String>();
                Map<String, String> notThen_name2valueList = new HashMap<String, String>();
                for(String[] names : then_names) {
                    then_name2valueList.put(names[0], (String)context.get(names[1]));
                    notThen_name2valueList.put(names[0], (String)context.get(names[2]));
                }

                RouteRule routeRule = RouteRule.createFromNameAndValueListString(
                        when_name2valueList, notWhen_name2valueList,
                        then_name2valueList, notThen_name2valueList);
               
                RouteRule result = null;
                if(black){
                    RouteRule.MatchPair matchPair = routeRule.getThenCondition().get("black");
                    Map<String, RouteRule.MatchPair> then = null;
                    if(null == matchPair) {
                        matchPair = new RouteRule.MatchPair();
                        then = new HashMap<String, RouteRule.MatchPair>();
                        then.put("black", matchPair);
                    }else{
                        matchPair.getMatches().clear();
                    }
                    matchPair.getMatches().add(String.valueOf(black));
                    result =  RouteRule.copyWithReplace(routeRule, null, then);
                }
               
                if(result == null){
                    result = routeRule;
                }
               
                if (result.getThenCondition().isEmpty()) {
                    context.put("message", getMessage("Update route error! then is empty."));
                    return false;
                }
               
                String matchRule = result.getWhenConditionString();
                String filterRule = result.getThenConditionString();
               
                //限制表达式的长度
                if (matchRule.length() > MAX_RULE_LENGTH) {
                    context.put("message", getMessage("When rule is too long!"));
                    return false;
                }
                if (filterRule.length() > MAX_RULE_LENGTH) {
                    context.put("message", getMessage("Then rule is too long!"));
                    return false;
                }
               
                int priority = 0;
                if (StringUtils.isNotEmpty((String)context.get("priority"))) {
                    priority = Integer.parseInt((String)context.get("priority"));
                }
               
                Route route = new Route();
                route.setRule(result.toString());
                route.setService(service);
                route.setPriority(priority);
                route.setName((String)context.get("name"));
                route.setUsername((String)context.get("operator"));
                route.setOperator((String)context.get("operatorAddress"));
                route.setId(Long.valueOf(idStr));
                route.setPriority(Integer.parseInt((String)context.get("priority")));
                route.setEnabled(oldRoute.isEnabled());
                routeService.updateRoute(route);
               
                Set<String> usernames = new HashSet<String>();
                usernames.add((String)context.get("operator"));
                usernames.add(route.getUsername());
                //RelateUserUtils.addOwnersOfService(usernames, route.getService(), ownerDAO);
               
                Map<String, Object> params = new HashMap<String, Object>();
                params.put("action", "update");
                params.put("route", route);
View Full Code Here

     */
    public void routeselect(Map<String, Object> context){
      long rid = Long.valueOf((String)context.get("id"));
        context.put("id", rid);
       
        Route route = routeService.findRoute(rid);
        if (route == null) {
            throw new IllegalStateException("Route(id=" + rid + ") is not existed!");
        }
       
        context.put("route", route);
        // 获取数据
        List<Consumer> consumers = consumerService.findByService(route.getService());
        context.put("consumers", consumers);
       
        Map<String, Boolean> matchRoute = new HashMap<String, Boolean>();
        for(Consumer c : consumers) {
            matchRoute.put(c.getAddress(), RouteUtils.matchRoute(c.getAddress(), null, route, null));
View Full Code Here

        if(StringUtils.isEmpty(rid)) {
            context.put("message", getMessage("MissRequestParameters", "id"));
        }
       
        Map<String, String> serviceUrls = new HashMap<String, String>();
        Route route = routeService.findRoute(Long.valueOf(rid));
        if(null == route) {
            context.put("message", getMessage("NoSuchRecord"));
        }
        List<Provider> providers = providerService.findByService(route.getService());
        if (providers != null) {
            for (Provider p : providers) {
                serviceUrls.put(p.getUrl(), p.getParameters());
            }
        }
View Full Code Here

        Set<String> consumerAddresses = toAddr(addr);
        Set<String> aimServices = toService(services);
        for(String aimService : aimServices) {
            boolean isFirst = false;
            List<Route> routes = routeService.findForceRouteByService(aimService);
            Route route = null;
            if(routes==null||routes.size()==0){
                isFirst = true;
                route  = new Route();
                route.setService(aimService);
                route.setForce(true);
                route.setName(aimService+" blackwhitelist");
                route.setFilterRule("false");
                route.setEnabled(true);
            }else{
                route = routes.get(0);
            }
            Map<String, MatchPair> when = null;
            MatchPair matchPair = null;
            if(isFirst){
                when = new HashMap<String, MatchPair>();
                matchPair = new MatchPair(new HashSet<String>(),new HashSet<String>());
                when.put("consumer.host", matchPair);
            }else{
                when = RouteRule.parseRule(route.getMatchRule());
                matchPair = when.get("consumer.host");
            }
            for (String consumerAddress : consumerAddresses) {
                if(Boolean.valueOf((String) context.get("allow"))){
                    matchPair.getUnmatches().add(Tool.getIP(consumerAddress));
                   
                }else{
                    matchPair.getMatches().add(Tool.getIP(consumerAddress));
                }
            }
            StringBuilder sb = new StringBuilder();
            RouteRule.contidionToString(sb,when);
            route.setMatchRule(sb.toString());
            route.setUsername(operator);
            if(isFirst){
                routeService.createRoute(route);
            }else{
                routeService.updateRoute(route);
            }
View Full Code Here

        URL url = pair.getValue();

        if (null == url)
            return null;

        Route r = new Route();
        r.setId(id);
        r.setName(url.getParameter("name"));
        r.setService(url.getServiceKey());
        r.setPriority(url.getParameter(Constants.PRIORITY_KEY, 0));
        r.setEnabled(url.getParameter(Constants.ENABLED_KEY, true));
        r.setForce(url.getParameter(Constants.FORCE_KEY, false));
        r.setRule(url.getParameterAndDecoded(Constants.RULE_KEY));
        return r;
    }
View Full Code Here

        }
        for(Map.Entry<String, Set<String>> entry : serviceAddresses.entrySet()) {
            String service = entry.getKey();
            boolean isFirst = false;
            List<Route> routes = routeService.findForceRouteByService(service);
            Route route = null;
            if(routes == null || routes.size() == 0){
                isFirst = true;
                route  = new Route();
                route.setService(service);
                route.setForce(true);
                route.setName(service+" blackwhitelist");
                route.setFilterRule("false");
                route.setEnabled(true);
            } else {
                route = routes.get(0);
            }
            Map<String, MatchPair> when = null;
            MatchPair matchPair = null;
            if(isFirst){
                when = new HashMap<String, MatchPair>();
                matchPair = new MatchPair(new HashSet<String>(),new HashSet<String>());
                when.put("consumer.host", matchPair);
            }else{
                when = RouteRule.parseRule(route.getMatchRule());
                matchPair = when.get("consumer.host");
            }
            if (only) {
              matchPair.getUnmatches().clear();
              matchPair.getMatches().clear();
              if (allow) {
                matchPair.getUnmatches().addAll(entry.getValue());
              } else {
                matchPair.getMatches().addAll(entry.getValue());
              }
            } else {
              for (String consumerAddress : entry.getValue()) {
                  if(matchPair.getUnmatches().size() > 0) { // 白名单优先
                    matchPair.getMatches().remove(consumerAddress); // 去掉黑名单中相同数据
                    if (allow) { // 如果允许访问
                      matchPair.getUnmatches().add(consumerAddress); // 加入白名单
                    } else { // 如果禁止访问
                      matchPair.getUnmatches().remove(consumerAddress); // 从白名单中去除
                    }
                    } else { // 黑名单生效
                      if (allow) { // 如果允许访问
                        matchPair.getMatches().remove(consumerAddress); // 从黑名单中去除
                      } else { // 如果禁止访问
                        matchPair.getMatches().add(consumerAddress); // 加入黑名单
                      }
                    }
                }
            }
            StringBuilder sb = new StringBuilder();
            RouteRule.contidionToString(sb,when);
            route.setMatchRule(sb.toString());
            route.setUsername(operator);
            if (matchPair.getMatches().size() > 0 || matchPair.getUnmatches().size() > 0) {
              if(isFirst) {
                  routeService.createRoute(route);
                } else {
                  routeService.updateRoute(route);
                }
            } else if (! isFirst) {
            routeService.deleteRoute(route.getId());
          }
        }
        return true;
    }
View Full Code Here

         services.add("com.alibaba.MemberService");
         services.add("com.alibaba.ViewCacheService");
         services.add("com.alibaba.PC2Service");
         services.add("service2");
        
         Route route = new Route();
         route.setMatchRule("service=com.alibaba.*,AuthService&service!=com.alibaba.DomainService,com.alibaba.ViewCacheService&consumer.host!=127.0.0.1,15.11.57.6&consumer.version = 2.0.0&consumer.version != 1.0.0");
         route.setFilterRule("provider.application=morgan,pc2&provider.host=10.16.26.51&provider.port=1020");
         RouteRule rr = RouteRule.parse(route);
         Collection<String> changedService = RouteRuleUtils.filterServiceByRule(services, rr);
         assertEquals(3,changedService.size());
    }
View Full Code Here

TOP

Related Classes of com.alibaba.dubbo.registry.common.domain.Route

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.