Search in sources :

Example 1 with RouteRule

use of com.alibaba.dubbo.registry.common.route.RouteRule in project dubbo by alibaba.

the class Routes method show.

/**
     * 显示路由详细信息
     * @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();
    }
}
Also used : ArrayList(java.util.ArrayList) RouteRule(com.alibaba.dubbo.registry.common.route.RouteRule) ParseException(java.text.ParseException) HashMap(java.util.HashMap) Map(java.util.Map) Route(com.alibaba.dubbo.registry.common.domain.Route)

Example 2 with RouteRule

use of com.alibaba.dubbo.registry.common.route.RouteRule in project dubbo by alibaba.

the class Routes method update.

/**
     * 保存更新数据到数据库中
     * @param context
     * @return
     */
public boolean update(Map<String, Object> context) {
    String idStr = (String) context.get("id");
    if (idStr != null && idStr.length() > 0) {
        String[] blacks = (String[]) context.get("black");
        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]));
                // value不为null的情况,这里处理,后面会保证
                notWhen_name2valueList.put(names[0], (String) context.get(names[2]));
            }
            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);
        } else {
            context.put("message", getMessage("MissRequestParameters", "name"));
        }
    } else {
        context.put("message", getMessage("MissRequestParameters", "id"));
    }
    return true;
}
Also used : HashMap(java.util.HashMap) RouteRule(com.alibaba.dubbo.registry.common.route.RouteRule) Route(com.alibaba.dubbo.registry.common.domain.Route) HashSet(java.util.HashSet)

Example 3 with RouteRule

use of com.alibaba.dubbo.registry.common.route.RouteRule in project dubbo by alibaba.

the class Routes method create.

/**
     * 保存路由信息到数据库中
     * @param context
     * @return
     */
public boolean create(Map<String, Object> context) {
    String name = (String) context.get("name");
    String service = (String) context.get("service");
    if (StringUtils.isNotEmpty(service) && StringUtils.isNotEmpty(name)) {
        checkService(service);
        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]));
            // value不为null的情况,这里处理,后面会保证
            notWhen_name2valueList.put(names[0], (String) context.get(names[2]));
        }
        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);
        if (routeRule.getThenCondition().isEmpty()) {
            context.put("message", getMessage("Add route error! then is empty."));
            return false;
        }
        String matchRule = routeRule.getWhenConditionString();
        String filterRule = routeRule.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;
        }
        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);
    }
    return true;
}
Also used : HashMap(java.util.HashMap) RouteRule(com.alibaba.dubbo.registry.common.route.RouteRule) Route(com.alibaba.dubbo.registry.common.domain.Route)

Example 4 with RouteRule

use of com.alibaba.dubbo.registry.common.route.RouteRule in project dubbo by alibaba.

the class RouteRuleUtilsTest method test_filterServiceByRule2.

@Test
public void test_filterServiceByRule2() throws Exception {
    List<String> services = new ArrayList<String>();
    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.PC2Service&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(2, changedService.size());
}
Also used : ArrayList(java.util.ArrayList) RouteRule(com.alibaba.dubbo.registry.common.route.RouteRule) Route(com.alibaba.dubbo.registry.common.domain.Route) Test(org.junit.Test)

Example 5 with RouteRule

use of com.alibaba.dubbo.registry.common.route.RouteRule in project dubbo by alibaba.

the class RouteRuleUtilsTest method test_filterServiceByRule.

@Test
public void test_filterServiceByRule() throws Exception {
    List<String> services = new ArrayList<String>();
    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());
}
Also used : ArrayList(java.util.ArrayList) RouteRule(com.alibaba.dubbo.registry.common.route.RouteRule) Route(com.alibaba.dubbo.registry.common.domain.Route) Test(org.junit.Test)

Aggregations

Route (com.alibaba.dubbo.registry.common.domain.Route)5 RouteRule (com.alibaba.dubbo.registry.common.route.RouteRule)5 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Test (org.junit.Test)2 ParseException (java.text.ParseException)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1